Revert "flacky unit test"
Chen Xu [Wed, 17 May 2017 00:09:49 +0000 (00:09 +0000)]
This reverts commit 0b187f0fba954859718c4ad8d1b4751343142d78.

Change-Id: I880cb6aef85eca42ddbdd74a7c9ac2cac215fd93

tests/telephonytests/src/com/android/internal/telephony/CarrierActionAgentTest.java
tests/telephonytests/src/com/android/internal/telephony/CarrierAppUtilsTest.java
tests/telephonytests/src/com/android/internal/telephony/ContextFixture.java
tests/telephonytests/src/com/android/internal/telephony/ImsSMSDispatcherTest.java
tests/telephonytests/src/com/android/internal/telephony/TelephonyTest.java

index bbeed03..a402f12 100644 (file)
@@ -24,10 +24,12 @@ import static org.mockito.Mockito.verify;
 
 import android.database.ContentObserver;
 import android.net.Uri;
+import android.os.Bundle;
 import android.os.Handler;
 import android.os.HandlerThread;
 import android.os.Message;
 import android.provider.Settings;
+import android.test.mock.MockContentProvider;
 import android.test.mock.MockContentResolver;
 import android.test.suitebuilder.annotation.SmallTest;
 
@@ -40,6 +42,7 @@ import org.mockito.Mock;
 public class CarrierActionAgentTest extends TelephonyTest {
     private CarrierActionAgent mCarrierActionAgentUT;
     private FakeContentResolver mFakeContentResolver;
+    private FakeContentProvider mFakeContentProvider;
     private static int DATA_CARRIER_ACTION_EVENT = 0;
     private static int RADIO_CARRIER_ACTION_EVENT = 1;
     @Mock
@@ -60,6 +63,23 @@ public class CarrierActionAgentTest extends TelephonyTest {
         }
     }
 
