toolkit/modules/tests/xpcshell/test_DirectoryLinksProvider.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial