From cac8494c04c65629b82a57adee347420a4e6f041 Mon Sep 17 00:00:00 2001 From: Masatoshi Kimura Date: Sat, 7 Oct 2017 06:04:56 +0900 Subject: [PATCH] Bug 1406594 - Fix history.search and history.getVisited so that they find hidden items correctly. r=aswan,bsilverberg I had to add a mochitest because PlacesUtils.history.insertMany cannot create hidden redirects. I also added a support for the "reload" transition while I am here. MozReview-Commit-ID: ECQp6pW2r8v --HG-- extra : rebase_source : d4456d32a57cdadec8ca383a2b09dee29e20b10f --- browser/components/extensions/ext-history.js | 3 + .../extensions/test/mochitest/.eslintrc.js | 10 +++ .../extensions/test/mochitest/mochitest.ini | 2 + .../mochitest/test_ext_history_redirect.html | 87 +++++++++++++++++++ .../test/xpcshell/test_ext_history.js | 77 ++++++++++++++++ 5 files changed, 179 insertions(+) create mode 100644 browser/components/extensions/test/mochitest/.eslintrc.js create mode 100644 browser/components/extensions/test/mochitest/test_ext_history_redirect.html diff --git a/browser/components/extensions/ext-history.js b/browser/components/extensions/ext-history.js index 54a23837b0fc..ca2d2cc9f145 100644 --- a/browser/components/extensions/ext-history.js +++ b/browser/components/extensions/ext-history.js @@ -21,6 +21,7 @@ const TRANSITION_TO_TRANSITION_TYPES_MAP = new Map([ ["auto_bookmark", nsINavHistoryService.TRANSITION_BOOKMARK], ["auto_subframe", nsINavHistoryService.TRANSITION_EMBED], ["manual_subframe", nsINavHistoryService.TRANSITION_FRAMED_LINK], + ["reload", nsINavHistoryService.TRANSITION_RELOAD], ]); let TRANSITION_TYPE_TO_TRANSITIONS_MAP = new Map(); @@ -189,6 +190,7 @@ this.history = class extends ExtensionAPI { } let options = PlacesUtils.history.getNewQueryOptions(); + options.includeHidden = true; options.sortingMode = options.SORT_BY_DATE_DESCENDING; options.maxResults = query.maxResults || 100; @@ -208,6 +210,7 @@ this.history = class extends ExtensionAPI { } let options = PlacesUtils.history.getNewQueryOptions(); + options.includeHidden = true; options.sortingMode = options.SORT_BY_DATE_DESCENDING; options.resultType = options.RESULTS_AS_VISIT; diff --git a/browser/components/extensions/test/mochitest/.eslintrc.js b/browser/components/extensions/test/mochitest/.eslintrc.js new file mode 100644 index 000000000000..9a8a1318d1ca --- /dev/null +++ b/browser/components/extensions/test/mochitest/.eslintrc.js @@ -0,0 +1,10 @@ +"use strict"; + +module.exports = { + "extends": "plugin:mozilla/mochitest-test", + + "env": { + "browser": true, + "webextensions": true, + }, +}; diff --git a/browser/components/extensions/test/mochitest/mochitest.ini b/browser/components/extensions/test/mochitest/mochitest.ini index f4fe07931480..e365d9930c8c 100644 --- a/browser/components/extensions/test/mochitest/mochitest.ini +++ b/browser/components/extensions/test/mochitest/mochitest.ini @@ -2,6 +2,8 @@ support-files = ../../../../../toolkit/components/extensions/test/mochitest/test_ext_all_apis.js ../../../../../toolkit/components/extensions/test/mochitest/file_sample.html + ../../../../../toolkit/components/extensions/test/mochitest/redirection.sjs tags = webextensions [test_ext_all_apis.html] +[test_ext_history_redirect.html] diff --git a/browser/components/extensions/test/mochitest/test_ext_history_redirect.html b/browser/components/extensions/test/mochitest/test_ext_history_redirect.html new file mode 100644 index 000000000000..0ea1ecfffcbd --- /dev/null +++ b/browser/components/extensions/test/mochitest/test_ext_history_redirect.html @@ -0,0 +1,87 @@ + + + + Test for simple WebExtension + + + + + + + + + + + diff --git a/browser/components/extensions/test/xpcshell/test_ext_history.js b/browser/components/extensions/test/xpcshell/test_ext_history.js index 740ae6a5faf9..5bcc93263fd0 100644 --- a/browser/components/extensions/test/xpcshell/test_ext_history.js +++ b/browser/components/extensions/test/xpcshell/test_ext_history.js @@ -404,6 +404,83 @@ add_task(async function test_get_visits() { await extension.unload(); }); +add_task(async function test_transition_types() { + const VISIT_URL_PREFIX = "http://example.com/"; + const TRANSITIONS = [ + ["link", Ci.nsINavHistoryService.TRANSITION_LINK], + ["typed", Ci.nsINavHistoryService.TRANSITION_TYPED], + ["auto_bookmark", Ci.nsINavHistoryService.TRANSITION_BOOKMARK], + // Only session history contains TRANSITION_EMBED visits, + // So global history query cannot find them. + // ["auto_subframe", Ci.nsINavHistoryService.TRANSITION_EMBED], + // Redirects are not correctly tested here because History::UpdatePlaces + // will not make redirect entries hidden. + ["link", Ci.nsINavHistoryService.TRANSITION_REDIRECT_PERMANENT], + ["link", Ci.nsINavHistoryService.TRANSITION_REDIRECT_TEMPORARY], + ["link", Ci.nsINavHistoryService.TRANSITION_DOWNLOAD], + ["manual_subframe", Ci.nsINavHistoryService.TRANSITION_FRAMED_LINK], + ["reload", Ci.nsINavHistoryService.TRANSITION_RELOAD], + ]; + + // pages/visits to add via History.insertMany + let pageInfos = []; + let visitDate = new Date(1999, 9, 9, 9, 9).getTime(); + for (let [, transitionType] of TRANSITIONS) { + pageInfos.push({ + url: VISIT_URL_PREFIX + transitionType + "/", + visits: [ + {transition: transitionType, date: new Date(visitDate -= 1000)}, + ], + }); + } + + function background() { + browser.test.onMessage.addListener(async (msg, url) => { + switch (msg) { + case "search": { + let results = await browser.history.search({text: "", startTime: new Date(0)}); + browser.test.sendMessage("search-result", results); + break; + } + case "get-visits": { + let results = await browser.history.getVisits({url}); + browser.test.sendMessage("get-visits-result", results); + break; + } + } + }); + + browser.test.sendMessage("ready"); + } + + let extension = ExtensionTestUtils.loadExtension({ + manifest: { + permissions: ["history"], + }, + background, + }); + + await PlacesTestUtils.clearHistory(); + await extension.startup(); + await extension.awaitMessage("ready"); + + await PlacesUtils.history.insertMany(pageInfos); + + extension.sendMessage("search"); + let results = await extension.awaitMessage("search-result"); + equal(results.length, pageInfos.length, "search returned expected length of results"); + for (let i = 0; i < pageInfos.length; ++i) { + equal(results[i].url, pageInfos[i].url, "search returned the expected url"); + + extension.sendMessage("get-visits", pageInfos[i].url); + let visits = await extension.awaitMessage("get-visits-result"); + equal(visits.length, 1, "getVisits returned expected length of visits"); + equal(visits[0].transition, TRANSITIONS[i][0], "getVisits returned the expected transition"); + } + + await extension.unload(); +}); + add_task(async function test_on_visited() { const SINGLE_VISIT_URL = "http://example.com/1/"; const DOUBLE_VISIT_URL = "http://example.com/2/";