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 storeKey = this.getContainerStoreKey(cookieStoreId);
|
||||||
const storageResponse = await this.area.get([storeKey]);
|
const storageResponse = await this.area.get([storeKey]);
|
||||||
if (storageResponse && storeKey in storageResponse) {
|
if (storageResponse && storeKey in storageResponse) {
|
||||||
|
if (!storageResponse[storeKey].macAddonUUID){
|
||||||
|
await identityState.addUUID(cookieStoreId);
|
||||||
|
return this.get(cookieStoreId);
|
||||||
|
}
|
||||||
return storageResponse[storeKey];
|
return storageResponse[storeKey];
|
||||||
}
|
}
|
||||||
const identities = await browser.contextualIdentities.query({});
|
const identities = await browser.contextualIdentities.query({});
|
||||||
|
@ -91,7 +95,7 @@ const identityState = {
|
||||||
const containerState = await this.storageArea.get(cookieStoreId);
|
const containerState = await this.storageArea.get(cookieStoreId);
|
||||||
containerState.macAddonUUID = uuid;
|
containerState.macAddonUUID = uuid;
|
||||||
await this.storageArea.set(cookieStoreId, containerState);
|
await this.storageArea.set(cookieStoreId, containerState);
|
||||||
return;
|
return uuid;
|
||||||
}
|
}
|
||||||
throw new Error ("cookieStoreId or uuid missing");
|
throw new Error ("cookieStoreId or uuid missing");
|
||||||
},
|
},
|
||||||
|
|
|
@ -37,7 +37,7 @@ const sync = {
|
||||||
},
|
},
|
||||||
|
|
||||||
async backup(options) {
|
async backup(options) {
|
||||||
console.log("backup");
|
if (SYNC_DEBUG) console.log("backup");
|
||||||
// remove listeners to avoid an infinite loop!
|
// remove listeners to avoid an infinite loop!
|
||||||
browser.storage.onChanged.removeListener(sync.storageArea.onChangedListener);
|
browser.storage.onChanged.removeListener(sync.storageArea.onChangedListener);
|
||||||
removeContextualIdentityListeners(syncCIListenerList);
|
removeContextualIdentityListeners(syncCIListenerList);
|
||||||
|
@ -281,51 +281,32 @@ async function reconcileSiteAssignments() {
|
||||||
|
|
||||||
for(const urlKey of Object.keys(assignedSitesFromSync)) {
|
for(const urlKey of Object.keys(assignedSitesFromSync)) {
|
||||||
const assignedSite = assignedSitesFromSync[urlKey];
|
const assignedSite = assignedSitesFromSync[urlKey];
|
||||||
if (assignedSitesLocal.hasOwnProperty(urlKey)) {
|
const syncUUID =
|
||||||
const syncUUID =
|
await lookupSyncSiteAssigmentIdentityUUID(
|
||||||
await lookupSyncSiteAssigmentIdentityUUID(
|
assignedSite, cookieStoreIDmap, urlKey
|
||||||
assignedSite, cookieStoreIDmap, urlKey
|
);
|
||||||
);
|
if (syncUUID) {
|
||||||
|
// Sync is truth.
|
||||||
const localIdentityUUID =
|
// Not even looking it up. Just overwrite
|
||||||
await lookupLocalSiteAssignmentIdentityUUID(urlKey);
|
console.log("new assignment ", assignedSite, ": ",
|
||||||
|
assignedSite.userContextId);
|
||||||
if (syncUUID === localIdentityUUID) {
|
const newUUID = cookieStoreIDmap[
|
||||||
continue;
|
"firefox-container-" + assignedSite.userContextId
|
||||||
}
|
];
|
||||||
// overwrite with Sync data. Sync is the source of truth
|
await setAssignmentWithUUID(newUUID, assignedSite, urlKey);
|
||||||
await setAssignmentWithUUID(syncUUID, assignedSite, urlKey);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
console.log("new assignment ", assignedSite, ": ",
|
|
||||||
assignedSite.userContextId);
|
|
||||||
const newUUID = cookieStoreIDmap[
|
|
||||||
"firefox-container-" + assignedSite.userContextId
|
|
||||||
];
|
|
||||||
await setAssignmentWithUUID(newUUID, assignedSite, urlKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function lookupLocalSiteAssignmentIdentityUUID(urlKey){
|
// if there's no syncUUID, something is wrong, since these site
|
||||||
const localAssignedSite =
|
// assignments are from sync
|
||||||
await assignManager.storageArea.getByUrlKey(urlKey);
|
throw new Error("Sync storage not aligned");
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function lookupSyncSiteAssigmentIdentityUUID(
|
async function lookupSyncSiteAssigmentIdentityUUID(
|
||||||
assignedSite,
|
assignedSite,
|
||||||
cookieStoreIDmap,
|
cookieStoreIDmap,
|
||||||
urlKey
|
|
||||||
){
|
){
|
||||||
if (!assignedSite.userContextId)
|
|
||||||
throw new Error (`${urlKey} userContextId does not exist`);
|
|
||||||
const syncCookieStoreId = "firefox-container-" + assignedSite.userContextId;
|
const syncCookieStoreId = "firefox-container-" + assignedSite.userContextId;
|
||||||
if (!cookieStoreIDmap[syncCookieStoreId])
|
|
||||||
throw new Error (syncCookieStoreId, " does not have a uuid");
|
|
||||||
return cookieStoreIDmap[syncCookieStoreId];
|
return cookieStoreIDmap[syncCookieStoreId];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@ describe("External Webextensions", () => {
|
||||||
const [promise] = background.browser.runtime.onMessageExternal.addListener.yield(message, sender);
|
const [promise] = background.browser.runtime.onMessageExternal.addListener.yield(message, sender);
|
||||||
const answer = await promise;
|
const answer = await promise;
|
||||||
expect(answer).to.deep.equal({
|
expect(answer).to.deep.equal({
|
||||||
|
hostname: "example.com",
|
||||||
userContextId: "1",
|
userContextId: "1",
|
||||||
neverAsk: false
|
neverAsk: false
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
describe("Sync", () => {
|
describe("Sync", () => {
|
||||||
let tab;
|
|
||||||
beforeEach(async () => {
|
|
||||||
tab = await helper.browser.initializeWithTab();
|
|
||||||
});
|
|
||||||
|
|
||||||
it.only("should init sync on startup", async () => {
|
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({
|
const mozContainer = await background.browser.contextualIdentities.create({
|
||||||
name: "Mozilla",
|
name: "Mozilla",
|
||||||
color: "red",
|
color: "red",
|
||||||
|
@ -55,12 +54,55 @@ describe("Sync", () => {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
console.log("!!!c");
|
||||||
await background.browser.runtime.onStartup.addListener.yield();
|
await background.browser.runtime.onStartup.addListener.yield();
|
||||||
await nextTick();
|
await nextTick();
|
||||||
|
|
||||||
const sync = await background.browser.storage.sync.get();
|
const sync = await background.browser.storage.sync.get();
|
||||||
|
console.log(await background.browser.storage.local.get());
|
||||||
|
|
||||||
expect(sync.identities.length).to.equal(5);
|
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": []
|
"achievements": []
|
||||||
});
|
});
|
||||||
window.browser.storage.local.set.resetHistory();
|
window.browser.storage.local.set.resetHistory();
|
||||||
|
window.browser.storage.sync.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,7 +30,41 @@ module.exports = {
|
||||||
|
|
||||||
async openNewTab(tab, options = {}) {
|
async openNewTab(tab, options = {}) {
|
||||||
return background.browser.tabs._create(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: {
|
popup: {
|
||||||
|
|
Loading…
Add table
Reference in a new issue