summaryrefslogtreecommitdiff
path: root/tests/compliancetests/src/com/android/cellbroadcastreceiver/compliancetests/CellBroadcastUiTest.java
blob: f0d28f709f0454f3910f8890889c3a9c0d81af5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.cellbroadcastreceiver.compliancetests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;

import android.app.KeyguardManager;
import android.app.LocaleManager;
import android.app.UiAutomation;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.LocaleList;
import android.provider.Settings;
import android.provider.Telephony;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiScrollable;
import android.support.test.uiautomator.UiSelector;
import android.support.test.uiautomator.Until;
import android.text.TextUtils;
import android.widget.LinearLayout;

import com.android.internal.util.HexDump;

import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Iterator;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;

@RunWith(JUnitParamsRunner.class)
public class CellBroadcastUiTest extends CellBroadcastBaseTest {
    private static final String TAG = "CellBroadcastUiTest";
    private static final int UI_TIMEOUT = 10000;
    private static final Uri CONTENT_URI = Uri.parse("content://cellbroadcasts");
    private static final String SELECT_BY_SERIAL_NUMBER =
            Telephony.CellBroadcasts.SERIAL_NUMBER + "=?";
    private static int sSerialId = 0;
    /** Bitmask for messages of ETWS type (including future extensions). */
    private static final int MESSAGE_ID_ETWS_TYPE_MASK = 0xFFF8;
    /** Value for messages of ETWS type after applying {@link #MESSAGE_ID_ETWS_TYPE_MASK}. */
    private static final int MESSAGE_ID_ETWS_TYPE = 0x1100; // 4352
    private static final String CELL_BROADCAST_LIST_ACTIVITY =
            "com.android.cellbroadcastreceiver.CellBroadcastSettings";
    private static final BySelector FULL_SCREEN_DIALOG =
            By.res("android:id/immersive_cling_title");
    private static final BySelector CLOSE_BUTTON =
            By.res("android:id/ok");

    private boolean mIsOptOutDialogHandled = false;

    @Before
    public void beforeTest() throws Exception {
        super.beforeTest();

        if ("testEmergencyAlertSettingsUi".equals(mTestNameRule.getMethodName())
                || "testAlertUiOnReceivedAlert".equals(mTestNameRule.getMethodName())) {
            KeyguardManager keyguardManager = getContext().getSystemService(KeyguardManager.class);
            assumeTrue("cannot test under secure keyguard",
                    keyguardManager != null && !keyguardManager.isKeyguardSecure());
            // dismiss keyguard and wait from idle
            if (keyguardManager != null && keyguardManager.isKeyguardLocked()) {
                dismissKeyGuard();
            }
        }
        if ("testAlertUiOnReceivedAlert".equals(mTestNameRule.getMethodName())) {
            PackageManager pm = getContext().getPackageManager();
            assumeTrue("FULL_ACCESS_CELL_BROADCAST_HISTORY permission "
                    + "is necessary for this test", pm.checkPermission(
                    "com.android.cellbroadcastservice.FULL_ACCESS_CELL_BROADCAST_HISTORY",
                    "com.android.shell") != PackageManager.PERMISSION_DENIED);
        }
        if ("testEmergencyAlertSettingsUi".equals(mTestNameRule.getMethodName())
                || "testAlertUiOnReceivedAlert".equals(mTestNameRule.getMethodName())) {
            sDevice.pressHome();
        }
    }

    @After
    public void afterTest() {
        if (sPreconditionError != 0) {
            return;
        }

        if ("testAlertUiOnReceivedAlert".equals(mTestNameRule.getMethodName())
                && (sSerialId > 0)) {
            deleteMessageWithShellPermissionIdentity();

            if (!mIsOptOutDialogHandled) {
                UiObject2 yesButton = sDevice.wait(Until.findObject(By.text("Yes")), 1000);
                if (yesButton != null) {
                    logd("yesButton click");
                    yesButton.click();
                    mIsOptOutDialogHandled = true;
                }
            }
        }

        if ("testEmergencyAlertSettingsUi".equals(mTestNameRule.getMethodName())
                || "testAlertUiOnReceivedAlert".equals(mTestNameRule.getMethodName())) {
            LocaleManager localeManager = getContext().getSystemService(LocaleManager.class);
            localeManager.setApplicationLocales(sPackageName, LocaleList.getEmptyLocaleList());
        }
    }

