Skip to content

Commit 22a0ce5

Browse files
committed
FCM Pre Modualization
1 parent ee421d2 commit 22a0ce5

File tree

15 files changed

+699
-267
lines changed

15 files changed

+699
-267
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
async function addPayloadToDb(payload) {
19+
const dbOpenReq = indexedDB.open(TEST_DB);
20+
21+
dbOpenReq.onupgradeneeded = () => {
22+
const db = dbOpenReq.result;
23+
24+
// store creation is a synchronized call
25+
console.log('creating object store...');
26+
db.createObjectStore(BACKGROUND_MESSAGES_OBJECT_STORE, {
27+
keyPath: BACKGROUND_MESSAGES_OBJECT_STORE_PRIMARY_KEY
28+
});
29+
};
30+
31+
dbOpenReq.onsuccess = () => {
32+
const db = dbOpenReq.result;
33+
34+
addPayloadToDbInternal(db, {
35+
...payload,
36+
// ndx is required as the primary key of the store. It doesn't have any other testing purpose
37+
ndx: BACKGROUND_MESSAGES_OBJECT_STORE_DEFAULT_NDX
38+
});
39+
};
40+
}
41+
42+
async function addPayloadToDbInternal(db, payload) {
43+
// onsuccess might race with onupgradeneeded. Consequently causing "object stores was not found" error. Therefore, wait briefly for db.createObjectStore to complete
44+
const delay = ms => new Promise(res => setTimeout(res, ms));
45+
await delay(/* milliseconds= */ 30000);
46+
47+
tx = db.transaction(BACKGROUND_MESSAGES_OBJECT_STORE, 'readwrite');
48+
49+
console.log('adding message payload to db: ' + JSON.stringify(payload));
50+
addReq = tx.objectStore(BACKGROUND_MESSAGES_OBJECT_STORE).add(payload);
51+
}

integration/messaging/test/static/sw-base.js

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
importScripts('../constants.js');
19+
importScripts('../helpers.js');
1920

