summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_tab_store.js
blob: 34227eee0717a3804da092843ae19e946e421b93 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

const { TabEngine, TabSetRecord } = ChromeUtils.import(
  "resource://services-sync/engines/tabs.js"
);
const { Service } = ChromeUtils.import("resource://services-sync/service.js");

async function getMockStore() {
  let engine = new TabEngine(Service);
  await engine.initialize();
  let store = engine._store;
  store.getTabState = mockGetTabState;
  store.shouldSkipWindow = mockShouldSkipWindow;
  return store;
}

add_task(async function test_create() {
  let engine = new TabEngine(Service);
  await engine.initialize();
  let store = engine._store;

  _("Create a first record");
  let rec = {
    id: "id1",
    clientName: "clientName1",
    cleartext: { foo: "bar" },
    modified: 1000,
  };
  await store.applyIncoming(rec);
  deepEqual(store._remoteClients.id1, { lastModified: 1000, foo: "bar" });

  _("Create a second record");
  rec = {
    id: "id2",
    clientName: "clientName2",
    cleartext: { foo2: "bar2" },
    modified: 2000,
  };
  await store.applyIncoming(rec);
  deepEqual(store._remoteClients.id2, { lastModified: 2000, foo2: "bar2" });

  _("Create a third record");
  rec = {
    id: "id3",
    clientName: "clientName3",
    cleartext: { foo3: "bar3" },
    modified: 3000,
  };
  await store.applyIncoming(rec);
  deepEqual(store._remoteClients.id3, { lastModified: 3000, foo3: "bar3" });
});

add_task(async function test_getAllTabs() {
  let store = await getMockStore();
  let tabs;

  let threeUrls = ["http://foo.com", "http://fuubar.com", "http://barbar.com"];

  store.getWindowEnumerator = mockGetWindowEnumerator.bind(
    this,
    "http://bar.com",
    1,
    1,
    () => 2,
    () => threeUrls
  );

  _("Get all tabs.");
  tabs = await store.getAllTabs();
  _("Tabs: " + JSON.stringify(tabs));
  equal(tabs.length, 1);
  equal(tabs[0].title, "title");
  equal(tabs[0].urlHistory.length, 2);
  equal(tabs[0].urlHistory[0], "http://foo.com");
  equal(tabs[0].urlHistory[1], "http://bar.com");
  equal(tabs[0].icon, "");
  equal(tabs[0].lastUsed, 1);

  _("Get all tabs, and check that filtering works.");
  let twoUrls = ["about:foo", "http://fuubar.com"];
  store.getWindowEnumerator = mockGetWindowEnumerator.bind(
    this,
    "http://foo.com",
    1,
    1,
    () => 2,
    () => twoUrls
  );
  tabs = await store.getAllTabs(true);
  _("Filtered: " + JSON.stringify(tabs));
  equal(tabs.length, 0);

  _("Get all tabs, and check that the entries safety limit works.");
  let allURLs = [];
  for (let i = 0; i < 50; i++) {
    allURLs.push("http://foo" + i + ".bar");
  }
  allURLs.splice(35, 0, "about:foo", "about:bar", "about:foobar");

  store.getWindowEnumerator = mockGetWindowEnumerator.bind(
    this,
    "http://bar.com",
    1,
    1,
    () => 45,
    () => allURLs
  );
  tabs = await store.getAllTabs(url => url.startsWith("about"));

  _("Sliced: " + JSON.stringify(tabs));
  equal(tabs.length, 1);
  equal(tabs[0].urlHistory.length, 5);
  equal(tabs[0].urlHistory[0], "http://foo40.bar");
  equal(tabs[0].urlHistory[4], "http://foo36.bar");
});

add_task(async function test_createRecord() {
  let store = await getMockStore();
  let record;

  store.getTabState = mockGetTabState;
  store.shouldSkipWindow = mockShouldSkipWindow;
  store.getWindowEnumerator = mockGetWindowEnumerator.bind(
    this,
    "http://foo.com",
    1,
    1
  );
  // This number is sensitive to our hard-coded default max record payload size
  // in service.js (256 * 1024).
  // It should be larger than how many records we can fit in a single payload.
  let numtabs = 2700;

  store.getWindowEnumerator = mockGetWindowEnumerator.bind(
    this,
    "http://foo.com",
    1,
    1
  );
  record = await store.createRecord("fake-guid");
  ok(record instanceof TabSetRecord);
  equal(record.tabs.length, 1);

  _("create a big record");
  store.getWindowEnumerator = mockGetWindowEnumerator.bind(
    this,
    "http://foo.com",
    1,
    numtabs
  );
  record = await store.createRecord("fake-guid");
  ok(record instanceof TabSetRecord);
  // This number is sensitive to our hard-coded default max record payload size
  // in service.js (256 * 1024). Given our mock session-store etc, it is the
  // actual max we can fit.
  equal(record.tabs.length, 2672);

  let maxSizeStub = sinon
    .stub(Service, "getMemcacheMaxRecordPayloadSize")
    .callsFake(() => 512 * 1024);
  try {
    numtabs = 5400;
    _("Modify the max record payload size and create a big record");
    store.getWindowEnumerator = mockGetWindowEnumerator.bind(
      this,
      "http://foo.com",
      1,
      numtabs
    );
    record = await store.createRecord("fake-guid");
    ok(record instanceof TabSetRecord);
    equal(record.tabs.length, 5365);
  } finally {
    maxSizeStub.restore();
  }
});