working with tests
This commit is contained in:
parent
8f0c2bbbad
commit
e41dccab3d
5 changed files with 107 additions and 44 deletions
|
@ -11,6 +11,10 @@ const identityState = {
|
|||
const storeKey = this.getContainerStoreKey(cookieStoreId);
|
||||
const storageResponse = await this.area.get([storeKey]);
|
||||
if (storageResponse && storeKey in storageResponse) {
|
||||
if (!storageResponse[storeKey].macAddonUUID){
|
||||
await identityState.addUUID(cookieStoreId);
|
||||
return this.get(cookieStoreId);
|
||||
}
|
||||
return storageResponse[storeKey];
|
||||
}
|
||||
const identities = await browser.contextualIdentities.query({});
|
||||
|
@ -91,7 +95,7 @@ const identityState = {
|
|||
const containerState = await this.storageArea.get(cookieStoreId);
|
||||
containerState.macAddonUUID = uuid;
|
||||
await this.storageArea.set(cookieStoreId, containerState);
|
||||
return;
|
||||
return uuid;
|
||||
}
|
||||
throw new Error ("cookieStoreId or uuid missing");
|
||||
},
|
||||
|
|
|
@ -37,7 +37,7 @@ const sync = {
|
|||
},
|
||||
|
||||
async backup(options) {
|
||||
console.log("backup");
|
||||
if (SYNC_DEBUG) console.log("backup");
|
||||
// remove listeners to avoid an infinite loop!
|
||||
browser.storage.onChanged.removeListener(sync.storageArea.onChangedListener);
|
||||
removeContextualIdentityListeners(syncCIListenerList);
|
||||
|
@ -281,51 +281,32 @@ async function reconcileSiteAssignments() {
|
|||
|
||||
for(const urlKey of Object.keys(assignedSitesFromSync)) {
|
||||
const assignedSite = assignedSitesFromSync[urlKey];
|
||||
if (assignedSitesLocal.hasOwnProperty(urlKey)) {
|
||||
const syncUUID =
|
||||
await lookupSyncSiteAssigmentIdentityUUID(
|
||||
assignedSite, cookieStoreIDmap, urlKey
|
||||
);
|
||||
|
||||
const localIdentityUUID =
|
||||
await lookupLocalSiteAssignmentIdentityUUID(urlKey);
|
||||
|
||||
if (syncUUID === localIdentityUUID) {
|
||||
continue;
|
||||
}
|
||||
// overwrite with Sync data. Sync is the source of truth
|
||||
await setAssignmentWithUUID(syncUUID, assignedSite, urlKey);
|
||||
const syncUUID =
|
||||
await lookupSyncSiteAssigmentIdentityUUID(
|
||||
assignedSite, cookieStoreIDmap, urlKey
|
||||
);
|
||||
if (syncUUID) {
|
||||
// Sync is truth.
|
||||
// Not even looking it up. Just overwrite
|
||||
console.log("new assignment ", assignedSite, ": ",
|
||||
assignedSite.userContextId);
|
||||
const newUUID = cookieStoreIDmap[
|
||||
"firefox-container-" + assignedSite.userContextId
|
||||
];
|
||||
await setAssignmentWithUUID(newUUID, assignedSite, urlKey);
|
||||
continue;
|
||||
}
|
||||
console.log("new assignment ", assignedSite, ": ",
|
||||
assignedSite.userContextId);
|
||||
const newUUID = cookieStoreIDmap[
|
||||
"firefox-container-" + assignedSite.userContextId
|
||||
];
|
||||
await setAssignmentWithUUID(newUUID, assignedSite, urlKey);
|
||||
}
|
||||
|
||||
async function lookupLocalSiteAssignmentIdentityUUID(urlKey){
|
||||
const localAssignedSite =
|
||||
await assignManager.storageArea.getByUrlKey(urlKey);
|
||||
if (!localAssignedSite || !localAssignedSite.userContextId)
|
||||
throw new Error (urlKey, "userContextId does not exist");
|
||||
const localCookieStoreId = "firefox-container-" +
|
||||
localAssignedSite.userContextId;
|
||||
return await identityState.storageArea
|
||||
.get(localCookieStoreId).macAddonUUID;
|
||||
// if there's no syncUUID, something is wrong, since these site
|
||||
// assignments are from sync
|
||||
throw new Error("Sync storage not aligned");
|
||||
}
|
||||
|
||||
async function lookupSyncSiteAssigmentIdentityUUID(
|
||||
assignedSite,
|
||||
cookieStoreIDmap,
|
||||
urlKey
|
||||
){
|
||||
if (!assignedSite.userContextId)
|
||||
throw new Error (`${urlKey} userContextId does not exist`);
|
||||
const syncCookieStoreId = "firefox-container-" + assignedSite.userContextId;
|
||||
if (!cookieStoreIDmap[syncCookieStoreId])
|
||||
throw new Error (syncCookieStoreId, " does not have a uuid");
|
||||
return cookieStoreIDmap[syncCookieStoreId];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@ describe("External Webextensions", () => {
|
|||
const [promise] = background.browser.runtime.onMessageExternal.addListener.yield(message, sender);
|
||||
const answer = await promise;
|
||||
expect(answer).to.deep.equal({
|
||||
hostname: "example.com",
|
||||
userContextId: "1",
|
||||
neverAsk: false
|
||||
});
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
describe("Sync", () => {
|
||||
let tab;
|
||||
beforeEach(async () => {
|
||||
tab = await helper.browser.initializeWithTab();
|
||||
});
|
||||
|
||||
it.only("should init sync on startup", async () => {
|
||||
console.log("!!!a")
|
||||
const tab = await helper.browser.initializeWithTab();
|
||||
console.log(await background.browser.storage.local.get());
|
||||
const mozContainer = await background.browser.contextualIdentities.create({
|
||||
name: "Mozilla",
|
||||
color: "red",
|
||||
|
@ -55,12 +54,55 @@ describe("Sync", () => {
|
|||
});
|
||||
}
|
||||
}));
|
||||
|
||||
console.log("!!!c");
|
||||
await background.browser.runtime.onStartup.addListener.yield();
|
||||
await nextTick();
|
||||
|
||||
const sync = await background.browser.storage.sync.get();
|
||||
console.log(await background.browser.storage.local.get());
|
||||
|
||||
expect(sync.identities.length).to.equal(5);
|
||||
console.log("!!!b");
|
||||
});
|
||||
});
|
||||
|
||||
it("should sync for the first time", async () => {
|
||||
const mozContainer = await background.browser.contextualIdentities.create({
|
||||
name:"Test",
|
||||
color:"green",
|
||||
icon:"pet"
|
||||
});
|
||||
console.log(await background.browser.contextualIdentities.query({}));
|
||||
await helper.browser.initSyncTest({localStorage:SYNC_TEST_1_LOCAL});
|
||||
console.log(await background.browser.storage.local.get());
|
||||
for (const containerName of SYNC_TEST_CONTAINERS) {
|
||||
const storageKeyString = "identitiesState@@_" + containerName;
|
||||
const answer = await background.browser.storage.local.get(storageKeyString);
|
||||
expect(answer[storageKeyString].hasOwnProperty("macAddonUUID")).to.be.true;
|
||||
}
|
||||
const storageKeyString = "identitiesState@@_" + mozContainer.cookieStoreId;
|
||||
const answer = await background.browser.storage.local.get(storageKeyString);
|
||||
expect(answer[storageKeyString].hasOwnProperty("macAddonUUID")).to.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
const SYNC_TEST_1_LOCAL = {
|
||||
"browserActionBadgesClicked":["6.1.1"],
|
||||
"containerTabsOpened":6,
|
||||
"identitiesState@@_firefox-container-1":{"hiddenTabs":[]},
|
||||
"identitiesState@@_firefox-container-2":{"hiddenTabs":[]},
|
||||
"identitiesState@@_firefox-container-3":{"hiddenTabs":[]},
|
||||
"identitiesState@@_firefox-container-4":{"hiddenTabs":[]},
|
||||
"identitiesState@@_firefox-container-6":{"hiddenTabs":[]},
|
||||
"identitiesState@@_firefox-default":{"hiddenTabs":[]},
|
||||
"onboarding-stage":5,
|
||||
"siteContainerMap@@_twitter.com":{"userContextId":"1","neverAsk":true},
|
||||
"siteContainerMap@@_www.facebook.com":{"userContextId":"2","neverAsk":true},
|
||||
"siteContainerMap@@_www.linkedin.com":{"userContextId":"4","neverAsk":false}
|
||||
};
|
||||
|
||||
const SYNC_TEST_CONTAINERS = [
|
||||
"firefox-container-1",
|
||||
"firefox-container-2",
|
||||
"firefox-container-3",
|
||||
"firefox-container-4"
|
||||
];
|
|
@ -19,6 +19,7 @@ module.exports = {
|
|||
"achievements": []
|
||||
});
|
||||
window.browser.storage.local.set.resetHistory();
|
||||
window.browser.storage.sync.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +30,41 @@ module.exports = {
|
|||
|
||||
async openNewTab(tab, options = {}) {
|
||||
return background.browser.tabs._create(tab, options);
|
||||
}
|
||||
},
|
||||
|
||||
async initSyncTest(details = {}) {
|
||||
if (!details.cookieStoreId) details.cookieStoreId = "firefox-default";
|
||||
if (!details.localStorage) {
|
||||
details.localStorage = {
|
||||
"browserActionBadgesClicked": [],
|
||||
"onboarding-stage": 5,
|
||||
"achievements": []
|
||||
};
|
||||
}
|
||||
if (!details.syncStorage) details.syncStorage = {};
|
||||
let tab;
|
||||
await buildDom({
|
||||
background: {
|
||||
async afterBuild(background) {
|
||||
tab = await background.browser.tabs._create({ cookieStoreId: details.cookieStoreId });
|
||||
}
|
||||
},
|
||||
popup: {
|
||||
jsdom: {
|
||||
beforeParse(window) {
|
||||
window.browser.storage.clear();
|
||||
window.browser.storage.local.set(details.localStorage);
|
||||
window.browser.storage.local.set.resetHistory();
|
||||
window.browser.storage.sync.clear();
|
||||
window.browser.storage.sync.set(details.syncStorage);
|
||||
window.browser.storage.sync.set.resetHistory();
|
||||
}
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
return tab;
|
||||
},
|
||||
},
|
||||
|
||||
popup: {
|
||||
|
|
Loading…
Add table
Reference in a new issue