test_000_frecency.js (6048B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /* 6 7 Autocomplete Frecency Tests 8 9 - adds a visit for each transition type (recent and old) 10 - search 11 - test number of matches 12 - test each item's location in results 13 - verify recent visits rank higher than older visits 14 15 */ 16 17 testEngine_setup(); 18 19 // A visit will be added for each transition. 20 const TRANSITIONS = { 21 link: PlacesUtils.history.TRANSITION_LINK, 22 redirectPermanent: PlacesUtils.history.TRANSITION_REDIRECT_PERMANENT, 23 redirectTemporary: PlacesUtils.history.TRANSITION_REDIRECT_TEMPORARY, 24 typed: PlacesUtils.history.TRANSITION_TYPED, 25 bookmark: PlacesUtils.history.TRANSITION_BOOKMARK, 26 embed: PlacesUtils.history.TRANSITION_EMBED, 27 download: PlacesUtils.history.TRANSITION_DOWNLOAD, 28 framedLink: PlacesUtils.history.TRANSITION_FRAMED_LINK, 29 reload: PlacesUtils.history.TRANSITION_RELOAD, 30 }; 31 32 // Despite visits being added for EMBED, DOWNLOAD, FRAMED_LINK, and RELOAD, 33 // we don't actually expect them to show up in results. 34 const EXPECTED_TRANSITION_ORDER = [ 35 // High bucket. 36 PlacesUtils.history.TRANSITION_BOOKMARK, 37 PlacesUtils.history.TRANSITION_TYPED, 38 // Medium bucket. 39 PlacesUtils.history.TRANSITION_REDIRECT_TEMPORARY, 40 PlacesUtils.history.TRANSITION_REDIRECT_PERMANENT, 41 PlacesUtils.history.TRANSITION_LINK, 42 ]; 43 44 async function createTestEntries(searchTerm) { 45 const testEntries = []; 46 47 for (let [transitionName, transitionType] of Object.entries(TRANSITIONS)) { 48 let uri = Services.io.newURI( 49 `https://${searchTerm}.com/${transitionName}/` 50 ); 51 52 let title = `${searchTerm} ${transitionName} test`; 53 54 if (transitionType === PlacesUtils.history.TRANSITION_BOOKMARK) { 55 await PlacesTestUtils.addBookmarkWithDetails({ 56 uri, 57 title, 58 }); 59 } 60 61 await PlacesTestUtils.addVisits({ 62 uri, 63 title, 64 transition: transitionType, 65 }); 66 67 if (transitionType === PlacesUtils.history.TRANSITION_TYPED) { 68 PlacesUtils.history.markPageAsTyped(uri); 69 } 70 71 testEntries.push({ 72 uri, 73 title, 74 transitionType, 75 transitionName, 76 age: "recent", 77 }); 78 } 79 80 // 180 days ago in microseconds. 81 // This number was chosen because we want a safe value that is so old, even 82 // a higher bucket wouldn't allow it to remain higher than a more recent 83 // visit that was in a lower non-zero bucket. 84 const OLD_TIME = (Date.now() - 180 * 24 * 60 * 60 * 1000) * 1000; 85 for (let [transitionName, transitionType] of Object.entries(TRANSITIONS)) { 86 let uri = Services.io.newURI( 87 `https://${searchTerm}.com/old/${transitionName}/` 88 ); 89 90 let title = `${searchTerm} ${transitionName} test`; 91 92 if (transitionType === PlacesUtils.history.TRANSITION_BOOKMARK) { 93 await PlacesTestUtils.addBookmarkWithDetails({ 94 uri, 95 title, 96 }); 97 } 98 99 await PlacesTestUtils.addVisits({ 100 uri, 101 title, 102 transition: transitionType, 103 visitDate: OLD_TIME, 104 }); 105 106 if (transitionType === PlacesUtils.history.TRANSITION_TYPED) { 107 PlacesUtils.history.markPageAsTyped(uri); 108 } 109 110 testEntries.push({ 111 uri, 112 title, 113 transitionType, 114 transitionName, 115 age: "old", 116 }); 117 } 118 119 return testEntries; 120 } 121 122 add_task(async function test_frecency() { 123 const searchTerm = "frecency"; 124 const testEntries = await createTestEntries(searchTerm); 125 126 // Disable autoFill for this test. 127 Services.prefs.setBoolPref("browser.urlbar.autoFill", false); 128 Services.prefs.setBoolPref("browser.urlbar.suggest.searches", false); 129 Services.prefs.setBoolPref( 130 "browser.urlbar.scotchBonnet.enableOverride", 131 false 132 ); 133 // always search in history + bookmarks, no matter what the default is 134 Services.prefs.setBoolPref("browser.urlbar.suggest.history", true); 135 Services.prefs.setBoolPref("browser.urlbar.suggest.bookmarks", true); 136 registerCleanupFunction(() => { 137 Services.prefs.clearUserPref("browser.urlbar.suggest.history"); 138 Services.prefs.clearUserPref("browser.urlbar.suggest.bookmarks"); 139 Services.prefs.clearUserPref("browser.urlbar.autoFill"); 140 Services.prefs.clearUserPref("browser.urlbar.suggest.searches"); 141 Services.prefs.clearUserPref("browser.urlbar.scotchBonnet.enableOverride"); 142 }); 143 144 // Make sure there's enough results returned 145 Services.prefs.setIntPref( 146 "browser.urlbar.maxRichResults", 147 // +1 for the heuristic search result. 148 testEntries.length + 1 149 ); 150 151 await PlacesTestUtils.promiseAsyncUpdates(); 152 153 // Perform the search. 154 let context = createContext(searchTerm, { isPrivate: false }); 155 156 let expectedResults = [ 157 makeSearchResult(context, { 158 engineName: SUGGESTIONS_ENGINE_NAME, 159 heuristic: true, 160 }), 161 ]; 162 163 // Add entries in expected recent frecency order. 164 for (let expectedTransition of EXPECTED_TRANSITION_ORDER) { 165 let entry = testEntries.find( 166 e => e.transitionType == expectedTransition && e.age == "recent" 167 ); 168 169 if (expectedTransition === PlacesUtils.history.TRANSITION_BOOKMARK) { 170 expectedResults.push( 171 makeBookmarkResult(context, { uri: entry.uri.spec, title: entry.title }) 172 ); 173 } else { 174 expectedResults.push( 175 makeVisitResult(context, { uri: entry.uri.spec, title: entry.title }) 176 ); 177 } 178 } 179 180 // Add entries in expected recent frecency order. 181 for (let expectedTransition of EXPECTED_TRANSITION_ORDER) { 182 let entry = testEntries.find( 183 e => e.transitionType == expectedTransition && e.age == "old" 184 ); 185 186 if (expectedTransition === PlacesUtils.history.TRANSITION_BOOKMARK) { 187 expectedResults.push( 188 makeBookmarkResult(context, { uri: entry.uri.spec, title: entry.title }) 189 ); 190 } else { 191 expectedResults.push( 192 makeVisitResult(context, { uri: entry.uri.spec, title: entry.title }) 193 ); 194 } 195 } 196 197 await check_results({ 198 context, 199 matches: expectedResults, 200 }); 201 });