    @Test
    @Parameters(method = "paramsCarrierAndChannelForTest")
    public void testAlertUiOnReceivedAlert(String carrierName, String channel) throws Throwable {
        logd("CellBroadcastUiTest#testAlertUiOnReceivedAlert");
        CellBroadcastCarrierTestConfig carrierInfo =
                new CellBroadcastCarrierTestConfig(sCarriersObject, carrierName);
        CellBroadcastChannelTestConfig channelInfo =
                new CellBroadcastChannelTestConfig(sChannelsObject, carrierName, channel);
        // setup mccmnc
        if (sInputMccMnc == null || (sInputMccMnc != null
                && !sInputMccMnc.equals(carrierInfo.mMccMnc))) {
            setSimInfo(carrierName);
        }

        // change language of CBR
        changeLocale(carrierInfo, sPackageName, true);

        if (!channelInfo.mChannelDefaultValue || TextUtils.isEmpty(channelInfo.mExpectedTitle)
                || channelInfo.mFilteredLanguageBySecondLanguagePref
                || channelInfo.mIsEnabledOnTestMode
                || !channelInfo.mNeedDisplay) {
            // let's skip for alerttitle
            return;
        }
        boolean isMessageEnglish = !channelInfo.mIgnoreMessageByLanguageFilter
                || (channelInfo.mIgnoreMessageByLanguageFilter && carrierInfo.mLanguageTag != null
                && !carrierInfo.mLanguageTag.equals("en"));

        // receive broadcast message
        receiveBroadcastMessage(channel, channelInfo.mWarningType, isMessageEnglish);

        logd("carrier " + carrierName + ", expectedTitle = "
                + channelInfo.mExpectedTitle + " for channel " + channel
                + ", alertTypeIsNotification = " + channelInfo.mAlertTypeIsNotification);
        if (channelInfo.mAlertTypeIsNotification) {
            verifyNotificationPosted(carrierName, channelInfo.mExpectedTitle, channel,
                    sPackageName, channelInfo.mIgnoreMessageByLanguageFilter);
        } else {
            verifyAlertDialogTitle(carrierName, channelInfo.mExpectedTitle, channel,
                    carrierInfo.mDisableNavigation, carrierInfo.mNeedFullScreen, sPackageName,
                    channelInfo.mIgnoreMessageByLanguageFilter);
        }
    }

    public void receiveBroadcastMessage(String channelName, String warningType,
            boolean isMessageEnglish) {
        int channel = Integer.parseInt(channelName);
        String hexChannel = String.format("%04X", channel);

        sSerialId++;
        String serialHexString = String.format("%04X", sSerialId);
        logd("receiveBroadcastMessage, channel = " + hexChannel
                + ", serialIdHexString = " + serialHexString);

        boolean isEtws = (channel & MESSAGE_ID_ETWS_TYPE_MASK) == MESSAGE_ID_ETWS_TYPE;
        String langCode = isMessageEnglish ? "01" : "00"; // 01 is english, 00 is german
        String etwsWarningType = TextUtils.isEmpty(warningType) ? "00" : warningType;
        String pduCode = isEtws ? etwsWarningType : langCode;
        String hexString = serialHexString + hexChannel + pduCode + "11D4F29C0E0AB2CB727A08";
        byte[] data = HexDump.hexStringToByteArray(hexString);

        sMockModemManager.newBroadcastSms(sSlotId, data);
    }

