summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_decode.js
blob: ae0b4dfda1b92e630d52ad240aa398e0f9693a33 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// This test makes sure (1) you can't break the urlbar by typing particular JSON
// or JS fragments into it, (2) urlbar.textValue shows URLs unescaped, and (3)
// the urlbar also shows the URLs embedded in action URIs unescaped.  See bug
// 1233672.

add_task(async function injectJSON() {
  let inputStrs = [
    'http://example.com/ ", "url": "bar',
    "http://example.com/\\",
    'http://example.com/"',
    'http://example.com/","url":"evil.com',
    "http://mozilla.org/\\u0020",
    'http://www.mozilla.org/","url":1e6,"some-key":"foo',
    'http://www.mozilla.org/","url":null,"some-key":"foo',
    'http://www.mozilla.org/","url":["foo","bar"],"some-key":"foo',
  ];
  for (let inputStr of inputStrs) {
    await checkInput(inputStr);
  }
  gURLBar.value = "";
  gURLBar.handleRevert();
  gURLBar.blur();
});

add_task(function losslessDecode() {
  let urlNoScheme = "example.com/\u30a2\u30a4\u30a6\u30a8\u30aa";
  let url = "http://" + urlNoScheme;
  const result = new UrlbarResult(
    UrlbarUtils.RESULT_TYPE.TAB_SWITCH,
    UrlbarUtils.RESULT_SOURCE.TABS,
    { url }
  );
  gURLBar.setValueFromResult({ result });
  // Since this is directly setting textValue, it is expected to be trimmed.
  Assert.equal(
    gURLBar.inputField.value,
    urlNoScheme,
    "The string displayed in the textbox should not be escaped"
  );
  gURLBar.value = "";
  gURLBar.handleRevert();
  gURLBar.blur();
});

add_task(async function actionURILosslessDecode() {
  let urlNoScheme = "example.com/\u30a2\u30a4\u30a6\u30a8\u30aa";
  let url = "http://" + urlNoScheme;
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: url,
  });

  // At this point the heuristic result is selected but the urlbar's value is
  // simply `url`.  Key down and back around until the heuristic result is
  // selected again.
  do {
    EventUtils.synthesizeKey("KEY_ArrowDown");
  } while (UrlbarTestUtils.getSelectedRowIndex(window) != 0);

  let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);

  Assert.equal(
    result.type,
    UrlbarUtils.RESULT_TYPE.URL,
    "Should have selected a result of URL type"
  );

  Assert.equal(
    gURLBar.inputField.value,
    urlNoScheme,
    "The string displayed in the textbox should not be escaped"
  );

  gURLBar.value = "";
  gURLBar.handleRevert();
  gURLBar.blur();
});

add_task(async function test_resultsDisplayDecoded() {
  await PlacesUtils.history.clear();
  await UrlbarTestUtils.formHistory.clear();

  await PlacesTestUtils.addVisits("http://example.com/%E9%A1%B5");

  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "example",
  });

  let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 1);
  Assert.equal(
    result.displayed.url,
    "http://example.com/\u9875",
    "Should be displayed the correctly unescaped URL"
  );
});

async function checkInput(inputStr) {
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: inputStr,
  });

  let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);

  // URL matches have their param.urls fixed up.
  let fixupInfo = Services.uriFixup.getFixupURIInfo(
    inputStr,
    Ci.nsIURIFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS |
      Ci.nsIURIFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP
  );
  let expectedVisitURL = fixupInfo.fixedURI.spec;

  Assert.equal(result.url, expectedVisitURL, "Should have the correct URL");
  Assert.equal(
    result.title,
    inputStr.replace("\\", "/"),
    "Should have the correct title"
  );
  Assert.equal(
    result.type,
    UrlbarUtils.RESULT_TYPE.URL,
    "Should have be a result of type URL"
  );

  Assert.equal(
    result.displayed.title,
    inputStr.replace("\\", "/"),
    "Should be displaying the correct text"
  );
  let [action] = await document.l10n.formatValues([
    { id: "urlbar-result-action-visit" },
  ]);
  Assert.equal(
    result.displayed.action,
    action,
    "Should be displaying the correct action text"
  );
}