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

"use strict";

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

/**
 * Tests that if the migration wizard is opened when the
 * MOZ_UNINSTALLER_PROFILE_REFRESH environment variable is defined,
 * that the migration.uninstaller_profile_refresh scalar is set,
 * and the environment variable is cleared.
 */
add_task(async function test_uninstaller_migration() {
  if (AppConstants.platform != "win") {
    return;
  }

  Services.env.set("MOZ_UNINSTALLER_PROFILE_REFRESH", "1");
  let wizardPromise = BrowserTestUtils.domWindowOpened();
  // Opening the migration wizard this way is a blocking function, so
  // we delegate it to a runnable.
  executeSoon(() => {
    MigrationUtils.showMigrationWizard(null, { isStartupMigration: true });
  });
  let wizardWin = await wizardPromise;

  await BrowserTestUtils.waitForEvent(wizardWin, "MigrationWizard:Ready");

  let scalars = TelemetryTestUtils.getProcessScalars("parent", false, true);
  TelemetryTestUtils.assertScalar(
    scalars,
    "migration.uninstaller_profile_refresh",
    1
  );

  Assert.equal(
    Services.env.get("MOZ_UNINSTALLER_PROFILE_REFRESH"),
    "",
    "Cleared MOZ_UNINSTALLER_PROFILE_REFRESH environment variable."
  );
  await BrowserTestUtils.closeWindow(wizardWin);
});

/**
 * Tests that we populate the migration.discovered_migrators keyed scalar
 * with a count of discovered browsers and profiles.
 */
add_task(async function test_discovered_migrators_keyed_scalar() {
  Services.telemetry.clearScalars();

  let sandbox = sinon.createSandbox();
  registerCleanupFunction(() => {
    sandbox.restore();
  });

  // We'll pretend that this system only has the
  // InternalTestingProfileMigrator and ChromeProfileMigrator around to
  // start
  sandbox.stub(MigrationUtils, "availableMigratorKeys").get(() => {
    return ["internal-testing", "chrome"];
  });

  // The InternalTestingProfileMigrator by default returns a single profile
  // from `getSourceProfiles`, and now we'll just prepare the
  // ChromeProfileMigrator to return two fake profiles.
  sandbox.stub(ChromeProfileMigrator.prototype, "getSourceProfiles").resolves([
    { id: "chrome-test-1", name: "Chrome test profile 1" },
    { id: "chrome-test-2", name: "Chrome test profile 2" },
  ]);

  sandbox
    .stub(ChromeProfileMigrator.prototype, "hasPermissions")
    .resolves(true);

  // We also need to ensure that the ChromeProfileMigrator actually has
  // some resources to migrate, otherwise it won't get listed.
  sandbox
    .stub(ChromeProfileMigrator.prototype, "getResources")
    .callsFake(() => {
      return Promise.resolve(
        Object.values(MigrationUtils.resourceType).map(resourceType => {
          return {
            type: resourceType,
            migrate: () => {},
          };
        })
      );
    });

  await withMigrationWizardDialog(async () => {
    let scalars = TelemetryTestUtils.getProcessScalars("parent", true, true);
    TelemetryTestUtils.assertKeyedScalar(
      scalars,
      "migration.discovered_migrators",
      InternalTestingProfileMigrator.key,
      1
    );
    TelemetryTestUtils.assertKeyedScalar(
      scalars,
      "migration.discovered_migrators",
      ChromeProfileMigrator.key,
      2
    );
  });

  // Now, reset, and we'll try the case where a migrator returns `null` from
  // `getSourceProfiles` using the InternalTestingProfileMigrator again.
  sandbox.restore();

  sandbox = sinon.createSandbox();

  sandbox.stub(MigrationUtils, "availableMigratorKeys").get(() => {
    return ["internal-testing"];
  });

  sandbox
    .stub(ChromeProfileMigrator.prototype, "getSourceProfiles")
    .resolves(null);

  await withMigrationWizardDialog(async () => {
    let scalars = TelemetryTestUtils.getProcessScalars("parent", true, true);
    TelemetryTestUtils.assertKeyedScalar(
      scalars,
      "migration.discovered_migrators",
      InternalTestingProfileMigrator.key,
      1
    );
  });

  sandbox.restore();
});

/**
 * Tests that we write to the FX_MIGRATION_ERRORS histogram when a
 * resource fails to migrate properly.
 */
add_task(async function test_fx_migration_errors() {
  let migration = waitForTestMigration(
    [
      MigrationUtils.resourceTypes.BOOKMARKS,
      MigrationUtils.resourceTypes.PASSWORDS,
    ],
    [
      MigrationUtils.resourceTypes.BOOKMARKS,
      MigrationUtils.resourceTypes.PASSWORDS,
    ],
    InternalTestingProfileMigrator.testProfile,
    [MigrationUtils.resourceTypes.PASSWORDS]
  );

  await withMigrationWizardDialog(async prefsWin => {
    let dialogBody = prefsWin.document.body;
    let wizard = dialogBody.querySelector("migration-wizard");
    let wizardDone = BrowserTestUtils.waitForEvent(
      wizard,
      "MigrationWizard:DoneMigration"
    );

    selectResourceTypesAndStartMigration(wizard, [
      MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.BOOKMARKS,
      MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.PASSWORDS,
    ]);
    await migration;
    await wizardDone;

    assertQuantitiesShown(
      wizard,
      [
        MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.BOOKMARKS,
        MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.PASSWORDS,
      ],
      [MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.PASSWORDS]
    );
  });
});