browser_inplace-editor_maxwidth.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. /* import-globals-from helper_inplace_editor.js */
  4. "use strict";
  5. loadHelperScript("helper_inplace_editor.js");
  6. const MAX_WIDTH = 300;
  7. const START_TEXT = "Start text";
  8. const LONG_TEXT = "I am a long text and I will not fit in a 300px container. " +
  9. "I expect the inplace editor to wrap.";
  10. // Test the inplace-editor behavior with a maxWidth configuration option
  11. // defined.
  12. add_task(function* () {
  13. yield addTab("data:text/html;charset=utf-8,inplace editor max width tests");
  14. let [host, , doc] = yield createHost();
  15. info("Testing the maxWidth option in pixels, to precisely check the size");
  16. yield new Promise(resolve => {
  17. createInplaceEditorAndClick({
  18. multiline: true,
  19. maxWidth: MAX_WIDTH,
  20. start: testMaxWidth,
  21. done: resolve
  22. }, doc, START_TEXT);
  23. });
  24. host.destroy();
  25. gBrowser.removeCurrentTab();
  26. });
  27. let testMaxWidth = Task.async(function* (editor) {
  28. is(editor.input.value, START_TEXT, "Span text content should be used");
  29. ok(editor.input.offsetWidth < MAX_WIDTH,
  30. "Input width should be strictly smaller than MAX_WIDTH");
  31. is(getLines(editor.input), 1, "Input should display 1 line of text");
  32. info("Check a text is on several lines if it does not fit MAX_WIDTH");
  33. for (let key of LONG_TEXT) {
  34. EventUtils.sendChar(key);
  35. checkScrollbars(editor.input);
  36. }
  37. is(editor.input.value, LONG_TEXT, "Long text should be the input value");
  38. is(editor.input.offsetWidth, MAX_WIDTH,
  39. "Input width should be the same as MAX_WIDTH");
  40. is(getLines(editor.input), 3, "Input should display 3 lines of text");
  41. checkScrollbars(editor.input);
  42. info("Delete all characters on line 3.");
  43. while (getLines(editor.input) === 3) {
  44. EventUtils.sendKey("BACK_SPACE");
  45. checkScrollbars(editor.input);
  46. }
  47. is(editor.input.offsetWidth, MAX_WIDTH,
  48. "Input width should be the same as MAX_WIDTH");
  49. is(getLines(editor.input), 2, "Input should display 2 lines of text");
  50. checkScrollbars(editor.input);
  51. info("Delete all characters on line 2.");
  52. while (getLines(editor.input) === 2) {
  53. EventUtils.sendKey("BACK_SPACE");
  54. checkScrollbars(editor.input);
  55. }
  56. is(getLines(editor.input), 1, "Input should display 1 line of text");
  57. checkScrollbars(editor.input);
  58. info("Delete all characters.");
  59. while (editor.input.value !== "") {
  60. EventUtils.sendKey("BACK_SPACE");
  61. checkScrollbars(editor.input);
  62. }
  63. ok(editor.input.offsetWidth < MAX_WIDTH,
  64. "Input width should again be strictly smaller than MAX_WIDTH");
  65. ok(editor.input.offsetWidth > 0,
  66. "Even with no content, the input has a non-zero width");
  67. is(getLines(editor.input), 1, "Input should display 1 line of text");
  68. checkScrollbars(editor.input);
  69. info("Leave the inplace-editor");
  70. EventUtils.sendKey("RETURN");
  71. });
  72. /**
  73. * Retrieve the current number of lines displayed in the provided textarea.
  74. *
  75. * @param {DOMNode} textarea
  76. * @return {Number} the number of lines
  77. */
  78. function getLines(textarea) {
  79. let win = textarea.ownerDocument.defaultView;
  80. let style = win.getComputedStyle(textarea);
  81. let lineHeight = style.getPropertyCSSValue("line-height").cssText;
  82. return Math.floor(textarea.clientHeight / parseFloat(lineHeight));
  83. }
  84. /**
  85. * Verify that the provided textarea has no vertical or horizontal scrollbar.
  86. *
  87. * @param {DOMNode} textarea
  88. */
  89. function checkScrollbars(textarea) {
  90. is(textarea.scrollHeight, textarea.clientHeight,
  91. "Textarea should never have vertical scrollbars");
  92. is(textarea.scrollWidth, textarea.clientWidth,
  93. "Textarea should never have horizontal scrollbars");
  94. }