1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.cellbroadcastreceiver;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.Intent;
22import android.os.Bundle;
23import android.os.IPowerManager;
24import android.os.PowerManager;
25import android.telephony.CellBroadcastMessage;
26import android.widget.TextView;
27
28import org.junit.After;
29import org.junit.Before;
30import org.mockito.ArgumentCaptor;
31import org.mockito.Captor;
32import org.mockito.Mock;
33import org.mockito.MockitoAnnotations;
34
35import java.util.ArrayList;
36
37import static org.mockito.Mockito.doReturn;
38import static org.mockito.Mockito.times;
39import static org.mockito.Mockito.verify;
40
41public class CellBroadcastAlertDialogTest extends
42        CellBroadcastActivityTestCase<CellBroadcastAlertDialog> {
43
44    @Mock
45    private NotificationManager mMockedNotificationManager;
46
47    @Mock
48    private IPowerManager.Stub mMockedPowerManagerService;
49
50    @Captor
51    private ArgumentCaptor<Integer> mInt;
52
53    @Captor
54    private ArgumentCaptor<Notification> mNotification;
55
56    private PowerManager mPowerManager;
57
58    public CellBroadcastAlertDialogTest() {
59        super(CellBroadcastAlertDialog.class);
60    }
61
62    @Override
63    protected Intent createActivityIntent() {
64        ArrayList<CellBroadcastMessage> messageList = new ArrayList<>(1);
65        messageList.add(new CellBroadcastMessage(
66                CellBroadcastAlertServiceTest.createMessage(12412)));
67
68        Intent intent = new Intent(getInstrumentation().getTargetContext(),
69                        CellBroadcastAlertDialog.class);
70        intent.putParcelableArrayListExtra(CellBroadcastMessage.SMS_CB_MESSAGE_EXTRA,
71                        messageList);
72        return intent;
73    }
74
75    @Before
76    public void setUp() throws Exception {
77        super.setUp();
78        MockitoAnnotations.initMocks(this);
79        injectSystemService(NotificationManager.class, mMockedNotificationManager);
80        // PowerManager is a final class so we can't use Mockito to mock it, but we can mock
81        // its underlying service.
82        doReturn(true).when(mMockedPowerManagerService).isInteractive();
83        mPowerManager = new PowerManager(mContext, mMockedPowerManagerService, null);
84        injectSystemService(PowerManager.class, mPowerManager);
85    }
86
87    @After
88    public void tearDown() throws Exception {
89        super.tearDown();
90    }
91
92    public void testTitleAndMessageText() throws Throwable {
93        startActivity();
94        waitForMs(100);
95
96        CharSequence etremeAlertString =
97                getActivity().getResources().getText(R.string.cmas_extreme_alert);
98        assertEquals(etremeAlertString, getActivity().getTitle());
99        assertEquals(etremeAlertString,
100                ((TextView) getActivity().findViewById(R.id.alertTitle)).getText());
101
102        assertEquals(CellBroadcastAlertServiceTest.createMessage(34596).getMessageBody(),
103                (String) ((TextView) getActivity().findViewById(R.id.message)).getText());
104
105        stopActivity();
106    }
107
108    public void testAddToNotification() throws Throwable {
109        startActivity();
110        waitForMs(100);
111        stopActivity();
112        waitForMs(100);
113        verify(mMockedNotificationManager, times(1)).notify(mInt.capture(),
114                mNotification.capture());
115        Bundle b = mNotification.getValue().extras;
116
117        assertEquals(1, (int) mInt.getValue());
118
119        assertEquals(getActivity().getTitle(), b.getCharSequence(Notification.EXTRA_TITLE));
120        assertEquals(CellBroadcastAlertServiceTest.createMessage(98235).getMessageBody(),
121                b.getCharSequence(Notification.EXTRA_TEXT));
122    }
123}