mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-11 04:15:43 +00:00
Backed out changeset 1151e3a09e77 (bug 1265836)
This commit is contained in:
parent
6cec6a812e
commit
8befd3936b
@ -4,32 +4,16 @@
|
||||
|
||||
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
||||
|
||||
Cu.import("resource://gre/modules/PlacesUtils.jsm");
|
||||
let History = PlacesUtils.history;
|
||||
XPCOMUtils.defineLazyGetter(this, "History", () => {
|
||||
Cu.import("resource://gre/modules/PlacesUtils.jsm");
|
||||
return PlacesUtils.history;
|
||||
});
|
||||
|
||||
Cu.import("resource://gre/modules/ExtensionUtils.jsm");
|
||||
const {
|
||||
normalizeTime,
|
||||
} = ExtensionUtils;
|
||||
|
||||
const TRANSITION_TO_TRANSITION_TYPES_MAP = new Map([
|
||||
["link", History.TRANSITION_LINK],
|
||||
["typed", History.TRANSITION_TYPED],
|
||||
["auto_bookmark", History.TRANSITION_BOOKMARK],
|
||||
["auto_subframe", History.TRANSITION_EMBED],
|
||||
["manual_subframe", History.TRANSITION_FRAMED_LINK],
|
||||
]);
|
||||
|
||||
function getTransitionType(transition) {
|
||||
// cannot set a default value for the transition argument as the framework sets it to null
|
||||
transition = transition || "link";
|
||||
let transitionType = TRANSITION_TO_TRANSITION_TYPES_MAP.get(transition);
|
||||
if (!transitionType) {
|
||||
throw new Error(`|${transition}| is not a supported transition for history`);
|
||||
}
|
||||
return transitionType;
|
||||
}
|
||||
|
||||
/*
|
||||
* Converts a nsINavHistoryResultNode into a plain object
|
||||
*
|
||||
@ -64,32 +48,6 @@ function convertNavHistoryContainerResultNode(container) {
|
||||
extensions.registerSchemaAPI("history", "history", (extension, context) => {
|
||||
return {
|
||||
history: {
|
||||
addUrl: function(details) {
|
||||
let transition, date;
|
||||
try {
|
||||
transition = getTransitionType(details.transition);
|
||||
} catch (error) {
|
||||
return Promise.reject({message: error.message});
|
||||
}
|
||||
if (details.visitTime) {
|
||||
date = normalizeTime(details.visitTime);
|
||||
}
|
||||
let pageInfo = {
|
||||
title: details.title,
|
||||
url: details.url,
|
||||
visits: [
|
||||
{
|
||||
transition,
|
||||
date,
|
||||
},
|
||||
],
|
||||
};
|
||||
try {
|
||||
return History.insert(pageInfo).then(() => undefined);
|
||||
} catch (error) {
|
||||
return Promise.reject({message: error.message});
|
||||
}
|
||||
},
|
||||
deleteAll: function() {
|
||||
return History.clear();
|
||||
},
|
||||
|
@ -187,8 +187,9 @@
|
||||
},
|
||||
{
|
||||
"name": "addUrl",
|
||||
"unsupported": true,
|
||||
"type": "function",
|
||||
"description": "Adds a URL to the history with a default visitTime of the current time and a default $(topic:transition-types)[transition type] of \"link\".",
|
||||
"description": "Adds a URL to the history at the current time with a $(topic:transition-types)[transition type] of \"link\".",
|
||||
"async": "callback",
|
||||
"parameters": [
|
||||
{
|
||||
@ -197,22 +198,7 @@
|
||||
"properties": {
|
||||
"url": {
|
||||
"type": "string",
|
||||
"description": "The URL to add. Must be a valid URL that can be added to history."
|
||||
},
|
||||
"title": {
|
||||
"type": "string",
|
||||
"optional": true,
|
||||
"description": "The title of the page."
|
||||
},
|
||||
"transition": {
|
||||
"$ref": "TransitionType",
|
||||
"optional": true,
|
||||
"description": "The $(topic:transition-types)[transition type] for this visit from its referrer."
|
||||
},
|
||||
"visitTime": {
|
||||
"$ref": "HistoryTime",
|
||||
"optional": true,
|
||||
"description": "The date when this visit occurred."
|
||||
"description": "The URL to add."
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -4,10 +4,6 @@
|
||||
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "PlacesTestUtils",
|
||||
"resource://testing-common/PlacesTestUtils.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
|
||||
"resource://gre/modules/PlacesUtils.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "ExtensionUtils",
|
||||
"resource://gre/modules/ExtensionUtils.jsm");
|
||||
|
||||
add_task(function* test_delete() {
|
||||
function background() {
|
||||
@ -34,6 +30,7 @@ add_task(function* test_delete() {
|
||||
}
|
||||
|
||||
const REFERENCE_DATE = new Date(1999, 9, 9, 9, 9);
|
||||
let {PlacesUtils} = Cu.import("resource://gre/modules/PlacesUtils.jsm", {});
|
||||
|
||||
let extension = ExtensionTestUtils.loadExtension({
|
||||
manifest: {
|
||||
@ -194,86 +191,3 @@ add_task(function* test_search() {
|
||||
yield extension.unload();
|
||||
yield PlacesTestUtils.clearHistory();
|
||||
});
|
||||
|
||||
add_task(function* test_add_url() {
|
||||
function background() {
|
||||
const TEST_DOMAIN = "http://example.com/";
|
||||
|
||||
browser.test.onMessage.addListener((msg, testData) => {
|
||||
let [details, type] = testData;
|
||||
details.url = details.url || `${TEST_DOMAIN}${type}`;
|
||||
if (msg === "add-url") {
|
||||
details.title = `Title for ${type}`;
|
||||
browser.history.addUrl(details).then(() => {
|
||||
return browser.history.search({text: details.url});
|
||||
}).then(results => {
|
||||
browser.test.assertEq(1, results.length, "1 result found when searching for added URL");
|
||||
browser.test.sendMessage("url-added", {details, result: results[0]});
|
||||
});
|
||||
} else if (msg === "expect-failure") {
|
||||
let expectedMsg = testData[2];
|
||||
browser.history.addUrl(details).then(() => {
|
||||
browser.test.fail(`Expected error thrown for ${type}`);
|
||||
}, error => {
|
||||
browser.test.assertTrue(
|
||||
error.message.includes(expectedMsg),
|
||||
`"Expected error thrown when trying to add a URL with ${type}`
|
||||
);
|
||||
browser.test.sendMessage("add-failed");
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
browser.test.sendMessage("ready");
|
||||
}
|
||||
|
||||
let addTestData = [
|
||||
[{}, "default"],
|
||||
[{visitTime: new Date()}, "with_date"],
|
||||
[{visitTime: Date.now()}, "with_ms_number"],
|
||||
[{visitTime: Date.now().toString()}, "with_ms_string"],
|
||||
[{visitTime: new Date().toISOString()}, "with_iso_string"],
|
||||
[{transition: "typed"}, "valid_transition"],
|
||||
];
|
||||
|
||||
let failTestData = [
|
||||
[{transition: "generated"}, "an invalid transition", "|generated| is not a supported transition for history"],
|
||||
[{visitTime: Date.now() + 1000000}, "a future date", "cannot be a future date"],
|
||||
[{url: "about.config"}, "an invalid url", "about.config is not a valid URL"],
|
||||
];
|
||||
|
||||
function* checkUrl(results) {
|
||||
ok(yield PlacesTestUtils.isPageInDB(results.details.url), `${results.details.url} found in history database`);
|
||||
ok(PlacesUtils.isValidGuid(results.result.id), "URL was added with a valid id");
|
||||
is(results.result.title, results.details.title, "URL was added with the correct title");
|
||||
if (results.details.visitTime) {
|
||||
is(results.result.lastVisitTime,
|
||||
Number(ExtensionUtils.normalizeTime(results.details.visitTime)),
|
||||
"URL was added with the correct date");
|
||||
}
|
||||
}
|
||||
|
||||
let extension = ExtensionTestUtils.loadExtension({
|
||||
manifest: {
|
||||
permissions: ["history"],
|
||||
},
|
||||
background: `(${background})()`,
|
||||
});
|
||||
|
||||
yield PlacesTestUtils.clearHistory();
|
||||
yield extension.startup();
|
||||
yield extension.awaitMessage("ready");
|
||||
|
||||
for (let data of addTestData) {
|
||||
extension.sendMessage("add-url", data);
|
||||
let results = yield extension.awaitMessage("url-added");
|
||||
yield checkUrl(results);
|
||||
}
|
||||
|
||||
for (let data of failTestData) {
|
||||
extension.sendMessage("expect-failure", data);
|
||||
yield extension.awaitMessage("add-failed");
|
||||
}
|
||||
|
||||
yield extension.unload();
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user