toolkit/components/search/tests/xpcshell/test_json_cache.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/search/tests/xpcshell/test_json_cache.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,295 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +/*
     1.8 + * Test initializing from the search cache.
     1.9 + */
    1.10 +
    1.11 +"use strict";
    1.12 +
    1.13 +const Cc = Components.classes;
    1.14 +const Ci = Components.interfaces;
    1.15 +
    1.16 +// Metadata to write to search-metadata.json for the test.
    1.17 +let gMetadata = {"[profile]/test-search-engine.xml":{"used":true}};
    1.18 +
    1.19 +/**
    1.20 + * Gets a directory from the directory service.
    1.21 + * @param aKey
    1.22 + *        The directory service key indicating the directory to get.
    1.23 + */
    1.24 +let _dirSvc = null;
    1.25 +function getDir(aKey, aIFace) {
    1.26 +  if (!aKey) {
    1.27 +    FAIL("getDir requires a directory key!");
    1.28 +  }
    1.29 +
    1.30 +  if (!_dirSvc) {
    1.31 +    _dirSvc = Cc["@mozilla.org/file/directory_service;1"].
    1.32 +               getService(Ci.nsIProperties);
    1.33 +  }
    1.34 +  return _dirSvc.get(aKey, aIFace || Ci.nsIFile);
    1.35 +}
    1.36 +
    1.37 +let cacheTemplate, appPluginsPath, profPlugins;
    1.38 +
    1.39 +/**
    1.40 + * Test reading from search.json
    1.41 + */
    1.42 +function run_test() {
    1.43 +  removeMetadata();
    1.44 +  removeCacheFile();
    1.45 +
    1.46 +  updateAppInfo();
    1.47 +
    1.48 +  let cacheTemplateFile = do_get_file("data/search.json");
    1.49 +  cacheTemplate = readJSONFile(cacheTemplateFile);
    1.50 +
    1.51 +  let engineFile = gProfD.clone();
    1.52 +  engineFile.append("searchplugins");
    1.53 +  engineFile.append("test-search-engine.xml");
    1.54 +  engineFile.parent.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
    1.55 +
    1.56 +  // Copy the test engine to the test profile.
    1.57 +  let engineTemplateFile = do_get_file("data/engine.xml");
    1.58 +  engineTemplateFile.copyTo(engineFile.parent, "test-search-engine.xml");
    1.59 +
    1.60 +  // Add the app's searchplugins directory to the cache so it won't be ignored.
    1.61 +  let appSearchPlugins = getDir(NS_APP_SEARCH_DIR);
    1.62 +  appPluginsPath = appSearchPlugins.path;
    1.63 +  cacheTemplate.directories[appPluginsPath] = {};
    1.64 +  cacheTemplate.directories[appPluginsPath].lastModifiedTime = appSearchPlugins.lastModifiedTime;
    1.65 +  cacheTemplate.directories[appPluginsPath].engines = [];
    1.66 +
    1.67 +  // Replace the profile placeholder with the correct path.
    1.68 +  profPlugins = engineFile.parent.path;
    1.69 +  cacheTemplate.directories[profPlugins] = cacheTemplate.directories["[profile]/searchplugins"];
    1.70 +  delete cacheTemplate.directories["[profile]/searchplugins"];
    1.71 +  cacheTemplate.directories[profPlugins].engines[0].filePath = engineFile.path;
    1.72 +  cacheTemplate.directories[profPlugins].lastModifiedTime = engineFile.parent.lastModifiedTime;
    1.73 +
    1.74 +  run_next_test();
    1.75 +}
    1.76 +
    1.77 +add_test(function prepare_test_data() {
    1.78 +
    1.79 +  let ostream = Cc["@mozilla.org/network/file-output-stream;1"].
    1.80 +                createInstance(Ci.nsIFileOutputStream);
    1.81 +  let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
    1.82 +                  createInstance(Ci.nsIScriptableUnicodeConverter);
    1.83 +
    1.84 +  // Write the modified cache template to the profile directory.
    1.85 +  let cacheFile = gProfD.clone();
    1.86 +  cacheFile.append("search.json");
    1.87 +  ostream.init(cacheFile, (MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE), FileUtils.PERMS_FILE,
    1.88 +               ostream.DEFER_OPEN);
    1.89 +  converter.charset = "UTF-8";
    1.90 +  let data = converter.convertToInputStream(JSON.stringify(cacheTemplate));
    1.91 +
    1.92 +  // Write to the cache and metadata files asynchronously before starting the search service.
    1.93 +  NetUtil.asyncCopy(data, ostream, function afterMetadataCopy(aResult) {
    1.94 +    do_check_true(Components.isSuccessCode(aResult));
    1.95 +    let metadataFile = gProfD.clone();
    1.96 +    metadataFile.append("search-metadata.json");
    1.97 +
    1.98 +    let ostream = Cc["@mozilla.org/network/file-output-stream;1"].
    1.99 +                  createInstance(Ci.nsIFileOutputStream);
   1.100 +    let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
   1.101 +                    createInstance(Ci.nsIScriptableUnicodeConverter);
   1.102 +
   1.103 +    ostream.init(metadataFile, (MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE), FileUtils.PERMS_FILE,
   1.104 +                 ostream.DEFER_OPEN);
   1.105 +    converter.charset = "UTF-8";
   1.106 +    let data = converter.convertToInputStream(JSON.stringify(gMetadata));
   1.107 +
   1.108 +    NetUtil.asyncCopy(data, ostream, function afterCacheCopy(aResult) {
   1.109 +      do_check_true(Components.isSuccessCode(aResult));
   1.110 +      run_next_test();
   1.111 +    });
   1.112 +  });
   1.113 +});
   1.114 +
   1.115 +/**
   1.116 + * Start the search service and confirm the engine properties match the expected values.
   1.117 + */
   1.118 +add_test(function test_cached_engine_properties() {
   1.119 +  do_print("init search service");
   1.120 +
   1.121 +  let search = Services.search.init(function initComplete(aResult) {
   1.122 +    do_print("init'd search service");
   1.123 +    do_check_true(Components.isSuccessCode(aResult));
   1.124 +
   1.125 +    let engines = Services.search.getEngines({});
   1.126 +    let engine = engines[0];
   1.127 +
   1.128 +    do_check_true(engine instanceof Ci.nsISearchEngine);
   1.129 +    isSubObjectOf(EXPECTED_ENGINE.engine, engine);
   1.130 +
   1.131 +    let engineFromSS = Services.search.getEngineByName(EXPECTED_ENGINE.engine.name);
   1.132 +    do_check_true(!!engineFromSS);
   1.133 +    isSubObjectOf(EXPECTED_ENGINE.engine, engineFromSS);
   1.134 +
   1.135 +    removeMetadata();
   1.136 +    removeCacheFile();
   1.137 +    run_next_test();
   1.138 +  });
   1.139 +});
   1.140 +
   1.141 +/**
   1.142 + * Test that the JSON cache written in the profile is correct.
   1.143 + */
   1.144 +add_test(function test_cache_write() {
   1.145 +  do_print("test cache writing");
   1.146 +
   1.147 +  let cache = gProfD.clone();
   1.148 +  cache.append("search.json");
   1.149 +  do_check_false(cache.exists());
   1.150 +
   1.151 +  do_print("Next step is forcing flush");
   1.152 +  do_timeout(0, function forceFlush() {
   1.153 +    do_print("Forcing flush");
   1.154 +    // Force flush
   1.155 +    // Note: the timeout is needed, to avoid some reentrency
   1.156 +    // issues in nsSearchService.
   1.157 +
   1.158 +    let cacheWriteObserver = {
   1.159 +      observe: function cacheWriteObserver_observe(aEngine, aTopic, aVerb) {
   1.160 +        if (aTopic != "browser-search-service" || aVerb != "write-cache-to-disk-complete") {
   1.161 +          return;
   1.162 +        }
   1.163 +        Services.obs.removeObserver(cacheWriteObserver, "browser-search-service");
   1.164 +        do_print("Cache write complete");
   1.165 +        do_check_true(cache.exists());
   1.166 +        // Check that the search.json cache matches the template
   1.167 +
   1.168 +        let cacheWritten = readJSONFile(cache);
   1.169 +        // Delete the app search plugins directory from the template since it's not currently written out.
   1.170 +        delete cacheTemplate.directories[appPluginsPath];
   1.171 +
   1.172 +        do_print("Check search.json");
   1.173 +        isSubObjectOf(cacheTemplate, cacheWritten);
   1.174 +
   1.175 +        run_next_test();
   1.176 +      }
   1.177 +    };
   1.178 +    Services.obs.addObserver(cacheWriteObserver, "browser-search-service", false);
   1.179 +
   1.180 +    Services.search.QueryInterface(Ci.nsIObserver).observe(null, "browser-search-engine-modified" , "engine-removed");
   1.181 +  });
   1.182 +});
   1.183 +
   1.184 +let EXPECTED_ENGINE = {
   1.185 +  engine: {
   1.186 +    name: "Test search engine",
   1.187 +    alias: null,
   1.188 +    description: "A test search engine (based on Google search)",
   1.189 +    searchForm: "http://www.google.com/",
   1.190 +    type: Ci.nsISearchEngine.TYPE_MOZSEARCH,
   1.191 +    wrappedJSObject: {
   1.192 +      "_i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Ff7%2F%2FP%2B%2BfDvGXQLIZgLEWgLOjlf7%2F%2F%2F%2F%2F%2F9QU90EAPQAAf8DAP0AAfMAAOUDAtr%2F%2F%2F%2F7%2B%2Fu2bCTIYwDPZgDBWQDSr4P%2F%2Fv%2F%2F%2FP5GRuABAPkAA%2FwBAfkDAPAAAesAAN%2F%2F%2B%2Fz%2F%2F%2F64g1C5VwDMYwK8Yg7y5tz8%2Fv%2FV1PYKDOcAAP0DAf4AAf0AAfYEAOwAAuAAAAD%2F%2FPvi28ymXyChTATRrIb8%2F%2F3v8fk6P8MAAdUCAvoAAP0CAP0AAfYAAO4AAACAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAQAA",
   1.193 +      _urls : [
   1.194 +        {
   1.195 +          type: "application/x-suggestions+json",
   1.196 +          method: "GET",
   1.197 +          template: "http://suggestqueries.google.com/complete/search?output=firefox&client=firefox" +
   1.198 +                      "&hl={moz:locale}&q={searchTerms}",
   1.199 +          params: "",
   1.200 +        },
   1.201 +        {
   1.202 +          type: "text/html",
   1.203 +          method: "GET",
   1.204 +          template: "http://www.google.com/search",
   1.205 +          resultDomain: "google.com",
   1.206 +          params: [
   1.207 +            {
   1.208 +              "name": "q",
   1.209 +              "value": "{searchTerms}",
   1.210 +              "purpose": undefined,
   1.211 +            },
   1.212 +            {
   1.213 +              "name": "ie",
   1.214 +              "value": "utf-8",
   1.215 +              "purpose": undefined,
   1.216 +            },
   1.217 +            {
   1.218 +              "name": "oe",
   1.219 +              "value": "utf-8",
   1.220 +              "purpose": undefined,
   1.221 +            },
   1.222 +            {
   1.223 +              "name": "aq",
   1.224 +              "value": "t",
   1.225 +              "purpose": undefined,
   1.226 +            },
   1.227 +            {
   1.228 +              "name": "client",
   1.229 +              "value": "firefox",
   1.230 +              "purpose": undefined,
   1.231 +            },
   1.232 +            {
   1.233 +              "name": "channel",
   1.234 +              "value": "fflb",
   1.235 +              "purpose": "keyword",
   1.236 +            },
   1.237 +            {
   1.238 +              "name": "channel",
   1.239 +              "value": "rcs",
   1.240 +              "purpose": "contextmenu",
   1.241 +            },
   1.242 +          ],
   1.243 +          mozparams: {
   1.244 +            "client": {
   1.245 +              "name": "client",
   1.246 +              "falseValue": "firefox",
   1.247 +              "trueValue": "firefox-a",
   1.248 +              "condition": "defaultEngine",
   1.249 +              "mozparam": true,
   1.250 +            },
   1.251 +          },
   1.252 +        },
   1.253 +        {
   1.254 +          type: "application/x-moz-default-purpose",
   1.255 +          method: "GET",
   1.256 +          template: "http://www.google.com/search",
   1.257 +          resultDomain: "purpose.google.com",
   1.258 +          params: [
   1.259 +            {
   1.260 +              "name": "q",
   1.261 +              "value": "{searchTerms}",
   1.262 +              "purpose": undefined,
   1.263 +            },
   1.264 +            {
   1.265 +              "name": "client",
   1.266 +              "value": "firefox",
   1.267 +              "purpose": undefined,
   1.268 +            },
   1.269 +            {
   1.270 +              "name": "channel",
   1.271 +              "value": "none",
   1.272 +              "purpose": "",
   1.273 +            },
   1.274 +            {
   1.275 +              "name": "channel",
   1.276 +              "value": "fflb",
   1.277 +              "purpose": "keyword",
   1.278 +            },
   1.279 +            {
   1.280 +              "name": "channel",
   1.281 +              "value": "rcs",
   1.282 +              "purpose": "contextmenu",
   1.283 +            },
   1.284 +          ],
   1.285 +          mozparams: {
   1.286 +            "client": {
   1.287 +              "name": "client",
   1.288 +              "falseValue": "firefox",
   1.289 +              "trueValue": "firefox-a",
   1.290 +              "condition": "defaultEngine",
   1.291 +              "mozparam": true,
   1.292 +            },
   1.293 +          },
   1.294 +        },
   1.295 +      ],
   1.296 +    },
   1.297 +  },
   1.298 +};

mercurial