summaryrefslogtreecommitdiffstats
path: root/browser/modules/test/browser_BrowserUITelemetry_syncedtabs.js
blob: d3e1eac57836da17cbcda43c8d5e18b749eb09bc (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
// Test the SyncedTabs counters in BrowserUITelemetry.
"use strict";

const { BrowserUITelemetry: BUIT } = Cu.import("resource:///modules/BrowserUITelemetry.jsm", {});
const {SyncedTabs} = Cu.import("resource://services-sync/SyncedTabs.jsm", {});

function mockSyncedTabs() {
  // Mock SyncedTabs.jsm
  let mockedInternal = {
    get isConfiguredToSyncTabs() { return true; },
    getTabClients() {
      return Promise.resolve([
        {
          id: "guid_desktop",
          type: "client",
          name: "My Desktop",
          tabs: [
            {
              title: "http://example.com/10",
              lastUsed: 10, // the most recent
            },
          ],
        }
      ]);
    },
    syncTabs() {
      return Promise.resolve();
    },
    hasSyncedThisSession: true,
  };

  let oldInternal = SyncedTabs._internal;
  SyncedTabs._internal = mockedInternal;

  // configure our broadcasters so we are in the right state.
  document.getElementById("sync-reauth-state").hidden = true;
  document.getElementById("sync-setup-state").hidden = true;
  document.getElementById("sync-syncnow-state").hidden = false;

  registerCleanupFunction(() => {
    SyncedTabs._internal = oldInternal;

    document.getElementById("sync-reauth-state").hidden = true;
    document.getElementById("sync-setup-state").hidden = false;
    document.getElementById("sync-syncnow-state").hidden = true;
  });
}

mockSyncedTabs();

function promiseTabsUpdated() {
  return new Promise(resolve => {
    Services.obs.addObserver(function onNotification(aSubject, aTopic, aData) {
      Services.obs.removeObserver(onNotification, aTopic);
      resolve();
    }, "synced-tabs-menu:test:tabs-updated", false);
  });
}

add_task(function* test_menu() {
  // Reset BrowserUITelemetry's world.
  BUIT._countableEvents = {};

  let tabsUpdated = promiseTabsUpdated();

  // check the button's functionality
  yield PanelUI.show();

  let syncButton = document.getElementById("sync-button");
  syncButton.click();

  yield tabsUpdated;
  // Get our 1 tab and click on it.
  let tabList = document.getElementById("PanelUI-remotetabs-tabslist");
  let tabEntry = tabList.firstChild.nextSibling;
  tabEntry.click();

  let counts = BUIT._countableEvents[BUIT.currentBucket];
  Assert.deepEqual(counts, {
    "click-builtin-item": { "sync-button": { left: 1 } },
    "synced-tabs": { open: { "toolbarbutton-subview": 1 } },
  });
});

add_task(function* test_sidebar() {
  // Reset BrowserUITelemetry's world.
  BUIT._countableEvents = {};

  yield SidebarUI.show('viewTabsSidebar');

  let syncedTabsDeckComponent = SidebarUI.browser.contentWindow.syncedTabsDeckComponent;

  syncedTabsDeckComponent._accountStatus = () => Promise.resolve(true);

  // Once the tabs container has been selected (which here means "'selected'
  // added to the class list") we are ready to test.
  let container = SidebarUI.browser.contentDocument.querySelector(".tabs-container");
  let promiseUpdated = BrowserTestUtils.waitForAttribute("class", container);

  yield syncedTabsDeckComponent.updatePanel();
  yield promiseUpdated;

  let selectedPanel = syncedTabsDeckComponent.container.querySelector(".sync-state.selected");
  let tab = selectedPanel.querySelector(".tab");
  tab.click();
  let counts = BUIT._countableEvents[BUIT.currentBucket];
  Assert.deepEqual(counts, {
    sidebar: {
      viewTabsSidebar: { show: 1 },
    },
    "synced-tabs": { open: { sidebar: 1 } }
  });
  yield SidebarUI.hide();
});