1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/sync/tests/unit/test_history_engine.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,144 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +Cu.import("resource://gre/modules/PlacesUtils.jsm"); 1.8 +Cu.import("resource://services-sync/constants.js"); 1.9 +Cu.import("resource://services-sync/engines/history.js"); 1.10 +Cu.import("resource://services-sync/engines.js"); 1.11 +Cu.import("resource://services-sync/identity.js"); 1.12 +Cu.import("resource://services-sync/record.js"); 1.13 +Cu.import("resource://services-sync/service.js"); 1.14 +Cu.import("resource://services-sync/util.js"); 1.15 +Cu.import("resource://testing-common/services/sync/utils.js"); 1.16 + 1.17 +Service.engineManager.clear(); 1.18 + 1.19 +add_test(function test_processIncoming_mobile_history_batched() { 1.20 + _("SyncEngine._processIncoming works on history engine."); 1.21 + 1.22 + let FAKE_DOWNLOAD_LIMIT = 100; 1.23 + 1.24 + Svc.Prefs.set("client.type", "mobile"); 1.25 + PlacesUtils.history.removeAllPages(); 1.26 + Service.engineManager.register(HistoryEngine); 1.27 + 1.28 + // A collection that logs each GET 1.29 + let collection = new ServerCollection(); 1.30 + collection.get_log = []; 1.31 + collection._get = collection.get; 1.32 + collection.get = function (options) { 1.33 + this.get_log.push(options); 1.34 + return this._get(options); 1.35 + }; 1.36 + 1.37 + let server = sync_httpd_setup({ 1.38 + "/1.1/foo/storage/history": collection.handler() 1.39 + }); 1.40 + 1.41 + new SyncTestingInfrastructure(server); 1.42 + 1.43 + // Let's create some 234 server side history records. They're all at least 1.44 + // 10 minutes old. 1.45 + let visitType = Ci.nsINavHistoryService.TRANSITION_LINK; 1.46 + for (var i = 0; i < 234; i++) { 1.47 + let id = 'record-no' + ("00" + i).slice(-3); 1.48 + let modified = Date.now()/1000 - 60*(i+10); 1.49 + let payload = encryptPayload({ 1.50 + id: id, 1.51 + histUri: "http://foo/bar?" + id, 1.52 + title: id, 1.53 + sortindex: i, 1.54 + visits: [{date: (modified - 5) * 1000000, type: visitType}], 1.55 + deleted: false}); 1.56 + 1.57 + let wbo = new ServerWBO(id, payload); 1.58 + wbo.modified = modified; 1.59 + collection.insertWBO(wbo); 1.60 + } 1.61 + 1.62 + let engine = Service.engineManager.get("history"); 1.63 + let meta_global = Service.recordManager.set(engine.metaURL, 1.64 + new WBORecord(engine.metaURL)); 1.65 + meta_global.payload.engines = {history: {version: engine.version, 1.66 + syncID: engine.syncID}}; 1.67 + 1.68 + try { 1.69 + 1.70 + _("On a mobile client, we get new records from the server in batches of 50."); 1.71 + engine._syncStartup(); 1.72 + 1.73 + // Fake a lower limit. 1.74 + engine.downloadLimit = FAKE_DOWNLOAD_LIMIT; 1.75 + _("Last modified: " + engine.lastModified); 1.76 + _("Processing..."); 1.77 + engine._processIncoming(); 1.78 + 1.79 + _("Last modified: " + engine.lastModified); 1.80 + engine._syncFinish(); 1.81 + 1.82 + // Back to the normal limit. 1.83 + _("Running again. Should fetch none, because of lastModified"); 1.84 + engine.downloadLimit = MAX_HISTORY_DOWNLOAD; 1.85 + _("Processing..."); 1.86 + engine._processIncoming(); 1.87 + 1.88 + _("Last modified: " + engine.lastModified); 1.89 + _("Running again. Expecting to pull everything"); 1.90 + 1.91 + engine.lastModified = undefined; 1.92 + engine.lastSync = 0; 1.93 + _("Processing..."); 1.94 + engine._processIncoming(); 1.95 + 1.96 + _("Last modified: " + engine.lastModified); 1.97 + 1.98 + // Verify that the right number of GET requests with the right 1.99 + // kind of parameters were made. 1.100 + do_check_eq(collection.get_log.length, 1.101 + // First try: 1.102 + 1 + // First 50... 1.103 + 1 + // 1 GUID fetch... 1.104 + // 1 fetch... 1.105 + Math.ceil((FAKE_DOWNLOAD_LIMIT - 50) / MOBILE_BATCH_SIZE) + 1.106 + // Second try: none 1.107 + // Third try: 1.108 + 1 + // First 50... 1.109 + 1 + // 1 GUID fetch... 1.110 + // 4 fetch... 1.111 + Math.ceil((234 - 50) / MOBILE_BATCH_SIZE)); 1.112 + 1.113 + // Check the structure of each HTTP request. 1.114 + do_check_eq(collection.get_log[0].full, 1); 1.115 + do_check_eq(collection.get_log[0].limit, MOBILE_BATCH_SIZE); 1.116 + do_check_eq(collection.get_log[1].full, undefined); 1.117 + do_check_eq(collection.get_log[1].sort, "index"); 1.118 + do_check_eq(collection.get_log[1].limit, FAKE_DOWNLOAD_LIMIT); 1.119 + do_check_eq(collection.get_log[2].full, 1); 1.120 + do_check_eq(collection.get_log[3].full, 1); 1.121 + do_check_eq(collection.get_log[3].limit, MOBILE_BATCH_SIZE); 1.122 + do_check_eq(collection.get_log[4].full, undefined); 1.123 + do_check_eq(collection.get_log[4].sort, "index"); 1.124 + do_check_eq(collection.get_log[4].limit, MAX_HISTORY_DOWNLOAD); 1.125 + for (let i = 0; i <= Math.floor((234 - 50) / MOBILE_BATCH_SIZE); i++) { 1.126 + let j = i + 5; 1.127 + do_check_eq(collection.get_log[j].full, 1); 1.128 + do_check_eq(collection.get_log[j].limit, undefined); 1.129 + if (i < Math.floor((234 - 50) / MOBILE_BATCH_SIZE)) 1.130 + do_check_eq(collection.get_log[j].ids.length, MOBILE_BATCH_SIZE); 1.131 + else 1.132 + do_check_eq(collection.get_log[j].ids.length, 234 % MOBILE_BATCH_SIZE); 1.133 + } 1.134 + 1.135 + } finally { 1.136 + PlacesUtils.history.removeAllPages(); 1.137 + server.stop(do_test_finished); 1.138 + Svc.Prefs.resetBranch(""); 1.139 + Service.recordManager.clearCache(); 1.140 + } 1.141 +}); 1.142 + 1.143 +function run_test() { 1.144 + generateNewKeys(Service.collectionKeys); 1.145 + 1.146 + run_next_test(); 1.147 +}