|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 Cu.import("resource://gre/modules/PlacesUtils.jsm"); |
|
5 Cu.import("resource://services-sync/constants.js"); |
|
6 Cu.import("resource://services-sync/engines/history.js"); |
|
7 Cu.import("resource://services-sync/engines.js"); |
|
8 Cu.import("resource://services-sync/identity.js"); |
|
9 Cu.import("resource://services-sync/record.js"); |
|
10 Cu.import("resource://services-sync/service.js"); |
|
11 Cu.import("resource://services-sync/util.js"); |
|
12 Cu.import("resource://testing-common/services/sync/utils.js"); |
|
13 |
|
14 Service.engineManager.clear(); |
|
15 |
|
16 add_test(function test_processIncoming_mobile_history_batched() { |
|
17 _("SyncEngine._processIncoming works on history engine."); |
|
18 |
|
19 let FAKE_DOWNLOAD_LIMIT = 100; |
|
20 |
|
21 Svc.Prefs.set("client.type", "mobile"); |
|
22 PlacesUtils.history.removeAllPages(); |
|
23 Service.engineManager.register(HistoryEngine); |
|
24 |
|
25 // A collection that logs each GET |
|
26 let collection = new ServerCollection(); |
|
27 collection.get_log = []; |
|
28 collection._get = collection.get; |
|
29 collection.get = function (options) { |
|
30 this.get_log.push(options); |
|
31 return this._get(options); |
|
32 }; |
|
33 |
|
34 let server = sync_httpd_setup({ |
|
35 "/1.1/foo/storage/history": collection.handler() |
|
36 }); |
|
37 |
|
38 new SyncTestingInfrastructure(server); |
|
39 |
|
40 // Let's create some 234 server side history records. They're all at least |
|
41 // 10 minutes old. |
|
42 let visitType = Ci.nsINavHistoryService.TRANSITION_LINK; |
|
43 for (var i = 0; i < 234; i++) { |
|
44 let id = 'record-no' + ("00" + i).slice(-3); |
|
45 let modified = Date.now()/1000 - 60*(i+10); |
|
46 let payload = encryptPayload({ |
|
47 id: id, |
|
48 histUri: "http://foo/bar?" + id, |
|
49 title: id, |
|
50 sortindex: i, |
|
51 visits: [{date: (modified - 5) * 1000000, type: visitType}], |
|
52 deleted: false}); |
|
53 |
|
54 let wbo = new ServerWBO(id, payload); |
|
55 wbo.modified = modified; |
|
56 collection.insertWBO(wbo); |
|
57 } |
|
58 |
|
59 let engine = Service.engineManager.get("history"); |
|
60 let meta_global = Service.recordManager.set(engine.metaURL, |
|
61 new WBORecord(engine.metaURL)); |
|
62 meta_global.payload.engines = {history: {version: engine.version, |
|
63 syncID: engine.syncID}}; |
|
64 |
|
65 try { |
|
66 |
|
67 _("On a mobile client, we get new records from the server in batches of 50."); |
|
68 engine._syncStartup(); |
|
69 |
|
70 // Fake a lower limit. |
|
71 engine.downloadLimit = FAKE_DOWNLOAD_LIMIT; |
|
72 _("Last modified: " + engine.lastModified); |
|
73 _("Processing..."); |
|
74 engine._processIncoming(); |
|
75 |
|
76 _("Last modified: " + engine.lastModified); |
|
77 engine._syncFinish(); |
|
78 |
|
79 // Back to the normal limit. |
|
80 _("Running again. Should fetch none, because of lastModified"); |
|
81 engine.downloadLimit = MAX_HISTORY_DOWNLOAD; |
|
82 _("Processing..."); |
|
83 engine._processIncoming(); |
|
84 |
|
85 _("Last modified: " + engine.lastModified); |
|
86 _("Running again. Expecting to pull everything"); |
|
87 |
|
88 engine.lastModified = undefined; |
|
89 engine.lastSync = 0; |
|
90 _("Processing..."); |
|
91 engine._processIncoming(); |
|
92 |
|
93 _("Last modified: " + engine.lastModified); |
|
94 |
|
95 // Verify that the right number of GET requests with the right |
|
96 // kind of parameters were made. |
|
97 do_check_eq(collection.get_log.length, |
|
98 // First try: |
|
99 1 + // First 50... |
|
100 1 + // 1 GUID fetch... |
|
101 // 1 fetch... |
|
102 Math.ceil((FAKE_DOWNLOAD_LIMIT - 50) / MOBILE_BATCH_SIZE) + |
|
103 // Second try: none |
|
104 // Third try: |
|
105 1 + // First 50... |
|
106 1 + // 1 GUID fetch... |
|
107 // 4 fetch... |
|
108 Math.ceil((234 - 50) / MOBILE_BATCH_SIZE)); |
|
109 |
|
110 // Check the structure of each HTTP request. |
|
111 do_check_eq(collection.get_log[0].full, 1); |
|
112 do_check_eq(collection.get_log[0].limit, MOBILE_BATCH_SIZE); |
|
113 do_check_eq(collection.get_log[1].full, undefined); |
|
114 do_check_eq(collection.get_log[1].sort, "index"); |
|
115 do_check_eq(collection.get_log[1].limit, FAKE_DOWNLOAD_LIMIT); |
|
116 do_check_eq(collection.get_log[2].full, 1); |
|
117 do_check_eq(collection.get_log[3].full, 1); |
|
118 do_check_eq(collection.get_log[3].limit, MOBILE_BATCH_SIZE); |
|
119 do_check_eq(collection.get_log[4].full, undefined); |
|
120 do_check_eq(collection.get_log[4].sort, "index"); |
|
121 do_check_eq(collection.get_log[4].limit, MAX_HISTORY_DOWNLOAD); |
|
122 for (let i = 0; i <= Math.floor((234 - 50) / MOBILE_BATCH_SIZE); i++) { |
|
123 let j = i + 5; |
|
124 do_check_eq(collection.get_log[j].full, 1); |
|
125 do_check_eq(collection.get_log[j].limit, undefined); |
|
126 if (i < Math.floor((234 - 50) / MOBILE_BATCH_SIZE)) |
|
127 do_check_eq(collection.get_log[j].ids.length, MOBILE_BATCH_SIZE); |
|
128 else |
|
129 do_check_eq(collection.get_log[j].ids.length, 234 % MOBILE_BATCH_SIZE); |
|
130 } |
|
131 |
|
132 } finally { |
|
133 PlacesUtils.history.removeAllPages(); |
|
134 server.stop(do_test_finished); |
|
135 Svc.Prefs.resetBranch(""); |
|
136 Service.recordManager.clearCache(); |
|
137 } |
|
138 }); |
|
139 |
|
140 function run_test() { |
|
141 generateNewKeys(Service.collectionKeys); |
|
142 |
|
143 run_next_test(); |
|
144 } |