test_IE_history.js (5405B)
1 "use strict"; 2 3 const { MockRegistrar } = ChromeUtils.importESModule( 4 "resource://testing-common/MockRegistrar.sys.mjs" 5 ); 6 7 // These match what we add to IE via InsertIEHistory.exe. 8 const TEST_ENTRIES = [ 9 { 10 url: "http://www.mozilla.org/1", 11 title: "Mozilla HTTP Test", 12 }, 13 { 14 url: "https://www.mozilla.org/2", 15 // Test character encoding with a fox emoji: 16 title: "Mozilla HTTPS Test 🦊", 17 }, 18 ]; 19 20 function insertIEHistory() { 21 let file = do_get_file("InsertIEHistory.exe", false); 22 let process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess); 23 process.init(file); 24 25 let args = []; 26 process.run(true, args, args.length); 27 28 Assert.ok(!process.isRunning, "Should be done running"); 29 Assert.equal(process.exitValue, 0, "Check exit code"); 30 } 31 32 add_task(async function setup() { 33 await PlacesUtils.history.clear(); 34 35 insertIEHistory(); 36 37 registerCleanupFunction(async () => { 38 await PlacesUtils.history.clear(); 39 }); 40 }); 41 42 add_task(async function test_IE_history() { 43 let migrator = await MigrationUtils.getMigrator("ie"); 44 Assert.ok(await migrator.isSourceAvailable(), "Source is available"); 45 46 await promiseMigration(migrator, MigrationUtils.resourceTypes.HISTORY); 47 48 for (let { url, title } of TEST_ENTRIES) { 49 let entry = await PlacesUtils.history.fetch(url, { includeVisits: true }); 50 Assert.equal(entry.url, url, "Should have the correct URL"); 51 Assert.equal(entry.title, title, "Should have the correct title"); 52 Assert.ok(!!entry.visits.length, "Should have some visits"); 53 } 54 55 await PlacesUtils.history.clear(); 56 }); 57 58 /** 59 * Tests that history visits from IE that have a visit date older than 60 * maxAgeInDays days do not get imported. 61 */ 62 add_task(async function test_IE_history_past_max_days() { 63 // The InsertIEHistory program inserts two history visits using the MS COM 64 // IUrlHistoryStg interface. That interface does not allow us to dictate 65 // the visit times of those history visits. Thankfully, we can temporarily 66 // mock out the @mozilla.org/profile/migrator/iehistoryenumerator;1 to return 67 // some entries that we expect to expire. 68 69 /** 70 * An implmentation of nsISimpleEnumerator that wraps a JavaScript Array. 71 */ 72 class nsSimpleEnumerator { 73 #items; 74 #nextIndex; 75 76 constructor(items) { 77 this.#items = items; 78 this.#nextIndex = 0; 79 } 80 81 hasMoreElements() { 82 return this.#nextIndex < this.#items.length; 83 } 84 85 getNext() { 86 if (!this.hasMoreElements()) { 87 throw Components.Exception("", Cr.NS_ERROR_NOT_AVAILABLE); 88 } 89 90 return this.#items[this.#nextIndex++]; 91 } 92 93 [Symbol.iterator]() { 94 return this.#items.values(); 95 } 96 97 QueryInterface = ChromeUtils.generateQI(["nsISimpleEnumerator"]); 98 } 99 100 Assert.less( 101 MigrationUtils.HISTORY_MAX_AGE_IN_DAYS, 102 300, 103 "This test expects the current pref to be less than the youngest expired visit." 104 ); 105 Assert.greater( 106 MigrationUtils.HISTORY_MAX_AGE_IN_DAYS, 107 160, 108 "This test expects the current pref to be greater than the oldest unexpired visit." 109 ); 110 111 const EXPIRED_VISITS = [ 112 new Map([ 113 ["uri", Services.io.newURI("https://test1.invalid")], 114 ["title", "Test history visit 1"], 115 ["time", PRTimeDaysAgo(500)], 116 ]), 117 new Map([ 118 ["uri", Services.io.newURI("https://test2.invalid")], 119 ["title", "Test history visit 2"], 120 ["time", PRTimeDaysAgo(450)], 121 ]), 122 new Map([ 123 ["uri", Services.io.newURI("https://test3.invalid")], 124 ["title", "Test history visit 3"], 125 ["time", PRTimeDaysAgo(300)], 126 ]), 127 ]; 128 129 const UNEXPIRED_VISITS = [ 130 new Map([ 131 ["uri", Services.io.newURI("https://test4.invalid")], 132 ["title", "Test history visit 4"], 133 ]), 134 new Map([ 135 ["uri", Services.io.newURI("https://test5.invalid")], 136 ["title", "Test history visit 5"], 137 ["time", PRTimeDaysAgo(160)], 138 ]), 139 new Map([ 140 ["uri", Services.io.newURI("https://test6.invalid")], 141 ["title", "Test history visit 6"], 142 ["time", PRTimeDaysAgo(50)], 143 ]), 144 new Map([ 145 ["uri", Services.io.newURI("https://test7.invalid")], 146 ["title", "Test history visit 7"], 147 ["time", PRTimeDaysAgo(0)], 148 ]), 149 ]; 150 151 let fakeIEHistoryEnumerator = MockRegistrar.register( 152 "@mozilla.org/profile/migrator/iehistoryenumerator;1", 153 new nsSimpleEnumerator([...EXPIRED_VISITS, ...UNEXPIRED_VISITS]) 154 ); 155 registerCleanupFunction(() => { 156 MockRegistrar.unregister(fakeIEHistoryEnumerator); 157 }); 158 159 let migrator = await MigrationUtils.getMigrator("ie"); 160 Assert.ok(await migrator.isSourceAvailable(), "Source is available"); 161 162 await promiseMigration(migrator, MigrationUtils.resourceTypes.HISTORY); 163 164 for (let visit of EXPIRED_VISITS) { 165 let entry = await PlacesUtils.history.fetch(visit.get("uri").spec, { 166 includeVisits: true, 167 }); 168 Assert.equal(entry, null, "Should not have found an entry."); 169 } 170 171 for (let visit of UNEXPIRED_VISITS) { 172 let entry = await PlacesUtils.history.fetch(visit.get("uri"), { 173 includeVisits: true, 174 }); 175 Assert.equal( 176 entry.url, 177 visit.get("uri").spec, 178 "Should have the correct URL" 179 ); 180 Assert.equal( 181 entry.title, 182 visit.get("title"), 183 "Should have the correct title" 184 ); 185 Assert.ok(!!entry.visits.length, "Should have some visits"); 186 } 187 188 await PlacesUtils.history.clear(); 189 });