Fixed flaky unit tests
[android/platform/frameworks/opt/telephony.git] / tests / telephonytests / src / com / android / internal / telephony / CarrierActionAgentTest.java
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  */
16 package com.android.internal.telephony;
17
18 import static com.android.internal.telephony.TelephonyTestUtils.waitForMs;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.mockito.Matchers.anyLong;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.verify;
24
25 import android.database.ContentObserver;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.os.HandlerThread;
30 import android.os.Message;
31 import android.provider.Settings;
32 import android.test.mock.MockContentProvider;
33 import android.test.mock.MockContentResolver;
34 import android.test.suitebuilder.annotation.SmallTest;
35
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.ArgumentCaptor;
40 import org.mockito.Mock;
41
42 public class CarrierActionAgentTest extends TelephonyTest {
43     private CarrierActionAgent mCarrierActionAgentUT;
44     private FakeContentResolver mFakeContentResolver;
45     private FakeContentProvider mFakeContentProvider;
46     private static int DATA_CARRIER_ACTION_EVENT = 0;
47     private static int RADIO_CARRIER_ACTION_EVENT = 1;
48     @Mock
49     private Handler mDataActionHandler;
50     @Mock
51     private Handler mRadioActionHandler;
52
53     private class FakeContentResolver extends MockContentResolver {
54         @Override
55         public void notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork) {
56             super.notifyChange(uri, observer, syncToNetwork);
57             logd("onChanged(uri=" + uri + ")" + observer);
58             if (observer != null) {
59                 observer.dispatchChange(false, uri);
60             } else {
61                 mCarrierActionAgentUT.getContentObserver().dispatchChange(false, uri);
62             }
63         }
64     }
65
66     private class FakeContentProvider extends MockContentProvider {
67         private int mExpectedValue;
68         public void simulateChange(Uri uri) {
69             mFakeContentResolver.notifyChange(uri, null);
70         }
71         @Override
72         public Bundle call(String method, String request, Bundle args) {
73             Bundle result = new Bundle();
74             if (Settings.CALL_METHOD_GET_GLOBAL.equals(method)) {
75                 result.putString(Settings.NameValueTable.VALUE, Integer.toString(mExpectedValue));
76             } else {
77                 mExpectedValue = Integer.parseInt(args.getString(Settings.NameValueTable.VALUE));
78             }
79             return result;
80         }
81     }
82
83     private class CarrierActionAgentHandler extends HandlerThread {
84
85         private CarrierActionAgentHandler(String name) {
86             super(name);
87         }
88
89         @Override
90         public void onLooperPrepared() {
91             mCarrierActionAgentUT = new CarrierActionAgent(mPhone);
92             mCarrierActionAgentUT.registerForCarrierAction(
93                     CarrierActionAgent.CARRIER_ACTION_SET_METERED_APNS_ENABLED, mDataActionHandler,
94                     DATA_CARRIER_ACTION_EVENT, null, false);
95             mCarrierActionAgentUT.registerForCarrierAction(
96                     CarrierActionAgent.CARRIER_ACTION_SET_RADIO_ENABLED, mRadioActionHandler,
97                     RADIO_CARRIER_ACTION_EVENT, null, false);
98             setReady(true);
99         }
100     }
101
102     @Before
103     public void setUp() throws Exception {
104         logd("CarrierActionAgentTest +Setup!");
105         super.setUp(getClass().getSimpleName());
106         mFakeContentResolver = new FakeContentResolver();
107         mFakeContentProvider = new FakeContentProvider();
108         mFakeContentResolver.addProvider(Settings.AUTHORITY, mFakeContentProvider);
109         doReturn(mFakeContentResolver).when(mContext).getContentResolver();
110         new CarrierActionAgentHandler(getClass().getSimpleName()).start();
111         waitUntilReady();
112         logd("CarrierActionAgentTest -Setup!");
113     }
114
115     @Test
116     @SmallTest
117     public void testCarrierActionResetOnAPM() {
118         Settings.Global.putInt(mFakeContentResolver, Settings.Global.AIRPLANE_MODE_ON, 1);
119         mFakeContentProvider.simulateChange(
120                 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON));
121         waitForMs(50);
122         ArgumentCaptor<Message> message = ArgumentCaptor.forClass(Message.class);
123
124         verify(mDataActionHandler).sendMessageAtTime(message.capture(), anyLong());
125         assertEquals(DATA_CARRIER_ACTION_EVENT, message.getValue().what);
126
127         verify(mRadioActionHandler).sendMessageAtTime(message.capture(), anyLong());
128         assertEquals(RADIO_CARRIER_ACTION_EVENT, message.getValue().what);
129         Settings.Global.putInt(mFakeContentResolver, Settings.Global.AIRPLANE_MODE_ON, 0);
130     }
131
132     @After
133     public void tearDown() throws Exception {
134         super.tearDown();
135     }
136 }