1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*globals ReaderMode */
var { utils: Cu } = Components;
Cu.import("resource://gre/modules/ReaderMode.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/Task.jsm");
var Reader = Services.wm.getMostRecentWindow("navigator:browser").Reader;
const URL_PREFIX = "http://mochi.test:8888/tests/robocop/reader_mode_pages/";
var TEST_PAGES = [
{
url: URL_PREFIX + "basic_article.html",
expected: {
title: "Article title",
byline: "by Jane Doe",
excerpt: "This is the article description.",
}
},
{
url: URL_PREFIX + "not_an_article.html",
expected: null
},
{
url: URL_PREFIX + "developer.mozilla.org/en/XULRunner/Build_Instructions.html",
expected: {
title: "Building XULRunner",
byline: null,
excerpt: "XULRunner is built using basically the same process as Firefox or other applications. Please read and follow the general Build Documentation for instructions on how to get sources and set up build prerequisites.",
}
},
];
add_task(function* test_article_not_found() {
let article = yield ReaderMode.getArticleFromCache(TEST_PAGES[0].url);
do_check_eq(article, null);
});
add_task(function* test_store_article() {
// Create an article object to store in the cache.
yield ReaderMode.storeArticleInCache({
url: TEST_PAGES[0].url,
content: "Lorem ipsum",
title: TEST_PAGES[0].expected.title,
byline: TEST_PAGES[0].expected.byline,
excerpt: TEST_PAGES[0].expected.excerpt,
});
let article = yield ReaderMode.getArticleFromCache(TEST_PAGES[0].url);
checkArticle(article, TEST_PAGES[0]);
});
add_task(function* test_remove_article() {
yield ReaderMode.removeArticleFromCache(TEST_PAGES[0].url);
let article = yield ReaderMode.getArticleFromCache(TEST_PAGES[0].url);
do_check_eq(article, null);
});
add_task(function* test_parse_articles() {
for (let testcase of TEST_PAGES) {
let article = yield ReaderMode.downloadAndParseDocument(testcase.url);
checkArticle(article, testcase);
}
});
add_task(function* test_migrate_cache() {
// Store an article in the old indexedDB reader mode cache.
let cacheDB = yield new Promise((resolve, reject) => {
let win = Services.wm.getMostRecentWindow("navigator:browser");
let request = win.indexedDB.open("about:reader", 1);
request.onerror = event => reject(request.error);
// This will always happen because there is no pre-existing data store.
request.onupgradeneeded = event => {
let cacheDB = event.target.result;
cacheDB.createObjectStore("articles", { keyPath: "url" });
};
request.onsuccess = event => resolve(event.target.result);
});
yield new Promise((resolve, reject) => {
let transaction = cacheDB.transaction(["articles"], "readwrite");
let store = transaction.objectStore("articles");
let request = store.add({
url: TEST_PAGES[0].url,
content: "Lorem ipsum",
title: TEST_PAGES[0].expected.title,
byline: TEST_PAGES[0].expected.byline,
excerpt: TEST_PAGES[0].expected.excerpt,
});
request.onerror = event => reject(request.error);
request.onsuccess = event => resolve();
});
// Migrate the cache.
yield Reader.migrateCache();
// Check to make sure the article made it into the new cache.
let article = yield ReaderMode.getArticleFromCache(TEST_PAGES[0].url);
checkArticle(article, TEST_PAGES[0]);
});
function checkArticle(article, testcase) {
if (testcase.expected == null) {
do_check_eq(article, null);
return;
}
do_check_neq(article, null);
do_check_eq(!!article.content, true); // A bit of a hack to avoid spamming the test log.
do_check_eq(article.url, testcase.url);
do_check_eq(article.title, testcase.expected.title);
do_check_eq(article.byline, testcase.expected.byline);
do_check_eq(article.excerpt, testcase.expected.excerpt);
}
run_next_test();
|