mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 1137355 - Add test coverage for the Data Choices notification bar in Telemetry. r=gfritzsche
This commit is contained in:
parent
82cf18528b
commit
e87f646256
@ -278,6 +278,8 @@ skip-if = os == "mac" || e10s # bug 967013; e10s: bug 1094761 - test hits the ne
|
||||
[browser_ctrlTab.js]
|
||||
[browser_datareporting_notification.js]
|
||||
skip-if = !datareporting
|
||||
[browser_datachoices_notification.js]
|
||||
skip-if = !datareporting
|
||||
[browser_devedition.js]
|
||||
[browser_devices_get_user_media.js]
|
||||
skip-if = buildapp == 'mulet' || (os == "linux" && debug) || e10s # linux: bug 976544; e10s: bug 1071623
|
||||
|
@ -0,0 +1,213 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
"use strict";
|
||||
|
||||
// Pass an empty scope object to the import to prevent "leaked window property"
|
||||
// errors in tests.
|
||||
let Preferences = Cu.import("resource://gre/modules/Preferences.jsm", {}).Preferences;
|
||||
let TelemetryReportingPolicy =
|
||||
Cu.import("resource://gre/modules/TelemetryReportingPolicy.jsm", {}).TelemetryReportingPolicy;
|
||||
|
||||
XPCOMUtils.defineLazyGetter(this, "gDatareportingService",
|
||||
() => Cc["@mozilla.org/datareporting/service;1"]
|
||||
.getService(Ci.nsISupports)
|
||||
.wrappedJSObject);
|
||||
|
||||
const PREF_BRANCH = "datareporting.policy.";
|
||||
const PREF_DRS_ENABLED = "datareporting.healthreport.service.enabled";
|
||||
const PREF_BYPASS_NOTIFICATION = PREF_BRANCH + "dataSubmissionPolicyBypassNotification";
|
||||
const PREF_CURRENT_POLICY_VERSION = PREF_BRANCH + "currentPolicyVersion";
|
||||
const PREF_ACCEPTED_POLICY_VERSION = PREF_BRANCH + "dataSubmissionPolicyAcceptedVersion";
|
||||
const PREF_ACCEPTED_POLICY_DATE = PREF_BRANCH + "dataSubmissionPolicyNotifiedTime";
|
||||
|
||||
const TEST_POLICY_VERSION = 37;
|
||||
|
||||
/**
|
||||
* Wait for a tick.
|
||||
*/
|
||||
function promiseNextTick() {
|
||||
return new Promise(resolve => executeSoon(resolve));
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for a notification to be shown in a notification box.
|
||||
* @param {Object} aNotificationBox The notification box.
|
||||
* @return {Promise} Resolved when the notification is displayed.
|
||||
*/
|
||||
function promiseWaitForAlertActive(aNotificationBox) {
|
||||
let deferred = PromiseUtils.defer();
|
||||
aNotificationBox.addEventListener("AlertActive", function onActive() {
|
||||
aNotificationBox.removeEventListener("AlertActive", onActive, true);
|
||||
deferred.resolve();
|
||||
});
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for a notification to be closed.
|
||||
* @param {Object} aNotification The notification.
|
||||
* @return {Promise} Resolved when the notification is closed.
|
||||
*/
|
||||
function promiseWaitForNotificationClose(aNotification) {
|
||||
let deferred = PromiseUtils.defer();
|
||||
waitForNotificationClose(aNotification, deferred.resolve);
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
let checkInfobarButton = Task.async(function* (aNotification) {
|
||||
// Check that the button on the data choices infobar does the right thing.
|
||||
let buttons = aNotification.getElementsByTagName("button");
|
||||
Assert.equal(buttons.length, 1, "There is 1 button in the data reporting notification.");
|
||||
let button = buttons[0];
|
||||
|
||||
// Add an observer to ensure the "advanced" pane opened (but don't bother
|
||||
// closing it - we close the entire window when done.)
|
||||
let paneLoadedPromise = promiseTopicObserved("advanced-pane-loaded");
|
||||
|
||||
// Click on the button.
|
||||
button.click();
|
||||
|
||||
// Wait for the preferences panel to open.
|
||||
let preferenceWindow = yield paneLoadedPromise;
|
||||
yield promiseNextTick();
|
||||
// If the prefs are being displayed in a dialog we need to close it.
|
||||
// If in a tab (ie, in-content prefs) it closes with the window.
|
||||
if (!Services.prefs.getBoolPref("browser.preferences.inContent")) {
|
||||
prefWin.close();
|
||||
}
|
||||
});
|
||||
|
||||
add_task(function* setup(){
|
||||
const drsEnabled = Preferences.get(PREF_DRS_ENABLED, true);
|
||||
const bypassNotification = Preferences.get(PREF_BYPASS_NOTIFICATION, true);
|
||||
const currentPolicyVersion = Preferences.get(PREF_CURRENT_POLICY_VERSION, 1);
|
||||
|
||||
// Register a cleanup function to reset our preferences.
|
||||
registerCleanupFunction(() => {
|
||||
Preferences.set(PREF_DRS_ENABLED, drsEnabled);
|
||||
Preferences.set(PREF_BYPASS_NOTIFICATION, bypassNotification);
|
||||
Preferences.set(PREF_CURRENT_POLICY_VERSION, currentPolicyVersion);
|
||||
|
||||
// Start polling again.
|
||||
gDatareportingService.policy.startPolling();
|
||||
|
||||
return closeAllNotifications();
|
||||
});
|
||||
|
||||
// Disable Healthreport/Data reporting service.
|
||||
Preferences.set(PREF_DRS_ENABLED, false);
|
||||
// Don't skip the infobar visualisation.
|
||||
Preferences.set(PREF_BYPASS_NOTIFICATION, false);
|
||||
// Set the current policy version.
|
||||
Preferences.set(PREF_CURRENT_POLICY_VERSION, TEST_POLICY_VERSION);
|
||||
|
||||
// Stop the polling to make sure no policy gets displayed by FHR.
|
||||
gDatareportingService.policy.stopPolling();
|
||||
});
|
||||
|
||||
function clearAcceptedPolicy() {
|
||||
// Reset the accepted policy.
|
||||
Preferences.reset(PREF_ACCEPTED_POLICY_VERSION);
|
||||
Preferences.reset(PREF_ACCEPTED_POLICY_DATE);
|
||||
}
|
||||
|
||||
add_task(function* test_single_window(){
|
||||
clearAcceptedPolicy();
|
||||
|
||||
// Close all the notifications, then try to trigger the data choices infobar.
|
||||
yield closeAllNotifications();
|
||||
|
||||
let notificationBox = document.getElementById("global-notificationbox");
|
||||
|
||||
// Make sure that we have a coherent initial state.
|
||||
Assert.equal(Preferences.get(PREF_ACCEPTED_POLICY_VERSION, 0), 0,
|
||||
"No version should be set on init.");
|
||||
Assert.equal(Preferences.get(PREF_ACCEPTED_POLICY_DATE, 0), 0,
|
||||
"No date should be set on init.");
|
||||
Assert.ok(!TelemetryReportingPolicy.testIsUserNotified(),
|
||||
"User not notified about datareporting policy.");
|
||||
|
||||
let alertShownPromise = promiseWaitForAlertActive(notificationBox);
|
||||
// This should be false and trigger the Infobar.
|
||||
Assert.ok(!TelemetryReportingPolicy.canUpload(),
|
||||
"User should not be allowed to upload and the infobar should be triggered.");
|
||||
|
||||
// Wait for the infobar to be displayed.
|
||||
yield alertShownPromise;
|
||||
|
||||
Assert.equal(notificationBox.allNotifications.length, 1, "Notification Displayed.");
|
||||
Assert.ok(TelemetryReportingPolicy.canUpload(), "User should be allowed to upload now.");
|
||||
|
||||
yield promiseNextTick();
|
||||
let promiseClosed = promiseWaitForNotificationClose(notificationBox.currentNotification);
|
||||
yield checkInfobarButton(notificationBox.currentNotification);
|
||||
yield promiseClosed;
|
||||
|
||||
Assert.equal(notificationBox.allNotifications.length, 0, "No notifications remain.");
|
||||
|
||||
// Check that we are still clear to upload and that the policy data is saved.
|
||||
Assert.ok(TelemetryReportingPolicy.canUpload());
|
||||
Assert.equal(TelemetryReportingPolicy.testIsUserNotified(), true,
|
||||
"User notified about datareporting policy.");
|
||||
Assert.equal(Preferences.get(PREF_ACCEPTED_POLICY_VERSION, 0), TEST_POLICY_VERSION,
|
||||
"Version pref set.");
|
||||
Assert.greater(parseInt(Preferences.get(PREF_ACCEPTED_POLICY_DATE, null), 10), -1,
|
||||
"Date pref set.");
|
||||
});
|
||||
|
||||
add_task(function* test_multiple_windows(){
|
||||
clearAcceptedPolicy();
|
||||
|
||||
// Close all the notifications, then try to trigger the data choices infobar.
|
||||
yield closeAllNotifications();
|
||||
|
||||
// Ensure we see the notification on all windows and that action on one window
|
||||
// results in dismiss on every window.
|
||||
let otherWindow = yield BrowserTestUtils.openNewBrowserWindow();
|
||||
|
||||
// Get the notification box for both windows.
|
||||
let notificationBoxes = [
|
||||
document.getElementById("global-notificationbox"),
|
||||
otherWindow.document.getElementById("global-notificationbox")
|
||||
];
|
||||
|
||||
Assert.ok(notificationBoxes[1], "2nd window has a global notification box.");
|
||||
|
||||
// Make sure that we have a coherent initial state.
|
||||
Assert.equal(Preferences.get(PREF_ACCEPTED_POLICY_VERSION, 0), 0, "No version should be set on init.");
|
||||
Assert.equal(Preferences.get(PREF_ACCEPTED_POLICY_DATE, 0), 0, "No date should be set on init.");
|
||||
Assert.ok(!TelemetryReportingPolicy.testIsUserNotified(), "User not notified about datareporting policy.");
|
||||
|
||||
let showAlertPromises = [
|
||||
promiseWaitForAlertActive(notificationBoxes[0]),
|
||||
promiseWaitForAlertActive(notificationBoxes[1])
|
||||
];
|
||||
|
||||
// This should be false and trigger the Infobar.
|
||||
Assert.ok(!TelemetryReportingPolicy.canUpload(),
|
||||
"User should not be allowed to upload and the infobar should be triggered.");
|
||||
|
||||
yield Promise.all(showAlertPromises);
|
||||
|
||||
// Both notification were displayed. Close one and check that both gets closed.
|
||||
let closeAlertPromises = [
|
||||
promiseWaitForNotificationClose(notificationBoxes[0].currentNotification),
|
||||
promiseWaitForNotificationClose(notificationBoxes[1].currentNotification)
|
||||
];
|
||||
notificationBoxes[0].currentNotification.close();
|
||||
yield Promise.all(closeAlertPromises);
|
||||
|
||||
// Close the second window we opened.
|
||||
yield BrowserTestUtils.closeWindow(otherWindow);
|
||||
|
||||
// Check that we are clear to upload and that the policy data us saved.
|
||||
Assert.ok(TelemetryReportingPolicy.canUpload(),"User should be allowed to upload now.");
|
||||
Assert.equal(TelemetryReportingPolicy.testIsUserNotified(), true,
|
||||
"User notified about datareporting policy.");
|
||||
Assert.equal(Preferences.get(PREF_ACCEPTED_POLICY_VERSION, 0), TEST_POLICY_VERSION,
|
||||
"Version pref set.");
|
||||
Assert.greater(parseInt(Preferences.get(PREF_ACCEPTED_POLICY_DATE, null), 10), -1,
|
||||
"Date pref set.");
|
||||
});
|
@ -47,30 +47,6 @@ function sendNotifyRequest(name) {
|
||||
return [policy, deferred.promise];
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for a <notification> to be closed then call the specified callback.
|
||||
*/
|
||||
function waitForNotificationClose(notification, cb) {
|
||||
let parent = notification.parentNode;
|
||||
|
||||
let observer = new MutationObserver(function onMutatations(mutations) {
|
||||
for (let mutation of mutations) {
|
||||
for (let i = 0; i < mutation.removedNodes.length; i++) {
|
||||
let node = mutation.removedNodes.item(i);
|
||||
|
||||
if (node != notification) {
|
||||
continue;
|
||||
}
|
||||
|
||||
observer.disconnect();
|
||||
cb();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
observer.observe(parent, {childList: true});
|
||||
}
|
||||
|
||||
let dumpAppender, rootLogger;
|
||||
|
||||
function test() {
|
||||
|
@ -9,6 +9,27 @@ XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "PlacesTestUtils",
|
||||
"resource://testing-common/PlacesTestUtils.jsm");
|
||||
|
||||
/**
|
||||
* Wait for a <notification> to be closed then call the specified callback.
|
||||
*/
|
||||
function waitForNotificationClose(notification, cb) {
|
||||
let parent = notification.parentNode;
|
||||
|
||||
let observer = new MutationObserver(function onMutatations(mutations) {
|
||||
for (let mutation of mutations) {
|
||||
for (let i = 0; i < mutation.removedNodes.length; i++) {
|
||||
let node = mutation.removedNodes.item(i);
|
||||
if (node != notification) {
|
||||
continue;
|
||||
}
|
||||
observer.disconnect();
|
||||
cb();
|
||||
}
|
||||
}
|
||||
});
|
||||
observer.observe(parent, {childList: true});
|
||||
}
|
||||
|
||||
function closeAllNotifications () {
|
||||
let notificationBox = document.getElementById("global-notificationbox");
|
||||
|
||||
|
@ -61,6 +61,17 @@ const NOTIFICATION_DELAY_FIRST_RUN_MSEC = 60 * 1000; // 60s
|
||||
// Same as above, for the next runs.
|
||||
const NOTIFICATION_DELAY_NEXT_RUNS_MSEC = 10 * 1000; // 10s
|
||||
|
||||
/**
|
||||
* This is a policy object used to override behavior within this module.
|
||||
* Tests override properties on this object to allow for control of behavior
|
||||
* that would otherwise be very hard to cover.
|
||||
*/
|
||||
let Policy = {
|
||||
now: () => new Date(),
|
||||
setShowInfobarTimeout: (callback, delayMs) => setTimeout(callback, delayMs),
|
||||
clearShowInfobarTimeout: (id) => clearTimeout(id),
|
||||
};
|
||||
|
||||
/**
|
||||
* Represents a request to display data policy.
|
||||
*
|
||||
@ -130,6 +141,27 @@ this.TelemetryReportingPolicy = {
|
||||
canUpload: function() {
|
||||
return TelemetryReportingPolicyImpl.canUpload();
|
||||
},
|
||||
|
||||
/**
|
||||
* Test only method, restarts the policy.
|
||||
*/
|
||||
reset: function() {
|
||||
return TelemetryReportingPolicyImpl.reset();
|
||||
},
|
||||
|
||||
/**
|
||||
* Test only method, used to check if user is notified of the policy in tests.
|
||||
*/
|
||||
testIsUserNotified: function() {
|
||||
return TelemetryReportingPolicyImpl.isUserNotifiedOfCurrentPolicy;
|
||||
},
|
||||
|
||||
/**
|
||||
* Test only method, used to simulate the infobar being shown in xpcshell tests.
|
||||
*/
|
||||
testInfobarShown: function() {
|
||||
return TelemetryReportingPolicyImpl._infobarShownCallback();
|
||||
},
|
||||
};
|
||||
|
||||
let TelemetryReportingPolicyImpl = {
|
||||
@ -251,6 +283,14 @@ let TelemetryReportingPolicyImpl = {
|
||||
return true;
|
||||
},
|
||||
|
||||
/**
|
||||
* Test only method, restarts the policy.
|
||||
*/
|
||||
reset: function() {
|
||||
this.shutdown();
|
||||
return this.setup();
|
||||
},
|
||||
|
||||
/**
|
||||
* Setup the policy.
|
||||
*/
|
||||
@ -272,7 +312,7 @@ let TelemetryReportingPolicyImpl = {
|
||||
|
||||
this._detachObservers();
|
||||
|
||||
clearTimeout(this._startupNotificationTimerId);
|
||||
Policy.clearShowInfobarTimeout(this._startupNotificationTimerId);
|
||||
},
|
||||
|
||||
/**
|
||||
@ -354,7 +394,7 @@ let TelemetryReportingPolicyImpl = {
|
||||
*/
|
||||
_recordNotificationData: function() {
|
||||
this._log.trace("_recordNotificationData");
|
||||
this.dataSubmissionPolicyNotifiedDate = new Date();
|
||||
this.dataSubmissionPolicyNotifiedDate = Policy.now();
|
||||
this.dataSubmissionPolicyAcceptedVersion = this.currentPolicyVersion;
|
||||
// The user was notified and the notification data saved: the notification
|
||||
// is no longer in progress.
|
||||
@ -370,7 +410,7 @@ let TelemetryReportingPolicyImpl = {
|
||||
const delay =
|
||||
isFirstRun ? NOTIFICATION_DELAY_FIRST_RUN_MSEC: NOTIFICATION_DELAY_NEXT_RUNS_MSEC;
|
||||
|
||||
this._startupNotificationTimerId = setTimeout(
|
||||
this._startupNotificationTimerId = Policy.setShowInfobarTimeout(
|
||||
// Calling |canUpload| eventually shows the infobar, if needed.
|
||||
() => this.canUpload(), delay);
|
||||
// We performed at least a run, flip the firstRun preference.
|
||||
|
@ -238,6 +238,7 @@ function fakeNow(...args) {
|
||||
Cu.import("resource://gre/modules/TelemetryController.jsm"),
|
||||
Cu.import("resource://gre/modules/TelemetryStorage.jsm"),
|
||||
Cu.import("resource://gre/modules/TelemetrySend.jsm"),
|
||||
Cu.import("resource://gre/modules/TelemetryReportingPolicy.jsm"),
|
||||
];
|
||||
|
||||
for (let m of modules) {
|
||||
@ -285,6 +286,8 @@ if (runningInParent) {
|
||||
Services.prefs.setCharPref("toolkit.telemetry.log.level", "Trace");
|
||||
// Telemetry archiving should be on.
|
||||
Services.prefs.setBoolPref("toolkit.telemetry.archive.enabled", true);
|
||||
// Telemetry xpcshell tests cannot show the infobar.
|
||||
Services.prefs.setBoolPref("datareporting.policy.dataSubmissionPolicyBypassNotification", true);
|
||||
|
||||
fakePingSendTimer((callback, timeout) => {
|
||||
Services.tm.mainThread.dispatch(() => callback(), Ci.nsIThread.DISPATCH_NORMAL);
|
||||
|
@ -0,0 +1,255 @@
|
||||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
// Test that TelemetryController sends close to shutdown don't lead
|
||||
// to AsyncShutdown timeouts.
|
||||
|
||||
"use strict";
|
||||
|
||||
Cu.import("resource://gre/modules/Preferences.jsm", this);
|
||||
Cu.import("resource://gre/modules/Services.jsm", this);
|
||||
Cu.import("resource://gre/modules/TelemetryController.jsm", this);
|
||||
Cu.import("resource://gre/modules/TelemetrySend.jsm", this);
|
||||
Cu.import("resource://gre/modules/TelemetryReportingPolicy.jsm", this);
|
||||
Cu.import("resource://gre/modules/TelemetryUtils.jsm", this);
|
||||
Cu.import("resource://gre/modules/Timer.jsm", this);
|
||||
Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
|
||||
|
||||
const PREF_BRANCH = "toolkit.telemetry.";
|
||||
const PREF_ENABLED = PREF_BRANCH + "enabled";
|
||||
const PREF_SERVER = PREF_BRANCH + "server";
|
||||
const PREF_DRS_ENABLED = "datareporting.healthreport.service.enabled";
|
||||
|
||||
const TEST_CHANNEL = "TestChannelABC";
|
||||
|
||||
const PREF_POLICY_BRANCH = "datareporting.policy.";
|
||||
const PREF_BYPASS_NOTIFICATION = PREF_POLICY_BRANCH + "dataSubmissionPolicyBypassNotification";
|
||||
const PREF_DATA_SUBMISSION_ENABLED = PREF_POLICY_BRANCH + "dataSubmissionEnabled";
|
||||
const PREF_CURRENT_POLICY_VERSION = PREF_POLICY_BRANCH + "currentPolicyVersion";
|
||||
const PREF_MINIMUM_POLICY_VERSION = PREF_POLICY_BRANCH + "minimumPolicyVersion";
|
||||
const PREF_MINIMUM_CHANNEL_POLICY_VERSION = PREF_MINIMUM_POLICY_VERSION + ".channel-" + TEST_CHANNEL;
|
||||
const PREF_ACCEPTED_POLICY_VERSION = PREF_POLICY_BRANCH + "dataSubmissionPolicyAcceptedVersion";
|
||||
const PREF_ACCEPTED_POLICY_DATE = PREF_POLICY_BRANCH + "dataSubmissionPolicyNotifiedTime";
|
||||
|
||||
function fakeShowPolicyTimeout(set, clear) {
|
||||
let reportingPolicy = Cu.import("resource://gre/modules/TelemetryReportingPolicy.jsm");
|
||||
reportingPolicy.Policy.setShowInfobarTimeout = set;
|
||||
reportingPolicy.Policy.clearShowInfobarTimeout = clear;
|
||||
}
|
||||
|
||||
function fakeResetAcceptedPolicy() {
|
||||
Preferences.reset(PREF_ACCEPTED_POLICY_DATE);
|
||||
Preferences.reset(PREF_ACCEPTED_POLICY_VERSION);
|
||||
}
|
||||
|
||||
function run_test() {
|
||||
// Addon manager needs a profile directory
|
||||
do_get_profile(true);
|
||||
loadAddonManager("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
|
||||
|
||||
Services.prefs.setBoolPref(PREF_ENABLED, true);
|
||||
// We need to disable FHR in order to use the policy from telemetry.
|
||||
Services.prefs.setBoolPref(PREF_DRS_ENABLED, false);
|
||||
// Don't bypass the notifications in this test, we'll fake it.
|
||||
Services.prefs.setBoolPref(PREF_BYPASS_NOTIFICATION, false);
|
||||
|
||||
TelemetryReportingPolicy.setup();
|
||||
|
||||
run_next_test();
|
||||
}
|
||||
|
||||
add_task(function* test_firstRun() {
|
||||
const PREF_FIRST_RUN = "toolkit.telemetry.reportingpolicy.firstRun";
|
||||
const FIRST_RUN_TIMEOUT_MSEC = 60 * 1000; // 60s
|
||||
const OTHER_RUNS_TIMEOUT_MSEC = 10 * 1000; // 10s
|
||||
|
||||
Preferences.reset(PREF_FIRST_RUN);
|
||||
|
||||
let startupTimeout = 0;
|
||||
fakeShowPolicyTimeout((callback, timeout) => startupTimeout = timeout, () => {});
|
||||
TelemetryReportingPolicy.reset();
|
||||
|
||||
Services.obs.notifyObservers(null, "sessionstore-windows-restored", null);
|
||||
Assert.equal(startupTimeout, FIRST_RUN_TIMEOUT_MSEC,
|
||||
"The infobar display timeout should be 60s on the first run.");
|
||||
|
||||
// Run again, and check that we actually wait only 10 seconds.
|
||||
TelemetryReportingPolicy.reset();
|
||||
Services.obs.notifyObservers(null, "sessionstore-windows-restored", null);
|
||||
Assert.equal(startupTimeout, OTHER_RUNS_TIMEOUT_MSEC,
|
||||
"The infobar display timeout should be 10s on other runs.");
|
||||
});
|
||||
|
||||
add_task(function* test_prefs() {
|
||||
TelemetryReportingPolicy.reset();
|
||||
|
||||
let now = fakeNow(2009, 11, 18);
|
||||
|
||||
// If the date is not valid (earlier than 2012), we don't regard the policy as accepted.
|
||||
TelemetryReportingPolicy.testInfobarShown();
|
||||
Assert.ok(!TelemetryReportingPolicy.testIsUserNotified());
|
||||
Assert.equal(Preferences.get(PREF_ACCEPTED_POLICY_DATE, null), 0,
|
||||
"Invalid dates should not make the policy accepted.");
|
||||
|
||||
// Check that the notification date and version are correctly saved to the prefs.
|
||||
now = fakeNow(2012, 11, 18);
|
||||
TelemetryReportingPolicy.testInfobarShown();
|
||||
Assert.equal(Preferences.get(PREF_ACCEPTED_POLICY_DATE, null), now.getTime(),
|
||||
"A valid date must correctly be saved.");
|
||||
|
||||
// Now that user is notified, check if we are allowed to upload.
|
||||
Assert.ok(TelemetryReportingPolicy.canUpload(),
|
||||
"We must be able to upload after the policy is accepted.");
|
||||
|
||||
// Disable submission and check that we're no longer allowed to upload.
|
||||
Preferences.set(PREF_DATA_SUBMISSION_ENABLED, false);
|
||||
Assert.ok(!TelemetryReportingPolicy.canUpload(),
|
||||
"We must not be able to upload if data submission is disabled.");
|
||||
|
||||
// Turn the submission back on.
|
||||
Preferences.set(PREF_DATA_SUBMISSION_ENABLED, true);
|
||||
Assert.ok(TelemetryReportingPolicy.canUpload(),
|
||||
"We must be able to upload if data submission is enabled and the policy was accepted.");
|
||||
|
||||
// Set a new minimum policy version and check that user is no longer notified.
|
||||
let newMinimum = Preferences.get(PREF_CURRENT_POLICY_VERSION, 1) + 1;
|
||||
Preferences.set(PREF_MINIMUM_POLICY_VERSION, newMinimum);
|
||||
Assert.ok(!TelemetryReportingPolicy.testIsUserNotified(),
|
||||
"A greater minimum policy version must invalidate the policy and disable upload.");
|
||||
|
||||
// Eventually accept the policy and make sure user is notified.
|
||||
Preferences.set(PREF_CURRENT_POLICY_VERSION, newMinimum);
|
||||
TelemetryReportingPolicy.testInfobarShown();
|
||||
Assert.ok(TelemetryReportingPolicy.testIsUserNotified(),
|
||||
"Accepting the policy again should show the user as notified.");
|
||||
Assert.ok(TelemetryReportingPolicy.canUpload(),
|
||||
"Accepting the policy again should let us upload data.");
|
||||
|
||||
// Set a new, per channel, minimum policy version. Start by setting a test current channel.
|
||||
let defaultPrefs = new Preferences({ defaultBranch: true });
|
||||
defaultPrefs.set("app.update.channel", TEST_CHANNEL);
|
||||
|
||||
// Increase and set the new minimum version, then check that we're not notified anymore.
|
||||
newMinimum++;
|
||||
Preferences.set(PREF_MINIMUM_CHANNEL_POLICY_VERSION, newMinimum);
|
||||
Assert.ok(!TelemetryReportingPolicy.testIsUserNotified(),
|
||||
"Increasing the minimum policy version should invalidate the policy.");
|
||||
|
||||
// Eventually accept the policy and make sure user is notified.
|
||||
Preferences.set(PREF_CURRENT_POLICY_VERSION, newMinimum);
|
||||
TelemetryReportingPolicy.testInfobarShown();
|
||||
Assert.ok(TelemetryReportingPolicy.testIsUserNotified(),
|
||||
"Accepting the policy again should show the user as notified.");
|
||||
Assert.ok(TelemetryReportingPolicy.canUpload(),
|
||||
"Accepting the policy again should let us upload data.");
|
||||
});
|
||||
|
||||
add_task(function* test_migratePrefs() {
|
||||
const DEPRECATED_FHR_PREFS = {
|
||||
"datareporting.policy.dataSubmissionPolicyAccepted": true,
|
||||
"datareporting.policy.dataSubmissionPolicyBypassAcceptance": true,
|
||||
"datareporting.policy.dataSubmissionPolicyResponseType": "foxyeah",
|
||||
"datareporting.policy.dataSubmissionPolicyResponseTime": Date.now().toString(),
|
||||
};
|
||||
|
||||
// Make sure the preferences are set before setting up the policy.
|
||||
for (let name in DEPRECATED_FHR_PREFS) {
|
||||
Preferences.set(name, DEPRECATED_FHR_PREFS[name]);
|
||||
}
|
||||
// Set up the policy.
|
||||
TelemetryReportingPolicy.reset();
|
||||
// They should have been removed by now.
|
||||
for (let name in DEPRECATED_FHR_PREFS) {
|
||||
Assert.ok(!Preferences.has(name), name + " should have been removed.");
|
||||
}
|
||||
});
|
||||
|
||||
add_task(function* test_userNotifiedOfCurrentPolicy() {
|
||||
fakeResetAcceptedPolicy();
|
||||
TelemetryReportingPolicy.reset();
|
||||
|
||||
// User should be reported as not notified by default.
|
||||
Assert.ok(!TelemetryReportingPolicy.testIsUserNotified(),
|
||||
"The initial state should be unnotified.");
|
||||
|
||||
// Forcing a policy version should not automatically make the user notified.
|
||||
Preferences.set(PREF_ACCEPTED_POLICY_VERSION,
|
||||
TelemetryReportingPolicy.DEFAULT_DATAREPORTING_POLICY_VERSION);
|
||||
Assert.ok(!TelemetryReportingPolicy.testIsUserNotified(),
|
||||
"The default state of the date should have a time of 0 and it should therefore fail");
|
||||
|
||||
// Showing the notification bar should make the user notified.
|
||||
let now = fakeNow(2012, 11, 11);
|
||||
TelemetryReportingPolicy.testInfobarShown();
|
||||
Assert.ok(TelemetryReportingPolicy.testIsUserNotified(),
|
||||
"Using the proper API causes user notification to report as true.");
|
||||
|
||||
// It is assumed that later versions of the policy will incorporate previous
|
||||
// ones, therefore this should also return true.
|
||||
let newVersion =
|
||||
Preferences.get(PREF_CURRENT_POLICY_VERSION, 1) + 1;
|
||||
Preferences.set(PREF_ACCEPTED_POLICY_VERSION, newVersion);
|
||||
Assert.ok(TelemetryReportingPolicy.testIsUserNotified(),
|
||||
"A future version of the policy should pass.");
|
||||
|
||||
newVersion =
|
||||
Preferences.get(PREF_CURRENT_POLICY_VERSION, 1) - 1;
|
||||
Preferences.set(PREF_ACCEPTED_POLICY_VERSION, newVersion);
|
||||
Assert.ok(!TelemetryReportingPolicy.testIsUserNotified(),
|
||||
"A previous version of the policy should fail.");
|
||||
});
|
||||
|
||||
add_task(function* test_canSend() {
|
||||
const TEST_PING_TYPE = "test-ping";
|
||||
|
||||
PingServer.start();
|
||||
Preferences.set(PREF_SERVER, "http://localhost:" + PingServer.port);
|
||||
|
||||
yield TelemetryController.reset();
|
||||
TelemetryReportingPolicy.reset();
|
||||
|
||||
// User should be reported as not notified by default.
|
||||
Assert.ok(!TelemetryReportingPolicy.testIsUserNotified(),
|
||||
"The initial state should be unnotified.");
|
||||
|
||||
// Assert if we receive any ping before the policy is accepted.
|
||||
PingServer.registerPingHandler(() => Assert.ok(false, "Should not have received any pings now"));
|
||||
yield TelemetryController.submitExternalPing(TEST_PING_TYPE, {});
|
||||
|
||||
// Reset the ping handler.
|
||||
PingServer.resetPingHandler();
|
||||
|
||||
// Fake the infobar: this should also trigger the ping send task.
|
||||
TelemetryReportingPolicy.testInfobarShown();
|
||||
let ping = yield PingServer.promiseNextPings(1);
|
||||
Assert.equal(ping.length, 1, "We should have received one ping.");
|
||||
Assert.equal(ping[0].type, TEST_PING_TYPE,
|
||||
"We should have received the previous ping.");
|
||||
|
||||
// Submit another ping, to make sure it gets sent.
|
||||
yield TelemetryController.submitExternalPing(TEST_PING_TYPE, {});
|
||||
|
||||
// Get the ping and check its type.
|
||||
ping = yield PingServer.promiseNextPings(1);
|
||||
Assert.equal(ping.length, 1, "We should have received one ping.");
|
||||
Assert.equal(ping[0].type, TEST_PING_TYPE, "We should have received the new ping.");
|
||||
|
||||
// Fake a restart with a pending ping.
|
||||
yield TelemetryController.addPendingPing(TEST_PING_TYPE, {});
|
||||
yield TelemetryController.reset();
|
||||
|
||||
// We should be immediately sending the ping out.
|
||||
ping = yield PingServer.promiseNextPings(1);
|
||||
Assert.equal(ping.length, 1, "We should have received one ping.");
|
||||
Assert.equal(ping[0].type, TEST_PING_TYPE, "We should have received the pending ping.");
|
||||
|
||||
// Submit another ping, to make sure it gets sent.
|
||||
yield TelemetryController.submitExternalPing(TEST_PING_TYPE, {});
|
||||
|
||||
// Get the ping and check its type.
|
||||
ping = yield PingServer.promiseNextPings(1);
|
||||
Assert.equal(ping.length, 1, "We should have received one ping.");
|
||||
Assert.equal(ping[0].type, TEST_PING_TYPE, "We should have received the new ping.");
|
||||
|
||||
yield PingServer.stop();
|
||||
});
|
@ -53,3 +53,4 @@ run-sequentially = Bug 1046307, test can fail intermittently when CPU load is hi
|
||||
[test_TelemetrySend.js]
|
||||
[test_ChildHistograms.js]
|
||||
skip-if = os == "android"
|
||||
[test_TelemetryReportingPolicy.js]
|
||||
|
Loading…
Reference in New Issue
Block a user