browser_css_statemachine.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. "use strict";
  4. const CSSCompleter = require("devtools/client/sourceeditor/css-autocompleter");
  5. const CSS_URI = "http://mochi.test:8888/browser/devtools/client/sourceeditor" +
  6. "/test/css_statemachine_testcases.css";
  7. const TESTS_URI = "http://mochi.test:8888/browser/devtools/client" +
  8. "/sourceeditor/test/css_statemachine_tests.json";
  9. const source = read(CSS_URI);
  10. const tests = eval(read(TESTS_URI));
  11. const TEST_URI = "data:text/html;charset=UTF-8," + encodeURIComponent(
  12. ["<!DOCTYPE html>",
  13. "<html>",
  14. " <head>",
  15. " <title>CSS State machine tests.</title>",
  16. " <style type='text/css'>",
  17. "#progress {",
  18. " width: 500px; height: 30px;",
  19. " border: 1px solid black;",
  20. " position: relative",
  21. "}",
  22. "#progress div {",
  23. " width: 0%; height: 100%;",
  24. " background: green;",
  25. " position: absolute;",
  26. " z-index: -1; top: 0",
  27. "}",
  28. "#progress.failed div {",
  29. " background: red !important;",
  30. "}",
  31. "#progress.failed:after {",
  32. " content: 'Some tests failed';",
  33. " color: white",
  34. "}",
  35. "#progress:before {",
  36. " content: 'Running test ' attr(data-progress) ' of " + tests.length + "';",
  37. " color: white;",
  38. " text-shadow: 0 0 2px darkgreen;",
  39. "}",
  40. " </style>",
  41. " </head>",
  42. " <body>",
  43. " <h2>State machine tests for CSS autocompleter.</h2><br>",
  44. " <div id='progress' data-progress='0'>",
  45. " <div></div>",
  46. " </div>",
  47. " </body>",
  48. " </html>"
  49. ].join("\n"));
  50. var doc = null;
  51. function test() {
  52. waitForExplicitFinish();
  53. gBrowser.selectedTab = gBrowser.addTab(TEST_URI);
  54. BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser).then(() => {
  55. doc = content.document;
  56. runTests();
  57. });
  58. }
  59. function runTests() {
  60. let completer = new CSSCompleter({cssProperties: getClientCssProperties()});
  61. let checkState = state => {
  62. if (state[0] == "null" && (!completer.state || completer.state == "null")) {
  63. return true;
  64. } else if (state[0] == completer.state && state[0] == "selector" &&
  65. state[1] == completer.selectorState &&
  66. state[2] == completer.completing &&
  67. state[3] == completer.selector) {
  68. return true;
  69. } else if (state[0] == completer.state && state[0] == "value" &&
  70. state[2] == completer.completing &&
  71. state[3] == completer.propertyName) {
  72. return true;
  73. } else if (state[0] == completer.state &&
  74. state[2] == completer.completing &&
  75. state[0] != "selector" && state[0] != "value") {
  76. return true;
  77. }
  78. return false;
  79. };
  80. let progress = doc.getElementById("progress");
  81. let progressDiv = doc.querySelector("#progress > div");
  82. let i = 0;
  83. for (let test of tests) {
  84. progress.dataset.progress = ++i;
  85. progressDiv.style.width = 100 * i / tests.length + "%";
  86. completer.resolveState(limit(source, test[0]),
  87. {line: test[0][0], ch: test[0][1]});
  88. if (checkState(test[1])) {
  89. ok(true, "Test " + i + " passed. ");
  90. } else {
  91. ok(false, "Test " + i + " failed. Expected state : [" + test[1] + "] " +
  92. "but found [" + completer.state + ", " + completer.selectorState +
  93. ", " + completer.completing + ", " +
  94. (completer.propertyName || completer.selector) + "].");
  95. progress.classList.add("failed");
  96. }
  97. }
  98. gBrowser.removeCurrentTab();
  99. finish();
  100. }