|
| 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 | +const testServer = require('./utils/test-server'); |
| 19 | +const sendMessage = require('./utils/sendMessage'); |
| 20 | +const retrieveToken = require('./utils/retrieveToken'); |
| 21 | +const seleniumAssistant = require('selenium-assistant'); |
| 22 | +const getReceivedBackgroundMessages = require('./utils/getReceivedBackgroundMessages'); |
| 23 | +const createPermittedWebDriver = require('./utils/createPermittedWebDriver'); |
| 24 | +const checkSendResponse = require('./utils/checkSendResponse'); |
| 25 | +const checkMessageReceived = require('./utils/checkMessageReceived'); |
| 26 | +const openNewTab = require('./utils/openNewTab'); |
| 27 | + |
| 28 | +const TEST_DOMAINS = ['valid-vapid-key-modern-sw']; |
| 29 | + |
| 30 | +// 4 minutes. The fact that the flow includes making a request to the Send Service, |
| 31 | +// storing/retrieving form indexedDb asynchronously makes these test units to have a execution time |
| 32 | +// variance. Therefore, allowing these units to have a longer time to work is crucial. |
| 33 | +const TIMEOUT_BACKGROUND_MESSAGE_TEST_UNIT_MILLISECONDS = 240000; |
| 34 | + |
| 35 | +// 1 minute. Wait for object store to be created and received message to be stored in idb. This |
| 36 | +// waiting time MUST be longer than the wait time for adding to db in the sw. |
| 37 | +const WAIT_TIME_BEFORE_RETRIEVING_BACKGROUND_MESSAGES_MILLISECONDS = 60000; |
| 38 | + |
| 39 | +const wait = ms => new Promise(res => setTimeout(res, ms)); |
| 40 | + |
| 41 | +// 50% of integration test run time is spent in testing receiving in background. Running these |
| 42 | +// slower tests last so other tests can fail quickly if they do fail. |
| 43 | +require('./test-token-delete'); |
| 44 | +require('./test-token-update'); |
| 45 | +require('./test-useValidManifest'); |
| 46 | +require('./test-useDefaultServiceWorker'); |
| 47 | +require('./test-receive-foreground'); |
| 48 | + |
| 49 | +describe('Firebase Messaging Integration Tests > Test Background Receive', function () { |
| 50 | + this.retries(2); |
| 51 | + let globalWebDriver; |
| 52 | + |
| 53 | + before(async function () { |
| 54 | + await testServer.start(); |
| 55 | + }); |
| 56 | + |
| 57 | + after(async function () { |
| 58 | + await testServer.stop(); |
| 59 | + }); |
| 60 | + |
| 61 | + // TODO: enable testing for firefox |
| 62 | + seleniumAssistant.getLocalBrowsers().forEach(assistantBrowser => { |
| 63 | + if (assistantBrowser.getId() !== 'chrome') { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + TEST_DOMAINS.forEach(domain => { |
| 68 | + describe(`Testing browser: ${assistantBrowser.getPrettyName()} : ${domain}`, function () { |
| 69 | + before(async function () { |
| 70 | + globalWebDriver = createPermittedWebDriver( |
| 71 | + /* browser= */ assistantBrowser.getId() |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it('Background app can receive a {} empty message from sw', async function () { |
| 76 | + this.timeout(TIMEOUT_BACKGROUND_MESSAGE_TEST_UNIT_MILLISECONDS); |
| 77 | + |
| 78 | + // Clearing the cache and db data by killing the previously instantiated driver. Note that |
| 79 | + // ideally this call is placed inside the after/before hooks. However, Mocha forbids |
| 80 | + // operations longer than 2s in hooks. Hence, this clearing call needs to be inside the |
| 81 | + // test unit. |
| 82 | + await seleniumAssistant.killWebDriver(globalWebDriver); |
| 83 | + |
| 84 | + globalWebDriver = createPermittedWebDriver( |
| 85 | + /* browser= */ assistantBrowser.getId() |
| 86 | + ); |
| 87 | + |
| 88 | + prepareBackgroundApp(globalWebDriver, domain); |
| 89 | + |
| 90 | + checkSendResponse( |
| 91 | + await sendMessage({ |
| 92 | + to: await retrieveToken(globalWebDriver) |
| 93 | + }) |
| 94 | + ); |
| 95 | + |
| 96 | + await wait( |
| 97 | + WAIT_TIME_BEFORE_RETRIEVING_BACKGROUND_MESSAGES_MILLISECONDS |
| 98 | + ); |
| 99 | + |
| 100 | + checkMessageReceived( |
| 101 | + await getReceivedBackgroundMessages(globalWebDriver), |
| 102 | + /* expectedNotificationPayload= */ null, |
| 103 | + /* expectedDataPayload= */ null, |
| 104 | + /* isLegacyPayload= */ false |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + it('Background app can receive a {"data"} message frow sw', async function () { |
| 109 | + this.timeout(TIMEOUT_BACKGROUND_MESSAGE_TEST_UNIT_MILLISECONDS); |
| 110 | + |
| 111 | + await seleniumAssistant.killWebDriver(globalWebDriver); |
| 112 | + |
| 113 | + globalWebDriver = createPermittedWebDriver( |
| 114 | + /* browser= */ assistantBrowser.getId() |
| 115 | + ); |
| 116 | + |
| 117 | + prepareBackgroundApp(globalWebDriver, domain); |
| 118 | + |
| 119 | + checkSendResponse( |
| 120 | + await sendMessage({ |
| 121 | + to: await retrieveToken(globalWebDriver), |
| 122 | + data: getTestDataPayload() |
| 123 | + }) |
| 124 | + ); |
| 125 | + |
| 126 | + await wait( |
| 127 | + WAIT_TIME_BEFORE_RETRIEVING_BACKGROUND_MESSAGES_MILLISECONDS |
| 128 | + ); |
| 129 | + |
| 130 | + checkMessageReceived( |
| 131 | + await getReceivedBackgroundMessages(globalWebDriver), |
| 132 | + /* expectedNotificationPayload= */ null, |
| 133 | + /* expectedDataPayload= */ getTestDataPayload() |
| 134 | + ); |
| 135 | + }); |
| 136 | + }); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
| 140 | + |
| 141 | +async function prepareBackgroundApp(globalWebDriver, domain) { |
| 142 | + await globalWebDriver.get(`${testServer.serverAddress}/${domain}/`); |
| 143 | + // TODO: remove the try/catch block once the underlying bug has been resolved. Shift window focus |
| 144 | + // away from app window so that background messages can be received/processed |
| 145 | + try { |
| 146 | + await openNewTab(globalWebDriver); |
| 147 | + } catch (err) { |
| 148 | + // ChromeDriver seems to have an open bug which throws "JavascriptError: javascript error: |
| 149 | + // circular reference". Nevertheless, a new tab can still be opened. Hence, just catch and |
| 150 | + // continue here. |
| 151 | + console.log('FCM (ignored on purpose): ' + err); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +function getTestDataPayload() { |
| 156 | + return { hello: 'world' }; |
| 157 | +} |
0 commit comments