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