browser_cmd_csscoverage_startstop.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 {gDevTools} = require("devtools/client/framework/devtools");
  6. const PAGE_1 = TEST_BASE_HTTPS + "browser_cmd_csscoverage_page1.html";
  7. const PAGE_2 = TEST_BASE_HTTPS + "browser_cmd_csscoverage_page2.html";
  8. const PAGE_3 = TEST_BASE_HTTPS + "browser_cmd_csscoverage_page3.html";
  9. const SHEET_A = TEST_BASE_HTTPS + "browser_cmd_csscoverage_sheetA.css";
  10. const SHEET_B = TEST_BASE_HTTPS + "browser_cmd_csscoverage_sheetB.css";
  11. const SHEET_C = TEST_BASE_HTTPS + "browser_cmd_csscoverage_sheetC.css";
  12. const SHEET_D = TEST_BASE_HTTPS + "browser_cmd_csscoverage_sheetD.css";
  13. add_task(function* () {
  14. let options = yield helpers.openTab("about:blank");
  15. yield helpers.openToolbar(options);
  16. let usage = yield csscoverage.getUsage(options.target);
  17. yield navigate(usage, options);
  18. yield checkPages(usage);
  19. yield checkEditorReport(usage);
  20. yield checkPageReport(usage);
  21. yield helpers.closeToolbar(options);
  22. yield helpers.closeTab(options);
  23. });
  24. /**
  25. * Visit all the pages in the test
  26. */
  27. function* navigate(usage, options) {
  28. yield usage.start(options.chromeWindow, options.target);
  29. ok(usage.isRunning(), "csscoverage is running");
  30. // Load page 1.
  31. options.browser.loadURI(PAGE_1);
  32. // And wait until page 1 and page 2 (an iframe inside page 1) are both loaded.
  33. yield Promise.all([
  34. BrowserTestUtils.browserLoaded(options.browser, false, PAGE_1),
  35. BrowserTestUtils.browserLoaded(options.browser, true, PAGE_2)
  36. ]);
  37. is(options.browser.currentURI.spec, PAGE_1, "page 1 loaded");
  38. // page 2 has JS that navigates to page 3 after a timeout.
  39. yield BrowserTestUtils.browserLoaded(options.browser, false, PAGE_3);
  40. is(options.browser.currentURI.spec, PAGE_3, "page 3 loaded");
  41. let toolboxReady = gDevTools.once("toolbox-ready");
  42. yield usage.stop();
  43. ok(!usage.isRunning(), "csscoverage not is running");
  44. yield toolboxReady;
  45. }
  46. /**
  47. * Check the expected pages have been visited
  48. */
  49. function* checkPages(usage) {
  50. // 'load' event order. '' is for the initial location
  51. let expectedVisited = [ "", PAGE_2, PAGE_1, PAGE_3 ];
  52. let actualVisited = yield usage._testOnlyVisitedPages();
  53. isEqualJson(actualVisited, expectedVisited, "Visited");
  54. }
  55. /**
  56. * Check that createEditorReport returns the expected JSON
  57. */
  58. function* checkEditorReport(usage) {
  59. // Page1
  60. let expectedPage1 = {
  61. reports: [
  62. {
  63. selectorText: ".page1-test2",
  64. start: { line: 8, column: 5 },
  65. }
  66. ]
  67. };
  68. let actualPage1 = yield usage.createEditorReport(PAGE_1 + " \u2192 <style> index 0");
  69. isEqualJson(actualPage1, expectedPage1, "Page1");
  70. // Page2
  71. let expectedPage2 = {
  72. reports: [
  73. {
  74. selectorText: ".page2-test2",
  75. start: { line: 9, column: 5 },
  76. },
  77. ]
  78. };
  79. let actualPage2 = yield usage.createEditorReport(PAGE_2 + " \u2192 <style> index 0");
  80. isEqualJson(actualPage2, expectedPage2, "Page2");
  81. // Page3a
  82. let expectedPage3a = {
  83. reports: [
  84. {
  85. selectorText: ".page3-test2",
  86. start: { line: 9, column: 5 },
  87. }
  88. ]
  89. };
  90. let actualPage3a = yield usage.createEditorReport(PAGE_3 + " \u2192 <style> index 0");
  91. isEqualJson(actualPage3a, expectedPage3a, "Page3a");
  92. // Page3b
  93. let expectedPage3b = {
  94. reports: [
  95. {
  96. selectorText: ".page3-test3",
  97. start: { line: 3, column: 5 },
  98. }
  99. ]
  100. };
  101. let actualPage3b = yield usage.createEditorReport(PAGE_3 + " \u2192 <style> index 1");
  102. isEqualJson(actualPage3b, expectedPage3b, "Page3b");
  103. // SheetA
  104. let expectedSheetA = {
  105. reports: [
  106. {
  107. selectorText: ".sheetA-test2",
  108. start: { line: 8, column: 1 },
  109. }
  110. ]
  111. };
  112. let actualSheetA = yield usage.createEditorReport(SHEET_A);
  113. isEqualJson(actualSheetA, expectedSheetA, "SheetA");
  114. // SheetB
  115. let expectedSheetB = {
  116. reports: [
  117. {
  118. selectorText: ".sheetB-test2",
  119. start: { line: 6, column: 1 },
  120. }
  121. ]
  122. };
  123. let actualSheetB = yield usage.createEditorReport(SHEET_B);
  124. isEqualJson(actualSheetB, expectedSheetB, "SheetB");
  125. // SheetC
  126. let expectedSheetC = {
  127. reports: [
  128. {
  129. selectorText: ".sheetC-test2",
  130. start: { line: 6, column: 1 },
  131. }
  132. ]
  133. };
  134. let actualSheetC = yield usage.createEditorReport(SHEET_C);
  135. isEqualJson(actualSheetC, expectedSheetC, "SheetC");
  136. // SheetD
  137. let expectedSheetD = {
  138. reports: [
  139. {
  140. selectorText: ".sheetD-test2",
  141. start: { line: 6, column: 1 },
  142. }
  143. ]
  144. };
  145. let actualSheetD = yield usage.createEditorReport(SHEET_D);
  146. isEqualJson(actualSheetD, expectedSheetD, "SheetD");
  147. }
  148. /**
  149. * Check that checkPageReport returns the expected JSON
  150. */
  151. function* checkPageReport(usage) {
  152. let actualReport = yield usage.createPageReport();
  153. // Quick check on trivial things. See doc comment for checkRuleProperties
  154. actualReport.preload.forEach(page => page.rules.forEach(checkRuleProperties));
  155. actualReport.unused.forEach(page => page.rules.forEach(checkRuleProperties));
  156. // Check the summary
  157. let expectedSummary = { "used": 92, "unused": 22, "preload": 28 };
  158. isEqualJson(actualReport.summary, expectedSummary, "summary");
  159. checkPageReportPreload(actualReport);
  160. checkPageReportUnused(actualReport);
  161. }
  162. /**
  163. * Check that checkPageReport returns the expected preload JSON
  164. */
  165. function checkPageReportPreload(actualReport) {
  166. // Check the preload header
  167. isEqualJson(actualReport.preload.length, 3, "preload length");
  168. // Check the preload rules
  169. isEqualJson(actualReport.preload[0].url, PAGE_2, "preload url 0");
  170. let expectedPreloadRules0 = [
  171. // TODO: This is already pre-loaded, we should note this
  172. {
  173. url: PAGE_2 + " \u2192 <style> index 0",
  174. start: { line: 5, column: 5 },
  175. selectorText: ".page2-test1"
  176. },
  177. {
  178. url: SHEET_A,
  179. start: { line: 4, column: 1 },
  180. selectorText: ".sheetA-test1"
  181. },
  182. {
  183. url: SHEET_A,
  184. start: { line: 16, column: 1 },
  185. selectorText: ".sheetA-test4"
  186. },
  187. {
  188. url: SHEET_B,
  189. start: { line: 2, column: 1 },
  190. selectorText: ".sheetB-test1"
  191. },
  192. {
  193. url: SHEET_B,
  194. start: { line: 14, column: 1 },
  195. selectorText: ".sheetB-test4"
  196. },
  197. {
  198. url: SHEET_D,
  199. start: { line: 2, column: 1 },
  200. selectorText: ".sheetD-test1"
  201. },
  202. {
  203. url: SHEET_D,
  204. start: { line: 14, column: 1 },
  205. selectorText: ".sheetD-test4"
  206. },
  207. {
  208. url: SHEET_C,
  209. start: { line: 2, column: 1 },
  210. selectorText: ".sheetC-test1"
  211. },
  212. {
  213. url: SHEET_C,
  214. start: { line: 14, column: 1 },
  215. selectorText: ".sheetC-test4"
  216. }
  217. ];
  218. isEqualJson(actualReport.preload[0].rules, expectedPreloadRules0, "preload rules 0");
  219. isEqualJson(actualReport.preload[1].url, PAGE_1, "preload url 1");
  220. let expectedPreloadRules1 = [
  221. {
  222. url: SHEET_A,
  223. start: { line: 4, column: 1 },
  224. selectorText: ".sheetA-test1"
  225. },
  226. {
  227. url: SHEET_A,
  228. start: { line: 12, column: 1 },
  229. selectorText: ".sheetA-test3"
  230. },
  231. {
  232. url: SHEET_B,
  233. start: { line: 2, column: 1 },
  234. selectorText: ".sheetB-test1"
  235. },
  236. {
  237. url: SHEET_B,
  238. start: { line: 10, column: 1 },
  239. selectorText: ".sheetB-test3"
  240. },
  241. {
  242. url: SHEET_D,
  243. start: { line: 2, column: 1 },
  244. selectorText: ".sheetD-test1"
  245. },
  246. {
  247. url: SHEET_D,
  248. start: { line: 10, column: 1 },
  249. selectorText: ".sheetD-test3"
  250. },
  251. {
  252. url: SHEET_C,
  253. start: { line: 2, column: 1 },
  254. selectorText: ".sheetC-test1"
  255. },
  256. {
  257. url: SHEET_C,
  258. start: { line: 10, column: 1 },
  259. selectorText: ".sheetC-test3"
  260. },
  261. {
  262. url: PAGE_1 + " \u2192 <style> index 0",
  263. start: { line: 4, column: 5 },
  264. selectorText: ".page1-test1"
  265. },
  266. {
  267. url: PAGE_1 + " \u2192 <style> index 0",
  268. start: { line: 12, column: 5 },
  269. selectorText: ".page1-test3:hover"
  270. }
  271. ];
  272. isEqualJson(actualReport.preload[1].rules, expectedPreloadRules1, "preload rules 1");
  273. isEqualJson(actualReport.preload[2].url, PAGE_3, "preload url 2");
  274. let expectedPreloadRules2 = [
  275. {
  276. url: SHEET_A,
  277. start: { line: 4, column: 1 },
  278. selectorText: ".sheetA-test1"
  279. },
  280. {
  281. url: SHEET_A,
  282. start: { line: 20, column: 1 },
  283. selectorText: ".sheetA-test5"
  284. },
  285. {
  286. url: SHEET_B,
  287. start: { line: 2, column: 1 },
  288. selectorText: ".sheetB-test1"
  289. },
  290. {
  291. url: SHEET_B,
  292. start: { line: 18, column: 1 },
  293. selectorText: ".sheetB-test5"
  294. },
  295. {
  296. url: SHEET_D,
  297. start: { line: 2, column: 1 },
  298. selectorText: ".sheetD-test1"
  299. },
  300. {
  301. url: SHEET_D,
  302. start: { line: 18, column: 1 },
  303. selectorText: ".sheetD-test5"
  304. },
  305. {
  306. url: SHEET_C,
  307. start: { line: 2, column: 1 },
  308. selectorText: ".sheetC-test1"
  309. },
  310. {
  311. url: SHEET_C,
  312. start: { line: 18, column: 1 },
  313. selectorText: ".sheetC-test5"
  314. },
  315. {
  316. url: PAGE_3 + " \u2192 <style> index 0",
  317. start: { line: 5, column: 5 },
  318. selectorText: ".page3-test1"
  319. },
  320. ];
  321. isEqualJson(actualReport.preload[2].rules, expectedPreloadRules2, "preload rules 2");
  322. }
  323. /**
  324. * Check that checkPageReport returns the expected unused JSON
  325. */
  326. function checkPageReportUnused(actualReport) {
  327. // Check the unused header
  328. isEqualJson(actualReport.unused.length, 8, "unused length");
  329. // Check the unused rules
  330. isEqualJson(actualReport.unused[0].url, PAGE_2 + " \u2192 <style> index 0", "unused url 0");
  331. let expectedUnusedRules0 = [
  332. {
  333. url: PAGE_2 + " \u2192 <style> index 0",
  334. start: { line: 9, column: 5 },
  335. selectorText: ".page2-test2"
  336. }
  337. ];
  338. isEqualJson(actualReport.unused[0].rules, expectedUnusedRules0, "unused rules 0");
  339. isEqualJson(actualReport.unused[1].url, SHEET_A, "unused url 1");
  340. let expectedUnusedRules1 = [
  341. {
  342. url: SHEET_A,
  343. start: { line: 8, column: 1 },
  344. selectorText: ".sheetA-test2"
  345. }
  346. ];
  347. isEqualJson(actualReport.unused[1].rules, expectedUnusedRules1, "unused rules 1");
  348. isEqualJson(actualReport.unused[2].url, SHEET_B, "unused url 2");
  349. let expectedUnusedRules2 = [
  350. {
  351. url: SHEET_B,
  352. start: { line: 6, column: 1 },
  353. selectorText: ".sheetB-test2"
  354. }
  355. ];
  356. isEqualJson(actualReport.unused[2].rules, expectedUnusedRules2, "unused rules 2");
  357. isEqualJson(actualReport.unused[3].url, SHEET_D, "unused url 3");
  358. let expectedUnusedRules3 = [
  359. {
  360. url: SHEET_D,
  361. start: { line: 6, column: 1 },
  362. selectorText: ".sheetD-test2"
  363. }
  364. ];
  365. isEqualJson(actualReport.unused[3].rules, expectedUnusedRules3, "unused rules 3");
  366. isEqualJson(actualReport.unused[4].url, SHEET_C, "unused url 4");
  367. let expectedUnusedRules4 = [
  368. {
  369. url: SHEET_C,
  370. start: { line: 6, column: 1 },
  371. selectorText: ".sheetC-test2"
  372. }
  373. ];
  374. isEqualJson(actualReport.unused[4].rules, expectedUnusedRules4, "unused rules 4");
  375. isEqualJson(actualReport.unused[5].url, PAGE_1 + " \u2192 <style> index 0", "unused url 5");
  376. let expectedUnusedRules5 = [
  377. {
  378. url: PAGE_1 + " \u2192 <style> index 0",
  379. start: { line: 8, column: 5 },
  380. selectorText: ".page1-test2"
  381. }
  382. ];
  383. isEqualJson(actualReport.unused[5].rules, expectedUnusedRules5, "unused rules 5");
  384. isEqualJson(actualReport.unused[6].url, PAGE_3 + " \u2192 <style> index 0", "unused url 6");
  385. let expectedUnusedRules6 = [
  386. {
  387. url: PAGE_3 + " \u2192 <style> index 0",
  388. start: { line: 9, column: 5 },
  389. selectorText: ".page3-test2"
  390. }
  391. ];
  392. isEqualJson(actualReport.unused[6].rules, expectedUnusedRules6, "unused rules 6");
  393. isEqualJson(actualReport.unused[7].url, PAGE_3 + " \u2192 <style> index 1", "unused url 7");
  394. let expectedUnusedRules7 = [
  395. {
  396. url: PAGE_3 + " \u2192 <style> index 1",
  397. start: { line: 3, column: 5 },
  398. selectorText: ".page3-test3"
  399. }
  400. ];
  401. isEqualJson(actualReport.unused[7].rules, expectedUnusedRules7, "unused rules 7");
  402. }
  403. /**
  404. * We do basic tests on the shortUrl and formattedCssText because they are
  405. * very derivative, and so make for fragile tests, and having done those quick
  406. * existence checks we remove them so the JSON check later can ignore them
  407. */
  408. function checkRuleProperties(rule, index) {
  409. is(typeof rule.shortUrl, "string", "typeof rule.shortUrl for " + index);
  410. is(rule.shortUrl.indexOf("http://"), -1, "http not in rule.shortUrl for" + index);
  411. delete rule.shortUrl;
  412. is(typeof rule.formattedCssText, "string", "typeof rule.formattedCssText for " + index);
  413. ok(rule.formattedCssText.indexOf("{") > 0, "{ in rule.formattedCssText for " + index);
  414. delete rule.formattedCssText;
  415. }
  416. /**
  417. * Utility to compare JSON structures
  418. */
  419. function isEqualJson(o1, o2, msg) {
  420. is(JSON.stringify(o1), JSON.stringify(o2), msg);
  421. }