summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/social/browser_social_activation.js
blob: 2af0d802123c90b6039d636d4ee788703e440e4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* 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/. */

//
// Whitelisting this test.
// As part of bug 1077403, the leaking uncaught rejection should be fixed.
//
thisTestLeaksUncaughtRejectionsAndShouldBeFixed("TypeError: Assert is null");


var SocialService = Cu.import("resource:///modules/SocialService.jsm", {}).SocialService;

var tabsToRemove = [];

function removeProvider(provider) {
  return new Promise(resolve => {
    // a full install sets the manifest into a pref, addProvider alone doesn't,
    // make sure we uninstall if the manifest was added.
    if (provider.manifest) {
      SocialService.uninstallProvider(provider.origin, resolve);
    } else {
      SocialService.disableProvider(provider.origin, resolve);
    }
  });
}

function postTestCleanup(callback) {
  Task.spawn(function* () {
    // any tabs opened by the test.
    for (let tab of tabsToRemove) {
      yield BrowserTestUtils.removeTab(tab);
    }
    tabsToRemove = [];
    // all the providers may have been added.
    while (Social.providers.length > 0) {
      yield removeProvider(Social.providers[0]);
    }
  }).then(callback);
}

function newTab(url) {
  return new Promise(resolve => {
    BrowserTestUtils.openNewForegroundTab(gBrowser, url).then(tab => {
      tabsToRemove.push(tab);
      resolve(tab);
    });
  });
}

function sendActivationEvent(tab, callback, nullManifest) {
  // hack Social.lastEventReceived so we don't hit the "too many events" check.
  Social.lastEventReceived = 0;
  BrowserTestUtils.synthesizeMouseAtCenter("#activation", {}, tab.linkedBrowser);
  executeSoon(callback);
}

function activateProvider(domain, callback, nullManifest) {
  let activationURL = domain+"/browser/browser/base/content/test/social/social_activate_basic.html"
  newTab(activationURL).then(tab => {
    sendActivationEvent(tab, callback, nullManifest);
  });
}

function activateIFrameProvider(domain, callback) {
  let activationURL = domain+"/browser/browser/base/content/test/social/social_activate_iframe.html"
  newTab(activationURL).then(tab => {
    sendActivationEvent(tab, callback, false);
  });
}

function waitForProviderLoad(origin) {
  return Promise.all([
    ensureFrameLoaded(gBrowser, origin + "/browser/browser/base/content/test/social/social_postActivation.html"),
  ]);
}

function getAddonItemInList(aId, aList) {
  var item = aList.firstChild;
  while (item) {
    if ("mAddon" in item && item.mAddon.id == aId) {
      aList.ensureElementIsVisible(item);
      return item;
    }
    item = item.nextSibling;
  }
  return null;
}

function clickAddonRemoveButton(tab, aCallback) {
  AddonManager.getAddonsByTypes(["service"], function(aAddons) {
    let addon = aAddons[0];

    let doc = tab.linkedBrowser.contentDocument;
    let list = doc.getElementById("addon-list");

    let item = getAddonItemInList(addon.id, list);
    let button = item._removeBtn;
    isnot(button, null, "Should have a remove button");
    ok(!button.disabled, "Button should not be disabled");

    // uninstall happens after about:addons tab is closed, so we wait on
    // disabled
    promiseObserverNotified("social:provider-disabled").then(() => {
      is(item.getAttribute("pending"), "uninstall", "Add-on should be uninstalling");
      executeSoon(function() { aCallback(addon); });
    });

    BrowserTestUtils.synthesizeMouseAtCenter(button, {}, tab.linkedBrowser);
  });
}

function activateOneProvider(manifest, finishActivation, aCallback) {
  info("activating provider "+manifest.name);
  let panel = document.getElementById("servicesInstall-notification");
  BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown").then(() => {
    ok(!panel.hidden, "servicesInstall-notification panel opened");
    if (finishActivation)
      panel.button.click();
    else
      panel.closebutton.click();
  });
  BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popuphidden").then(() => {
    ok(panel.hidden, "servicesInstall-notification panel hidden");
    if (!finishActivation) {
      ok(panel.hidden, "activation panel is not showing");
      executeSoon(aCallback);
    } else {
      waitForProviderLoad(manifest.origin).then(() => {
        checkSocialUI();
        executeSoon(aCallback);
      });
    }
  });

  // the test will continue as the popup events fire...
  activateProvider(manifest.origin, function() {
    info("waiting on activation panel to open/close...");
  });
}

