summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/unit/test_PUIU_setCharsetForPage.js
blob: db213971e9d79499dcb0e239dd335161c9f1ef04 (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { PrivateBrowsingUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/PrivateBrowsingUtils.sys.mjs"
);

const UTF8 = "UTF-8";
const UTF16 = "UTF-16";

const TEST_URI = "http://foo.com";
const TEST_BOOKMARKED_URI = "http://bar.com";

add_task(function setup() {
  let savedIsWindowPrivateFunc = PrivateBrowsingUtils.isWindowPrivate;
  PrivateBrowsingUtils.isWindowPrivate = () => false;

  registerCleanupFunction(() => {
    PrivateBrowsingUtils.isWindowPrivate = savedIsWindowPrivateFunc;
  });
});

add_task(async function test_simple_add() {
  // add pages to history
  await PlacesTestUtils.addVisits(TEST_URI);
  await PlacesTestUtils.addVisits(TEST_BOOKMARKED_URI);

  // create bookmarks on TEST_BOOKMARKED_URI
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: TEST_BOOKMARKED_URI,
    title: TEST_BOOKMARKED_URI.spec,
  });
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    url: TEST_BOOKMARKED_URI,
    title: TEST_BOOKMARKED_URI.spec,
  });

  // set charset on not-bookmarked page
  await PlacesUIUtils.setCharsetForPage(TEST_URI, UTF16, {});
  // set charset on bookmarked page
  await PlacesUIUtils.setCharsetForPage(TEST_BOOKMARKED_URI, UTF16, {});

  let pageInfo = await PlacesUtils.history.fetch(TEST_URI, {
    includeAnnotations: true,
  });
  Assert.equal(
    pageInfo.annotations.get(PlacesUtils.CHARSET_ANNO),
    UTF16,
    "Should return correct charset for a not-bookmarked page"
  );

  pageInfo = await PlacesUtils.history.fetch(TEST_BOOKMARKED_URI, {
    includeAnnotations: true,
  });
  Assert.equal(
    pageInfo.annotations.get(PlacesUtils.CHARSET_ANNO),
    UTF16,
    "Should return correct charset for a bookmarked page"
  );

  await PlacesUtils.history.clear();

  pageInfo = await PlacesUtils.history.fetch(TEST_URI, {
    includeAnnotations: true,
  });
  Assert.ok(
    !pageInfo,
    "Should not return pageInfo for a page after history cleared"
  );

  pageInfo = await PlacesUtils.history.fetch(TEST_BOOKMARKED_URI, {
    includeAnnotations: true,
  });
  Assert.equal(
    pageInfo.annotations.get(PlacesUtils.CHARSET_ANNO),
    UTF16,
    "Charset should still be set for a bookmarked page after history clear"
  );

  await PlacesUIUtils.setCharsetForPage(TEST_BOOKMARKED_URI, "");
  pageInfo = await PlacesUtils.history.fetch(TEST_BOOKMARKED_URI, {
    includeAnnotations: true,
  });
  Assert.strictEqual(
    pageInfo.annotations.get(PlacesUtils.CHARSET_ANNO),
    undefined,
    "Should not have a charset after it has been removed from the page"
  );
});

add_task(async function test_utf8_clears_saved_anno() {
  await PlacesUtils.history.clear();
  await PlacesTestUtils.addVisits(TEST_URI);

  // set charset on bookmarked page
  await PlacesUIUtils.setCharsetForPage(TEST_URI, UTF16, {});

  let pageInfo = await PlacesUtils.history.fetch(TEST_URI, {
    includeAnnotations: true,
  });
  Assert.equal(
    pageInfo.annotations.get(PlacesUtils.CHARSET_ANNO),
    UTF16,
    "Should return correct charset for a not-bookmarked page"
  );

  // Now set the bookmark to a UTF-8 charset.
  await PlacesUIUtils.setCharsetForPage(TEST_URI, UTF8, {});

  pageInfo = await PlacesUtils.history.fetch(TEST_URI, {
    includeAnnotations: true,
  });
  Assert.strictEqual(
    pageInfo.annotations.get(PlacesUtils.CHARSET_ANNO),
    undefined,
    "Should have removed the charset for a UTF-8 page."
  );
});

add_task(async function test_private_browsing_not_saved() {
  await PlacesUtils.history.clear();
  await PlacesTestUtils.addVisits(TEST_URI);

  // set charset on bookmarked page, but pretend this is a private browsing window.
  PrivateBrowsingUtils.isWindowPrivate = () => true;
  await PlacesUIUtils.setCharsetForPage(TEST_URI, UTF16, {});

  let pageInfo = await PlacesUtils.history.fetch(TEST_URI, {
    includeAnnotations: true,
  });
  Assert.strictEqual(
    pageInfo.annotations.get(PlacesUtils.CHARSET_ANNO),
    undefined,
    "Should not have set the charset in a private browsing window."
  );
});