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.internal.telephony;
18
19import android.content.Intent;
20import android.os.HandlerThread;
21import android.os.Message;
22import android.provider.Telephony;
23import android.test.suitebuilder.annotation.SmallTest;
24import android.util.Log;
25
26import com.android.internal.telephony.test.SimulatedCommands;
27import com.android.internal.telephony.test.SimulatedCommandsVerifier;
28
29import org.junit.After;
30import org.junit.Before;
31import org.junit.Test;
32import org.mockito.ArgumentCaptor;
33import org.mockito.Mock;
34import org.mockito.MockitoAnnotations;
35
36import java.lang.reflect.Field;
37
38import static org.junit.Assert.*;
39import static org.mockito.Mockito.*;
40
41public class SmsStorageMonitorTest extends TelephonyTest {
42
43    private SmsStorageMonitor mSmsStorageMonitor;
44    private SmsStorageMonitorTestHandler mSmsStorageMonitorTestHandler;
45
46    private class SmsStorageMonitorTestHandler extends HandlerThread {
47
48        private SmsStorageMonitorTestHandler(String name) {
49            super(name);
50        }
51
52        @Override
53        public void onLooperPrepared() {
54            mSmsStorageMonitor = new SmsStorageMonitor(mPhone);
55            setReady(true);
56        }
57    }
58
59    @Before
60    public void setUp() throws Exception {
61        super.setUp(getClass().getSimpleName());
62        mSmsStorageMonitorTestHandler = new SmsStorageMonitorTestHandler(TAG);
63        mSmsStorageMonitorTestHandler.start();
64        waitUntilReady();
65    }
66
67    @After
68    public void tearDown() throws Exception {
69        mSmsStorageMonitor = null;
70        mSmsStorageMonitorTestHandler.quit();
71        super.tearDown();
72    }
73
74    @Test @SmallTest
75    public void testEventIccFull() {
76        // Notify icc sms full
77        mSimulatedCommands.notifyIccSmsFull();
78        TelephonyTestUtils.waitForMs(50);
79
80        // SIM_FULL_ACTION intent should be broadcast
81        ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class);
82        verify(mContextFixture.getTestDouble()).sendBroadcast(intentArgumentCaptor.capture());
83        assertEquals(Telephony.Sms.Intents.SIM_FULL_ACTION,
84                intentArgumentCaptor.getValue().getAction());
85    }
86
87    @Test @SmallTest
88    public void testSmsMemoryStatus() {
89        // Notify radio on
90        mSimulatedCommands.notifyRadioOn();
91        TelephonyTestUtils.waitForMs(50);
92
93        verify(mSimulatedCommandsVerifier, never()).reportSmsMemoryStatus(anyBoolean(),
94                any(Message.class));
95
96        // Send DEVICE_STORAGE_FULL
97        mContextFixture.getTestDouble().sendBroadcast(
98                new Intent(Intent.ACTION_DEVICE_STORAGE_FULL));
99        TelephonyTestUtils.waitForMs(50);
100
101        verify(mSimulatedCommandsVerifier).reportSmsMemoryStatus(eq(false), any(Message.class));
102        assertFalse(mSmsStorageMonitor.isStorageAvailable());
103
104        mSimulatedCommands.notifyRadioOn();
105        TelephonyTestUtils.waitForMs(50);
106
107        verify(mSimulatedCommandsVerifier).reportSmsMemoryStatus(eq(false), any(Message.class));
108
109        // Send DEVICE_STORAGE_NOT_FULL
110        mContextFixture.getTestDouble().sendBroadcast(
111                new Intent(Intent.ACTION_DEVICE_STORAGE_NOT_FULL));
112        TelephonyTestUtils.waitForMs(50);
113
114        verify(mSimulatedCommandsVerifier).reportSmsMemoryStatus(eq(true), any(Message.class));
115        assertTrue(mSmsStorageMonitor.isStorageAvailable());
116
117        mSimulatedCommands.notifyRadioOn();
118        TelephonyTestUtils.waitForMs(50);
119
120        verify(mSimulatedCommandsVerifier).reportSmsMemoryStatus(eq(true), any(Message.class));
121    }
122}