+    private class FakeContentProvider extends MockContentProvider {
+        private int mExpectedValue;
+        public void simulateChange(Uri uri) {
+            mFakeContentResolver.notifyChange(uri, null);
+        }
+        @Override
+        public Bundle call(String method, String request, Bundle args) {
+            Bundle result = new Bundle();
+            if (Settings.CALL_METHOD_GET_GLOBAL.equals(method)) {
+                result.putString(Settings.NameValueTable.VALUE, Integer.toString(mExpectedValue));
+            } else {
+                mExpectedValue = Integer.parseInt(args.getString(Settings.NameValueTable.VALUE));
+            }
+            return result;
+        }
+    }
+
     private class CarrierActionAgentHandler extends HandlerThread {
 
         private CarrierActionAgentHandler(String name) {
@@ -84,6 +104,8 @@ public class CarrierActionAgentTest extends TelephonyTest {
         logd("CarrierActionAgentTest +Setup!");
         super.setUp(getClass().getSimpleName());
         mFakeContentResolver = new FakeContentResolver();
+        mFakeContentProvider = new FakeContentProvider();
+        mFakeContentResolver.addProvider(Settings.AUTHORITY, mFakeContentProvider);
         doReturn(mFakeContentResolver).when(mContext).getContentResolver();
         new CarrierActionAgentHandler(getClass().getSimpleName()).start();
         waitUntilReady();
@@ -94,8 +116,8 @@ public class CarrierActionAgentTest extends TelephonyTest {
     @SmallTest
     public void testCarrierActionResetOnAPM() {
         Settings.Global.putInt(mFakeContentResolver, Settings.Global.AIRPLANE_MODE_ON, 1);
-        mFakeContentResolver.notifyChange(
-                Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), null);
+        mFakeContentProvider.simulateChange(
+                Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON));
         waitForMs(50);
         ArgumentCaptor<Message> message = ArgumentCaptor.forClass(Message.class);
 
index 132c9dd..9974a6d 100644 (file)
 
 package com.android.internal.telephony;
 
-import android.content.ContentResolver;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.IPackageManager;
 import android.content.pm.PackageManager;
+import android.os.Bundle;
 import android.provider.Settings;
 import android.telephony.TelephonyManager;
+import android.test.InstrumentationTestCase;
+import android.test.mock.MockContentProvider;
+import android.test.mock.MockContentResolver;
 import android.test.suitebuilder.annotation.SmallTest;
 import android.util.ArrayMap;
 
-import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mock;
 import org.mockito.Mockito;
@@ -34,7 +36,7 @@ import org.mockito.MockitoAnnotations;
 import java.util.ArrayList;
 import java.util.List;
 
-public class CarrierAppUtilsTest extends TelephonyTest {
+public class CarrierAppUtilsTest extends InstrumentationTestCase {
     private static final String CARRIER_APP = "com.example.carrier";
     private static final String[] CARRIER_APPS = new String[] { CARRIER_APP };
     private static final String ASSOCIATED_APP = "com.example.associated";
@@ -49,19 +51,22 @@ public class CarrierAppUtilsTest extends TelephonyTest {
 
     @Mock private IPackageManager mPackageManager;
     @Mock private TelephonyManager mTelephonyManager;
-    private ContentResolver mContentResolver;
+    private SettingsMockContentProvider mContentProvider;
+    private MockContentResolver mContentResolver;
 
-    @Before
+    @Override
     public void setUp() throws Exception {
-        logd("CarrierAppUtilsTest +Setup!");
-        super.setUp(getClass().getSimpleName());
+        super.setUp();
         System.setProperty("dexmaker.dexcache",
-                TestApplication.getAppContext().getCacheDir().getPath());
+                getInstrumentation().getTargetContext().getCacheDir().getPath());
         Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
-        mContentResolver = mContext.getContentResolver();
-        Settings.Secure.putIntForUser(mContentResolver,
-                Settings.Secure.CARRIER_APPS_HANDLED, 0, USER_ID);
-        logd("CarrierAppUtilsTest -Setup!");
+        MockitoAnnotations.initMocks(this);
+
+        mContentResolver = new MockContentResolver();
+        mContentProvider = new SettingsMockContentProvider();
+        mContentResolver.addProvider(Settings.AUTHORITY, mContentProvider);
+        Settings.Secure.putIntForUser(
+                mContentResolver, Settings.Secure.CARRIER_APPS_HANDLED, 0, USER_ID);
     }
 
     /** No apps configured - should do nothing. */
@@ -662,5 +667,21 @@ public class CarrierAppUtilsTest extends TelephonyTest {
                 .grantDefaultPermissionsToEnabledCarrierApps(
                         Mockito.any(String[].class), Mockito.anyInt());
     }
+
+    class SettingsMockContentProvider extends MockContentProvider {
+        private int mExpectedValue;
+
+        @Override
+        public Bundle call(String method, String request, Bundle args) {
+            Bundle result = new Bundle();
+            if (Settings.CALL_METHOD_GET_SECURE.equals(method)) {
+                result.putString(Settings.NameValueTable.VALUE, Integer.toString(mExpectedValue));
+            } else {
+                mExpectedValue = Integer.parseInt(args.getString(Settings.NameValueTable.VALUE));
+            }
+            return result;
+        }
+    }
+
 }
 
index 529c5d4..419e61b 100644 (file)
@@ -530,9 +530,6 @@ public class ContextFixture implements TestFixture<Context> {
         doReturn(mConfiguration).when(mResources).getConfiguration();
 
         mContentResolver.addProvider(Settings.AUTHORITY, mContentProvider);
-        // Settings caches the provider after first get/set call, this is needed to make sure
-        // Settings is using mContentProvider as the cached provider across all tests.
-        Settings.Global.getInt(mContentResolver, Settings.Global.AIRPLANE_MODE_ON, 0);
         mContentResolver.addProvider(ServiceStateTable.AUTHORITY, mContentProvider);
         mPermissionTable.add(PERMISSION_ENABLE_ALL);
     }
index 03e0a6a..1a66bb5 100644 (file)
@@ -169,7 +169,7 @@ public class ImsSMSDispatcherTest extends TelephonyTest {
                 eq(0), any(Message.class));
     }
 
-    @Test @SmallTest @FlakyTest @Ignore
+    @Test @SmallTest
     public void testInjectNullSmsPdu() throws Exception {
         // unmock ActivityManager to be able to register receiver, create real PendingIntent and
         // receive TEST_INTENT
index b38659c..ed5ca43 100644 (file)
@@ -41,6 +41,7 @@ import android.os.IDeviceIdleController;
 import android.os.RegistrantList;
 import android.os.ServiceManager;
 import android.provider.BlockedNumberContract;
+import android.provider.Settings;
 import android.telephony.ServiceState;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
@@ -441,6 +442,9 @@ public abstract class TelephonyTest {
         mSST.mSS = mServiceState;
         mServiceManagerMockedServices.put("connectivity_metrics_logger", mConnMetLoggerBinder);
 
+        Settings.Global.putInt(mContext.getContentResolver(),
+                Settings.Global.ENABLE_CELLULAR_ON_BOOT, 1);
+
         setReady(false);
     }