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

/**
 * Tests that nsBrowserGlue correctly imports bookmarks from distribution.ini.
 */

const PREF_SMART_BOOKMARKS_VERSION = "browser.places.smartBookmarksVersion";
const PREF_BMPROCESSED = "distribution.516444.bookmarksProcessed";
const PREF_DISTRIBUTION_ID = "distribution.id";

const TOPICDATA_DISTRIBUTION_CUSTOMIZATION = "force-distribution-customization";
const TOPIC_CUSTOMIZATION_COMPLETE = "distribution-customization-complete";
const TOPIC_BROWSERGLUE_TEST = "browser-glue-test";

function run_test() {
  // Set special pref to load distribution.ini from the profile folder.
  Services.prefs.setBoolPref("distribution.testing.loadFromProfile", true);

  // Copy distribution.ini file to the profile dir.
  let distroDir = gProfD.clone();
  distroDir.leafName = "distribution";
  let iniFile = distroDir.clone();
  iniFile.append("distribution.ini");
  if (iniFile.exists()) {
    iniFile.remove(false);
    print("distribution.ini already exists, did some test forget to cleanup?");
  }

  let testDistributionFile = gTestDir.clone();
  testDistributionFile.append("distribution.ini");
  testDistributionFile.copyTo(distroDir, "distribution.ini");
  Assert.ok(testDistributionFile.exists());

  run_next_test();
}

do_register_cleanup(function () {
  // Remove the distribution file, even if the test failed, otherwise all
  // next tests will import it.
  let iniFile = gProfD.clone();
  iniFile.leafName = "distribution";
  iniFile.append("distribution.ini");
  if (iniFile.exists()) {
    iniFile.remove(false);
  }
  Assert.ok(!iniFile.exists());
});

add_task(function* () {
  // Disable Smart Bookmarks creation.
  Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, -1);

  // Initialize Places through the History Service and check that a new
  // database has been created.
  Assert.equal(PlacesUtils.history.databaseStatus,
               PlacesUtils.history.DATABASE_STATUS_CREATE);

  // Force distribution.
  let glue = Cc["@mozilla.org/browser/browserglue;1"].getService(Ci.nsIObserver)
  glue.observe(null, TOPIC_BROWSERGLUE_TEST, TOPICDATA_DISTRIBUTION_CUSTOMIZATION);

  // Test will continue on customization complete notification.
  yield promiseTopicObserved(TOPIC_CUSTOMIZATION_COMPLETE);

  // Check the custom bookmarks exist on menu.
  let menuItem = yield PlacesUtils.bookmarks.fetch({
    parentGuid: PlacesUtils.bookmarks.menuGuid,
    index: 0
  });
  Assert.equal(menuItem.title, "Menu Link Before");

  menuItem = yield PlacesUtils.bookmarks.fetch({
    parentGuid: PlacesUtils.bookmarks.menuGuid,
    index: 1 + DEFAULT_BOOKMARKS_ON_MENU
  });
  Assert.equal(menuItem.title, "Menu Link After");

  // Check no favicon or keyword exists for this bookmark
  yield Assert.rejects(waitForResolvedPromise(() => {
    return PlacesUtils.promiseFaviconData(menuItem.url.href);
  }, "Favicon not found", 10), /Favicon\snot\sfound/, "Favicon not found");

  let keywordItem = yield PlacesUtils.keywords.fetch({
    url: menuItem.url.href
  });
  Assert.strictEqual(keywordItem, null);

  // Check the custom bookmarks exist on toolbar.
  let toolbarItem = yield PlacesUtils.bookmarks.fetch({
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    index: 0
  });
  Assert.equal(toolbarItem.title, "Toolbar Link Before");

  // Check the custom favicon and keyword exist for this bookmark
  let faviconItem = yield waitForResolvedPromise(() => {
    return PlacesUtils.promiseFaviconData(toolbarItem.url.href);
  }, "Favicon not found", 10);
  Assert.equal(faviconItem.uri.spec, "https://example.org/favicon.png");
  Assert.greater(faviconItem.dataLen, 0);
  Assert.equal(faviconItem.mimeType, "image/png");

  let base64Icon = "data:image/png;base64," +
      base64EncodeString(String.fromCharCode.apply(String, faviconItem.data));
  Assert.equal(base64Icon, SMALLPNG_DATA_URI.spec);

  keywordItem = yield PlacesUtils.keywords.fetch({
    url: toolbarItem.url.href
  });
  Assert.notStrictEqual(keywordItem, null);
  Assert.equal(keywordItem.keyword, "e:t:b");

  toolbarItem = yield PlacesUtils.bookmarks.fetch({
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    index: 1 + DEFAULT_BOOKMARKS_ON_TOOLBAR
  });
  Assert.equal(toolbarItem.title, "Toolbar Link After");

  // Check the bmprocessed pref has been created.
  Assert.ok(Services.prefs.getBoolPref(PREF_BMPROCESSED));

  // Check distribution prefs have been created.
  Assert.equal(Services.prefs.getCharPref(PREF_DISTRIBUTION_ID), "516444");
});