var gTestDomains = ["https://example.com", "https://test1.example.com", "https://test2.example.com"];
var gProviders = [
  {
    name: "provider 1",
    origin: "https://example.com",
    shareURL: "https://example.com/browser/browser/base/content/test/social/social_share.html?provider1",
    iconURL: "chrome://branding/content/icon48.png"
  },
  {
    name: "provider 2",
    origin: "https://test1.example.com",
    shareURL: "https://test1.example.com/browser/browser/base/content/test/social/social_share.html?provider2",
    iconURL: "chrome://branding/content/icon64.png"
  },
  {
    name: "provider 3",
    origin: "https://test2.example.com",
    shareURL: "https://test2.example.com/browser/browser/base/content/test/social/social_share.html?provider2",
    iconURL: "chrome://branding/content/about-logo.png"
  }
];


function test() {
  PopupNotifications.panel.setAttribute("animate", "false");
  registerCleanupFunction(function () {
    PopupNotifications.panel.removeAttribute("animate");
  });
  waitForExplicitFinish();
  SpecialPowers.pushPrefEnv({"set": [["dom.ipc.processCount", 1]]}, () => {
    runSocialTests(tests, undefined, postTestCleanup);
  });
}

var tests = {
  testActivationWrongOrigin: function(next) {
    // At this stage none of our providers exist, so we expect failure.
    Services.prefs.setBoolPref("social.remote-install.enabled", false);
    activateProvider(gTestDomains[0], function() {
      is(SocialUI.enabled, false, "SocialUI is not enabled");
      let panel = document.getElementById("servicesInstall-notification");
      ok(panel.hidden, "activation panel still hidden");
      checkSocialUI();
      Services.prefs.clearUserPref("social.remote-install.enabled");
      next();
    });
  },

  testIFrameActivation: function(next) {
    activateIFrameProvider(gTestDomains[0], function() {
      is(SocialUI.enabled, false, "SocialUI is not enabled");
      let panel = document.getElementById("servicesInstall-notification");
      ok(panel.hidden, "activation panel still hidden");
      checkSocialUI();
      next();
    });
  },

  testActivationFirstProvider: function(next) {
    // first up we add a manifest entry for a single provider.
    activateOneProvider(gProviders[0], false, function() {
      // we deactivated leaving no providers left, so Social is disabled.
      checkSocialUI();
      next();
    });
  },

  testActivationMultipleProvider: function(next) {
    // The trick with this test is to make sure that Social.providers[1] is
    // the current provider when doing the undo - this makes sure that the
    // Social code doesn't fallback to Social.providers[0], which it will
    // do in some cases (but those cases do not include what this test does)
    // first enable the 2 providers
    SocialService.addProvider(gProviders[0], function() {
      SocialService.addProvider(gProviders[1], function() {
        checkSocialUI();
        // activate the last provider.
        activateOneProvider(gProviders[2], false, function() {
          // we deactivated - the first provider should be enabled.
          checkSocialUI();
          next();
        });
      });
    });
  },

  testAddonManagerDoubleInstall: function(next) {
    // Create a new tab and load about:addons
    let addonsTab = gBrowser.addTab();
    gBrowser.selectedTab = addonsTab;
    BrowserOpenAddonsMgr('addons://list/service');
    gBrowser.selectedBrowser.addEventListener("load", function tabLoad() {
      gBrowser.selectedBrowser.removeEventListener("load", tabLoad, true);
      is(addonsTab.linkedBrowser.currentURI.spec, "about:addons", "about:addons should load into blank tab.");

      activateOneProvider(gProviders[0], true, function() {
        info("first activation completed");
        is(gBrowser.contentDocument.location.href, gProviders[0].origin + "/browser/browser/base/content/test/social/social_postActivation.html", "postActivationURL loaded");
        BrowserTestUtils.removeTab(gBrowser.selectedTab).then(() => {
          is(gBrowser.contentDocument.location.href, gProviders[0].origin + "/browser/browser/base/content/test/social/social_activate_basic.html", "activation page selected");
          BrowserTestUtils.removeTab(gBrowser.selectedTab).then(() => {
            tabsToRemove.pop();
            // uninstall the provider
            clickAddonRemoveButton(addonsTab, function(addon) {
              checkSocialUI();
              activateOneProvider(gProviders[0], true, function() {
                info("second activation completed");
                is(gBrowser.contentDocument.location.href, gProviders[0].origin + "/browser/browser/base/content/test/social/social_postActivation.html", "postActivationURL loaded");
                BrowserTestUtils.removeTab(gBrowser.selectedTab).then(() => {

                  // after closing the addons tab, verify provider is still installed
                  AddonManager.getAddonsByTypes(["service"], function(aAddons) {
                    is(aAddons.length, 1, "there can be only one");

                    let doc = addonsTab.linkedBrowser.contentDocument;
                    let list = doc.getElementById("addon-list");
                    is(list.childNodes.length, 1, "only one addon is displayed");

                    BrowserTestUtils.removeTab(addonsTab).then(next);
                  });
                });
              });
            });
          });
        });
      });
    }, true);
  }
}