summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/unifiedtoolbar/test/browser/browser_toolbarMigration.js
blob: c2ca1147fdef3ab015228e9de8bf4d1646b0adca (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
/* 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/. */

"use strict";

const { migrateToolbarForSpace } = ChromeUtils.importESModule(
  "resource:///modules/ToolbarMigration.sys.mjs"
);
const { getState, storeState } = ChromeUtils.importESModule(
  "resource:///modules/CustomizationState.mjs"
);
const { EXTENSION_PREFIX } = ChromeUtils.importESModule(
  "resource:///modules/CustomizableItems.sys.mjs"
);
const { getCachedAllowedSpaces, setCachedAllowedSpaces } = ChromeUtils.import(
  "resource:///modules/ExtensionToolbarButtons.jsm"
);

const MESSENGER_WINDOW = "chrome://messenger/content/messenger.xhtml";
const EXTENSION_ID = "thunderbird-compact-light@mozilla.org";

add_setup(() => {
  storeState({});
});

add_task(async function test_migrate_extension() {
  Services.xulStore.setValue(MESSENGER_WINDOW, "mail-bar3", "currentset", "");
  Services.xulStore.setValue(
    MESSENGER_WINDOW,
    "mail-bar3",
    "defaultset",
    "button-getmsg,button-newmsg,separator,button-tag,qfb-show-filter-bar,spring,gloda-search,thunderbird-compact-light_mozilla_org-browserAction-toolbarbutton,button-appmenu"
  );
  Services.xulStore.setValue(
    MESSENGER_WINDOW,
    "mail-bar3",
    "extensionset",
    "thunderbird-compact-light_mozilla_org-browserAction-toolbarbutton"
  );
  const extensionPref = Services.prefs.getStringPref(
    "extensions.webextensions.uuids",
    ""
  );
  const parsedPref = JSON.parse(extensionPref || "{}");
  if (!parsedPref.hasOwnProperty(EXTENSION_ID)) {
    parsedPref[EXTENSION_ID] = "foo";
    Services.prefs.setStringPref(
      "extensions.webextensions.uuids",
      JSON.stringify(parsedPref)
    );
  }

  migrateToolbarForSpace("mail");

  const newState = getState();

  Assert.deepEqual(
    newState.mail,
    [
      "get-messages",
      "write-message",
      "tag-message",
      "quick-filter-bar",
      "spacer",
      `${EXTENSION_PREFIX}${EXTENSION_ID}`,
      "spacer",
    ],
    "Extension button was converted to new ID format"
  );
  Assert.ok(
    !Services.xulStore.hasValue(MESSENGER_WINDOW, "mail-bar3", "currentset"),
    "Old toolbar state is cleared"
  );
  Assert.ok(
    !Services.xulStore.hasValue(MESSENGER_WINDOW, "mail-bar3", "defaultset"),
    "Old toolbar default state is cleared"
  );
  Assert.ok(
    !Services.xulStore.hasValue(MESSENGER_WINDOW, "mail-bar3", "extensionset"),
    "Old toolbar extension state is cleared"
  );
  Assert.deepEqual(
    Object.fromEntries(getCachedAllowedSpaces()),
    { [EXTENSION_ID]: ["mail"] },
    "Extension set migrated to new persistent extension state"
  );

  storeState({});
  setCachedAllowedSpaces(new Map());
  if (extensionPref) {
    Services.prefs.setStringPref(
      "extensions.webextensions.uuids",
      extensionPref
    );
  } else {
    Services.prefs.clearUserPref("extensions.webextensions.uuids");
  }
});