2013-05-06 21:10:28 -07:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
|
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
|
|
|
|
const TEST_PAGE_URL = "http://mochi.test:8888/browser/toolkit/components/thumbnails/test/thumbnails_background.sjs";
|
|
|
|
|
|
|
|
const imports = {};
|
|
|
|
Cu.import("resource://gre/modules/BackgroundPageThumbs.jsm", imports);
|
|
|
|
Cu.import("resource://gre/modules/PageThumbs.jsm", imports);
|
|
|
|
Cu.import("resource://gre/modules/Task.jsm", imports);
|
|
|
|
Cu.import("resource://gre/modules/commonjs/sdk/core/promise.js", imports);
|
|
|
|
registerCleanupFunction(function () {
|
|
|
|
imports.BackgroundPageThumbs._destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
function test() {
|
2013-09-23 02:27:26 -07:00
|
|
|
requestLongerTimeout(2);
|
2013-05-06 21:10:28 -07:00
|
|
|
waitForExplicitFinish();
|
|
|
|
spawnNextTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
function spawnNextTest() {
|
|
|
|
if (!tests.length) {
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2013-09-08 17:25:13 -07:00
|
|
|
|
|
|
|
let nextTest = tests.shift();
|
|
|
|
info("starting sub-test " + nextTest.name);
|
|
|
|
imports.Task.spawn(nextTest).then(spawnNextTest, function onError(err) {
|
2013-05-06 21:10:28 -07:00
|
|
|
ok(false, err);
|
|
|
|
spawnNextTest();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let tests = [
|
|
|
|
|
|
|
|
function basic() {
|
|
|
|
let url = "http://www.example.com/";
|
|
|
|
let file = fileForURL(url);
|
|
|
|
ok(!file.exists(), "Thumbnail should not be cached yet.");
|
|
|
|
|
|
|
|
let capturedURL = yield capture(url);
|
|
|
|
is(capturedURL, url, "Captured URL should be URL passed to capture");
|
|
|
|
|
|
|
|
ok(file.exists(), "Thumbnail should be cached after capture: " + file.path);
|
|
|
|
file.remove(false);
|
|
|
|
},
|
|
|
|
|
|
|
|
function queueing() {
|
|
|
|
let deferred = imports.Promise.defer();
|
|
|
|
let urls = [
|
|
|
|
"http://www.example.com/0",
|
|
|
|
"http://www.example.com/1",
|
2013-07-11 20:59:16 -07:00
|
|
|
// an item that will timeout to ensure timeouts work and we resume.
|
|
|
|
testPageURL({ wait: 2002 }),
|
2013-05-06 21:10:28 -07:00
|
|
|
"http://www.example.com/2",
|
|
|
|
];
|
|
|
|
let files = urls.map(fileForURL);
|
|
|
|
files.forEach(f => ok(!f.exists(), "Thumbnail should not be cached yet."));
|
|
|
|
urls.forEach(function (url) {
|
2013-08-24 01:10:52 -07:00
|
|
|
let isTimeoutTest = url.indexOf("wait") >= 0;
|
2013-05-06 21:10:28 -07:00
|
|
|
imports.BackgroundPageThumbs.capture(url, {
|
2013-07-11 20:59:16 -07:00
|
|
|
timeout: isTimeoutTest ? 100 : 30000,
|
2013-05-06 21:10:28 -07:00
|
|
|
onDone: function onDone(capturedURL) {
|
|
|
|
ok(urls.length > 0, "onDone called, so URLs should still remain");
|
|
|
|
is(capturedURL, urls.shift(),
|
|
|
|
"Captured URL should be currently expected URL (i.e., " +
|
|
|
|
"capture() callbacks should be called in the correct order)");
|
|
|
|
let file = files.shift();
|
2013-07-11 20:59:16 -07:00
|
|
|
if (isTimeoutTest) {
|
|
|
|
ok(!file.exists(),
|
|
|
|
"Thumbnail shouldn't exist for timed out capture: " + file.path);
|
|
|
|
} else {
|
|
|
|
ok(file.exists(),
|
|
|
|
"Thumbnail should be cached after capture: " + file.path);
|
|
|
|
}
|
2013-05-06 21:10:28 -07:00
|
|
|
if (!urls.length)
|
|
|
|
deferred.resolve();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
yield deferred.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
function timeout() {
|
|
|
|
let deferred = imports.Promise.defer();
|
2013-05-21 19:45:32 -07:00
|
|
|
let url = testPageURL({ wait: 30000 });
|
2013-05-06 21:10:28 -07:00
|
|
|
let file = fileForURL(url);
|
|
|
|
ok(!file.exists(), "Thumbnail should not be cached already.");
|
|
|
|
let numCalls = 0;
|
2013-05-21 19:45:32 -07:00
|
|
|
imports.BackgroundPageThumbs.capture(url, {
|
2013-05-06 21:10:28 -07:00
|
|
|
timeout: 0,
|
|
|
|
onDone: function onDone(capturedURL) {
|
|
|
|
is(capturedURL, url, "Captured URL should be URL passed to capture");
|
|
|
|
is(numCalls++, 0, "onDone should be called only once");
|
|
|
|
ok(!file.exists(),
|
|
|
|
"Capture timed out so thumbnail should not be cached: " + file.path);
|
|
|
|
deferred.resolve();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
yield deferred.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
function redirect() {
|
|
|
|
let finalURL = "http://example.com/redirected";
|
|
|
|
let originalURL = testPageURL({ redirect: finalURL });
|
|
|
|
|
|
|
|
let originalFile = fileForURL(originalURL);
|
|
|
|
ok(!originalFile.exists(),
|
|
|
|
"Thumbnail file for original URL should not exist yet.");
|
|
|
|
|
|
|
|
let finalFile = fileForURL(finalURL);
|
|
|
|
ok(!finalFile.exists(),
|
|
|
|
"Thumbnail file for final URL should not exist yet.");
|
|
|
|
|
|
|
|
let capturedURL = yield capture(originalURL);
|
|
|
|
is(capturedURL, originalURL,
|
|
|
|
"Captured URL should be URL passed to capture");
|
|
|
|
ok(originalFile.exists(),
|
|
|
|
"Thumbnail for original URL should be cached: " + originalFile.path);
|
|
|
|
ok(finalFile.exists(),
|
|
|
|
"Thumbnail for final URL should be cached: " + finalFile.path);
|
|
|
|
|
|
|
|
originalFile.remove(false);
|
|
|
|
finalFile.remove(false);
|
|
|
|
},
|
|
|
|
|
|
|
|
function destroyBrowserTimeout() {
|
|
|
|
let url1 = "http://example.com/1";
|
|
|
|
let file1 = fileForURL(url1);
|
|
|
|
ok(!file1.exists(), "First file should not exist yet.");
|
|
|
|
|
|
|
|
let url2 = "http://example.com/2";
|
|
|
|
let file2 = fileForURL(url2);
|
|
|
|
ok(!file2.exists(), "Second file should not exist yet.");
|
|
|
|
|
|
|
|
let defaultTimeout = imports.BackgroundPageThumbs._destroyBrowserTimeout;
|
|
|
|
imports.BackgroundPageThumbs._destroyBrowserTimeout = 1000;
|
|
|
|
|
|
|
|
yield capture(url1);
|
|
|
|
ok(file1.exists(), "First file should exist after capture.");
|
2013-07-11 00:50:55 -07:00
|
|
|
file1.remove(false);
|
2013-05-06 21:10:28 -07:00
|
|
|
|
|
|
|
yield wait(2000);
|
|
|
|
is(imports.BackgroundPageThumbs._thumbBrowser, undefined,
|
|
|
|
"Thumb browser should be destroyed after timeout.");
|
2013-09-08 17:16:21 -07:00
|
|
|
imports.BackgroundPageThumbs._destroyBrowserTimeout = defaultTimeout;
|
2013-05-06 21:10:28 -07:00
|
|
|
|
|
|
|
yield capture(url2);
|
|
|
|
ok(file2.exists(), "Second file should exist after capture.");
|
2013-07-11 00:50:55 -07:00
|
|
|
file2.remove(false);
|
2013-05-06 21:10:28 -07:00
|
|
|
|
|
|
|
isnot(imports.BackgroundPageThumbs._thumbBrowser, undefined,
|
|
|
|
"Thumb browser should exist immediately after capture.");
|
|
|
|
},
|
2013-05-23 20:13:23 -07:00
|
|
|
|
|
|
|
function privateBrowsingActive() {
|
|
|
|
let url = "http://example.com/";
|
|
|
|
let file = fileForURL(url);
|
|
|
|
ok(!file.exists(), "Thumbnail file should not already exist.");
|
|
|
|
|
|
|
|
let win = yield openPrivateWindow();
|
|
|
|
let capturedURL = yield capture(url);
|
|
|
|
is(capturedURL, url, "Captured URL should be URL passed to capture.");
|
2013-09-08 17:25:13 -07:00
|
|
|
ok(file.exists(),
|
|
|
|
"Thumbnail file should be created even when a private window is open.");
|
|
|
|
file.remove(false);
|
2013-05-23 20:13:23 -07:00
|
|
|
|
|
|
|
win.close();
|
|
|
|
},
|
2013-05-31 22:52:51 -07:00
|
|
|
|
2013-07-12 21:03:15 -07:00
|
|
|
function openPrivateWindowDuringCapture() {
|
|
|
|
let url = "http://example.com/";
|
|
|
|
let file = fileForURL(url);
|
|
|
|
ok(!file.exists(), "Thumbnail file should not already exist.");
|
|
|
|
|
|
|
|
let deferred = imports.Promise.defer();
|
|
|
|
|
|
|
|
let waitCount = 0;
|
|
|
|
function maybeFinish() {
|
|
|
|
if (++waitCount == 2)
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
imports.BackgroundPageThumbs.capture(url, {
|
|
|
|
onDone: function (capturedURL) {
|
|
|
|
is(capturedURL, url, "Captured URL should be URL passed to capture.");
|
2013-09-08 17:25:13 -07:00
|
|
|
ok(file.exists(),
|
|
|
|
"Thumbnail file should be created even though a private window " +
|
2013-07-12 21:03:15 -07:00
|
|
|
"was opened during the capture.");
|
2013-09-08 17:25:13 -07:00
|
|
|
file.remove(false);
|
2013-07-12 21:03:15 -07:00
|
|
|
maybeFinish();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Opening the private window at this point relies on a couple of
|
|
|
|
// implementation details: (1) The capture will start immediately and
|
|
|
|
// synchronously (since at this point in the test, the service is
|
|
|
|
// initialized and its queue is empty), and (2) when it starts the capture
|
|
|
|
// registers with the window watcher.
|
|
|
|
openPrivateWindow().then(function (win) {
|
|
|
|
win.close();
|
|
|
|
maybeFinish();
|
|
|
|
});
|
|
|
|
|
|
|
|
yield deferred.promise;
|
|
|
|
},
|
|
|
|
|
2013-09-08 17:25:13 -07:00
|
|
|
function noCookiesSent() {
|
2013-05-31 22:52:51 -07:00
|
|
|
// Visit the test page in the browser and tell it to set a cookie.
|
|
|
|
let url = testPageURL({ setGreenCookie: true });
|
|
|
|
let tab = gBrowser.loadOneTab(url, { inBackground: false });
|
|
|
|
let browser = tab.linkedBrowser;
|
|
|
|
yield onPageLoad(browser);
|
|
|
|
|
|
|
|
// The root element of the page shouldn't be green yet.
|
|
|
|
let greenStr = "rgb(0, 255, 0)";
|
|
|
|
isnot(browser.contentDocument.documentElement.style.backgroundColor,
|
|
|
|
greenStr,
|
|
|
|
"The page shouldn't be green yet.");
|
|
|
|
|
|
|
|
// Cookie should be set now. Reload the page to verify. Its root element
|
|
|
|
// will be green if the cookie's set.
|
|
|
|
browser.reload();
|
|
|
|
yield onPageLoad(browser);
|
|
|
|
is(browser.contentDocument.documentElement.style.backgroundColor,
|
|
|
|
greenStr,
|
|
|
|
"The page should be green now.");
|
|
|
|
|
|
|
|
// Capture the page. Get the image data of the capture and verify it's not
|
|
|
|
// green. (Checking only the first pixel suffices.)
|
|
|
|
yield capture(url);
|
|
|
|
let file = fileForURL(url);
|
|
|
|
ok(file.exists(), "Thumbnail file should exist after capture.");
|
|
|
|
|
|
|
|
let deferred = imports.Promise.defer();
|
|
|
|
retrieveImageDataForURL(url, function ([r, g, b]) {
|
|
|
|
isnot([r, g, b].toString(), [0, 255, 0].toString(),
|
|
|
|
"The captured page should not be green.");
|
|
|
|
gBrowser.removeTab(tab);
|
2013-05-30 18:17:41 -07:00
|
|
|
file.remove(false);
|
2013-05-31 22:52:51 -07:00
|
|
|
deferred.resolve();
|
|
|
|
});
|
|
|
|
yield deferred.promise;
|
|
|
|
},
|
2013-05-30 18:17:41 -07:00
|
|
|
|
2013-09-08 17:25:13 -07:00
|
|
|
// check that if a page captured in the background attempts to set a cookie,
|
|
|
|
// that cookie is not saved for subsequent requests.
|
|
|
|
function noCookiesStored() {
|
|
|
|
let url = testPageURL({ setRedCookie: true });
|
|
|
|
let file = fileForURL(url);
|
2013-09-08 21:36:38 -07:00
|
|
|
ok(!file.exists(), "Thumbnail file should not exist before capture.");
|
|
|
|
yield capture(url);
|
2013-09-08 17:25:13 -07:00
|
|
|
ok(file.exists(), "Thumbnail file should exist after capture.");
|
2013-09-08 21:34:15 -07:00
|
|
|
file.remove(false);
|
2013-09-08 17:25:13 -07:00
|
|
|
// now load it up in a browser - it should *not* be red, otherwise the
|
|
|
|
// cookie above was saved.
|
|
|
|
let tab = gBrowser.loadOneTab(url, { inBackground: false });
|
|
|
|
let browser = tab.linkedBrowser;
|
|
|
|
yield onPageLoad(browser);
|
|
|
|
|
|
|
|
// The root element of the page shouldn't be red.
|
|
|
|
let redStr = "rgb(255, 0, 0)";
|
|
|
|
isnot(browser.contentDocument.documentElement.style.backgroundColor,
|
|
|
|
redStr,
|
|
|
|
"The page shouldn't be red.");
|
|
|
|
gBrowser.removeTab(tab);
|
|
|
|
},
|
|
|
|
|
2013-05-30 18:17:41 -07:00
|
|
|
// the following tests attempt to display modal dialogs. The test just
|
|
|
|
// relies on the fact that if the dialog was displayed the test will hang
|
|
|
|
// and timeout. IOW - the tests would pass if the dialogs appear and are
|
|
|
|
// manually closed by the user - so don't do that :) (obviously there is
|
|
|
|
// noone available to do that when run via tbpl etc, so this should be safe,
|
|
|
|
// and it's tricky to use the window-watcher to check a window *does not*
|
|
|
|
// appear - how long should the watcher be active before assuming it's not
|
|
|
|
// going to appear?)
|
|
|
|
function noAuthPrompt() {
|
2013-09-18 23:06:20 -07:00
|
|
|
let url = "http://mochi.test:8888/browser/browser/base/content/test/general/authenticate.sjs?user=anyone";
|
2013-05-30 18:17:41 -07:00
|
|
|
let file = fileForURL(url);
|
|
|
|
ok(!file.exists(), "Thumbnail file should not already exist.");
|
|
|
|
|
|
|
|
let capturedURL = yield capture(url);
|
|
|
|
is(capturedURL, url, "Captured URL should be URL passed to capture.");
|
|
|
|
ok(file.exists(),
|
|
|
|
"Thumbnail file should exist even though it requires auth.");
|
|
|
|
file.remove(false);
|
|
|
|
},
|
|
|
|
|
|
|
|
function noAlert() {
|
2013-09-18 17:21:03 -07:00
|
|
|
let url = "data:text/html,<script>try { alert('yo!'); } catch (e) {}</script>";
|
2013-05-30 18:17:41 -07:00
|
|
|
let file = fileForURL(url);
|
|
|
|
ok(!file.exists(), "Thumbnail file should not already exist.");
|
|
|
|
|
|
|
|
let capturedURL = yield capture(url);
|
|
|
|
is(capturedURL, url, "Captured URL should be URL passed to capture.");
|
|
|
|
ok(file.exists(),
|
|
|
|
"Thumbnail file should exist even though it alerted.");
|
|
|
|
file.remove(false);
|
|
|
|
},
|
2013-07-11 00:50:55 -07:00
|
|
|
|
|
|
|
function noDuplicates() {
|
|
|
|
let deferred = imports.Promise.defer();
|
|
|
|
let url = "http://example.com/1";
|
|
|
|
let file = fileForURL(url);
|
|
|
|
ok(!file.exists(), "Thumbnail file should not already exist.");
|
|
|
|
let numCallbacks = 0;
|
|
|
|
let doneCallback = function(doneUrl) {
|
|
|
|
is(doneUrl, url, "called back with correct url");
|
|
|
|
numCallbacks += 1;
|
|
|
|
// We will delete the file after the first callback, then check it
|
|
|
|
// still doesn't exist on the second callback, which should give us
|
|
|
|
// confidence that we didn't end up with 2 different captures happening
|
|
|
|
// for the same url...
|
|
|
|
if (numCallbacks == 1) {
|
|
|
|
ok(file.exists(), "Thumbnail file should now exist.");
|
|
|
|
file.remove(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (numCallbacks == 2) {
|
|
|
|
ok(!file.exists(), "Thumbnail file should still be deleted.");
|
|
|
|
// and that's all we expect, so we are done...
|
|
|
|
deferred.resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ok(false, "only expecting 2 callbacks");
|
|
|
|
}
|
|
|
|
imports.BackgroundPageThumbs.capture(url, {onDone: doneCallback});
|
|
|
|
imports.BackgroundPageThumbs.capture(url, {onDone: doneCallback});
|
|
|
|
yield deferred.promise;
|
2013-07-12 21:03:15 -07:00
|
|
|
},
|
2013-09-12 09:25:57 -07:00
|
|
|
|
|
|
|
function capIfMissing() {
|
|
|
|
let url = "http://example.com/";
|
|
|
|
let file = fileForURL(url);
|
|
|
|
ok(!file.exists(), "Thumbnail file should not already exist.");
|
|
|
|
|
|
|
|
let capturedURL = yield captureIfMissing(url);
|
|
|
|
is(capturedURL, url, "Captured URL should be URL passed to capture");
|
|
|
|
ok(file.exists(), "Thumbnail should be cached after capture: " + file.path);
|
|
|
|
|
|
|
|
let past = Date.now() - 1000000000;
|
|
|
|
let pastFudge = past + 30000;
|
|
|
|
file.lastModifiedTime = past;
|
|
|
|
ok(file.lastModifiedTime < pastFudge, "Last modified time should stick!");
|
|
|
|
capturedURL = yield captureIfMissing(url);
|
|
|
|
is(capturedURL, url, "Captured URL should be URL passed to second capture");
|
|
|
|
ok(file.exists(), "Thumbnail should remain cached after second capture: " +
|
|
|
|
file.path);
|
|
|
|
ok(file.lastModifiedTime < pastFudge,
|
|
|
|
"File should not have been overwritten");
|
|
|
|
|
|
|
|
file.remove(false);
|
|
|
|
},
|
2013-05-06 21:10:28 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
function capture(url, options) {
|
2013-09-12 09:25:57 -07:00
|
|
|
return captureWithMethod("capture", url, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
function captureIfMissing(url, options) {
|
|
|
|
return captureWithMethod("captureIfMissing", url, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
function captureWithMethod(methodName, url, options={}) {
|
2013-05-06 21:10:28 -07:00
|
|
|
let deferred = imports.Promise.defer();
|
|
|
|
options.onDone = function onDone(capturedURL) {
|
|
|
|
deferred.resolve(capturedURL);
|
|
|
|
};
|
2013-09-12 09:25:57 -07:00
|
|
|
imports.BackgroundPageThumbs[methodName](url, options);
|
2013-05-06 21:10:28 -07:00
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
function testPageURL(opts) {
|
|
|
|
return TEST_PAGE_URL + "?" + encodeURIComponent(JSON.stringify(opts || {}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function fileForURL(url) {
|
|
|
|
let path = imports.PageThumbsStorage.getFilePathForURL(url);
|
|
|
|
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
|
|
|
|
file.initWithPath(path);
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
function wait(ms) {
|
|
|
|
let deferred = imports.Promise.defer();
|
|
|
|
setTimeout(function onTimeout() {
|
|
|
|
deferred.resolve();
|
|
|
|
}, ms);
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
2013-05-23 20:13:23 -07:00
|
|
|
|
|
|
|
function openPrivateWindow() {
|
|
|
|
let deferred = imports.Promise.defer();
|
|
|
|
// from OpenBrowserWindow in browser.js
|
|
|
|
let win = window.openDialog("chrome://browser/content/", "_blank",
|
|
|
|
"chrome,all,dialog=no,private",
|
|
|
|
"about:privatebrowsing");
|
|
|
|
win.addEventListener("load", function load(event) {
|
|
|
|
if (event.target == win.document) {
|
|
|
|
win.removeEventListener("load", load);
|
|
|
|
deferred.resolve(win);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
2013-05-31 22:52:51 -07:00
|
|
|
|
|
|
|
function onPageLoad(browser) {
|
|
|
|
let deferred = imports.Promise.defer();
|
|
|
|
browser.addEventListener("load", function load(event) {
|
|
|
|
if (event.target == browser.contentWindow.document) {
|
|
|
|
browser.removeEventListener("load", load, true);
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
return deferred.promise;
|
|
|
|
}
|