Wed, 31 Dec 2014 06:09:35 +0100
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 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | /** |
michael@0 | 7 | * This file tests the DirectoryLinksProvider singleton in the DirectoryLinksProvider.jsm module. |
michael@0 | 8 | */ |
michael@0 | 9 | |
michael@0 | 10 | const { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components; |
michael@0 | 11 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 12 | Cu.import("resource://gre/modules/DirectoryLinksProvider.jsm"); |
michael@0 | 13 | Cu.import("resource://gre/modules/Promise.jsm"); |
michael@0 | 14 | |
michael@0 | 15 | const DIRECTORY_FRECENCY = 1000; |
michael@0 | 16 | const kTestSource = 'data:application/json,{"en-US": [{"url":"http://example.com","title":"TestSource"}]}'; |
michael@0 | 17 | |
michael@0 | 18 | function isIdentical(actual, expected) { |
michael@0 | 19 | if (expected == null) { |
michael@0 | 20 | do_check_eq(actual, expected); |
michael@0 | 21 | } |
michael@0 | 22 | else if (typeof expected == "object") { |
michael@0 | 23 | // Make sure all the keys match up |
michael@0 | 24 | do_check_eq(Object.keys(actual).sort() + "", Object.keys(expected).sort()); |
michael@0 | 25 | |
michael@0 | 26 | // Recursively check each value individually |
michael@0 | 27 | Object.keys(expected).forEach(key => { |
michael@0 | 28 | isIdentical(actual[key], expected[key]); |
michael@0 | 29 | }); |
michael@0 | 30 | } |
michael@0 | 31 | else { |
michael@0 | 32 | do_check_eq(actual, expected); |
michael@0 | 33 | } |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | function fetchData(provider) { |
michael@0 | 37 | let deferred = Promise.defer(); |
michael@0 | 38 | |
michael@0 | 39 | provider.getLinks(linkData => { |
michael@0 | 40 | deferred.resolve(linkData); |
michael@0 | 41 | }); |
michael@0 | 42 | return deferred.promise; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | function run_test() { |
michael@0 | 46 | run_next_test(); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | add_task(function test_DirectoryLinksProvider__linkObservers() { |
michael@0 | 50 | let deferred = Promise.defer(); |
michael@0 | 51 | let testObserver = { |
michael@0 | 52 | onManyLinksChanged: function() { |
michael@0 | 53 | deferred.resolve(); |
michael@0 | 54 | } |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | let provider = DirectoryLinksProvider; |
michael@0 | 58 | provider.init(); |
michael@0 | 59 | provider.addObserver(testObserver); |
michael@0 | 60 | do_check_eq(provider._observers.length, 1); |
michael@0 | 61 | Services.prefs.setCharPref(provider._prefs['linksURL'], kTestSource); |
michael@0 | 62 | |
michael@0 | 63 | yield deferred.promise; |
michael@0 | 64 | provider._removeObservers(); |
michael@0 | 65 | do_check_eq(provider._observers.length, 0); |
michael@0 | 66 | |
michael@0 | 67 | provider.reset(); |
michael@0 | 68 | Services.prefs.clearUserPref(provider._prefs['linksURL']); |
michael@0 | 69 | }); |
michael@0 | 70 | |
michael@0 | 71 | add_task(function test_DirectoryLinksProvider__linksURL_locale() { |
michael@0 | 72 | let data = { |
michael@0 | 73 | "en-US": [{url: "http://example.com", title: "US"}], |
michael@0 | 74 | "zh-CN": [ |
michael@0 | 75 | {url: "http://example.net", title: "CN"}, |
michael@0 | 76 | {url:"http://example.net/2", title: "CN2"} |
michael@0 | 77 | ], |
michael@0 | 78 | }; |
michael@0 | 79 | let dataURI = 'data:application/json,' + JSON.stringify(data); |
michael@0 | 80 | |
michael@0 | 81 | let provider = DirectoryLinksProvider; |
michael@0 | 82 | Services.prefs.setCharPref(provider._prefs['linksURL'], dataURI); |
michael@0 | 83 | Services.prefs.setCharPref('general.useragent.locale', 'en-US'); |
michael@0 | 84 | |
michael@0 | 85 | // set up the observer |
michael@0 | 86 | provider.init(); |
michael@0 | 87 | do_check_eq(provider._linksURL, dataURI); |
michael@0 | 88 | |
michael@0 | 89 | let links; |
michael@0 | 90 | let expected_data; |
michael@0 | 91 | |
michael@0 | 92 | links = yield fetchData(provider); |
michael@0 | 93 | do_check_eq(links.length, 1); |
michael@0 | 94 | expected_data = [{url: "http://example.com", title: "US", frecency: DIRECTORY_FRECENCY, lastVisitDate: 1}]; |
michael@0 | 95 | isIdentical(links, expected_data); |
michael@0 | 96 | |
michael@0 | 97 | Services.prefs.setCharPref('general.useragent.locale', 'zh-CN'); |
michael@0 | 98 | |
michael@0 | 99 | links = yield fetchData(provider); |
michael@0 | 100 | do_check_eq(links.length, 2) |
michael@0 | 101 | expected_data = [ |
michael@0 | 102 | {url: "http://example.net", title: "CN", frecency: DIRECTORY_FRECENCY, lastVisitDate: 2}, |
michael@0 | 103 | {url: "http://example.net/2", title: "CN2", frecency: DIRECTORY_FRECENCY, lastVisitDate: 1} |
michael@0 | 104 | ]; |
michael@0 | 105 | isIdentical(links, expected_data); |
michael@0 | 106 | |
michael@0 | 107 | provider.reset(); |
michael@0 | 108 | Services.prefs.clearUserPref('general.useragent.locale'); |
michael@0 | 109 | Services.prefs.clearUserPref(provider._prefs['linksURL']); |
michael@0 | 110 | }); |
michael@0 | 111 | |
michael@0 | 112 | add_task(function test_DirectoryLinksProvider__prefObserver_url() { |
michael@0 | 113 | let provider = DirectoryLinksProvider; |
michael@0 | 114 | Services.prefs.setCharPref('general.useragent.locale', 'en-US'); |
michael@0 | 115 | Services.prefs.setCharPref(provider._prefs['linksURL'], kTestSource); |
michael@0 | 116 | |
michael@0 | 117 | // set up the observer |
michael@0 | 118 | provider.init(); |
michael@0 | 119 | do_check_eq(provider._linksURL, kTestSource); |
michael@0 | 120 | |
michael@0 | 121 | let links = yield fetchData(provider); |
michael@0 | 122 | do_check_eq(links.length, 1); |
michael@0 | 123 | let expectedData = [{url: "http://example.com", title: "TestSource", frecency: DIRECTORY_FRECENCY, lastVisitDate: 1}]; |
michael@0 | 124 | isIdentical(links, expectedData); |
michael@0 | 125 | |
michael@0 | 126 | // tests these 2 things: |
michael@0 | 127 | // 1. observer trigger on pref change |
michael@0 | 128 | // 2. invalid source url |
michael@0 | 129 | let exampleUrl = 'http://nosuchhost.localhost/bad'; |
michael@0 | 130 | Services.prefs.setCharPref(provider._prefs['linksURL'], exampleUrl); |
michael@0 | 131 | |
michael@0 | 132 | do_check_eq(provider._linksURL, exampleUrl); |
michael@0 | 133 | |
michael@0 | 134 | let newLinks = yield fetchData(provider); |
michael@0 | 135 | isIdentical(newLinks, []); |
michael@0 | 136 | |
michael@0 | 137 | provider.reset(); |
michael@0 | 138 | Services.prefs.clearUserPref('general.useragent.locale') |
michael@0 | 139 | Services.prefs.clearUserPref(provider._prefs['linksURL']); |
michael@0 | 140 | }); |
michael@0 | 141 | |
michael@0 | 142 | add_task(function test_DirectoryLinksProvider_getLinks_noLocaleData() { |
michael@0 | 143 | let provider = DirectoryLinksProvider; |
michael@0 | 144 | Services.prefs.setCharPref('general.useragent.locale', 'zh-CN'); |
michael@0 | 145 | Services.prefs.setCharPref(provider._prefs['linksURL'], kTestSource); |
michael@0 | 146 | |
michael@0 | 147 | let links = yield fetchData(provider); |
michael@0 | 148 | do_check_eq(links.length, 0); |
michael@0 | 149 | provider.reset(); |
michael@0 | 150 | Services.prefs.clearUserPref('general.useragent.locale') |
michael@0 | 151 | Services.prefs.clearUserPref(provider._prefs['linksURL']); |
michael@0 | 152 | }); |