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

"use strict";

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

/**
 * Tests that if we don't have permission to read the contents
 * of ~/Library/Safari, that we can ask for permission to do that.
 *
 * This involves presenting the user with some instructions, and then
 * showing a native folder picker for the user to select the
 * ~/Library/Safari folder. This seems to give us read access to the
 * folder contents.
 *
 * Revoking permissions for reading the ~/Library/Safari folder is
 * not something that we know how to do just yet. It seems to be
 * something involving macOS's System Integrity Protection. This test
 * mocks out and simulates the actual permissions mechanism to make
 * this test run reliably and repeatably.
 */
add_task(async function test_safari_permissions() {
  let sandbox = sinon.createSandbox();
  registerCleanupFunction(() => {
    sandbox.restore();
  });

  let safariMigrator = new SafariProfileMigrator();
  sandbox.stub(MigrationUtils, "getMigrator").resolves(safariMigrator);

  sandbox
    .stub(SafariProfileMigrator.prototype, "hasPermissions")
    .onFirstCall()
    .resolves(false)
    .onSecondCall()
    .resolves(true);

  sandbox
    .stub(SafariProfileMigrator.prototype, "getPermissions")
    .resolves(true);

  sandbox
    .stub(SafariProfileMigrator.prototype, "getResources")
    .callsFake(() => {
      return Promise.resolve([
        {
          type: MigrationUtils.resourceTypes.BOOKMARKS,
          migrate: () => {},
        },
      ]);
    });

  let didMigration = new Promise(resolve => {
    sandbox
      .stub(SafariProfileMigrator.prototype, "migrate")
      .callsFake((aResourceTypes, aStartup, aProfile, aProgressCallback) => {
        Assert.ok(
          !aStartup,
          "Migrator should not have been called as a startup migration."
        );

        aProgressCallback(MigrationUtils.resourceTypes.BOOKMARKS);
        Services.obs.notifyObservers(null, "Migration:Ended");
        resolve();
      });
  });

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

    let shadow = wizard.openOrClosedShadowRoot;

    info("Choosing Safari");
    let panelItem = wizard.querySelector(
      `panel-item[key="${SafariProfileMigrator.key}"]`
    );
    panelItem.click();

    // Let's just choose "Bookmarks" for now.
    let resourceTypeList = shadow.querySelector("#resource-type-list");
    let resourceNodes = resourceTypeList.querySelectorAll(
      `label[data-resource-type]`
    );
    for (let resourceNode of resourceNodes) {
      resourceNode.control.checked =
        resourceNode.dataset.resourceType ==
        MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.BOOKMARKS;
    }

    let deck = shadow.querySelector("#wizard-deck");
    let switchedToSafariPermissionPage =
      BrowserTestUtils.waitForMutationCondition(
        deck,
        { attributeFilter: ["selected-view"] },
        () => {
          return (
            deck.getAttribute("selected-view") ==
            "page-" + MigrationWizardConstants.PAGES.SAFARI_PERMISSION
          );
        }
      );

    let importButton = shadow.querySelector("#import");
    importButton.click();
    await switchedToSafariPermissionPage;
    Assert.ok(true, "Went to Safari permission page after attempting import.");

    let requestPermissions = shadow.querySelector(
      "#safari-request-permissions"
    );
    requestPermissions.click();
    await didMigration;
    Assert.ok(true, "Completed migration");

    let dialog = prefsWin.document.querySelector("#migrationWizardDialog");
    let doneButton = shadow.querySelector(
      "div[name='page-progress'] .done-button"
    );
    let dialogClosed = BrowserTestUtils.waitForEvent(dialog, "close");

    doneButton.click();
    await dialogClosed;
    await wizardDone;
  });
});