    private void verifyAlertDialogTitle(String carrier, String title, String channel,
            boolean disableNavigation, boolean needsFullScreen, String packageName,
            boolean ignoreMessageByLanguageFilter) {
        boolean expectedResult = ignoreMessageByLanguageFilter ? false : true;
        if (needsFullScreen && !isConfirmedForFullScreenGuide(getContext())) {
            checkForFullScreenGuide();
        }
        boolean result = false;
        String outputTitle = null;
        UiObject2 item = sDevice.wait(Until.findObject(By.res(packageName, "alertTitle")),
                UI_TIMEOUT);
        if (item != null) {
            outputTitle = item.getText();
            if (outputTitle != null && outputTitle.startsWith(title)) {
                result = true;
            }
            if (disableNavigation) {
                // for certain country like chile, check if system navigation bar is disabled
                sDevice.openNotification();
                UiSelector notificationStackScroller = new UiSelector()
                        .packageName("com.android.systemui")
                        .resourceId("com.android.systemui:id/notification_stack_scroller");
                UiObject widget = new UiObject(notificationStackScroller);
                boolean canOpenNotification = widget.waitForExists(3000);
                assertEquals("carrier=" + carrier + ", channel=" + channel
                        + ", system ui should be disabled. ", canOpenNotification, false);
            }
            // dismiss dialog
            UiObject2 okItem = sDevice.wait(Until.findObject(
                    By.res(packageName, "dismissButton")), UI_TIMEOUT);
            if (okItem != null) {
                okItem.click();
            }
        }
        assertEquals("carrier=" + carrier + ", channel=" + channel
                + ", output title=" + outputTitle
                + ", expected title=" + title, expectedResult, result);
    }

    /** Pulls down notification shade and verifies that message text is found. */
    private void verifyNotificationPosted(String carrier, String title, String channel,
            String packageName, boolean ignoreMessageByLanguageFilter)
            throws UiObjectNotFoundException {
        boolean expectedResult = ignoreMessageByLanguageFilter ? false : true;

        // open notification shade
        sDevice.openNotification();
        boolean hasObject = sDevice.wait(Until.hasObject(By.text(title)), UI_TIMEOUT);
        if (hasObject) {
            dismissNotificationAsNeeded(title, packageName);
        }
        assertEquals("carrier=" + carrier + ", channel=" + channel
                + ", expected title=" + title, expectedResult, hasObject);
    }

    private void dismissNotificationAsNeeded(String title, String packageName)
            throws UiObjectNotFoundException {
        UiSelector notificationStackScroller = new UiSelector()
                .packageName("com.android.systemui")
                .resourceId("com.android.systemui:id/notification_stack_scroller");
        UiObject object = sDevice.findObject(notificationStackScroller);
        UiObject object2 = object.getChild(new UiSelector().textContains(title));
        if (object2 != null) {
            object2.click();
            UiObject2 okItem = sDevice.wait(Until.findObject(
                    By.res(packageName, "dismissButton")), UI_TIMEOUT);
            if (okItem != null) {
                okItem.click();
            }
        }
    }

    @Test
    @Parameters(method = "paramsForTest")
    public void testEmergencyAlertSettingsUi(String carrierName) throws Throwable {
        logd("CellBroadcastUiTest#testEmergencyAlertSettingsUi");
        CellBroadcastCarrierTestConfig carrierInfo =
                new CellBroadcastCarrierTestConfig(sCarriersObject, carrierName);
        // setup mccmnc
        if (sInputMccMnc == null || (sInputMccMnc != null
                && !sInputMccMnc.equals(carrierInfo.mMccMnc))) {
            setSimInfo(carrierName);
        }

        // change language of CBR
        changeLocale(carrierInfo, sPackageName, false);

        // launch setting activity of CBR
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(new ComponentName(sPackageName, CELL_BROADCAST_LIST_ACTIVITY));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getContext().startActivity(intent);

        UiScrollable listView = new UiScrollable(new UiSelector().resourceId("android:id"
                + "/list_container"));
        listView.waitForExists(2000);

        // check if there is each setting menu
        JSONObject settingsForCarrier = sSettingsObject.getJSONObject(carrierName);
        for (Iterator<String> iterator = settingsForCarrier.keys(); iterator.hasNext(); ) {
            String settingName = iterator.next();
            CellBroadcastSettingTestConfig settingInfo =
                    new CellBroadcastSettingTestConfig(settingsForCarrier, settingName);
            if (!settingInfo.mIsToggleAvailability) {
                continue;
            }
            UiObject item = null;
            try {
                item = listView.getChildByText(new UiSelector()
                        .className(LinearLayout.class.getName()), settingName, true);
            } catch (UiObjectNotFoundException e) {
                logd("let's scrollforward if it cannot find switch");
                listView.scrollForward();
                try {
                    item = listView.getChildByText(new UiSelector()
                                    .className(LinearLayout.class.getName()),
                            settingName, true);
                } catch (UiObjectNotFoundException e2) {
                    assertTrue("carrier=" + carrierName + ", settingName="
                            + settingName, false);
                }
                return;
            }
            UiObject itemSwitch = null;
            try {
                itemSwitch = item.getChild(new UiSelector()
                        .className(android.widget.Switch.class.getName()));
                logd("itemSwitch = " + itemSwitch.isChecked());
            } catch (UiObjectNotFoundException e) {
                logd("switch not found, let's find again");
                String searchText = settingName;
                if (settingInfo.mSummary != null) {
                    searchText = settingInfo.mSummary;
                } else {
                    // let's scrollforward if it cannot find switch
                    listView.scrollForward();
                }
                item = listView.getChildByText(new UiSelector()
                        .className(LinearLayout.class.getName()), searchText, true);
                itemSwitch = item.getChild(new UiSelector()
                        .className(android.widget.Switch.class.getName()));
            }
            assertEquals("carrierName=" + carrierName + ", settingName=" + settingName
                    + ", expectedSwitchValue=" + settingInfo.mIsToggleAvailability,
                    settingInfo.mExpectedSwitchValue, itemSwitch.isChecked());
        }
        sDevice.pressBack();
    }

