summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_import_button.js
blob: 146fd746f8803a501aceb480ff0a53b4920e1af4 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const kPref = "browser.bookmarks.addedImportButton";

/**
 * Verify that we add the import button only if there aren't enough bookmarks
 * in the toolbar.
 */
add_task(async function test_bookmark_import_button() {
  let bookmarkCount = (
    await PlacesUtils.bookmarks.fetch(PlacesUtils.bookmarks.toolbarGuid)
  ).childCount;
  Assert.less(bookmarkCount, 3, "we should start with less than 3 bookmarks");

  ok(
    !document.getElementById("import-button"),
    "Shouldn't have button to start with."
  );
  await PlacesUIUtils.maybeAddImportButton();
  ok(document.getElementById("import-button"), "Button should be added.");
  is(Services.prefs.getBoolPref(kPref), true, "Pref should be set.");

  CustomizableUI.reset();

  // Add some bookmarks. This should stop the import button from being inserted.
  let parentGuid = PlacesUtils.bookmarks.toolbarGuid;
  let bookmarks = await Promise.all(
    ["firefox", "rules", "yo"].map(n =>
      PlacesUtils.bookmarks.insert({
        parentGuid,
        url: `https://example.com/${n}`,
        title: n.toString(),
      })
    )
  );

  // Ensure we remove items after this task, or worst-case after this test
  // file has completed.
  let removeAllBookmarks = () => {
    let removals = bookmarks.map(b => PlacesUtils.bookmarks.remove(b.guid));
    bookmarks = [];
    return Promise.all(removals);
  };
  registerCleanupFunction(removeAllBookmarks);

  await PlacesUIUtils.maybeAddImportButton();
  ok(
    !document.getElementById("import-button"),
    "Button should not be added if we have bookmarks."
  );

  // Just in case, for future tests we run:
  CustomizableUI.reset();

  await removeAllBookmarks();
});

/**
 * Verify the button gets removed when we import bookmarks successfully.
 */
add_task(async function test_bookmark_import_button_removal() {
  let bookmarkCount = (
    await PlacesUtils.bookmarks.fetch(PlacesUtils.bookmarks.toolbarGuid)
  ).childCount;
  Assert.less(bookmarkCount, 3, "we should start with less than 3 bookmarks");

  ok(
    !document.getElementById("import-button"),
    "Shouldn't have button to start with."
  );
  await PlacesUIUtils.maybeAddImportButton();
  ok(document.getElementById("import-button"), "Button should be added.");
  is(Services.prefs.getBoolPref(kPref), true, "Pref should be set.");

  PlacesUIUtils.removeImportButtonWhenImportSucceeds();

  Services.obs.notifyObservers(
    null,
    "Migration:ItemAfterMigrate",
    MigrationUtils.resourceTypes.BOOKMARKS
  );

  is(
    Services.prefs.getBoolPref(kPref, false),
    true,
    "Pref should stay without import."
  );
  ok(document.getElementById("import-button"), "Button should still be there.");

  // OK, actually add some bookmarks:
  MigrationUtils._importQuantities.bookmarks = 5;
  Services.obs.notifyObservers(
    null,
    "Migration:ItemAfterMigrate",
    MigrationUtils.resourceTypes.BOOKMARKS
  );

  is(Services.prefs.prefHasUserValue(kPref), false, "Pref should be removed.");
  ok(
    !document.getElementById("import-button"),
    "Button should have been removed."
  );

  // Reset this, otherwise subsequent tests are going to have a bad time.
  MigrationUtils._importQuantities.bookmarks = 0;
});

/**
 * Check that if the user removes the button, the next startup
 * we clear the pref and stop monitoring to remove the item.
 */
add_task(async function test_bookmark_import_button_removal_cleanup() {
  let bookmarkCount = (
    await PlacesUtils.bookmarks.fetch(PlacesUtils.bookmarks.toolbarGuid)
  ).childCount;
  Assert.less(bookmarkCount, 3, "we should start with less than 3 bookmarks");

  ok(
    !document.getElementById("import-button"),
    "Shouldn't have button to start with."
  );
  await PlacesUIUtils.maybeAddImportButton();
  ok(document.getElementById("import-button"), "Button should be added.");
  is(Services.prefs.getBoolPref(kPref), true, "Pref should be set.");

  // Simulate the user removing the item.
  CustomizableUI.removeWidgetFromArea("import-button");

  // We'll call this next startup:
  PlacesUIUtils.removeImportButtonWhenImportSucceeds();

  // And it should clean up the pref:
  is(Services.prefs.prefHasUserValue(kPref), false, "Pref should be removed.");
});

/**
 * Check that if migration (silently) errors, we still remove the button
 * _if_ we imported any bookmarks.
 */
add_task(async function test_bookmark_import_button_errors() {
  let bookmarkCount = (
    await PlacesUtils.bookmarks.fetch(PlacesUtils.bookmarks.toolbarGuid)
  ).childCount;
  Assert.less(bookmarkCount, 3, "we should start with less than 3 bookmarks");

  ok(
    !document.getElementById("import-button"),
    "Shouldn't have button to start with."
  );
  await PlacesUIUtils.maybeAddImportButton();
  ok(document.getElementById("import-button"), "Button should be added.");
  is(Services.prefs.getBoolPref(kPref), true, "Pref should be set.");

  PlacesUIUtils.removeImportButtonWhenImportSucceeds();

  Services.obs.notifyObservers(
    null,
    "Migration:ItemError",
    MigrationUtils.resourceTypes.BOOKMARKS
  );

  is(
    Services.prefs.getBoolPref(kPref, false),
    true,
    "Pref should stay when fatal error happens."
  );
  ok(document.getElementById("import-button"), "Button should still be there.");

  // OK, actually add some bookmarks:
  MigrationUtils._importQuantities.bookmarks = 5;
  Services.obs.notifyObservers(
    null,
    "Migration:ItemError",
    MigrationUtils.resourceTypes.BOOKMARKS
  );

  is(Services.prefs.prefHasUserValue(kPref), false, "Pref should be removed.");
  ok(
    !document.getElementById("import-button"),
    "Button should have been removed."
  );

  MigrationUtils._importQuantities.bookmarks = 0;
});