1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/modules/tests/xpcshell/test_DirectoryLinksProvider.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,152 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ 1.6 + */ 1.7 +"use strict"; 1.8 + 1.9 +/** 1.10 + * This file tests the DirectoryLinksProvider singleton in the DirectoryLinksProvider.jsm module. 1.11 + */ 1.12 + 1.13 +const { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components; 1.14 +Cu.import("resource://gre/modules/Services.jsm"); 1.15 +Cu.import("resource://gre/modules/DirectoryLinksProvider.jsm"); 1.16 +Cu.import("resource://gre/modules/Promise.jsm"); 1.17 + 1.18 +const DIRECTORY_FRECENCY = 1000; 1.19 +const kTestSource = 'data:application/json,{"en-US": [{"url":"http://example.com","title":"TestSource"}]}'; 1.20 + 1.21 +function isIdentical(actual, expected) { 1.22 + if (expected == null) { 1.23 + do_check_eq(actual, expected); 1.24 + } 1.25 + else if (typeof expected == "object") { 1.26 + // Make sure all the keys match up 1.27 + do_check_eq(Object.keys(actual).sort() + "", Object.keys(expected).sort()); 1.28 + 1.29 + // Recursively check each value individually 1.30 + Object.keys(expected).forEach(key => { 1.31 + isIdentical(actual[key], expected[key]); 1.32 + }); 1.33 + } 1.34 + else { 1.35 + do_check_eq(actual, expected); 1.36 + } 1.37 +} 1.38 + 1.39 +function fetchData(provider) { 1.40 + let deferred = Promise.defer(); 1.41 + 1.42 + provider.getLinks(linkData => { 1.43 + deferred.resolve(linkData); 1.44 + }); 1.45 + return deferred.promise; 1.46 +} 1.47 + 1.48 +function run_test() { 1.49 + run_next_test(); 1.50 +} 1.51 + 1.52 +add_task(function test_DirectoryLinksProvider__linkObservers() { 1.53 + let deferred = Promise.defer(); 1.54 + let testObserver = { 1.55 + onManyLinksChanged: function() { 1.56 + deferred.resolve(); 1.57 + } 1.58 + } 1.59 + 1.60 + let provider = DirectoryLinksProvider; 1.61 + provider.init(); 1.62 + provider.addObserver(testObserver); 1.63 + do_check_eq(provider._observers.length, 1); 1.64 + Services.prefs.setCharPref(provider._prefs['linksURL'], kTestSource); 1.65 + 1.66 + yield deferred.promise; 1.67 + provider._removeObservers(); 1.68 + do_check_eq(provider._observers.length, 0); 1.69 + 1.70 + provider.reset(); 1.71 + Services.prefs.clearUserPref(provider._prefs['linksURL']); 1.72 +}); 1.73 + 1.74 +add_task(function test_DirectoryLinksProvider__linksURL_locale() { 1.75 + let data = { 1.76 + "en-US": [{url: "http://example.com", title: "US"}], 1.77 + "zh-CN": [ 1.78 + {url: "http://example.net", title: "CN"}, 1.79 + {url:"http://example.net/2", title: "CN2"} 1.80 + ], 1.81 + }; 1.82 + let dataURI = 'data:application/json,' + JSON.stringify(data); 1.83 + 1.84 + let provider = DirectoryLinksProvider; 1.85 + Services.prefs.setCharPref(provider._prefs['linksURL'], dataURI); 1.86 + Services.prefs.setCharPref('general.useragent.locale', 'en-US'); 1.87 + 1.88 + // set up the observer 1.89 + provider.init(); 1.90 + do_check_eq(provider._linksURL, dataURI); 1.91 + 1.92 + let links; 1.93 + let expected_data; 1.94 + 1.95 + links = yield fetchData(provider); 1.96 + do_check_eq(links.length, 1); 1.97 + expected_data = [{url: "http://example.com", title: "US", frecency: DIRECTORY_FRECENCY, lastVisitDate: 1}]; 1.98 + isIdentical(links, expected_data); 1.99 + 1.100 + Services.prefs.setCharPref('general.useragent.locale', 'zh-CN'); 1.101 + 1.102 + links = yield fetchData(provider); 1.103 + do_check_eq(links.length, 2) 1.104 + expected_data = [ 1.105 + {url: "http://example.net", title: "CN", frecency: DIRECTORY_FRECENCY, lastVisitDate: 2}, 1.106 + {url: "http://example.net/2", title: "CN2", frecency: DIRECTORY_FRECENCY, lastVisitDate: 1} 1.107 + ]; 1.108 + isIdentical(links, expected_data); 1.109 + 1.110 + provider.reset(); 1.111 + Services.prefs.clearUserPref('general.useragent.locale'); 1.112 + Services.prefs.clearUserPref(provider._prefs['linksURL']); 1.113 +}); 1.114 + 1.115 +add_task(function test_DirectoryLinksProvider__prefObserver_url() { 1.116 + let provider = DirectoryLinksProvider; 1.117 + Services.prefs.setCharPref('general.useragent.locale', 'en-US'); 1.118 + Services.prefs.setCharPref(provider._prefs['linksURL'], kTestSource); 1.119 + 1.120 + // set up the observer 1.121 + provider.init(); 1.122 + do_check_eq(provider._linksURL, kTestSource); 1.123 + 1.124 + let links = yield fetchData(provider); 1.125 + do_check_eq(links.length, 1); 1.126 + let expectedData = [{url: "http://example.com", title: "TestSource", frecency: DIRECTORY_FRECENCY, lastVisitDate: 1}]; 1.127 + isIdentical(links, expectedData); 1.128 + 1.129 + // tests these 2 things: 1.130 + // 1. observer trigger on pref change 1.131 + // 2. invalid source url 1.132 + let exampleUrl = 'http://nosuchhost.localhost/bad'; 1.133 + Services.prefs.setCharPref(provider._prefs['linksURL'], exampleUrl); 1.134 + 1.135 + do_check_eq(provider._linksURL, exampleUrl); 1.136 + 1.137 + let newLinks = yield fetchData(provider); 1.138 + isIdentical(newLinks, []); 1.139 + 1.140 + provider.reset(); 1.141 + Services.prefs.clearUserPref('general.useragent.locale') 1.142 + Services.prefs.clearUserPref(provider._prefs['linksURL']); 1.143 +}); 1.144 + 1.145 +add_task(function test_DirectoryLinksProvider_getLinks_noLocaleData() { 1.146 + let provider = DirectoryLinksProvider; 1.147 + Services.prefs.setCharPref('general.useragent.locale', 'zh-CN'); 1.148 + Services.prefs.setCharPref(provider._prefs['linksURL'], kTestSource); 1.149 + 1.150 + let links = yield fetchData(provider); 1.151 + do_check_eq(links.length, 0); 1.152 + provider.reset(); 1.153 + Services.prefs.clearUserPref('general.useragent.locale') 1.154 + Services.prefs.clearUserPref(provider._prefs['linksURL']); 1.155 +});