summaryrefslogtreecommitdiffstats
path: root/toolkit/components/contextualidentity/tests/unit/test_migratedFile.js
blob: dd0558134206d2a5e33898b34c7525326abaa9c7 (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
"use strict";

const profileDir = do_get_profile();

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

const TEST_STORE_FILE_PATH = PathUtils.join(
  profileDir.path,
  "test-containers.json"
);

// Test the containers JSON file migrations.
add_task(async function migratedFile() {
  // Let's create a file that has to be migrated.
  const oldFileData = {
    version: 2,
    lastUserContextId: 6,
    identities: [
      {
        userContextId: 1,
        public: true,
        icon: "fingerprint",
        color: "blue",
        l10nID: "userContextPersonal.label",
        accessKey: "userContextPersonal.accesskey",
      },
      {
        userContextId: 2,
        public: true,
        icon: "briefcase",
        color: "orange",
        l10nID: "userContextWork.label",
        accessKey: "userContextWork.accesskey",
      },
      {
        userContextId: 3,
        public: true,
        icon: "dollar",
        color: "green",
        l10nID: "userContextBanking.label",
        accessKey: "userContextBanking.accesskey",
      },
      {
        userContextId: 4,
        public: true,
        icon: "cart",
        color: "pink",
        l10nID: "userContextShopping.label",
        accessKey: "userContextShopping.accesskey",
      },
      {
        userContextId: 5,
        public: false,
        icon: "",
        color: "",
        name: "userContextIdInternal.thumbnail",
        accessKey: "",
      },
      {
        userContextId: 6,
        public: true,
        icon: "cart",
        color: "ping",
        name: "Custom user-created identity",
      },
    ],
  };

  await IOUtils.writeJSON(TEST_STORE_FILE_PATH, oldFileData, {
    tmpPath: TEST_STORE_FILE_PATH + ".tmp",
  });

  let cis =
    ContextualIdentityService.createNewInstanceForTesting(TEST_STORE_FILE_PATH);
  ok(!!cis, "We have our instance of ContextualIdentityService");

  // Check that the custom user-created identity exists.

  const expectedPublicLength = oldFileData.identities.filter(
    identity => identity.public
  ).length;
  const publicIdentities = cis.getPublicIdentities();
  const oldLastIdentity =
    oldFileData.identities[oldFileData.identities.length - 1];
  const customUserCreatedIdentity = publicIdentities
    .filter(identity => identity.name === oldLastIdentity.name)
    .pop();

  equal(
    publicIdentities.length,
    expectedPublicLength,
    "We should have the expected number of public identities"
  );
  ok(!!customUserCreatedIdentity, "Got the custom user-created identity");

  Assert.deepEqual(
    cis.getPublicUserContextIds(),
    cis.getPublicIdentities().map(identity => identity.userContextId),
    "getPublicUserContextIds has matching user context IDs"
  );

  // Check that the reserved userContextIdInternal.webextStorageLocal identity exists.

  const webextStorageLocalPrivateId =
    ContextualIdentityService._defaultIdentities
      .filter(
        identity => identity.name === "userContextIdInternal.webextStorageLocal"
      )
      .pop().userContextId;

  const privWebExtStorageLocal = cis.getPrivateIdentity(
    "userContextIdInternal.webextStorageLocal"
  );
  equal(
    privWebExtStorageLocal && privWebExtStorageLocal.userContextId,
    webextStorageLocalPrivateId,
    "We should have the default userContextIdInternal.webextStorageLocal private identity"
  );
});