browser_dbg_sources-labels.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/ */
  4. /**
  5. * Tests that urls are correctly shortened to unique labels.
  6. */
  7. const TAB_URL = EXAMPLE_URL + "doc_recursion-stack.html";
  8. const { ELLIPSIS } = require("devtools/shared/l10n");
  9. function test() {
  10. let gTab, gPanel, gDebugger;
  11. let gSources, gUtils;
  12. let options = {
  13. source: TAB_URL,
  14. line: 1
  15. };
  16. initDebugger(TAB_URL, options).then(Task.async(function* ([aTab,, aPanel]) {
  17. gTab = aTab;
  18. gPanel = aPanel;
  19. gDebugger = gPanel.panelWin;
  20. gSources = gDebugger.DebuggerView.Sources;
  21. gUtils = gDebugger.SourceUtils;
  22. let nananana = new Array(20).join(NaN);
  23. // Test trimming url queries.
  24. let someUrl = "a/b/c.d?test=1&random=4#reference";
  25. let shortenedUrl = "a/b/c.d";
  26. is(gUtils.trimUrlQuery(someUrl), shortenedUrl,
  27. "Trimming the url query isn't done properly.");
  28. // Test trimming long urls with an ellipsis.
  29. let largeLabel = new Array(100).join("Beer can in Jamaican sounds like Bacon!");
  30. let trimmedLargeLabel = gUtils.trimUrlLength(largeLabel, 1234);
  31. is(trimmedLargeLabel.length, 1235,
  32. "Trimming large labels isn't done properly.");
  33. ok(trimmedLargeLabel.endsWith(ELLIPSIS),
  34. "Trimming large labels should add an ellipsis at the end : " + ELLIPSIS);
  35. // Test the sources list behaviour with certain urls.
  36. let urls = [
  37. { href: "http://some.address.com/random/", leaf: "subrandom/" },
  38. { href: "http://some.address.com/random/", leaf: "suprandom/?a=1" },
  39. { href: "http://some.address.com/random/", leaf: "?a=1" },
  40. { href: "https://another.address.org/random/subrandom/", leaf: "page.html" },
  41. { href: "ftp://interesting.address.org/random/", leaf: "script.js" },
  42. { href: "ftp://interesting.address.com/random/", leaf: "script.js" },
  43. { href: "ftp://interesting.address.com/random/", leaf: "x/script.js" },
  44. { href: "ftp://interesting.address.com/random/", leaf: "x/y/script.js?a=1" },
  45. { href: "ftp://interesting.address.com/random/x/", leaf: "y/script.js?a=1&b=2" },
  46. { href: "ftp://interesting.address.com/random/x/y/", leaf: "script.js?a=1&b=2&c=3" },
  47. { href: "ftp://interesting.address.com/random/", leaf: "x/y/script.js?a=2" },
  48. { href: "ftp://interesting.address.com/random/x/", leaf: "y/script.js?a=2&b=3" },
  49. { href: "ftp://interesting.address.com/random/x/y/", leaf: "script.js?a=2&b=3&c=4" },
  50. { href: "file://random/", leaf: "script_t1.js&a=1&b=2&c=3" },
  51. { href: "file://random/", leaf: "script_t2_1.js#id" },
  52. { href: "file://random/", leaf: "script_t2_2.js?a" },
  53. { href: "file://random/", leaf: "script_t2_3.js&b" },
  54. { href: "resource://random/", leaf: "script_t3_1.js#id?a=1&b=2" },
  55. { href: "resource://random/", leaf: "script_t3_2.js?a=1&b=2#id" },
  56. { href: "resource://random/", leaf: "script_t3_3.js&a=1&b=2#id" },
  57. { href: nananana, leaf: "Batman!" + "{trim me, now and forevermore}" }
  58. ];
  59. is(gSources.itemCount, 1,
  60. "Should contain the original source label in the sources widget.");
  61. is(gSources.selectedIndex, 0,
  62. "The first item in the sources widget should be selected (1).");
  63. is(gSources.selectedItem.attachment.label, "doc_recursion-stack.html",
  64. "The first item in the sources widget should be selected (2).");
  65. is(getSelectedSourceURL(gSources), TAB_URL,
  66. "The first item in the sources widget should be selected (3).");
  67. let id = 0;
  68. for (let { href, leaf } of urls) {
  69. let url = href + leaf;
  70. let actor = "actor" + id++;
  71. let label = gUtils.trimUrlLength(gUtils.getSourceLabel(url));
  72. let group = gUtils.getSourceGroup(url);
  73. let dummy = document.createElement("label");
  74. dummy.setAttribute("value", label);
  75. gSources.push([dummy, actor], {
  76. attachment: {
  77. source: { actor: actor, url: url },
  78. label: label,
  79. group: group
  80. }
  81. });
  82. }
  83. info("Source locations:");
  84. info(gSources.values.toSource());
  85. info("Source attachments:");
  86. info(gSources.attachments.toSource());
  87. for (let { href, leaf, dupe } of urls) {
  88. let url = href + leaf;
  89. if (dupe) {
  90. ok(!gSources.containsValue(getSourceActor(gSources, url)), "Shouldn't contain source: " + url);
  91. } else {
  92. ok(gSources.containsValue(getSourceActor(gSources, url)), "Should contain source: " + url);
  93. }
  94. }
  95. ok(gSources.getItemForAttachment(e => e.label == "random/subrandom/"),
  96. "Source (0) label is incorrect.");
  97. ok(gSources.getItemForAttachment(e => e.label == "random/suprandom/?a=1"),
  98. "Source (1) label is incorrect.");
  99. ok(gSources.getItemForAttachment(e => e.label == "random/?a=1"),
  100. "Source (2) label is incorrect.");
  101. ok(gSources.getItemForAttachment(e => e.label == "page.html"),
  102. "Source (3) label is incorrect.");
  103. ok(gSources.getItemForAttachment(e => e.label == "script.js"),
  104. "Source (4) label is incorrect.");
  105. ok(gSources.getItemForAttachment(e => e.label == "random/script.js"),
  106. "Source (5) label is incorrect.");
  107. ok(gSources.getItemForAttachment(e => e.label == "random/x/script.js"),
  108. "Source (6) label is incorrect.");
  109. ok(gSources.getItemForAttachment(e => e.label == "script.js?a=1"),
  110. "Source (7) label is incorrect.");
  111. ok(gSources.getItemForAttachment(e => e.label == "script_t1.js"),
  112. "Source (8) label is incorrect.");
  113. ok(gSources.getItemForAttachment(e => e.label == "script_t2_1.js"),
  114. "Source (9) label is incorrect.");
  115. ok(gSources.getItemForAttachment(e => e.label == "script_t2_2.js"),
  116. "Source (10) label is incorrect.");
  117. ok(gSources.getItemForAttachment(e => e.label == "script_t2_3.js"),
  118. "Source (11) label is incorrect.");
  119. ok(gSources.getItemForAttachment(e => e.label == "script_t3_1.js"),
  120. "Source (12) label is incorrect.");
  121. ok(gSources.getItemForAttachment(e => e.label == "script_t3_2.js"),
  122. "Source (13) label is incorrect.");
  123. ok(gSources.getItemForAttachment(e => e.label == "script_t3_3.js"),
  124. "Source (14) label is incorrect.");
  125. ok(gSources.getItemForAttachment(e => e.label == nananana + "Batman!" + ELLIPSIS),
  126. "Source (15) label is incorrect.");
  127. is(gSources.itemCount, urls.filter(({ dupe }) => !dupe).length + 1,
  128. "Didn't get the correct number of sources in the list.");
  129. is(gSources.getItemByValue(getSourceActor(gSources, "http://some.address.com/random/subrandom/")).attachment.label,
  130. "random/subrandom/",
  131. "gSources.getItemByValue isn't functioning properly (0).");
  132. is(gSources.getItemByValue(getSourceActor(gSources, "http://some.address.com/random/suprandom/?a=1")).attachment.label,
  133. "random/suprandom/?a=1",
  134. "gSources.getItemByValue isn't functioning properly (1).");
  135. is(gSources.getItemForAttachment(e => e.label == "random/subrandom/").attachment.source.url,
  136. "http://some.address.com/random/subrandom/",
  137. "gSources.getItemForAttachment isn't functioning properly (0).");
  138. is(gSources.getItemForAttachment(e => e.label == "random/suprandom/?a=1").attachment.source.url,
  139. "http://some.address.com/random/suprandom/?a=1",
  140. "gSources.getItemForAttachment isn't functioning properly (1).");
  141. closeDebuggerAndFinish(gPanel);
  142. }));
  143. }