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

Cu.import("resource://services-sync/constants.js");
Cu.import("resource://services-sync/engines/history.js");
Cu.import("resource://services-sync/engines.js");
Cu.import("resource://services-sync/identity.js");
Cu.import("resource://services-sync/record.js");
Cu.import("resource://services-sync/service.js");
Cu.import("resource://services-sync/util.js");
Cu.import("resource://testing-common/services/sync/utils.js");

Service.engineManager.clear();

add_test(function test_setup() {
  PlacesTestUtils.clearHistory().then(run_next_test);
});

add_test(function test_processIncoming_mobile_history_batched() {
  _("SyncEngine._processIncoming works on history engine.");

  let FAKE_DOWNLOAD_LIMIT = 100;

  Svc.Prefs.set("client.type", "mobile");
  Service.engineManager.register(HistoryEngine);

  // A collection that logs each GET
  let collection = new ServerCollection();
  collection.get_log = [];
  collection._get = collection.get;
  collection.get = function (options) {
    this.get_log.push(options);
    return this._get(options);
  };

  let server = sync_httpd_setup({
    "/1.1/foo/storage/history": collection.handler()
  });

  new SyncTestingInfrastructure(server);

  // Let's create some 234 server side history records. They're all at least
  // 10 minutes old.
  let visitType = Ci.nsINavHistoryService.TRANSITION_LINK;
  for (var i = 0; i < 234; i++) {
    let id = 'record-no' + ("00" + i).slice(-3);
    let modified = Date.now()/1000 - 60*(i+10);
    let payload = encryptPayload({
      id: id,
      histUri: "http://foo/bar?" + id,
        title: id,
        sortindex: i,
        visits: [{date: (modified - 5) * 1000000, type: visitType}],
        deleted: false});

    let wbo = new ServerWBO(id, payload);
    wbo.modified = modified;
    collection.insertWBO(wbo);
  }

  let engine = Service.engineManager.get("history");
  let meta_global = Service.recordManager.set(engine.metaURL,
                                              new WBORecord(engine.metaURL));
  meta_global.payload.engines = {history: {version: engine.version,
                                           syncID: engine.syncID}};

  try {

    _("On a mobile client, we get new records from the server in batches of 50.");
    engine._syncStartup();

    // Fake a lower limit.
    engine.downloadLimit = FAKE_DOWNLOAD_LIMIT;
    _("Last modified: " + engine.lastModified);
    _("Processing...");
    engine._processIncoming();

    _("Last modified: " + engine.lastModified);
    engine._syncFinish();

    // Back to the normal limit.
    _("Running again. Should fetch none, because of lastModified");
    engine.downloadLimit = MAX_HISTORY_DOWNLOAD;
    _("Processing...");
    engine._processIncoming();

    _("Last modified: " + engine.lastModified);
    _("Running again. Expecting to pull everything");

    engine.lastModified = undefined;
    engine.lastSync     = 0;
    _("Processing...");
    engine._processIncoming();

    _("Last modified: " + engine.lastModified);

    // Verify that the right number of GET requests with the right
    // kind of parameters were made.
    do_check_eq(collection.get_log.length,
        // First try:
        1 +    // First 50...
        1 +    // 1 GUID fetch...
               // 1 fetch...
        Math.ceil((FAKE_DOWNLOAD_LIMIT - 50) / MOBILE_BATCH_SIZE) +
        // Second try: none
        // Third try:
        1 +    // First 50...
        1 +    // 1 GUID fetch...
               // 4 fetch...
        Math.ceil((234 - 50) / MOBILE_BATCH_SIZE));

    // Check the structure of each HTTP request.
    do_check_eq(collection.get_log[0].full, 1);
    do_check_eq(collection.get_log[0].limit, MOBILE_BATCH_SIZE);
    do_check_eq(collection.get_log[1].full, undefined);
    do_check_eq(collection.get_log[1].sort, "index");
    do_check_eq(collection.get_log[1].limit, FAKE_DOWNLOAD_LIMIT);
    do_check_eq(collection.get_log[2].full, 1);
    do_check_eq(collection.get_log[3].full, 1);
    do_check_eq(collection.get_log[3].limit, MOBILE_BATCH_SIZE);
    do_check_eq(collection.get_log[4].full, undefined);
    do_check_eq(collection.get_log[4].sort, "index");
    do_check_eq(collection.get_log[4].limit, MAX_HISTORY_DOWNLOAD);
    for (let i = 0; i <= Math.floor((234 - 50) / MOBILE_BATCH_SIZE); i++) {
      let j = i + 5;
      do_check_eq(collection.get_log[j].full, 1);
      do_check_eq(collection.get_log[j].limit, undefined);
      if (i < Math.floor((234 - 50) / MOBILE_BATCH_SIZE))
        do_check_eq(collection.get_log[j].ids.length, MOBILE_BATCH_SIZE);
      else
        do_check_eq(collection.get_log[j].ids.length, 234 % MOBILE_BATCH_SIZE);
    }

  } finally {
    PlacesTestUtils.clearHistory().then(() => {
      server.stop(do_test_finished);
      Svc.Prefs.resetBranch("");
      Service.recordManager.clearCache();
    });
  }
});

function run_test() {
  generateNewKeys(Service.collectionKeys);

  run_next_test();
}