browser_cmd_csscoverage_oneshot.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* Any copyright is dedicated to the Public Domain.
  2. * http://creativecommons.org/publicdomain/zero/1.0/ */
  3. // Tests that the addon commands works as they should
  4. const csscoverage = require("devtools/shared/fronts/csscoverage");
  5. const PAGE_1 = TEST_BASE_HTTPS + "browser_cmd_csscoverage_page1.html";
  6. const PAGE_2 = TEST_BASE_HTTPS + "browser_cmd_csscoverage_page2.html";
  7. const PAGE_3 = TEST_BASE_HTTPS + "browser_cmd_csscoverage_page3.html";
  8. const SHEET_A = TEST_BASE_HTTPS + "browser_cmd_csscoverage_sheetA.css";
  9. const SHEET_B = TEST_BASE_HTTPS + "browser_cmd_csscoverage_sheetB.css";
  10. const SHEET_C = TEST_BASE_HTTPS + "browser_cmd_csscoverage_sheetC.css";
  11. const SHEET_D = TEST_BASE_HTTPS + "browser_cmd_csscoverage_sheetD.css";
  12. add_task(function* () {
  13. let options = yield helpers.openTab(PAGE_3);
  14. yield helpers.openToolbar(options);
  15. let usage = yield csscoverage.getUsage(options.target);
  16. yield navigate(usage, options);
  17. yield checkPages(usage);
  18. yield checkEditorReport(usage);
  19. // usage.createPageReport is not supported for usage.oneshot data as of
  20. // bug 1035300 because the page report assumed we have preload data which
  21. // oneshot can't gather. The ideal solution is to have a special no-preload
  22. // mode for the page report, but since oneshot isn't needed for the UI to
  23. // function, we're currently not supporting page report for oneshot data
  24. // yield checkPageReport(usage);
  25. yield helpers.closeToolbar(options);
  26. yield helpers.closeTab(options);
  27. });
  28. /**
  29. * Just check current page
  30. */
  31. function* navigate(usage, options) {
  32. ok(!usage.isRunning(), "csscoverage is not running");
  33. yield usage.oneshot();
  34. ok(!usage.isRunning(), "csscoverage is still not running");
  35. }
  36. /**
  37. * Check the expected pages have been visited
  38. */
  39. function* checkPages(usage) {
  40. let expectedVisited = [ PAGE_3 ];
  41. let actualVisited = yield usage._testOnlyVisitedPages();
  42. isEqualJson(actualVisited, expectedVisited, "Visited");
  43. }
  44. /**
  45. * Check that createEditorReport returns the expected JSON
  46. */
  47. function* checkEditorReport(usage) {
  48. // Page1
  49. let expectedPage1 = { reports: [] };
  50. let actualPage1 = yield usage.createEditorReport(PAGE_1 + " \u2192 <style> index 0");
  51. isEqualJson(actualPage1, expectedPage1, "Page1");
  52. // Page2
  53. let expectedPage2 = { reports: [] };
  54. let actualPage2 = yield usage.createEditorReport(PAGE_2 + " \u2192 <style> index 0");
  55. isEqualJson(actualPage2, expectedPage2, "Page2");
  56. // Page3a
  57. let expectedPage3a = {
  58. reports: [
  59. {
  60. selectorText: ".page3-test2",
  61. start: { line: 9, column: 5 },
  62. }
  63. ]
  64. };
  65. let actualPage3a = yield usage.createEditorReport(PAGE_3 + " \u2192 <style> index 0");
  66. isEqualJson(actualPage3a, expectedPage3a, "Page3a");
  67. // Page3b
  68. let expectedPage3b = {
  69. reports: [
  70. {
  71. selectorText: ".page3-test3",
  72. start: { line: 3, column: 5 },
  73. }
  74. ]
  75. };
  76. let actualPage3b = yield usage.createEditorReport(PAGE_3 + " \u2192 <style> index 1");
  77. isEqualJson(actualPage3b, expectedPage3b, "Page3b");
  78. // SheetA
  79. let expectedSheetA = {
  80. reports: [
  81. {
  82. selectorText: ".sheetA-test2",
  83. start: { line: 8, column: 1 },
  84. },
  85. {
  86. selectorText: ".sheetA-test3",
  87. start: { line: 12, column: 1 },
  88. },
  89. {
  90. selectorText: ".sheetA-test4",
  91. start: { line: 16, column: 1 },
  92. }
  93. ]
  94. };
  95. let actualSheetA = yield usage.createEditorReport(SHEET_A);
  96. isEqualJson(actualSheetA, expectedSheetA, "SheetA");
  97. // SheetB
  98. let expectedSheetB = {
  99. reports: [
  100. {
  101. selectorText: ".sheetB-test2",
  102. start: { line: 6, column: 1 },
  103. },
  104. {
  105. selectorText: ".sheetB-test3",
  106. start: { line: 10, column: 1 },
  107. },
  108. {
  109. selectorText: ".sheetB-test4",
  110. start: { line: 14, column: 1 },
  111. }
  112. ]
  113. };
  114. let actualSheetB = yield usage.createEditorReport(SHEET_B);
  115. isEqualJson(actualSheetB, expectedSheetB, "SheetB");
  116. // SheetC
  117. let expectedSheetC = {
  118. reports: [
  119. {
  120. selectorText: ".sheetC-test2",
  121. start: { line: 6, column: 1 },
  122. },
  123. {
  124. selectorText: ".sheetC-test3",
  125. start: { line: 10, column: 1 },
  126. },
  127. {
  128. selectorText: ".sheetC-test4",
  129. start: { line: 14, column: 1 },
  130. }
  131. ]
  132. };
  133. let actualSheetC = yield usage.createEditorReport(SHEET_C);
  134. isEqualJson(actualSheetC, expectedSheetC, "SheetC");
  135. // SheetD
  136. let expectedSheetD = {
  137. reports: [
  138. {
  139. selectorText: ".sheetD-test2",
  140. start: { line: 6, column: 1 },
  141. },
  142. {
  143. selectorText: ".sheetD-test3",
  144. start: { line: 10, column: 1 },
  145. },
  146. {
  147. selectorText: ".sheetD-test4",
  148. start: { line: 14, column: 1 },
  149. }
  150. ]
  151. };
  152. let actualSheetD = yield usage.createEditorReport(SHEET_D);
  153. isEqualJson(actualSheetD, expectedSheetD, "SheetD");
  154. }
  155. /**
  156. * Check that checkPageReport returns the expected JSON
  157. */
  158. function* checkPageReport(usage) {
  159. let actualReport = yield usage.createPageReport();
  160. // Quick check on trivial things. See doc comment for checkRuleProperties
  161. actualReport.preload.forEach(page => page.rules.forEach(checkRuleProperties));
  162. actualReport.unused.forEach(page => page.rules.forEach(checkRuleProperties));
  163. // Check the summary
  164. let expectedSummary = { "used": 23, "unused": 9, "preload": 0 };
  165. isEqualJson(actualReport.summary, expectedSummary, "summary");
  166. // Check the preload header
  167. isEqualJson(actualReport.preload.length, 0, "preload length");
  168. // Check the unused header
  169. isEqualJson(actualReport.unused.length, 6, "unused length");
  170. // Check the unused rules
  171. isEqualJson(actualReport.unused[0].url, PAGE_3 + " \u2192 <style> index 0", "unused url 0");
  172. let expectedUnusedRules0 = [
  173. {
  174. "url": PAGE_3 + " \u2192 <style> index 0",
  175. "start": { "line": 9, "column": 5 },
  176. "selectorText": ".page3-test2"
  177. }
  178. ];
  179. isEqualJson(actualReport.unused[0].rules, expectedUnusedRules0, "unused rules 0");
  180. isEqualJson(actualReport.unused[1].url, PAGE_3 + " \u2192 <style> index 1", "unused url 1");
  181. let expectedUnusedRules1 = [
  182. {
  183. "url": PAGE_3 + " \u2192 <style> index 1",
  184. "start": { "line": 3, "column": 5 },
  185. "selectorText": ".page3-test3"
  186. }
  187. ];
  188. isEqualJson(actualReport.unused[1].rules, expectedUnusedRules1, "unused rules 1");
  189. isEqualJson(actualReport.unused[2].url, SHEET_A, "unused url 2");
  190. let expectedUnusedRules2 = [
  191. {
  192. "url": SHEET_A,
  193. "start": { "line": 8, "column": 1 },
  194. "selectorText": ".sheetA-test2"
  195. },
  196. {
  197. "url": SHEET_A,
  198. "start": { "line": 12, "column": 1 },
  199. "selectorText": ".sheetA-test3"
  200. },
  201. {
  202. "url": SHEET_A,
  203. "start": { "line": 16, "column": 1 },
  204. "selectorText": ".sheetA-test4"
  205. }
  206. ];
  207. isEqualJson(actualReport.unused[2].rules, expectedUnusedRules2, "unused rules 2");
  208. isEqualJson(actualReport.unused[3].url, SHEET_B, "unused url 3");
  209. let expectedUnusedRules3 = [
  210. {
  211. "url": SHEET_B,
  212. "start": { "line": 6, "column": 1 },
  213. "selectorText": ".sheetB-test2"
  214. },
  215. {
  216. "url": SHEET_B,
  217. "start": { "line": 10, "column": 1 },
  218. "selectorText": ".sheetB-test3"
  219. },
  220. {
  221. "url": SHEET_B,
  222. "start": { "line": 14, "column": 1 },
  223. "selectorText": ".sheetB-test4"
  224. }
  225. ];
  226. isEqualJson(actualReport.unused[3].rules, expectedUnusedRules3, "unused rules 3");
  227. isEqualJson(actualReport.unused[4].url, SHEET_D, "unused url 4");
  228. let expectedUnusedRules4 = [
  229. {
  230. "url": SHEET_D,
  231. "start": { "line": 6, "column": 1 },
  232. "selectorText": ".sheetD-test2"
  233. },
  234. {
  235. "url": SHEET_D,
  236. "start": { "line": 10, "column": 1 },
  237. "selectorText": ".sheetD-test3"
  238. },
  239. {
  240. "url": SHEET_D,
  241. "start": { "line": 14, "column": 1 },
  242. "selectorText": ".sheetD-test4"
  243. }
  244. ];
  245. isEqualJson(actualReport.unused[4].rules, expectedUnusedRules4, "unused rules 4");
  246. isEqualJson(actualReport.unused[5].url, SHEET_C, "unused url 5");
  247. let expectedUnusedRules5 = [
  248. {
  249. "url": SHEET_C,
  250. "start": { "line": 6, "column": 1 },
  251. "selectorText": ".sheetC-test2"
  252. },
  253. {
  254. "url": SHEET_C,
  255. "start": { "line": 10, "column": 1 },
  256. "selectorText": ".sheetC-test3"
  257. },
  258. {
  259. "url": SHEET_C,
  260. "start": { "line": 14, "column": 1 },
  261. "selectorText": ".sheetC-test4"
  262. }
  263. ];
  264. isEqualJson(actualReport.unused[5].rules, expectedUnusedRules5, "unused rules 5");
  265. }
  266. /**
  267. * We do basic tests on the shortUrl and formattedCssText because they are
  268. * very derivative, and so make for fragile tests, and having done those quick
  269. * existence checks we remove them so the JSON check later can ignore them
  270. */
  271. function checkRuleProperties(rule, index) {
  272. is(typeof rule.shortUrl, "string", "typeof rule.shortUrl for " + index);
  273. is(rule.shortUrl.indexOf("http://"), -1, "http not in rule.shortUrl for" + index);
  274. delete rule.shortUrl;
  275. is(typeof rule.formattedCssText, "string", "typeof rule.formattedCssText for " + index);
  276. ok(rule.formattedCssText.indexOf("{") > 0, "{ in rule.formattedCssText for " + index);
  277. delete rule.formattedCssText;
  278. }
  279. /**
  280. * Utility to compare JSON structures
  281. */
  282. function isEqualJson(o1, o2, msg) {
  283. is(JSON.stringify(o1), JSON.stringify(o2), msg);
  284. }