summaryrefslogtreecommitdiffstats
path: root/browser/components/preferences/tests/browser_sync_pairing.js
blob: 6491007a3833bc8d7375f46ed93e3224a0598146 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { UIState } = ChromeUtils.importESModule(
  "resource://services-sync/UIState.sys.mjs"
);
const { FxAccountsPairingFlow } = ChromeUtils.importESModule(
  "resource://gre/modules/FxAccountsPairing.sys.mjs"
);

// Use sinon for mocking.
const { sinon } = ChromeUtils.importESModule(
  "resource://testing-common/Sinon.sys.mjs"
);

let flowCounter = 0;

add_setup(async function () {
  Services.prefs.setBoolPref("identity.fxaccounts.pairing.enabled", true);
  // Sync start-up might interfere with our tests, don't let UIState send UI updates.
  const origNotifyStateUpdated = UIState._internal.notifyStateUpdated;
  UIState._internal.notifyStateUpdated = () => {};

  const origGet = UIState.get;
  UIState.get = () => {
    return { status: UIState.STATUS_SIGNED_IN, email: "foo@bar.com" };
  };

  const origStart = FxAccountsPairingFlow.start;
  FxAccountsPairingFlow.start = ({ emitter: e }) => {
    return `https://foo.bar/${flowCounter++}`;
  };

  registerCleanupFunction(() => {
    UIState._internal.notifyStateUpdated = origNotifyStateUpdated;
    UIState.get = origGet;
    FxAccountsPairingFlow.start = origStart;
  });
});

add_task(async function testShowsQRCode() {
  await runWithPairingDialog(async win => {
    let doc = win.document;
    let qrContainer = doc.getElementById("qrContainer");
    let qrWrapper = doc.getElementById("qrWrapper");

    await TestUtils.waitForCondition(
      () => qrWrapper.getAttribute("pairing-status") == "ready"
    );

    // Verify that a QRcode is being shown.
    Assert.ok(
      qrContainer.style.backgroundImage.startsWith(
        `url("data:image/gif;base64,R0lGODdhOgA6AIAAAAAAAP///ywAAAAAOgA6AAAC/4yPqcvtD6OctNqLs968+w+G4gKU5nkiJYO2JuW6KsDGKEw3a7AbPZ+r4Ry7nzFIQkKKN6Avlzowo78`
      )
    );

    // Close the dialog.
    let promiseUnloaded = BrowserTestUtils.waitForEvent(win, "unload");
    gBrowser.contentDocument.querySelector(".dialogClose").click();

    info("waiting for dialog to unload");
    await promiseUnloaded;
  });
});

add_task(async function testCantShowQrCode() {
  const origStart = FxAccountsPairingFlow.start;
  FxAccountsPairingFlow.start = async () => {
    throw new Error("boom");
  };
  await runWithPairingDialog(async win => {
    let doc = win.document;
    let qrWrapper = doc.getElementById("qrWrapper");

    await TestUtils.waitForCondition(
      () => qrWrapper.getAttribute("pairing-status") == "error"
    );

    // Close the dialog.
    let promiseUnloaded = BrowserTestUtils.waitForEvent(win, "unload");
    gBrowser.contentDocument.querySelector(".dialogClose").click();

    info("waiting for dialog to unload");
    await promiseUnloaded;
  });
  FxAccountsPairingFlow.start = origStart;
});

add_task(async function testSwitchToWebContent() {
  await runWithPairingDialog(async win => {
    let doc = win.document;
    let qrWrapper = doc.getElementById("qrWrapper");

    await TestUtils.waitForCondition(
      () => qrWrapper.getAttribute("pairing-status") == "ready"
    );

    const spySwitchURL = sinon.spy(win.gFxaPairDeviceDialog, "_switchToUrl");
    const emitter = win.gFxaPairDeviceDialog._emitter;
    emitter.emit("view:SwitchToWebContent", "about:robots");

    Assert.equal(spySwitchURL.callCount, 1);
  });
});

add_task(async function testError() {
  await runWithPairingDialog(async win => {
    let doc = win.document;
    let qrWrapper = doc.getElementById("qrWrapper");

    await TestUtils.waitForCondition(
      () => qrWrapper.getAttribute("pairing-status") == "ready"
    );

    const emitter = win.gFxaPairDeviceDialog._emitter;
    emitter.emit("view:Error");

    await TestUtils.waitForCondition(
      () => qrWrapper.getAttribute("pairing-status") == "error"
    );

    // Close the dialog.
    let promiseUnloaded = BrowserTestUtils.waitForEvent(win, "unload");
    gBrowser.contentDocument.querySelector(".dialogClose").click();

    info("waiting for dialog to unload");
    await promiseUnloaded;
  });
});

async function runWithPairingDialog(test) {
  await openPreferencesViaOpenPreferencesAPI("paneSync", { leaveOpen: true });

  let promiseSubDialogLoaded = promiseLoadSubDialog(
    "chrome://browser/content/preferences/fxaPairDevice.xhtml"
  );
  gBrowser.contentWindow.gSyncPane.pairAnotherDevice();

  let win = await promiseSubDialogLoaded;

  await test(win);

  sinon.restore();

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
}