Skip to content

Commit 82af359

Browse files
committed
Persistence file
1 parent ed931cb commit 82af359

File tree

1 file changed

+113
-0
lines changed
  • packages-exp/auth-compat-exp/test/integration/webdriver/static

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
const INDEXED_DB_NAME = 'firebaseLocalStorageDb';
19+
20+
// Save these variables for test utils use below, since some tests may delete them.
21+
const indexedDB = window.indexedDB;
22+
const localStorage = window.localStorage;
23+
const sessionStorage = window.sessionStorage;
24+
25+
export async function clearPersistence() {
26+
sessionStorage.clear();
27+
localStorage.clear();
28+
return dbPromise(indexedDB.deleteDatabase(INDEXED_DB_NAME)).catch(
29+
() => undefined
30+
);
31+
}
32+
33+
export async function localStorageSnap() {
34+
return dumpStorage(localStorage);
35+
}
36+
export async function localStorageSet(dict) {
37+
setInStorage(localStorage, dict);
38+
}
39+
export async function sessionStorageSnap() {
40+
return dumpStorage(sessionStorage);
41+
}
42+
export async function sessionStorageSet(dict) {
43+
setInStorage(sessionStorage, dict);
44+
}
45+
46+
const DB_OBJECTSTORE_NAME = 'firebaseLocalStorage';
47+
48+
export async function indexedDBSnap() {
49+
const db = await dbPromise(indexedDB.open(INDEXED_DB_NAME));
50+
let entries;
51+
try {
52+
const store = db
53+
.transaction([DB_OBJECTSTORE_NAME], 'readonly')
54+
.objectStore(DB_OBJECTSTORE_NAME);
55+
entries = await dbPromise(store.getAll());
56+
} catch {
57+
// May throw if DB_OBJECTSTORE_NAME is never created -- this is normal.
58+
return {};
59+
}
60+
const result = {};
61+
for (const { fbase_key: key, value } of entries) {
62+
result[key] = value;
63+
}
64+
return result;
65+
}
66+
67+
// Mock functions for testing edge cases
68+
export async function makeIndexedDBReadonly() {
69+
IDBObjectStore.prototype.add = IDBObjectStore.prototype.put = () => {
70+
return {
71+
error: 'add/put is disabled for test purposes',
72+
readyState: 'done',
73+
addEventListener(event, listener) {
74+
if (event === 'error') {
75+
void Promise.resolve({}).then(listener);
76+
}
77+
}
78+
};
79+
};
80+
}
81+
82+
function dumpStorage(storage) {
83+
const result = {};
84+
for (let i = 0; i < storage.length; i++) {
85+
const key = storage.key(i);
86+
result[key] = JSON.parse(storage.getItem(key));
87+
}
88+
return result;
89+
}
90+
91+
function setInStorage(storage, dict) {
92+
for (const [key, value] of Object.entries(dict)) {
93+
if (value === undefined) {
94+
storage.removeItem(key);
95+
} else {
96+
storage.setItem(key, JSON.stringify(value));
97+
}
98+
}
99+
}
100+
101+
function dbPromise(dbRequest) {
102+
return new Promise((resolve, reject) => {
103+
dbRequest.addEventListener('success', () => {
104+
resolve(dbRequest.result);
105+
});
106+
dbRequest.addEventListener('error', () => {
107+
reject(dbRequest.error);
108+
});
109+
dbRequest.addEventListener('blocked', () => {
110+
reject(dbRequest.error || 'blocked');
111+
});
112+
});
113+
}

0 commit comments

Comments
 (0)