summaryrefslogtreecommitdiffstats
path: root/browser/components/search/test/browser_webapi.js
blob: d8161ffbe528d89bd2f1f5655999c3355bf238c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var ROOT = getRootDirectory(gTestPath).replace("chrome://mochitests/content", "http://example.com");
const searchBundle = Services.strings.createBundle("chrome://global/locale/search/search.properties");
const brandBundle = Services.strings.createBundle("chrome://branding/locale/brand.properties");
const brandName = brandBundle.GetStringFromName("brandShortName");

function getString(key, ...params) {
  return searchBundle.formatStringFromName(key, params, params.length);
}

function AddSearchProvider(...args) {
  return gBrowser.addTab(ROOT + "webapi.html?" + encodeURIComponent(JSON.stringify(args)));
}

function promiseDialogOpened() {
  return new Promise((resolve, reject) => {
    Services.wm.addListener({
      onOpenWindow: function(xulWin) {
        Services.wm.removeListener(this);

        let win = xulWin.QueryInterface(Ci.nsIInterfaceRequestor)
                        .getInterface(Ci.nsIDOMWindow);
        waitForFocus(() => {
          if (win.location == "chrome://global/content/commonDialog.xul")
            resolve(win)
          else
            reject();
        }, win);
      }
    });
  });
}

add_task(function* test_working() {
  gBrowser.selectedTab = AddSearchProvider(ROOT + "testEngine.xml");

  let dialog = yield promiseDialogOpened();
  is(dialog.args.promptType, "confirmEx", "Should see the confirmation dialog.");
  is(dialog.args.text, getString("addEngineConfirmation", "Foo", "example.com"),
     "Should have seen the right install message");
  dialog.document.documentElement.cancelDialog();

  gBrowser.removeCurrentTab();
});

add_task(function* test_HTTP() {
  gBrowser.selectedTab = AddSearchProvider(ROOT.replace("http:", "HTTP:") + "testEngine.xml");

  let dialog = yield promiseDialogOpened();
  is(dialog.args.promptType, "confirmEx", "Should see the confirmation dialog.");
  is(dialog.args.text, getString("addEngineConfirmation", "Foo", "example.com"),
     "Should have seen the right install message");
  dialog.document.documentElement.cancelDialog();

  gBrowser.removeCurrentTab();
});

add_task(function* test_relative() {
  gBrowser.selectedTab = AddSearchProvider("testEngine.xml");

  let dialog = yield promiseDialogOpened();
  is(dialog.args.promptType, "confirmEx", "Should see the confirmation dialog.");
  is(dialog.args.text, getString("addEngineConfirmation", "Foo", "example.com"),
     "Should have seen the right install message");
  dialog.document.documentElement.cancelDialog();

  gBrowser.removeCurrentTab();
});

add_task(function* test_invalid() {
  gBrowser.selectedTab = AddSearchProvider("z://foobar");

  let dialog = yield promiseDialogOpened();
  is(dialog.args.promptType, "alert", "Should see the alert dialog.");
  is(dialog.args.text, getString("error_invalid_engine_msg", brandName),
     "Should have seen the right error message")
  dialog.document.documentElement.acceptDialog();

  gBrowser.removeCurrentTab();
});

add_task(function* test_missing() {
  let url = ROOT + "foobar.xml";
  gBrowser.selectedTab = AddSearchProvider(url);

  let dialog = yield promiseDialogOpened();
  is(dialog.args.promptType, "alert", "Should see the alert dialog.");
  is(dialog.args.text, getString("error_loading_engine_msg2", brandName, url),
     "Should have seen the right error message")
  dialog.document.documentElement.acceptDialog();

  gBrowser.removeCurrentTab();
});