Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2a703488 authored by Amit Mahajan's avatar Amit Mahajan Committed by Android Partner Code Review
Browse files

Merge "Unit tests for SmsStorageMonitor" into mm-wireless-dev

parents 7d905156 ceea0fa1
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -1107,7 +1107,8 @@ public class SimulatedCommands extends BaseCommands

    @Override
    public void reportSmsMemoryStatus(boolean available, Message result) {
        unimplemented(result);
        resultSuccess(result, null);
        SimulatedCommandsVerifier.getInstance().reportSmsMemoryStatus(available, result);
    }

    @Override
@@ -1869,4 +1870,14 @@ public class SimulatedCommands extends BaseCommands
            mGsmBroadcastSmsRegistrant.notifyRegistrant(new AsyncResult(null, result, null));
        }
    }

    public void notifyIccSmsFull() {
        if (mIccSmsFullRegistrant != null) {
            mIccSmsFullRegistrant.notifyRegistrant();
        }
    }

    public void notifyRadioOn() {
        mOnRegistrants.notifyRegistrants();
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -219,7 +219,8 @@ public class ContextFixture implements TestFixture<Context> {

        @Override
        public void sendBroadcast(Intent intent, String receiverPermission) {
            // TODO -- need to ensure this is captured
            logd("sendBroadcast called for " + intent.getAction());
            sendBroadcast(intent);
        }

        @Override
+159 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 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.internal.telephony;

import android.content.Intent;
import android.os.HandlerThread;
import android.os.Message;
import android.provider.Telephony;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;

import com.android.internal.telephony.test.SimulatedCommands;
import com.android.internal.telephony.test.SimulatedCommandsVerifier;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.lang.reflect.Field;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

public class SmsStorageMonitorTest {
    private static final String TAG = "SmsStorageMonitorTest";

    @Mock
    Phone mPhone;
    @Mock
    SimulatedCommandsVerifier mSimulatedCommandsVerifier;

    private SimulatedCommands mSimulatedCommands;
    private ContextFixture mContextFixture;
    private SmsStorageMonitor mSmsStorageMonitor;

    private Object mLock = new Object();
    private boolean mReady;

    private class SmsStorageMonitorTestHandler extends HandlerThread {

        private SmsStorageMonitorTestHandler(String name) {
            super(name);
        }

        @Override
        public void onLooperPrepared() {
            mSmsStorageMonitor = new SmsStorageMonitor(mPhone);
            synchronized (mLock) {
                mReady = true;
            }
        }
    }

    private void waitUntilReady() {
        while(true) {
            synchronized (mLock) {
                if (mReady) {
                    break;
                }
            }
        }
    }

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        Field field = SimulatedCommandsVerifier.class.getDeclaredField("sInstance");
        field.setAccessible(true);
        field.set(null, mSimulatedCommandsVerifier);

        mSimulatedCommands = new SimulatedCommands();
        mContextFixture = new ContextFixture();

        doReturn(mContextFixture.getTestDouble()).when(mPhone).getContext();
        mPhone.mCi = mSimulatedCommands;

        mReady = false;
        new SmsStorageMonitorTestHandler(TAG).start();
        waitUntilReady();
    }

    @After
    public void tearDown() throws Exception {
        mSmsStorageMonitor = null;
    }

    @Test @SmallTest
    public void testEventIccFull() {
        // Notify icc sms full
        mSimulatedCommands.notifyIccSmsFull();
        TelephonyTestUtils.waitForMs(50);

        // SIM_FULL_ACTION intent should be broadcast
        ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class);
        verify(mContextFixture.getTestDouble()).sendBroadcast(intentArgumentCaptor.capture());
        assertEquals(Telephony.Sms.Intents.SIM_FULL_ACTION,
                intentArgumentCaptor.getValue().getAction());
    }

    @Test @SmallTest
    public void testSmsMemoryStatus() {
        // Notify radio on
        mSimulatedCommands.notifyRadioOn();
        TelephonyTestUtils.waitForMs(50);

        verify(mSimulatedCommandsVerifier, never()).reportSmsMemoryStatus(anyBoolean(),
                any(Message.class));

        // Send DEVICE_STORAGE_FULL
        mContextFixture.getTestDouble().sendBroadcast(
                new Intent(Intent.ACTION_DEVICE_STORAGE_FULL));
        TelephonyTestUtils.waitForMs(50);

        verify(mSimulatedCommandsVerifier).reportSmsMemoryStatus(eq(false), any(Message.class));
        assertFalse(mSmsStorageMonitor.isStorageAvailable());

        mSimulatedCommands.notifyRadioOn();
        TelephonyTestUtils.waitForMs(50);

        verify(mSimulatedCommandsVerifier).reportSmsMemoryStatus(eq(false), any(Message.class));

        // Send DEVICE_STORAGE_NOT_FULL
        mContextFixture.getTestDouble().sendBroadcast(
                new Intent(Intent.ACTION_DEVICE_STORAGE_NOT_FULL));
        TelephonyTestUtils.waitForMs(50);

        verify(mSimulatedCommandsVerifier).reportSmsMemoryStatus(eq(true), any(Message.class));
        assertTrue(mSmsStorageMonitor.isStorageAvailable());

        mSimulatedCommands.notifyRadioOn();
        TelephonyTestUtils.waitForMs(50);

        verify(mSimulatedCommandsVerifier).reportSmsMemoryStatus(eq(true), any(Message.class));


    }

    private static void logd(String s) {
        Log.d(TAG, s);
    }
}
 No newline at end of file