toolkit/components/search/tests/xpcshell/test_nocache.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_nocache.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,101 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +let Cu = Components.utils;
     1.8 +let Ci = Components.interfaces;
     1.9 +
    1.10 +Cu.import("resource://gre/modules/osfile.jsm");
    1.11 +Cu.import("resource://gre/modules/Task.jsm");
    1.12 +Cu.import("resource://testing-common/httpd.js");
    1.13 +
    1.14 +/*
    1.15 + * test_nocache: Start search engine
    1.16 + * - without search.json
    1.17 + *
    1.18 + * Ensure that :
    1.19 + * - nothing explodes;
    1.20 + * - search.json is created.
    1.21 + */
    1.22 +
    1.23 +
    1.24 +function run_test()
    1.25 +{
    1.26 +  removeCache();
    1.27 +  updateAppInfo();
    1.28 +  do_load_manifest("data/chrome.manifest");
    1.29 +
    1.30 +  let httpServer = new HttpServer();
    1.31 +  httpServer.start(-1);
    1.32 +  httpServer.registerDirectory("/", do_get_cwd());
    1.33 +
    1.34 +  let search = Services.search;
    1.35 +
    1.36 +  do_test_pending();
    1.37 +
    1.38 +  // Check that cache is created at startup
    1.39 +  afterCache(function cacheCreated() {
    1.40 +    // Check that search.json has been created.
    1.41 +    let cache = gProfD.clone();
    1.42 +    cache.append("search.json");
    1.43 +    do_check_true(cache.exists());
    1.44 +  });
    1.45 +
    1.46 +  // Perform initialization
    1.47 +  search.init(function ss_initialized(rv) {
    1.48 +    do_check_true(Components.isSuccessCode(rv));
    1.49 +
    1.50 +    do_print("Setting up observer");
    1.51 +    function observer(aSubject, aTopic, aData) {
    1.52 +      do_print("Observing topic " + aTopic);
    1.53 +      if ("engine-added" == aData) {
    1.54 +        let engine = search.getEngineByName("Test search engine");
    1.55 +        if (!engine) {
    1.56 +          return;
    1.57 +        }
    1.58 +        Services.obs.removeObserver(observer, "browser-search-engine-modified");
    1.59 +        do_print("Engine has been added, let's wait for the cache to be built");
    1.60 +        afterCache(function() {
    1.61 +          do_print("Success");
    1.62 +
    1.63 +          Task.spawn(function task() {
    1.64 +            do_print("Searching test engine in cache");
    1.65 +            try {
    1.66 +              let path = OS.Path.join(OS.Constants.Path.profileDir, "search.json");
    1.67 +              let data = yield OS.File.read(path);
    1.68 +              let text = new TextDecoder().decode(data);
    1.69 +              let cache = JSON.parse(text);
    1.70 +              let found = false;
    1.71 +              for (let dirName in cache.directories) {
    1.72 +                for (let engine of cache.directories[dirName].engines) {
    1.73 +                  if (engine._id == "[app]/test-search-engine.xml") {
    1.74 +                    found = true;
    1.75 +                    break;
    1.76 +                  }
    1.77 +                }
    1.78 +                if (found) {
    1.79 +                  break;
    1.80 +                }
    1.81 +              }
    1.82 +              do_check_true(found);
    1.83 +            } catch (ex) {
    1.84 +              do_throw(ex);
    1.85 +            } finally {
    1.86 +              removeCache();
    1.87 +              httpServer.stop(function() {
    1.88 +                // httpServer doesn't seem to stop cleanly if there is no callback
    1.89 +              });
    1.90 +              do_test_finished();
    1.91 +            }
    1.92 +          });
    1.93 +        });
    1.94 +      }
    1.95 +    };
    1.96 +    Services.obs.addObserver(observer, "browser-search-engine-modified", false);
    1.97 +
    1.98 +    // Add an engine, check if it appears in the cache
    1.99 +    search.addEngine("http://localhost:" + httpServer.identity.primaryPort +
   1.100 +                     "/data/engine.xml",
   1.101 +                     Ci.nsISearchEngine.DATA_XML,
   1.102 +                     null, false);
   1.103 +  });
   1.104 +}

mercurial