Bug 914926 - Fix workers on startup when social disabled. r=markh

CLOSED TREE
This commit is contained in:
Shane Caraveo 2013-09-14 10:16:47 -07:00
Родитель 818c1c026f
Коммит 1d4216da91
11 изменённых файлов: 269 добавлений и 8 удалений

Просмотреть файл

@ -7,6 +7,7 @@ function test() {
Services.prefs.setBoolPref("social.allowMultipleWorkers", true); Services.prefs.setBoolPref("social.allowMultipleWorkers", true);
runSocialTestWithProvider(gProviders, function (finishcb) { runSocialTestWithProvider(gProviders, function (finishcb) {
Social.enabled = true;
runSocialTests(tests, undefined, undefined, function() { runSocialTests(tests, undefined, undefined, function() {
Services.prefs.clearUserPref("social.allowMultipleWorkers"); Services.prefs.clearUserPref("social.allowMultipleWorkers");
finishcb(); finishcb();

Просмотреть файл

@ -34,9 +34,8 @@ function prefObserver(subject, topic, data) {
if (enable && !Social.provider) { if (enable && !Social.provider) {
// this will result in setting Social.provider // this will result in setting Social.provider
SocialService.getOrderedProviderList(function(providers) { SocialService.getOrderedProviderList(function(providers) {
Social._updateProviderCache(providers);
Social.enabled = true; Social.enabled = true;
Services.obs.notifyObservers(null, "social:providers-changed", null); Social._updateProviderCache(providers);
}); });
} else if (!enable && Social.provider) { } else if (!enable && Social.provider) {
Social.provider = null; Social.provider = null;
@ -168,7 +167,7 @@ this.Social = {
// Retrieve the current set of providers, and set the current provider. // Retrieve the current set of providers, and set the current provider.
SocialService.getOrderedProviderList(function (providers) { SocialService.getOrderedProviderList(function (providers) {
Social._updateProviderCache(providers); Social._updateProviderCache(providers);
Social._updateWorkerState(true); Social._updateWorkerState(SocialService.enabled);
}); });
} }
@ -181,10 +180,16 @@ this.Social = {
Services.obs.notifyObservers(null, "social:" + topic, origin); Services.obs.notifyObservers(null, "social:" + topic, origin);
return; return;
} }
if (topic == "provider-enabled" || topic == "provider-disabled") { if (topic == "provider-enabled") {
Social._updateProviderCache(providers);
Social._updateWorkerState(Social.enabled);
Services.obs.notifyObservers(null, "social:" + topic, origin);
return;
}
if (topic == "provider-disabled") {
// a provider was removed from the list of providers, that does not
// affect worker state for other providers
Social._updateProviderCache(providers); Social._updateProviderCache(providers);
Social._updateWorkerState(true);
Services.obs.notifyObservers(null, "social:providers-changed", null);
Services.obs.notifyObservers(null, "social:" + topic, origin); Services.obs.notifyObservers(null, "social:" + topic, origin);
return; return;
} }
@ -194,7 +199,6 @@ this.Social = {
Social._updateProviderCache(providers); Social._updateProviderCache(providers);
let provider = Social._getProviderFromOrigin(origin); let provider = Social._getProviderFromOrigin(origin);
provider.reload(); provider.reload();
Services.obs.notifyObservers(null, "social:providers-changed", null);
} }
}); });
}, },
@ -210,6 +214,7 @@ this.Social = {
// Called to update our cache of providers and set the current provider // Called to update our cache of providers and set the current provider
_updateProviderCache: function (providers) { _updateProviderCache: function (providers) {
this.providers = providers; this.providers = providers;
Services.obs.notifyObservers(null, "social:providers-changed", null);
// If social is currently disabled there's nothing else to do other than // If social is currently disabled there's nothing else to do other than
// to notify about the lack of a provider. // to notify about the lack of a provider.

Просмотреть файл

@ -4,4 +4,4 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this # 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/. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
DIRS += ['chrome'] DIRS += ['chrome', 'unit']

Просмотреть файл

@ -0,0 +1,9 @@
# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# 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/.
DIRS += ['social']
XPCSHELL_TESTS_MANIFESTS += ['social/xpcshell.ini']

Просмотреть файл

@ -0,0 +1,11 @@
# 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/.
XPCSHELL_RESOURCES = \
xpcshell.ini \
head.js \
blocklist.xml \
test_social.js \
test_socialDisabledStartup.js \
$(NULL)

Просмотреть файл

@ -0,0 +1,6 @@
<?xml version="1.0"?>
<blocklist xmlns="http://www.mozilla.org/2006/addons-blocklist">
<emItems>
<emItem blockID="s1" id="bad.com@services.mozilla.org"></emItem>
</emItems>
</blocklist>

Просмотреть файл

@ -0,0 +1,146 @@
/* 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/. */
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
var Social, SocialService;
let manifests = [
{
name: "provider 1",
origin: "https://example1.com",
sidebarURL: "https://example1.com/sidebar/",
},
{
name: "provider 2",
origin: "https://example2.com",
sidebarURL: "https://example1.com/sidebar/",
}
];
const MANIFEST_PREFS = Services.prefs.getBranch("social.manifest.");
// SocialProvider class relies on blocklisting being enabled. To enable
// blocklisting, we have to setup an app and initialize the blocklist (see
// initApp below).
const gProfD = do_get_profile();
const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1";
const XULAPPINFO_CID = Components.ID("{c763b610-9d49-455a-bbd2-ede71682a1ac}");
function createAppInfo(id, name, version, platformVersion) {
gAppInfo = {
// nsIXULAppInfo
vendor: "Mozilla",
name: name,
ID: id,
version: version,
appBuildID: "2007010101",
platformVersion: platformVersion ? platformVersion : "1.0",
platformBuildID: "2007010101",
// nsIXULRuntime
inSafeMode: false,
logConsoleErrors: true,
OS: "XPCShell",
XPCOMABI: "noarch-spidermonkey",
invalidateCachesOnRestart: function invalidateCachesOnRestart() {
// Do nothing
},
// nsICrashReporter
annotations: {},
annotateCrashReport: function(key, data) {
this.annotations[key] = data;
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULAppInfo,
Ci.nsIXULRuntime,
Ci.nsICrashReporter,
Ci.nsISupports])
};
var XULAppInfoFactory = {
createInstance: function (outer, iid) {
if (outer != null)
throw Components.results.NS_ERROR_NO_AGGREGATION;
return gAppInfo.QueryInterface(iid);
}
};
var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
registrar.registerFactory(XULAPPINFO_CID, "XULAppInfo",
XULAPPINFO_CONTRACTID, XULAppInfoFactory);
}
function initApp() {
createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9");
// prepare a blocklist file for the blocklist service
var blocklistFile = gProfD.clone();
blocklistFile.append("blocklist.xml");
if (blocklistFile.exists())
blocklistFile.remove(false);
var source = do_get_file("blocklist.xml");
source.copyTo(gProfD, "blocklist.xml");
}
function setManifestPref(manifest) {
let string = Cc["@mozilla.org/supports-string;1"].
createInstance(Ci.nsISupportsString);
string.data = JSON.stringify(manifest);
Services.prefs.setComplexValue("social.manifest." + manifest.origin, Ci.nsISupportsString, string);
}
function do_wait_observer(topic, cb) {
function observer(subject, topic, data) {
Services.obs.removeObserver(observer, topic);
cb();
}
Services.obs.addObserver(observer, topic, false);
}
function do_initialize_social(enabledOnStartup, cb) {
initApp();
manifests.forEach(function (manifest) {
setManifestPref(manifest);
});
// Set both providers active and flag the first one as "current"
let activeVal = Cc["@mozilla.org/supports-string;1"].
createInstance(Ci.nsISupportsString);
let active = {};
for (let m of manifests)
active[m.origin] = 1;
activeVal.data = JSON.stringify(active);
Services.prefs.setComplexValue("social.activeProviders",
Ci.nsISupportsString, activeVal);
Services.prefs.setCharPref("social.provider.current", manifests[0].origin);
Services.prefs.setBoolPref("social.enabled", enabledOnStartup);
do_register_cleanup(function() {
manifests.forEach(function (manifest) {
Services.prefs.clearUserPref("social.manifest." + manifest.origin);
});
Services.prefs.clearUserPref("social.enabled");
Services.prefs.clearUserPref("social.provider.current");
Services.prefs.clearUserPref("social.activeProviders");
});
// expecting 2 providers installed
do_wait_observer("social:providers-changed", function() {
do_check_eq(Social.providers.length, 2, "2 providers installed");
cb();
});
// import and initialize everything
SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService;
do_check_eq(SocialService.enabled, enabledOnStartup, "service is doing its thing");
do_check_true(SocialService.hasEnabledProviders, "Service has enabled providers");
Social = Cu.import("resource:///modules/Social.jsm", {}).Social;
do_check_false(Social.initialized, "Social is not initialized");
Social.init();
do_check_true(Social.initialized, "Social is initialized");
}

Просмотреть файл

@ -0,0 +1,6 @@
# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# 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/.

Просмотреть файл

@ -0,0 +1,34 @@
/* 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/. */
function run_test() {
// we are testing worker startup specifically
Services.prefs.setBoolPref("social.allowMultipleWorkers", true);
do_register_cleanup(function() {
Services.prefs.clearUserPref("social.allowMultipleWorkers");
});
do_test_pending();
add_test(testStartupEnabled);
add_test(testDisableAfterStartup);
do_initialize_social(true, run_next_test);
}
function testStartupEnabled() {
// wait on startup before continuing
do_check_eq(Social.providers.length, 2, "two social providers enabled");
do_check_true(Social.providers[0].enabled, "provider is enabled");
do_check_true(Social.providers[1].enabled, "provider is enabled");
run_next_test();
}
function testDisableAfterStartup() {
do_wait_observer("social:provider-set", function() {
do_check_eq(Social.enabled, false, "Social is disabled");
do_check_false(Social.providers[0].enabled, "provider is enabled");
do_check_false(Social.providers[1].enabled, "provider is enabled");
do_test_finished();
run_next_test();
});
Social.enabled = false;
}

Просмотреть файл

@ -0,0 +1,35 @@
/* 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/. */
function run_test() {
// we are testing worker startup specifically
Services.prefs.setBoolPref("social.allowMultipleWorkers", true);
do_register_cleanup(function() {
Services.prefs.clearUserPref("social.allowMultipleWorkers");
});
do_test_pending();
add_test(testStartupDisabled);
add_test(testEnableAfterStartup);
do_initialize_social(false, run_next_test);
}
function testStartupDisabled() {
// wait on startup before continuing
do_check_eq(Social.providers.length, 2, "two social providers available");
do_check_false(Social.providers[0].enabled, "provider is enabled");
do_check_false(Social.providers[1].enabled, "provider is enabled");
run_next_test();
}
function testEnableAfterStartup() {
do_wait_observer("social:provider-set", function() {
do_check_true(Social.enabled, "Social is enabled");
do_check_eq(Social.providers.length, 2, "two social providers available");
do_check_true(Social.providers[0].enabled, "provider is enabled");
do_check_true(Social.providers[1].enabled, "provider is enabled");
do_test_finished();
run_next_test();
});
Social.enabled = true;
}

Просмотреть файл

@ -0,0 +1,8 @@
[DEFAULT]
head = head.js
tail =
firefox-appdir = browser
[test_social.js]
[test_socialDisabledStartup.js]