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

/**
 * This tests checks that the remote tab result is displayed and can be
 * selected.
 */

"use strict";

const { SyncedTabs } = ChromeUtils.importESModule(
  "resource://services-sync/SyncedTabs.sys.mjs"
);

const TEST_URL = `${TEST_BASE_URL}dummy_page.html`;

const REMOTE_TAB = {
  id: "7cqCr77ptzX3",
  type: "client",
  lastModified: 1492201200,
  name: "zcarter's Nightly on MacBook-Pro-25",
  clientType: "desktop",
  tabs: [
    {
      type: "tab",
      title: "Test Remote",
      url: TEST_URL,
      icon: UrlbarUtils.ICON.DEFAULT,
      client: "7cqCr77ptzX3",
      lastUsed: Math.floor(Date.now() / 1000),
    },
  ],
};

add_setup(async function () {
  sandbox = sinon.createSandbox();

  let originalSyncedTabsInternal = SyncedTabs._internal;
  SyncedTabs._internal = {
    isConfiguredToSyncTabs: true,
    hasSyncedThisSession: true,
    getTabClients() {
      return Promise.resolve([]);
    },
    syncTabs() {
      return Promise.resolve();
    },
  };

  // Tell the Sync XPCOM service it is initialized.
  let weaveXPCService = Cc["@mozilla.org/weave/service;1"].getService(
    Ci.nsISupports
  ).wrappedJSObject;
  let oldWeaveServiceReady = weaveXPCService.ready;
  weaveXPCService.ready = true;

  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.urlbar.autoFill", false],
      ["services.sync.username", "fake"],
      ["services.sync.syncedTabs.showRemoteTabs", true],
    ],
  });

  sandbox
    .stub(SyncedTabs._internal, "getTabClients")
    .callsFake(() => Promise.resolve(Cu.cloneInto([REMOTE_TAB], {})));

  registerCleanupFunction(async () => {
    sandbox.restore();
    weaveXPCService.ready = oldWeaveServiceReady;
    SyncedTabs._internal = originalSyncedTabsInternal;
  });
});

add_task(async function test_remotetab_opens() {
  await BrowserTestUtils.withNewTab(
    { url: "about:robots", gBrowser },
    async function () {
      await UrlbarTestUtils.promiseAutocompleteResultPopup({
        window,
        value: "Test Remote",
      });

      // There should be two items in the pop-up, the first is the default search
      // suggestion, the second is the remote tab.

      let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 1);

      Assert.equal(
        result.type,
        UrlbarUtils.RESULT_TYPE.REMOTE_TAB,
        "Should be the remote tab entry"
      );

      // The URL is going to open in the current tab as it is currently about:blank
      let promiseTabLoaded = BrowserTestUtils.browserLoaded(
        gBrowser.selectedBrowser
      );
      EventUtils.synthesizeKey("KEY_ArrowDown");
      EventUtils.synthesizeKey("KEY_Enter");
      await promiseTabLoaded;

      Assert.equal(
        gBrowser.selectedTab.linkedBrowser.currentURI.spec,
        TEST_URL,
        "correct URL loaded"
      );
    }
  );
});