|
1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 Components.utils.import("resource://gre/modules/Services.jsm"); |
|
7 |
|
8 var Cc = Components.classes; |
|
9 var Ci = Components.interfaces; |
|
10 |
|
11 function search_observer(aSubject, aTopic, aData) { |
|
12 let engine = aSubject.QueryInterface(Ci.nsISearchEngine); |
|
13 do_print("Observer: " + aData + " for " + engine.name); |
|
14 |
|
15 if (aData != "engine-added") |
|
16 return; |
|
17 |
|
18 if (engine.name != "Test search engine") |
|
19 return; |
|
20 |
|
21 function check_submission(aExpected, aSearchTerm, aType) { |
|
22 do_check_eq(engine.getSubmission(aSearchTerm, aType).uri.spec, "http://example.com/search" + aExpected); |
|
23 } |
|
24 |
|
25 // Force the type and check for the expected URL |
|
26 check_submission("?q=foo", "foo", "text/html"); |
|
27 check_submission("/tablet?q=foo", "foo", "application/x-moz-tabletsearch"); |
|
28 check_submission("/phone?q=foo", "foo", "application/x-moz-phonesearch"); |
|
29 |
|
30 // Let the service pick the appropriate type based on the device |
|
31 // and check for expected URL |
|
32 let sysInfo = Cc["@mozilla.org/system-info;1"].getService(Ci.nsIPropertyBag2); |
|
33 if (sysInfo.get("tablet")) { |
|
34 do_print("Device: tablet"); |
|
35 check_submission("/tablet?q=foo", "foo", null); |
|
36 } else { |
|
37 do_print("Device: phone"); |
|
38 check_submission("/phone?q=foo", "foo", null); |
|
39 } |
|
40 |
|
41 run_next_test(); |
|
42 }; |
|
43 |
|
44 add_test(function test_default() { |
|
45 do_register_cleanup(function cleanup() { |
|
46 Services.obs.removeObserver(search_observer, "browser-search-engine-modified"); |
|
47 }); |
|
48 |
|
49 Services.obs.addObserver(search_observer, "browser-search-engine-modified", false); |
|
50 |
|
51 do_print("Loading search engine"); |
|
52 Services.search.addEngine("http://mochi.test:8888/tests/robocop/devicesearch.xml", Ci.nsISearchEngine.DATA_XML, null, false); |
|
53 }); |
|
54 |
|
55 run_next_test(); |