2021
// HEAD targets served through express
2122
importScripts('/firebase-app.js');
@@ -27,45 +28,10 @@ const messaging = firebase.messaging();
2728
messaging.setBackgroundMessageHandler(payload => {
2829
console.log(
2930
TAG +
30-
'a background message is received: ' +
31+
'a background message is received in the background handler hook: ' +
3132
JSON.stringify(payload) +
3233
'. Storing it into idb for tests to read...'
3334
);
3435

3536
addPayloadToDb(payload);
3637
});
37-
38-
async function addPayloadToDb(payload) {
39-
const dbOpenReq = indexedDB.open(TEST_DB);
40-
41-
dbOpenReq.onupgradeneeded = () => {
42-
const db = dbOpenReq.result;
43-
44-
// store creation is a synchronized call
45-
console.log('creating object store...');
46-
db.createObjectStore(BACKGROUND_MESSAGES_OBJECT_STORE, {
47-
keyPath: BACKGROUND_MESSAGES_OBJECT_STORE_PRIMARY_KEY
48-
});
49-
};
50-
51-
dbOpenReq.onsuccess = () => {
52-
const db = dbOpenReq.result;
53-
54-
addPayloadToDbInternal(db, {
55-
...payload,
56-
// ndx is required as the primary key of the store. It doesn't have any other testing purpose
57-
ndx: BACKGROUND_MESSAGES_OBJECT_STORE_DEFAULT_NDX
58-
});
59-
};
60-
}
61-
62-
async function addPayloadToDbInternal(db, payload) {
63-
// onsuccess might race with onupgradeneeded. Consequently causing " object stores was not found" error. Therefore, wait briefly for db.createObjectStore to complete
64-
const delay = ms => new Promise(res => setTimeout(res, ms));
65-
await delay(/* milliseconds= */ 30000);
66-
67-
tx = db.transaction(BACKGROUND_MESSAGES_OBJECT_STORE, 'readwrite');
68-
69-
console.log('adding message payload to db: ' + JSON.stringify(payload));
70-
addReq = tx.objectStore(BACKGROUND_MESSAGES_OBJECT_STORE).add(payload);
71-
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<html>
2+
<head>
3+
<title>FCM Demo</title>
4+
<meta name="viewport" content="width=device-width,initial-scale=1" />
5+
</head>
6+
<body>
7+
<h1>Valid <strong>WITH</strong> VAPID Key - Modern SW</h1>
8+
9+
<script src="/firebase-app.js"></script>
10+
<script src="/firebase-messaging.js"></script>
11+
<script src="../app.js"></script>
12+
<script src="../constants.js"></script>
13+
<script src="https://cdnjs.cloudflare.com/ajax/libs/sinon.js/4.1.3/sinon.min.js"></script>
14+
<script>
15+
navigator.serviceWorker
16+
.register('./sw.js')
17+
.then(reg => {
18+
window.__test = new window.DemoApp(FIREBASE_CONFIG, {
19+
swReg: reg,
20+
vapidKey: PUBLIC_VAPID_KEY
21+
});
22+
})
23+
.catch(error => {
24+
console.log('Error registering FCM SW: ' + error);
25+
});
26+
</script>
27+
</body>
28+
</html>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @license
3+
* Copyright 2017 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+
importScripts('../constants.js');
19+
importScripts('../helpers.js');
20+
21+
// HEAD targets served through express
22+
importScripts('/firebase-app.js');
23+
importScripts('/firebase-messaging.js');
24+
25+
firebase.initializeApp(FIREBASE_CONFIG);
26+
const messaging = firebase.messaging();
27+
28+
messaging.onBackgroundMessage(payload => {
29+
console.log(
30+
TAG +
31+
'a background message is received in the onBackgroundMessage hook: ' +
32+
JSON.stringify(payload) +
33+
'. Storing it into idb for tests to read...'
34+
);
35+
36+
addPayloadToDb(payload);
37+
});

integration/messaging/test/test-send.js

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const getReceivedBackgroundMessages = require('./utils/getReceivedBackgroundMess
2424
const openNewTab = require('./utils/openNewTab');
2525
const createPermittedWebDriver = require('./utils/createPermittedWebDriver');
2626

27-
const TEST_DOMAIN = 'valid-vapid-key';
27+
const TEST_DOMAINS = ['valid-vapid-key', 'valid-vapid-key-modern-sw'];
2828
const TEST_PROJECT_SENDER_ID = '750970317741';
2929
const DEFAULT_COLLAPSE_KEY_VALUE = 'do_not_collapse';
3030
const FIELD_FROM = 'from';
@@ -58,69 +58,71 @@ describe('Starting Integration Test > Sending and Receiving ', function() {
5858
return;
5959
}
6060

61-
describe(`Testing browser: ${assistantBrowser.getPrettyName()} : ${TEST_DOMAIN}`, function() {
62-
before(async function() {
63-
globalWebDriver = createPermittedWebDriver(
64-
/* browser= */ assistantBrowser.getId()
65-
);
66-
});
67-
68-
it('Background app can receive a {} empty message from sw', async function() {
69-
this.timeout(TIMEOUT_BACKGROUND_MESSAGE_TEST_UNIT_MILLISECONDS);
70-
71-
// Clearing the cache and db data by killing the previously instantiated driver. Note that ideally this call is placed inside the after/before hooks. However, Mocha forbids operations longer than 2s in hooks. Hence, this clearing call needs to be inside the test unit.
72-
await seleniumAssistant.killWebDriver(globalWebDriver);
73-
74-
globalWebDriver = createPermittedWebDriver(
75-
/* browser= */ assistantBrowser.getId()
76-
);
77-
78-
prepareBackgroundApp(globalWebDriver);
79-
80-
checkSendResponse(
81-
await sendMessage({
82-
to: await retrieveToken(globalWebDriver)
83-
})
84-
);
85-
86-
await wait(
87-
WAIT_TIME_BEFORE_RETRIEVING_BACKGROUND_MESSAGES_MILLISECONDS
88-
);
89-
90-
checkMessageReceived(
91-
await getReceivedBackgroundMessages(globalWebDriver),
92-
/* expectedNotificationPayload= */ null,
93-
/* expectedDataPayload= */ null
94-
);
95-
});
96-
97-
it('Background app can receive a {"data"} message frow sw', async function() {
98-
this.timeout(TIMEOUT_BACKGROUND_MESSAGE_TEST_UNIT_MILLISECONDS);
99-
100-
await seleniumAssistant.killWebDriver(globalWebDriver);
101-
102-
globalWebDriver = createPermittedWebDriver(
103-
/* browser= */ assistantBrowser.getId()
104-
);
105-
106-
prepareBackgroundApp(globalWebDriver);
107-
108-
checkSendResponse(
109-
await sendMessage({
110-
to: await retrieveToken(globalWebDriver),
111-
data: getTestDataPayload()
112-
})
113-
);
114-
115-
await wait(
116-
WAIT_TIME_BEFORE_RETRIEVING_BACKGROUND_MESSAGES_MILLISECONDS
117-
);
118-
119-
checkMessageReceived(
120-
await getReceivedBackgroundMessages(globalWebDriver),
121-
/* expectedNotificationPayload= */ null,
122-
/* expectedDataPayload= */ getTestDataPayload()
123-
);
61+
TEST_DOMAINS.forEach(domain => {
62+
describe(`Testing browser: ${assistantBrowser.getPrettyName()} : ${domain}`, function() {
63+
before(async function() {
64+
globalWebDriver = createPermittedWebDriver(
65+
/* browser= */ assistantBrowser.getId()
66+
);
67+
});
68+
69+
it('Background app can receive a {} empty message from sw', async function() {
70+
this.timeout(TIMEOUT_BACKGROUND_MESSAGE_TEST_UNIT_MILLISECONDS);
71+
72+
// Clearing the cache and db data by killing the previously instantiated driver. Note that ideally this call is placed inside the after/before hooks. However, Mocha forbids operations longer than 2s in hooks. Hence, this clearing call needs to be inside the test unit.
73+
await seleniumAssistant.killWebDriver(globalWebDriver);
74+
75+
globalWebDriver = createPermittedWebDriver(
76+
/* browser= */ assistantBrowser.getId()
77+
);
78+
79+
prepareBackgroundApp(globalWebDriver, domain);
80+
81+
checkSendResponse(
82+
await sendMessage({
83+
to: await retrieveToken(globalWebDriver)
84+
})
85+
);
86+
87+
await wait(
88+
WAIT_TIME_BEFORE_RETRIEVING_BACKGROUND_MESSAGES_MILLISECONDS
89+
);
90+
91+
checkMessageReceived(
92+
await getReceivedBackgroundMessages(globalWebDriver),
93+
/* expectedNotificationPayload= */ null,
94+
/* expectedDataPayload= */ null
95+
);
96+
});
97+
98+
it('Background app can receive a {"data"} message frow sw', async function() {
99+
this.timeout(TIMEOUT_BACKGROUND_MESSAGE_TEST_UNIT_MILLISECONDS);
100+
101+
await seleniumAssistant.killWebDriver(globalWebDriver);
102+
103+
globalWebDriver = createPermittedWebDriver(
104+
/* browser= */ assistantBrowser.getId()
105+
);
106+
107+
prepareBackgroundApp(globalWebDriver, domain);
108+
109+
checkSendResponse(
110+
await sendMessage({
111+
to: await retrieveToken(globalWebDriver),
112+
data: getTestDataPayload()
113+
})
114+
);
115+
116+
await wait(
117+
WAIT_TIME_BEFORE_RETRIEVING_BACKGROUND_MESSAGES_MILLISECONDS
118+
);
119+
120+
checkMessageReceived(
121+
await getReceivedBackgroundMessages(globalWebDriver),
122+
/* expectedNotificationPayload= */ null,
123+
/* expectedDataPayload= */ getTestDataPayload()
124+
);
125+
});
124126
});
125127
});
126128
});
@@ -168,8 +170,8 @@ function getTestDataPayload() {
168170
return { hello: 'world' };
169171
}
170172

171-
async function prepareBackgroundApp(globalWebDriver) {
172-
await globalWebDriver.get(`${testServer.serverAddress}/${TEST_DOMAIN}/`);
173+
async function prepareBackgroundApp(globalWebDriver, domain) {
174+
await globalWebDriver.get(`${testServer.serverAddress}/${domain}/`);
173175

174176
// TODO: remove the try/catch block once the underlying bug has been resolved.
175177
// Shift window focus away from app window so that background messages can be received/processed

0 commit comments

Comments
 (0)