test_AboutNewTabService.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* Any copyright is dedicated to the Public Domain.
  2. * http://creativecommons.org/publicdomain/zero/1.0/
  3. */
  4. "use strict";
  5. const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
  6. const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
  7. const {AppConstants} = ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
  8. XPCOMUtils.defineLazyServiceGetter(this, "aboutNewTabService",
  9. "@mozilla.org/browser/aboutnewtab-service;1",
  10. "nsIAboutNewTabService");
  11. const IS_RELEASE_OR_BETA = AppConstants.RELEASE_OR_BETA;
  12. const DOWNLOADS_URL = "chrome://browser/content/downloads/contentAreaDownloadsView.xul";
  13. const SEPARATE_PRIVILEGED_CONTENT_PROCESS_PREF = "browser.tabs.remote.separatePrivilegedContentProcess";
  14. const ACTIVITY_STREAM_DEBUG_PREF = "browser.newtabpage.activity-stream.debug";
  15. function cleanup() {
  16. Services.prefs.clearUserPref(SEPARATE_PRIVILEGED_CONTENT_PROCESS_PREF);
  17. Services.prefs.clearUserPref(ACTIVITY_STREAM_DEBUG_PREF);
  18. aboutNewTabService.resetNewTabURL();
  19. }
  20. registerCleanupFunction(cleanup);
  21. let ACTIVITY_STREAM_URL;
  22. let ACTIVITY_STREAM_DEBUG_URL;
  23. function setExpectedUrlsWithScripts() {
  24. ACTIVITY_STREAM_URL = "resource://activity-stream/prerendered/en-US/activity-stream.html";
  25. ACTIVITY_STREAM_DEBUG_URL = "resource://activity-stream/prerendered/static/activity-stream-debug.html";
  26. }
  27. function setExpectedUrlsWithoutScripts() {
  28. ACTIVITY_STREAM_URL = "resource://activity-stream/prerendered/en-US/activity-stream-noscripts.html";
  29. // Debug urls are the same as non-debug because debug scripts load dynamically
  30. ACTIVITY_STREAM_DEBUG_URL = ACTIVITY_STREAM_URL;
  31. }
  32. function nextChangeNotificationPromise(aNewURL, testMessage) {
  33. return new Promise(resolve => {
  34. Services.obs.addObserver(function observer(aSubject, aTopic, aData) { // jshint unused:false
  35. Services.obs.removeObserver(observer, aTopic);
  36. Assert.equal(aData, aNewURL, testMessage);
  37. resolve();
  38. }, "newtab-url-changed");
  39. });
  40. }
  41. function setPrivilegedContentProcessPref(usePrivilegedContentProcess) {
  42. if (usePrivilegedContentProcess === Services.prefs.getBoolPref(SEPARATE_PRIVILEGED_CONTENT_PROCESS_PREF)) {
  43. return Promise.resolve();
  44. }
  45. let notificationPromise = nextChangeNotificationPromise("about:newtab");
  46. Services.prefs.setBoolPref(SEPARATE_PRIVILEGED_CONTENT_PROCESS_PREF, usePrivilegedContentProcess);
  47. return notificationPromise;
  48. }
  49. // Default expected URLs to files with scripts in them.
  50. setExpectedUrlsWithScripts();
  51. function addTestsWithPrivilegedContentProcessPref(test) {
  52. add_task(async () => {
  53. await setPrivilegedContentProcessPref(true);
  54. setExpectedUrlsWithoutScripts();
  55. await test();
  56. });
  57. add_task(async () => {
  58. await setPrivilegedContentProcessPref(false);
  59. setExpectedUrlsWithScripts();
  60. await test();
  61. });
  62. }
  63. function setBoolPrefAndWaitForChange(pref, value, testMessage) {
  64. return new Promise(resolve => {
  65. Services.obs.addObserver(function observer(aSubject, aTopic, aData) { // jshint unused:false
  66. Services.obs.removeObserver(observer, aTopic);
  67. Assert.equal(aData, aboutNewTabService.newTabURL, testMessage);
  68. resolve();
  69. }, "newtab-url-changed");
  70. Services.prefs.setBoolPref(pref, value);
  71. });
  72. }
  73. add_task(async function test_as_initial_values() {
  74. Assert.ok(aboutNewTabService.activityStreamEnabled,
  75. ".activityStreamEnabled should be set to the correct initial value");
  76. // This pref isn't defined on release or beta, so we fall back to false
  77. Assert.equal(aboutNewTabService.activityStreamDebug, Services.prefs.getBoolPref(ACTIVITY_STREAM_DEBUG_PREF, false),
  78. ".activityStreamDebug should be set to the correct initial value");
  79. });
  80. /**
  81. * Test the overriding of the default URL
  82. */
  83. add_task(async function test_override_activity_stream_disabled() {
  84. let notificationPromise;
  85. // override with some remote URL
  86. let url = "http://example.com/";
  87. notificationPromise = nextChangeNotificationPromise(url);
  88. aboutNewTabService.newTabURL = url;
  89. await notificationPromise;
  90. Assert.ok(aboutNewTabService.overridden, "Newtab URL should be overridden");
  91. Assert.ok(!aboutNewTabService.activityStreamEnabled, "Newtab activity stream should not be enabled");
  92. Assert.equal(aboutNewTabService.newTabURL, url, "Newtab URL should be the custom URL");
  93. // test reset with activity stream disabled
  94. notificationPromise = nextChangeNotificationPromise("about:newtab");
  95. aboutNewTabService.resetNewTabURL();
  96. await notificationPromise;
  97. Assert.ok(!aboutNewTabService.overridden, "Newtab URL should not be overridden");
  98. Assert.equal(aboutNewTabService.newTabURL, "about:newtab", "Newtab URL should be the default");
  99. // test override to a chrome URL
  100. notificationPromise = nextChangeNotificationPromise(DOWNLOADS_URL);
  101. aboutNewTabService.newTabURL = DOWNLOADS_URL;
  102. await notificationPromise;
  103. Assert.ok(aboutNewTabService.overridden, "Newtab URL should be overridden");
  104. Assert.equal(aboutNewTabService.newTabURL, DOWNLOADS_URL, "Newtab URL should be the custom URL");
  105. cleanup();
  106. });
  107. addTestsWithPrivilegedContentProcessPref(async function test_override_activity_stream_enabled() {
  108. Assert.equal(aboutNewTabService.defaultURL, ACTIVITY_STREAM_URL,
  109. "Newtab URL should be the default activity stream URL");
  110. Assert.ok(!aboutNewTabService.overridden, "Newtab URL should not be overridden");
  111. Assert.ok(aboutNewTabService.activityStreamEnabled, "Activity Stream should be enabled");
  112. // change to a chrome URL while activity stream is enabled
  113. let notificationPromise = nextChangeNotificationPromise(DOWNLOADS_URL);
  114. aboutNewTabService.newTabURL = DOWNLOADS_URL;
  115. await notificationPromise;
  116. Assert.equal(aboutNewTabService.newTabURL, DOWNLOADS_URL,
  117. "Newtab URL set to chrome url");
  118. Assert.equal(aboutNewTabService.defaultURL, ACTIVITY_STREAM_URL,
  119. "Newtab URL defaultURL still set to the default activity stream URL");
  120. Assert.ok(aboutNewTabService.overridden, "Newtab URL should be overridden");
  121. Assert.ok(!aboutNewTabService.activityStreamEnabled, "Activity Stream should not be enabled");
  122. cleanup();
  123. });
  124. addTestsWithPrivilegedContentProcessPref(async function test_default_url() {
  125. Assert.equal(aboutNewTabService.defaultURL, ACTIVITY_STREAM_URL,
  126. "Newtab defaultURL initially set to AS url");
  127. // Only debug variants aren't available on release/beta
  128. if (!IS_RELEASE_OR_BETA) {
  129. await setBoolPrefAndWaitForChange(ACTIVITY_STREAM_DEBUG_PREF, true,
  130. "A notification occurs after changing the debug pref to true");
  131. Assert.equal(aboutNewTabService.activityStreamDebug, true,
  132. "the .activityStreamDebug property is set to true");
  133. Assert.equal(aboutNewTabService.defaultURL, ACTIVITY_STREAM_DEBUG_URL,
  134. "Newtab defaultURL set to debug AS url after the pref has been changed");
  135. await setBoolPrefAndWaitForChange(ACTIVITY_STREAM_DEBUG_PREF, false,
  136. "A notification occurs after changing the debug pref to false");
  137. } else {
  138. Services.prefs.setBoolPref(ACTIVITY_STREAM_DEBUG_PREF, true);
  139. Assert.equal(aboutNewTabService.activityStreamDebug, false,
  140. "the .activityStreamDebug property is remains false");
  141. }
  142. Assert.equal(aboutNewTabService.defaultURL, ACTIVITY_STREAM_URL,
  143. "Newtab defaultURL set to un-prerendered AS if prerender is false and debug is false");
  144. cleanup();
  145. });
  146. addTestsWithPrivilegedContentProcessPref(async function test_welcome_url() {
  147. Assert.equal(aboutNewTabService.welcomeURL, ACTIVITY_STREAM_URL,
  148. "Newtab welcomeURL set to un-prerendered AS when debug disabled.");
  149. Assert.equal(aboutNewTabService.welcomeURL, aboutNewTabService.defaultURL,
  150. "Newtab welcomeURL is equal to defaultURL when prerendering disabled and debug disabled.");
  151. // Only debug variants aren't available on release/beta
  152. if (!IS_RELEASE_OR_BETA) {
  153. await setBoolPrefAndWaitForChange(ACTIVITY_STREAM_DEBUG_PREF, true,
  154. "A notification occurs after changing the debug pref to true.");
  155. Assert.equal(aboutNewTabService.welcomeURL, ACTIVITY_STREAM_DEBUG_URL,
  156. "Newtab welcomeURL set to un-prerendered debug AS when debug enabled");
  157. }
  158. cleanup();
  159. });
  160. add_task(function test_locale() {
  161. Assert.equal(aboutNewTabService.activityStreamLocale, "en-US",
  162. "The locale for testing should be en-US");
  163. });
  164. /**
  165. * Tests response to updates to prefs
  166. */
  167. addTestsWithPrivilegedContentProcessPref(async function test_updates() {
  168. // Simulates a "cold-boot" situation, with some pref already set before testing a series
  169. // of changes.
  170. aboutNewTabService.resetNewTabURL(); // need to set manually because pref notifs are off
  171. let notificationPromise;
  172. // test update fires on override and reset
  173. let testURL = "https://example.com/";
  174. notificationPromise = nextChangeNotificationPromise(
  175. testURL, "a notification occurs on override");
  176. aboutNewTabService.newTabURL = testURL;
  177. await notificationPromise;
  178. // from overridden to default
  179. notificationPromise = nextChangeNotificationPromise(
  180. "about:newtab", "a notification occurs on reset");
  181. aboutNewTabService.resetNewTabURL();
  182. Assert.ok(aboutNewTabService.activityStreamEnabled, "Activity Stream should be enabled");
  183. Assert.equal(aboutNewTabService.defaultURL, ACTIVITY_STREAM_URL, "Default URL should be the activity stream page");
  184. await notificationPromise;
  185. // reset twice, only one notification for default URL
  186. notificationPromise = nextChangeNotificationPromise(
  187. "about:newtab", "reset occurs");
  188. aboutNewTabService.resetNewTabURL();
  189. await notificationPromise;
  190. cleanup();
  191. });