browser/components/search/test/browser_addEngine.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/search/test/browser_addEngine.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,136 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +var gSS = Services.search;
     1.8 +
     1.9 +function observer(aSubject, aTopic, aData) {
    1.10 +  if (!gCurrentTest) {
    1.11 +    info("Observer called with no test active");
    1.12 +    return;
    1.13 +  }
    1.14 +
    1.15 +  let engine = aSubject.QueryInterface(Ci.nsISearchEngine);
    1.16 +  info("Observer: " + aData + " for " + engine.name);
    1.17 +  let method;
    1.18 +  switch (aData) {
    1.19 +    case "engine-added":
    1.20 +      if (gCurrentTest.added)
    1.21 +        method = "added"
    1.22 +      break;
    1.23 +    case "engine-current":
    1.24 +      if (gCurrentTest.current)
    1.25 +        method = "current";
    1.26 +      break;
    1.27 +    case "engine-removed":
    1.28 +      if (gCurrentTest.removed)
    1.29 +        method = "removed";
    1.30 +      break;
    1.31 +  }
    1.32 +
    1.33 +  if (method)
    1.34 +    gCurrentTest[method](engine);
    1.35 +}
    1.36 +
    1.37 +function checkEngine(checkObj, engineObj) {
    1.38 +  info("Checking engine");
    1.39 +  for (var prop in checkObj)
    1.40 +    is(checkObj[prop], engineObj[prop], prop + " is correct");
    1.41 +}
    1.42 +
    1.43 +var gTests = [
    1.44 +  {
    1.45 +    name: "opensearch install",
    1.46 +    engine: {
    1.47 +      name: "Foo",
    1.48 +      alias: null,
    1.49 +      description: "Foo Search",
    1.50 +      searchForm: "http://mochi.test:8888/browser/browser/components/search/test/",
    1.51 +      type: Ci.nsISearchEngine.TYPE_OPENSEARCH
    1.52 +    },
    1.53 +    run: function () {
    1.54 +      gSS.addEngine("http://mochi.test:8888/browser/browser/components/search/test/testEngine.xml",
    1.55 +                    Ci.nsISearchEngine.DATA_XML, "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGklEQVQoz2NgGB6AnZ1dUlJSXl4eSDIyMhLW4Ovr%2B%2Fr168uXL69Zs4YoG%2BLi4i5dusTExMTGxsbNzd3f37937976%2BnpmZmagbHR09J49e5YvX66kpATVEBYW9ubNm2nTphkbG7e2tp44cQLIuHfvXm5urpaWFlDKysqqu7v73LlzECMYIiIiHj58mJCQoKKicvXq1bS0NKBgW1vbjh074uPjgeqAXE1NzSdPnvDz84M0AEUvXLgAsW379u1z5swBen3jxo2zZ892cHB4%2BvQp0KlAfwI1cHJyghQFBwfv2rULokFXV%2FfixYu7d%2B8GGqGgoMDKyrpu3br9%2B%2FcDuXl5eVA%2FAEWBfoWHAdAYoNuAYQ0XAeoUERFhGDYAAPoUaT2dfWJuAAAAAElFTkSuQmCC",
    1.56 +                    false);
    1.57 +    },
    1.58 +    added: function (engine) {
    1.59 +      ok(engine, "engine was added.");
    1.60 +
    1.61 +      checkEngine(this.engine, engine);
    1.62 +
    1.63 +      let engineFromSS = gSS.getEngineByName(this.engine.name);
    1.64 +      is(engine, engineFromSS, "engine is obtainable via getEngineByName");
    1.65 +
    1.66 +      let aEngine = gSS.getEngineByAlias("fooalias");
    1.67 +      ok(!aEngine, "Alias was not parsed from engine description");
    1.68 +
    1.69 +      gSS.currentEngine = engine;
    1.70 +    },
    1.71 +    current: function (engine) {
    1.72 +      let currentEngine = gSS.currentEngine;
    1.73 +      is(engine, currentEngine, "engine is current");
    1.74 +      is(engine.name, this.engine.name, "current engine was changed successfully");
    1.75 +
    1.76 +      gSS.removeEngine(engine);
    1.77 +    },
    1.78 +    removed: function (engine) {
    1.79 +      let currentEngine = gSS.currentEngine;
    1.80 +      ok(currentEngine, "An engine is present.");
    1.81 +      isnot(currentEngine.name, this.engine.name, "Current engine reset after removal");
    1.82 +
    1.83 +      nextTest();
    1.84 +    }
    1.85 +  },
    1.86 +  {
    1.87 +    name: "sherlock install",
    1.88 +    engine: {
    1.89 +      name: "Test Sherlock",
    1.90 +      alias: null,
    1.91 +      description: "Test Description",
    1.92 +      searchForm: "http://example.com/searchform",
    1.93 +      type: Ci.nsISearchEngine.TYPE_SHERLOCK
    1.94 +    },
    1.95 +    run: function () {
    1.96 +      gSS.addEngine("http://mochi.test:8888/browser/browser/components/search/test/testEngine.src",
    1.97 +                    Ci.nsISearchEngine.DATA_TEXT, "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGklEQVQoz2NgGB6AnZ1dUlJSXl4eSDIyMhLW4Ovr%2B%2Fr168uXL69Zs4YoG%2BLi4i5dusTExMTGxsbNzd3f37937976%2BnpmZmagbHR09J49e5YvX66kpATVEBYW9ubNm2nTphkbG7e2tp44cQLIuHfvXm5urpaWFlDKysqqu7v73LlzECMYIiIiHj58mJCQoKKicvXq1bS0NKBgW1vbjh074uPjgeqAXE1NzSdPnvDz84M0AEUvXLgAsW379u1z5swBen3jxo2zZ892cHB4%2BvQp0KlAfwI1cHJyghQFBwfv2rULokFXV%2FfixYu7d%2B8GGqGgoMDKyrpu3br9%2B%2FcDuXl5eVA%2FAEWBfoWHAdAYoNuAYQ0XAeoUERFhGDYAAPoUaT2dfWJuAAAAAElFTkSuQmCC",
    1.98 +                    false);
    1.99 +    },
   1.100 +    added: function (engine) {
   1.101 +      ok(engine, "engine was added.");
   1.102 +      checkEngine(this.engine, engine);
   1.103 +
   1.104 +      let engineFromSS = gSS.getEngineByName(this.engine.name);
   1.105 +      is(engineFromSS, engine, "engine is obtainable via getEngineByName");
   1.106 +
   1.107 +      gSS.removeEngine(engine);
   1.108 +    },
   1.109 +    removed: function (engine) {
   1.110 +      let currentEngine = gSS.currentEngine;
   1.111 +      ok(currentEngine, "An engine is present.");
   1.112 +      isnot(currentEngine.name, this.engine.name, "Current engine reset after removal");
   1.113 +
   1.114 +      nextTest();
   1.115 +    }
   1.116 +  }
   1.117 +];
   1.118 +
   1.119 +var gCurrentTest = null;
   1.120 +function nextTest() {
   1.121 +  if (gTests.length) {
   1.122 +    gCurrentTest = gTests.shift();
   1.123 +    info("Running " + gCurrentTest.name);
   1.124 +    gCurrentTest.run();
   1.125 +  } else
   1.126 +    executeSoon(finish);
   1.127 +}
   1.128 +
   1.129 +function test() {
   1.130 +  waitForExplicitFinish();
   1.131 +  Services.obs.addObserver(observer, "browser-search-engine-modified", false);
   1.132 +  registerCleanupFunction(cleanup);
   1.133 +
   1.134 +  nextTest();
   1.135 +}
   1.136 +
   1.137 +function cleanup() {
   1.138 +  Services.obs.removeObserver(observer, "browser-search-engine-modified");
   1.139 +}

mercurial