toolkit/components/search/tests/xpcshell/test_serialize_file.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/ */
     4 /*
     5  * test_serialize_file: Add test engines
     6  *
     7  * Ensure that :
     8  * - File is created.
     9  * - File size is bigger than 0.
    10  * - lazySerializeTask updates the file.
    11  */
    13 const Ci = Components.interfaces;
    14 const Cu = Components.utils;
    16 Cu.import("resource://testing-common/httpd.js");
    17 Cu.import("resource://gre/modules/NetUtil.jsm");
    19 add_test(function test_batchTask() {
    20   let observer = function(aSubject, aTopic, aData) {
    21     if (aTopic == "browser-search-engine-modified" && aData == "engine-loaded") {
    22       let engine1 = Services.search.getEngineByName("Test search engine");
    23       let engine2 = Services.search.getEngineByName("Sherlock test search engine");
    24       if (engine1 && engine2) {
    25         Services.obs.removeObserver(observer, aTopic);
    26         // Test that files are written correctly.
    27         let engineFile1 = engine1.wrappedJSObject._file;
    28         let engineFile2 = engine2.wrappedJSObject._file;
    29         do_check_true(engineFile1.exists());
    30         do_check_true(engineFile2.exists());
    31         do_check_neq(engineFile1.fileSize, 0);
    32         do_check_neq(engineFile2.fileSize, 0);
    33         run_next_test();
    34       }
    35     }
    36   }
    38   Services.obs.addObserver(observer, "browser-search-engine-modified", false);
    39   Services.search.addEngine("http://localhost:4444/data/engine.xml",
    40                             Ci.nsISearchEngine.DATA_XML, null, false);
    41   Services.search.addEngine("http://localhost:4444/data/engine.src",
    42                             Ci.nsISearchEngine.DATA_TEXT,
    43                             "http://localhost:4444/data/ico-size-16x16-png.ico",
    44                             false);
    45 });
    47 add_test(function test_addParam() {
    48   let engine = Services.search.getEngineByName("Test search engine");
    49   engine.addParam("param-name", "param-value", null);
    51   function readAsyncFile(aFile, aCallback) {
    52     NetUtil.asyncFetch(aFile, function(inputStream, status) {
    53       do_check_true(Components.isSuccessCode(status));
    55       let data = NetUtil.readInputStreamToString(inputStream, inputStream.available());
    56       aCallback(data);
    57     });
    58   }
    60   let observer = function(aSubject, aTopic, aData) {
    61     // The sherlock engine file may still be updated because the icon takes
    62     // time be loaded, therefore, the engine name is checked here.
    63     aSubject.QueryInterface(Ci.nsIFile);
    64     if (aTopic == "browser-search-service" &&
    65         aData == "write-engine-to-disk-complete" &&
    66         aSubject.leafName == "test-search-engine.xml") {
    67       Services.obs.removeObserver(observer, aTopic);
    69       let engineFile = engine.wrappedJSObject._file;
    71       readAsyncFile(engineFile, function(engineData) {
    72         do_check_true(engineData.indexOf("param-name") > 0);
    73         run_next_test();
    74       });
    75     }
    76   }
    77   Services.obs.addObserver(observer, "browser-search-service", false);
    78 });
    80 function run_test() {
    81   updateAppInfo();
    83   let httpServer = new HttpServer();
    84   httpServer.start(4444);
    85   httpServer.registerDirectory("/", do_get_cwd());
    87   do_register_cleanup(function cleanup() {
    88     httpServer.stop(function() {});
    89   });
    91   run_next_test();
    92 }

mercurial