1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/tests/testDeviceSearchEngine.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,55 @@ 1.4 +// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +Components.utils.import("resource://gre/modules/Services.jsm"); 1.10 + 1.11 +var Cc = Components.classes; 1.12 +var Ci = Components.interfaces; 1.13 + 1.14 +function search_observer(aSubject, aTopic, aData) { 1.15 + let engine = aSubject.QueryInterface(Ci.nsISearchEngine); 1.16 + do_print("Observer: " + aData + " for " + engine.name); 1.17 + 1.18 + if (aData != "engine-added") 1.19 + return; 1.20 + 1.21 + if (engine.name != "Test search engine") 1.22 + return; 1.23 + 1.24 + function check_submission(aExpected, aSearchTerm, aType) { 1.25 + do_check_eq(engine.getSubmission(aSearchTerm, aType).uri.spec, "http://example.com/search" + aExpected); 1.26 + } 1.27 + 1.28 + // Force the type and check for the expected URL 1.29 + check_submission("?q=foo", "foo", "text/html"); 1.30 + check_submission("/tablet?q=foo", "foo", "application/x-moz-tabletsearch"); 1.31 + check_submission("/phone?q=foo", "foo", "application/x-moz-phonesearch"); 1.32 + 1.33 + // Let the service pick the appropriate type based on the device 1.34 + // and check for expected URL 1.35 + let sysInfo = Cc["@mozilla.org/system-info;1"].getService(Ci.nsIPropertyBag2); 1.36 + if (sysInfo.get("tablet")) { 1.37 + do_print("Device: tablet"); 1.38 + check_submission("/tablet?q=foo", "foo", null); 1.39 + } else { 1.40 + do_print("Device: phone"); 1.41 + check_submission("/phone?q=foo", "foo", null); 1.42 + } 1.43 + 1.44 + run_next_test(); 1.45 +}; 1.46 + 1.47 +add_test(function test_default() { 1.48 + do_register_cleanup(function cleanup() { 1.49 + Services.obs.removeObserver(search_observer, "browser-search-engine-modified"); 1.50 + }); 1.51 + 1.52 + Services.obs.addObserver(search_observer, "browser-search-engine-modified", false); 1.53 + 1.54 + do_print("Loading search engine"); 1.55 + Services.search.addEngine("http://mochi.test:8888/tests/robocop/devicesearch.xml", Ci.nsISearchEngine.DATA_XML, null, false); 1.56 +}); 1.57 + 1.58 +run_next_test();