michael@0: // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: var Cc = Components.classes; michael@0: var Ci = Components.interfaces; michael@0: michael@0: function search_observer(aSubject, aTopic, aData) { michael@0: let engine = aSubject.QueryInterface(Ci.nsISearchEngine); michael@0: do_print("Observer: " + aData + " for " + engine.name); michael@0: michael@0: if (aData != "engine-added") michael@0: return; michael@0: michael@0: if (engine.name != "Test search engine") michael@0: return; michael@0: michael@0: function check_submission(aExpected, aSearchTerm, aType) { michael@0: do_check_eq(engine.getSubmission(aSearchTerm, aType).uri.spec, "http://example.com/search" + aExpected); michael@0: } michael@0: michael@0: // Force the type and check for the expected URL michael@0: check_submission("?q=foo", "foo", "text/html"); michael@0: check_submission("/tablet?q=foo", "foo", "application/x-moz-tabletsearch"); michael@0: check_submission("/phone?q=foo", "foo", "application/x-moz-phonesearch"); michael@0: michael@0: // Let the service pick the appropriate type based on the device michael@0: // and check for expected URL michael@0: let sysInfo = Cc["@mozilla.org/system-info;1"].getService(Ci.nsIPropertyBag2); michael@0: if (sysInfo.get("tablet")) { michael@0: do_print("Device: tablet"); michael@0: check_submission("/tablet?q=foo", "foo", null); michael@0: } else { michael@0: do_print("Device: phone"); michael@0: check_submission("/phone?q=foo", "foo", null); michael@0: } michael@0: michael@0: run_next_test(); michael@0: }; michael@0: michael@0: add_test(function test_default() { michael@0: do_register_cleanup(function cleanup() { michael@0: Services.obs.removeObserver(search_observer, "browser-search-engine-modified"); michael@0: }); michael@0: michael@0: Services.obs.addObserver(search_observer, "browser-search-engine-modified", false); michael@0: michael@0: do_print("Loading search engine"); michael@0: Services.search.addEngine("http://mochi.test:8888/tests/robocop/devicesearch.xml", Ci.nsISearchEngine.DATA_XML, null, false); michael@0: }); michael@0: michael@0: run_next_test();