toolkit/modules/tests/xpcshell/test_NewTabUtils.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 // See also browser/base/content/test/newtab/.
michael@0 5
michael@0 6 const { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
michael@0 7 Cu.import("resource://gre/modules/NewTabUtils.jsm");
michael@0 8 Cu.import("resource://gre/modules/Promise.jsm");
michael@0 9
michael@0 10 function run_test() {
michael@0 11 run_next_test();
michael@0 12 }
michael@0 13
michael@0 14 add_test(function multipleProviders() {
michael@0 15 // Make each provider generate NewTabUtils.links.maxNumLinks links to check
michael@0 16 // that no more than maxNumLinks are actually returned in the merged list.
michael@0 17 let evenLinks = makeLinks(0, 2 * NewTabUtils.links.maxNumLinks, 2);
michael@0 18 let evenProvider = new TestProvider(done => done(evenLinks));
michael@0 19 let oddLinks = makeLinks(0, 2 * NewTabUtils.links.maxNumLinks - 1, 2);
michael@0 20 let oddProvider = new TestProvider(done => done(oddLinks));
michael@0 21
michael@0 22 NewTabUtils.initWithoutProviders();
michael@0 23 NewTabUtils.links.addProvider(evenProvider);
michael@0 24 NewTabUtils.links.addProvider(oddProvider);
michael@0 25
michael@0 26 // This is sync since the providers' getLinks are sync.
michael@0 27 NewTabUtils.links.populateCache(function () {}, false);
michael@0 28
michael@0 29 let links = NewTabUtils.links.getLinks();
michael@0 30 let expectedLinks = makeLinks(NewTabUtils.links.maxNumLinks,
michael@0 31 2 * NewTabUtils.links.maxNumLinks,
michael@0 32 1);
michael@0 33 do_check_eq(links.length, NewTabUtils.links.maxNumLinks);
michael@0 34 do_check_links(links, expectedLinks);
michael@0 35
michael@0 36 NewTabUtils.links.removeProvider(evenProvider);
michael@0 37 NewTabUtils.links.removeProvider(oddProvider);
michael@0 38 run_next_test();
michael@0 39 });
michael@0 40
michael@0 41 add_test(function changeLinks() {
michael@0 42 let expectedLinks = makeLinks(0, 20, 2);
michael@0 43 let provider = new TestProvider(done => done(expectedLinks));
michael@0 44
michael@0 45 NewTabUtils.initWithoutProviders();
michael@0 46 NewTabUtils.links.addProvider(provider);
michael@0 47
michael@0 48 // This is sync since the provider's getLinks is sync.
michael@0 49 NewTabUtils.links.populateCache(function () {}, false);
michael@0 50
michael@0 51 do_check_links(NewTabUtils.links.getLinks(), expectedLinks);
michael@0 52
michael@0 53 // Notify of a new link.
michael@0 54 let newLink = makeLink(19);
michael@0 55 expectedLinks.splice(1, 0, newLink);
michael@0 56 provider.notifyLinkChanged(newLink);
michael@0 57 do_check_links(NewTabUtils.links.getLinks(), expectedLinks);
michael@0 58
michael@0 59 // Notify of a link that's changed sort criteria.
michael@0 60 newLink.frecency = 17;
michael@0 61 expectedLinks.splice(1, 1);
michael@0 62 expectedLinks.splice(2, 0, newLink);
michael@0 63 provider.notifyLinkChanged({
michael@0 64 url: newLink.url,
michael@0 65 frecency: 17,
michael@0 66 });
michael@0 67 do_check_links(NewTabUtils.links.getLinks(), expectedLinks);
michael@0 68
michael@0 69 // Notify of a link that's changed title.
michael@0 70 newLink.title = "My frecency is now 17";
michael@0 71 provider.notifyLinkChanged({
michael@0 72 url: newLink.url,
michael@0 73 title: newLink.title,
michael@0 74 });
michael@0 75 do_check_links(NewTabUtils.links.getLinks(), expectedLinks);
michael@0 76
michael@0 77 // Notify of a new link again, but this time make it overflow maxNumLinks.
michael@0 78 provider.maxNumLinks = expectedLinks.length;
michael@0 79 newLink = makeLink(21);
michael@0 80 expectedLinks.unshift(newLink);
michael@0 81 expectedLinks.pop();
michael@0 82 do_check_eq(expectedLinks.length, provider.maxNumLinks); // Sanity check.
michael@0 83 provider.notifyLinkChanged(newLink);
michael@0 84 do_check_links(NewTabUtils.links.getLinks(), expectedLinks);
michael@0 85
michael@0 86 // Notify of many links changed.
michael@0 87 expectedLinks = makeLinks(0, 3, 1);
michael@0 88 provider.notifyManyLinksChanged();
michael@0 89 // NewTabUtils.links will now repopulate its cache, which is sync since
michael@0 90 // the provider's getLinks is sync.
michael@0 91 do_check_links(NewTabUtils.links.getLinks(), expectedLinks);
michael@0 92
michael@0 93 NewTabUtils.links.removeProvider(provider);
michael@0 94 run_next_test();
michael@0 95 });
michael@0 96
michael@0 97 add_task(function oneProviderAlreadyCached() {
michael@0 98 let links1 = makeLinks(0, 10, 1);
michael@0 99 let provider1 = new TestProvider(done => done(links1));
michael@0 100
michael@0 101 NewTabUtils.initWithoutProviders();
michael@0 102 NewTabUtils.links.addProvider(provider1);
michael@0 103
michael@0 104 // This is sync since the provider's getLinks is sync.
michael@0 105 NewTabUtils.links.populateCache(function () {}, false);
michael@0 106 do_check_links(NewTabUtils.links.getLinks(), links1);
michael@0 107
michael@0 108 let links2 = makeLinks(10, 20, 1);
michael@0 109 let provider2 = new TestProvider(done => done(links2));
michael@0 110 NewTabUtils.links.addProvider(provider2);
michael@0 111
michael@0 112 NewTabUtils.links.populateCache(function () {}, false);
michael@0 113 do_check_links(NewTabUtils.links.getLinks(), links2.concat(links1));
michael@0 114
michael@0 115 NewTabUtils.links.removeProvider(provider1);
michael@0 116 NewTabUtils.links.removeProvider(provider2);
michael@0 117 });
michael@0 118
michael@0 119 add_task(function newLowRankedLink() {
michael@0 120 // Init a provider with 10 links and make its maximum number also 10.
michael@0 121 let links = makeLinks(0, 10, 1);
michael@0 122 let provider = new TestProvider(done => done(links));
michael@0 123 provider.maxNumLinks = links.length;
michael@0 124
michael@0 125 NewTabUtils.initWithoutProviders();
michael@0 126 NewTabUtils.links.addProvider(provider);
michael@0 127
michael@0 128 // This is sync since the provider's getLinks is sync.
michael@0 129 NewTabUtils.links.populateCache(function () {}, false);
michael@0 130 do_check_links(NewTabUtils.links.getLinks(), links);
michael@0 131
michael@0 132 // Notify of a new link that's low-ranked enough not to make the list.
michael@0 133 let newLink = makeLink(0);
michael@0 134 provider.notifyLinkChanged(newLink);
michael@0 135 do_check_links(NewTabUtils.links.getLinks(), links);
michael@0 136
michael@0 137 // Notify about the new link's title change.
michael@0 138 provider.notifyLinkChanged({
michael@0 139 url: newLink.url,
michael@0 140 title: "a new title",
michael@0 141 });
michael@0 142 do_check_links(NewTabUtils.links.getLinks(), links);
michael@0 143
michael@0 144 NewTabUtils.links.removeProvider(provider);
michael@0 145 });
michael@0 146
michael@0 147 function TestProvider(getLinksFn) {
michael@0 148 this.getLinks = getLinksFn;
michael@0 149 this._observers = new Set();
michael@0 150 }
michael@0 151
michael@0 152 TestProvider.prototype = {
michael@0 153 addObserver: function (observer) {
michael@0 154 this._observers.add(observer);
michael@0 155 },
michael@0 156 notifyLinkChanged: function (link) {
michael@0 157 this._notifyObservers("onLinkChanged", link);
michael@0 158 },
michael@0 159 notifyManyLinksChanged: function () {
michael@0 160 this._notifyObservers("onManyLinksChanged");
michael@0 161 },
michael@0 162 _notifyObservers: function (observerMethodName, arg) {
michael@0 163 for (let obs of this._observers) {
michael@0 164 if (obs[observerMethodName])
michael@0 165 obs[observerMethodName](this, arg);
michael@0 166 }
michael@0 167 },
michael@0 168 };
michael@0 169
michael@0 170 function do_check_links(actualLinks, expectedLinks) {
michael@0 171 do_check_true(Array.isArray(actualLinks));
michael@0 172 do_check_eq(actualLinks.length, expectedLinks.length);
michael@0 173 for (let i = 0; i < expectedLinks.length; i++) {
michael@0 174 let expected = expectedLinks[i];
michael@0 175 let actual = actualLinks[i];
michael@0 176 do_check_eq(actual.url, expected.url);
michael@0 177 do_check_eq(actual.title, expected.title);
michael@0 178 do_check_eq(actual.frecency, expected.frecency);
michael@0 179 do_check_eq(actual.lastVisitDate, expected.lastVisitDate);
michael@0 180 }
michael@0 181 }
michael@0 182
michael@0 183 function makeLinks(frecRangeStart, frecRangeEnd, step) {
michael@0 184 let links = [];
michael@0 185 // Remember, links are ordered by frecency descending.
michael@0 186 for (let i = frecRangeEnd; i > frecRangeStart; i -= step) {
michael@0 187 links.push(makeLink(i));
michael@0 188 }
michael@0 189 return links;
michael@0 190 }
michael@0 191
michael@0 192 function makeLink(frecency) {
michael@0 193 return {
michael@0 194 url: "http://example.com/" + frecency,
michael@0 195 title: "My frecency is " + frecency,
michael@0 196 frecency: frecency,
michael@0 197 lastVisitDate: 0,
michael@0 198 };
michael@0 199 }

mercurial