    private void dismissKeyGuard() throws Exception {
        logd("dismissKeyGuard");
        sDevice.wakeUp();
        sDevice.executeShellCommand("wm dismiss-keyguard");
    }

    private boolean checkForFullScreenGuide() {
        logd("checkForFullScreenGuide");
        UiObject2 viewObject = sDevice.wait(Until.findObject(FULL_SCREEN_DIALOG),
                UI_TIMEOUT);
        if (viewObject != null) {
            logd("Found full screen dialog, dismissing.");
            UiObject2 okButton = sDevice.wait(Until.findObject(CLOSE_BUTTON), UI_TIMEOUT);
            if (okButton != null) {
                okButton.click();
                return true;
            } else {
                logd("Unable to dismiss full screen dialog");
            }
        }
        return false;
    }

    private boolean isConfirmedForFullScreenGuide(Context context) {
        logd("isConfirmedForFullScreenGuide");
        String value = null;
        boolean isConfirmed = false;
        try {
            value = Settings.Secure.getString(context.getContentResolver(),
                    Settings.Secure.IMMERSIVE_MODE_CONFIRMATIONS);
            isConfirmed = "confirmed".equals(value);
            logd("Loaded isConfirmed = " + isConfirmed);
        } catch (Throwable t) {
            logd("Error loading confirmations, value=" + value);
        }
        return isConfirmed;
    }

    private void changeLocale(CellBroadcastCarrierTestConfig info,
            String packageName, boolean checkAlertUi) {
        LocaleManager localeManager = getContext().getSystemService(LocaleManager.class);
        if (info.mLanguageTag != null && (checkAlertUi || info.mCheckSettingWithMainLanguage)) {
            logd("setApplicationLocales " + info.mLanguageTag);
            localeManager.setApplicationLocales(packageName,
                    LocaleList.forLanguageTags(info.mLanguageTag));
        } else {
            logd("setApplicationLocales to default");
            localeManager.setApplicationLocales(packageName,
                    LocaleList.forLanguageTags("en-US"));
        }
    }

    private void deleteMessageWithShellPermissionIdentity() {
        UiAutomation uiAutomation = sInstrumentation.getUiAutomation();
        uiAutomation.adoptShellPermissionIdentity();
        try {
            getContext().getContentResolver().delete(CONTENT_URI,
                    SELECT_BY_SERIAL_NUMBER, new String[]{String.valueOf(sSerialId)});
        } catch (SecurityException e) {
            logd("runWithShellPermissionIdentity exception = " + e);
        } finally {
            uiAutomation.dropShellPermissionIdentity();
        }
    }
}