summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_bookmarkProperties_addKeywordForThisSearch.js
blob: 514519810ac83f4b410745de5ca85a100c4f54e5 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"use strict";

const TEST_URL =
  "http://mochi.test:8888/browser/browser/components/places/tests/browser/keyword_form.html";

let contentAreaContextMenu = document.getElementById("contentAreaContextMenu");

add_task(async function add_keyword() {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: TEST_URL,
    },
    async function (browser) {
      // We must wait for the context menu code to build metadata.
      await openContextMenuForContentSelector(
        browser,
        '#form1 > input[name="search"]'
      );

      await withBookmarksDialog(
        false,
        function () {
          AddKeywordForSearchField();
          contentAreaContextMenu.hidePopup();
        },
        async function (dialogWin) {
          let acceptBtn = dialogWin.document
            .getElementById("bookmarkpropertiesdialog")
            .getButton("accept");
          Assert.ok(acceptBtn.disabled, "Accept button is disabled");

          let promiseKeywordNotification = PlacesTestUtils.waitForNotification(
            "bookmark-keyword-changed",
            events => events.some(event => event.keyword === "kw")
          );

          fillBookmarkTextField("editBMPanel_keywordField", "kw", dialogWin);

          Assert.ok(!acceptBtn.disabled, "Accept button is enabled");

          acceptBtn.click();
          await promiseKeywordNotification;

          // After the notification, the keywords cache will update asynchronously.
          info("Check the keyword entry has been created");
          let entry;
          await TestUtils.waitForCondition(async function () {
            entry = await PlacesUtils.keywords.fetch("kw");
            return !!entry;
          }, "Unable to find the expected keyword");
          Assert.equal(entry.keyword, "kw", "keyword is correct");
          Assert.equal(entry.url.href, TEST_URL, "URL is correct");
          Assert.equal(
            entry.postData,
            "accenti%3D%E0%E8%EC%F2%F9&search%3D%25s",
            "POST data is correct"
          );
          let bm = await PlacesUtils.bookmarks.fetch({ url: TEST_URL });
          Assert.equal(
            bm.parentGuid,
            await PlacesUIUtils.defaultParentGuid,
            "Should have created the keyword in the right folder."
          );

          info("Check the charset has been saved");
          let pageInfo = await PlacesUtils.history.fetch(TEST_URL, {
            includeAnnotations: true,
          });
          Assert.equal(
            pageInfo.annotations.get(PlacesUtils.CHARSET_ANNO),
            "windows-1252",
            "charset is correct"
          );

          // Now check getShortcutOrURI.
          let data = await UrlbarUtils.getShortcutOrURIAndPostData("kw test");
          Assert.equal(
            getPostDataString(data.postData),
            "accenti=\u00E0\u00E8\u00EC\u00F2\u00F9&search=test",
            "getShortcutOrURI POST data is correct"
          );
          Assert.equal(data.url, TEST_URL, "getShortcutOrURI URL is correct");
        },
        () => PlacesUtils.bookmarks.eraseEverything()
      );
    }
  );
});

add_task(async function reopen_same_field() {
  await PlacesUtils.keywords.insert({
    url: TEST_URL,
    keyword: "kw",
    postData: "accenti%3D%E0%E8%EC%F2%F9&search%3D%25s",
  });
  registerCleanupFunction(async function () {
    await PlacesUtils.keywords.remove("kw");
  });
  // Reopening on the same input field should show the existing keyword.
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: TEST_URL,
    },
    async function (browser) {
      // We must wait for the context menu code to build metadata.
      await openContextMenuForContentSelector(
        browser,
        '#form1 > input[name="search"]'
      );

      await withBookmarksDialog(
        true,
        function () {
          AddKeywordForSearchField();
          contentAreaContextMenu.hidePopup();
        },
        async function (dialogWin) {
          let elt = dialogWin.document.getElementById(
            "editBMPanel_keywordField"
          );
          Assert.equal(elt.value, "kw", "Keyword should be the previous value");

          let acceptBtn = dialogWin.document
            .getElementById("bookmarkpropertiesdialog")
            .getButton("accept");
          ok(!acceptBtn.disabled, "Accept button is enabled");
        },
        () => PlacesUtils.bookmarks.eraseEverything()
      );
    }
  );
});

add_task(async function open_other_field() {
  await PlacesUtils.keywords.insert({
    url: TEST_URL,
    keyword: "kw2",
    postData: "search%3D%25s",
  });
  registerCleanupFunction(async function () {
    await PlacesUtils.keywords.remove("kw2");
  });
  // Reopening on another field of the same page that has different postData
  // should not show the existing keyword.
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: TEST_URL,
    },
    async function (browser) {
      // We must wait for the context menu code to build metadata.
      await openContextMenuForContentSelector(
        browser,
        '#form2 > input[name="search"]'
      );

      await withBookmarksDialog(
        true,
        function () {
          AddKeywordForSearchField();
          contentAreaContextMenu.hidePopup();
        },
        function (dialogWin) {
          let acceptBtn = dialogWin.document
            .getElementById("bookmarkpropertiesdialog")
            .getButton("accept");
          ok(acceptBtn.disabled, "Accept button is disabled");

          let elt = dialogWin.document.getElementById(
            "editBMPanel_keywordField"
          );
          is(elt.value, "");
        },
        () => PlacesUtils.bookmarks.eraseEverything()
      );
    }
  );
});

function getPostDataString(stream) {
  let sis = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(
    Ci.nsIScriptableInputStream
  );
  sis.init(stream);
  return sis.read(stream.available()).split("\n").pop();
}