browser_newtab_overrides.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. XPCOMUtils.defineLazyServiceGetter(this, "aboutNewTabService",
  3. "@mozilla.org/browser/aboutnewtab-service;1",
  4. "nsIAboutNewTabService");
  5. registerCleanupFunction(() => {
  6. aboutNewTabService.resetNewTabURL();
  7. });
  8. function nextChangeNotificationPromise(aNewURL, testMessage) {
  9. return TestUtils.topicObserved("newtab-url-changed", function observer(aSubject, aData) { // jshint unused:false
  10. Assert.equal(aData, aNewURL, testMessage);
  11. return true;
  12. });
  13. }
  14. /*
  15. * Tests that the default newtab page is always returned when one types "about:newtab" in the URL bar,
  16. * even when overridden.
  17. */
  18. add_task(async function redirector_ignores_override() {
  19. let overrides = [
  20. "chrome://browser/content/aboutRobots.xhtml",
  21. "about:home",
  22. ];
  23. for (let overrideURL of overrides) {
  24. let notificationPromise = nextChangeNotificationPromise(overrideURL, `newtab page now points to ${overrideURL}`);
  25. aboutNewTabService.newTabURL = overrideURL;
  26. await notificationPromise;
  27. Assert.ok(aboutNewTabService.overridden, "url has been overridden");
  28. let tabOptions = {
  29. gBrowser,
  30. url: "about:newtab",
  31. };
  32. /*
  33. * Simulate typing "about:newtab" in the url bar.
  34. *
  35. * Bug 1240169 - We expect the redirector to lead the user to "about:newtab", the default URL,
  36. * due to invoking AboutRedirector. A user interacting with the chrome otherwise would lead
  37. * to the overriding URLs.
  38. */
  39. await BrowserTestUtils.withNewTab(tabOptions, async browser => {
  40. await ContentTask.spawn(browser, {}, async () => {
  41. Assert.equal(content.location.href, "about:newtab", "Got right URL");
  42. Assert.equal(content.document.location.href, "about:newtab", "Got right URL");
  43. Assert.notEqual(content.document.nodePrincipal,
  44. Services.scriptSecurityManager.getSystemPrincipal(),
  45. "activity stream principal should not match systemPrincipal");
  46. });
  47. }); // jshint ignore:line
  48. }
  49. });
  50. /*
  51. * Tests loading an overridden newtab page by simulating opening a newtab page from chrome
  52. */
  53. add_task(async function override_loads_in_browser() {
  54. let overrides = [
  55. "chrome://browser/content/aboutRobots.xhtml",
  56. "about:home",
  57. " about:home",
  58. ];
  59. for (let overrideURL of overrides) {
  60. let notificationPromise = nextChangeNotificationPromise(overrideURL.trim(), `newtab page now points to ${overrideURL}`);
  61. aboutNewTabService.newTabURL = overrideURL;
  62. await notificationPromise;
  63. Assert.ok(aboutNewTabService.overridden, "url has been overridden");
  64. // simulate a newtab open as a user would
  65. BrowserOpenTab(); // jshint ignore:line
  66. let browser = gBrowser.selectedBrowser;
  67. await BrowserTestUtils.browserLoaded(browser);
  68. await ContentTask.spawn(browser, {url: overrideURL}, async args => {
  69. Assert.equal(content.location.href, args.url.trim(), "Got right URL");
  70. Assert.equal(content.document.location.href, args.url.trim(), "Got right URL");
  71. }); // jshint ignore:line
  72. BrowserTestUtils.removeTab(gBrowser.selectedTab);
  73. }
  74. });
  75. /*
  76. * Tests edge cases when someone overrides the newtabpage with whitespace
  77. */
  78. add_task(async function override_blank_loads_in_browser() {
  79. let overrides = [
  80. "",
  81. " ",
  82. "\n\t",
  83. " about:blank",
  84. ];
  85. for (let overrideURL of overrides) {
  86. let notificationPromise = nextChangeNotificationPromise("about:blank", "newtab page now points to about:blank");
  87. aboutNewTabService.newTabURL = overrideURL;
  88. await notificationPromise;
  89. Assert.ok(aboutNewTabService.overridden, "url has been overridden");
  90. // simulate a newtab open as a user would
  91. BrowserOpenTab(); // jshint ignore:line
  92. let browser = gBrowser.selectedBrowser;
  93. await BrowserTestUtils.browserLoaded(browser);
  94. await ContentTask.spawn(browser, {}, async () => {
  95. Assert.equal(content.location.href, "about:blank", "Got right URL");
  96. Assert.equal(content.document.location.href, "about:blank", "Got right URL");
  97. }); // jshint ignore:line
  98. BrowserTestUtils.removeTab(gBrowser.selectedTab);
  99. }
  100. });