1/*
2 * Copyright (C) 2017 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 */
16package com.android.internal.telephony;
17
18import static com.android.internal.telephony.TelephonyTestUtils.waitForMs;
19
20import static org.junit.Assert.assertEquals;
21import static org.mockito.Matchers.anyLong;
22import static org.mockito.Mockito.doReturn;
23import static org.mockito.Mockito.verify;
24
25import android.database.ContentObserver;
26import android.net.Uri;
27import android.os.Handler;
28import android.os.HandlerThread;
29import android.os.Message;
30import android.provider.Settings;
31import android.test.mock.MockContentResolver;
32import android.test.suitebuilder.annotation.SmallTest;
33
34import org.junit.After;
35import org.junit.Before;
36import org.junit.Test;
37import org.mockito.ArgumentCaptor;
38import org.mockito.Mock;
39
40public class CarrierActionAgentTest extends TelephonyTest {
41    private CarrierActionAgent mCarrierActionAgentUT;
42    private FakeContentResolver mFakeContentResolver;
43    private static int DATA_CARRIER_ACTION_EVENT = 0;
44    private static int RADIO_CARRIER_ACTION_EVENT = 1;
45    @Mock
46    private Handler mDataActionHandler;
47    @Mock
48    private Handler mRadioActionHandler;
49
50    private class FakeContentResolver extends MockContentResolver {
51        @Override
52        public void notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork) {
53            super.notifyChange(uri, observer, syncToNetwork);
54            logd("onChanged(uri=" + uri + ")" + observer);
55            if (observer != null) {
56                observer.dispatchChange(false, uri);
57            } else {
58                mCarrierActionAgentUT.getContentObserver().dispatchChange(false, uri);
59            }
60        }
61    }
62
63    private class CarrierActionAgentHandler extends HandlerThread {
64
65        private CarrierActionAgentHandler(String name) {
66            super(name);
67        }
68
69        @Override
70        public void onLooperPrepared() {
71            mCarrierActionAgentUT = new CarrierActionAgent(mPhone);
72            mCarrierActionAgentUT.registerForCarrierAction(
73                    CarrierActionAgent.CARRIER_ACTION_SET_METERED_APNS_ENABLED, mDataActionHandler,
74                    DATA_CARRIER_ACTION_EVENT, null, false);
75            mCarrierActionAgentUT.registerForCarrierAction(
76                    CarrierActionAgent.CARRIER_ACTION_SET_RADIO_ENABLED, mRadioActionHandler,
77                    RADIO_CARRIER_ACTION_EVENT, null, false);
78            setReady(true);
79        }
80    }
81
82    @Before
83    public void setUp() throws Exception {
84        logd("CarrierActionAgentTest +Setup!");
85        super.setUp(getClass().getSimpleName());
86        mFakeContentResolver = new FakeContentResolver();
87        doReturn(mFakeContentResolver).when(mContext).getContentResolver();
88        new CarrierActionAgentHandler(getClass().getSimpleName()).start();
89        waitUntilReady();
90        logd("CarrierActionAgentTest -Setup!");
91    }
92
93    @Test
94    @SmallTest
95    public void testCarrierActionResetOnAPM() {
96        Settings.Global.putInt(mFakeContentResolver, Settings.Global.AIRPLANE_MODE_ON, 1);
97        mFakeContentResolver.notifyChange(
98                Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), null);
99        waitForMs(200);
100        ArgumentCaptor<Message> message = ArgumentCaptor.forClass(Message.class);
101
102        verify(mDataActionHandler).sendMessageAtTime(message.capture(), anyLong());
103        assertEquals(DATA_CARRIER_ACTION_EVENT, message.getValue().what);
104
105        verify(mRadioActionHandler).sendMessageAtTime(message.capture(), anyLong());
106        assertEquals(RADIO_CARRIER_ACTION_EVENT, message.getValue().what);
107    }
108
109    @After
110    public void tearDown() throws Exception {
111        Settings.Global.putInt(mFakeContentResolver, Settings.Global.AIRPLANE_MODE_ON, 0);
112        super.tearDown();
113    }
114}
115