summaryrefslogtreecommitdiffstats
path: root/browser/components/migration/tests/unit/test_Chrome_bookmarks.js
blob: c10ab6dcd64324f8d523cf25ac94432ffbd9d4bc (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
189
190
191
192
193
194
195
196
197
198
199
"use strict";

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

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

let rootDir = do_get_file("chromefiles/", true);

add_task(async function setup_fakePaths() {
  let pathId;
  if (AppConstants.platform == "macosx") {
    pathId = "ULibDir";
  } else if (AppConstants.platform == "win") {
    pathId = "LocalAppData";
  } else {
    pathId = "Home";
  }
  registerFakePath(pathId, rootDir);
});

add_task(async function setup_initialBookmarks() {
  let bookmarks = [];
  for (let i = 0; i < PlacesUIUtils.NUM_TOOLBAR_BOOKMARKS_TO_UNHIDE + 1; i++) {
    bookmarks.push({ url: "https://example.com/" + i, title: "" + i });
  }

  // Ensure we have enough items in both the menu and toolbar to trip creating a "from" folder.
  await PlacesUtils.bookmarks.insertTree({
    guid: PlacesUtils.bookmarks.toolbarGuid,
    children: bookmarks,
  });
  await PlacesUtils.bookmarks.insertTree({
    guid: PlacesUtils.bookmarks.menuGuid,
    children: bookmarks,
  });
});

async function testBookmarks(migratorKey, subDirs) {
  if (AppConstants.platform == "macosx") {
    subDirs.unshift("Application Support");
  } else if (AppConstants.platform == "win") {
    subDirs.push("User Data");
  } else {
    subDirs.unshift(".config");
  }

  let target = rootDir.clone();
  // Pretend this is the default profile
  subDirs.push("Default");
  while (subDirs.length) {
    target.append(subDirs.shift());
  }

  await IOUtils.makeDirectory(target.path, {
    createAncestor: true,
    ignoreExisting: true,
  });

  // Copy Favicons database into Default profile
  const sourcePath = do_get_file(
    "AppData/Local/Google/Chrome/User Data/Default/Favicons"
  ).path;
  await IOUtils.copy(sourcePath, target.path);

  // Get page url for each favicon
  let faviconURIs = await MigrationUtils.getRowsFromDBWithoutLocks(
    sourcePath,
    "Chrome Bookmark Favicons",
    `select page_url from icon_mapping`
  );

  target.append("Bookmarks");
  await IOUtils.remove(target.path, { ignoreAbsent: true });

  let bookmarksData = createChromeBookmarkStructure();
  await IOUtils.writeJSON(target.path, bookmarksData);

  let migrator = await MigrationUtils.getMigrator(migratorKey);
  // Sanity check for the source.
  Assert.ok(await migrator.isSourceAvailable());

  let itemsSeen = { bookmarks: 0, folders: 0 };
  let listener = events => {
    for (let event of events) {
      itemsSeen[
        event.itemType == PlacesUtils.bookmarks.TYPE_FOLDER
          ? "folders"
          : "bookmarks"
      ]++;
    }
  };

  PlacesUtils.observers.addListener(["bookmark-added"], listener);
  const PROFILE = {
    id: "Default",
    name: "Default",
  };
  let observerNotified = false;
  Services.obs.addObserver((aSubject, aTopic, aData) => {
    let [toolbar, visibility] = JSON.parse(aData);
    Assert.equal(
      toolbar,
      CustomizableUI.AREA_BOOKMARKS,
      "Notification should be received for bookmarks toolbar"
    );
    Assert.equal(
      visibility,
      "true",
      "Notification should say to reveal the bookmarks toolbar"
    );
    observerNotified = true;
  }, "browser-set-toolbar-visibility");
  const initialToolbarCount = await getFolderItemCount(
    PlacesUtils.bookmarks.toolbarGuid
  );
  const initialUnfiledCount = await getFolderItemCount(
    PlacesUtils.bookmarks.unfiledGuid
  );
  const initialmenuCount = await getFolderItemCount(
    PlacesUtils.bookmarks.menuGuid
  );

  await promiseMigration(
    migrator,
    MigrationUtils.resourceTypes.BOOKMARKS,
    PROFILE
  );
  const postToolbarCount = await getFolderItemCount(
    PlacesUtils.bookmarks.toolbarGuid
  );
  const postUnfiledCount = await getFolderItemCount(
    PlacesUtils.bookmarks.unfiledGuid
  );
  const postmenuCount = await getFolderItemCount(
    PlacesUtils.bookmarks.menuGuid
  );

  Assert.equal(
    postUnfiledCount - initialUnfiledCount,
    210,
    "Should have seen 210 items in unsorted bookmarks"
  );
  Assert.equal(
    postToolbarCount - initialToolbarCount,
    105,
    "Should have seen 105 items in toolbar"
  );
  Assert.equal(
    postmenuCount - initialmenuCount,
    0,
    "Should have seen 0 items in menu toolbar"
  );

  PlacesUtils.observers.removeListener(["bookmark-added"], listener);

  Assert.equal(itemsSeen.bookmarks, 300, "Should have seen 300 bookmarks.");
  Assert.equal(itemsSeen.folders, 15, "Should have seen 15 folders.");
  Assert.equal(
    MigrationUtils._importQuantities.bookmarks,
    itemsSeen.bookmarks + itemsSeen.folders,
    "Telemetry reporting correct."
  );
  Assert.ok(observerNotified, "The observer should be notified upon migration");
  let pageUrls = Array.from(faviconURIs, f =>
    Services.io.newURI(f.getResultByName("page_url"))
  );
  await assertFavicons(pageUrls);
}

add_task(async function test_Chrome() {
  // Expire all favicons before the test to make sure favicons are imported
  PlacesUtils.favicons.expireAllFavicons();
  let subDirs =
    AppConstants.platform == "linux" ? ["google-chrome"] : ["Google", "Chrome"];
  await testBookmarks("chrome", subDirs);
});

add_task(async function test_ChromiumEdge() {
  PlacesUtils.favicons.expireAllFavicons();
  if (AppConstants.platform == "linux") {
    // Edge isn't available on Linux.
    return;
  }
  let subDirs =
    AppConstants.platform == "macosx"
      ? ["Microsoft Edge"]
      : ["Microsoft", "Edge"];
  await testBookmarks("chromium-edge", subDirs);
});

async function getFolderItemCount(guid) {
  let results = await PlacesUtils.promiseBookmarksTree(guid);

  return results.itemsCount;
}