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

Commit 5480dd20 authored by Harry Cutts's avatar Harry Cutts Committed by Android (Google) Code Review
Browse files

Merge changes I52095a8a,Ibf4bfa48,If42843a3 into main

* changes:
  TestableSettingsProvider: small tidy-ups
  TestableContext: override finished in TestWatcher
  TestableSettingsProvider: tear down properly
parents 9856f7bc a87d0d33
Loading
Loading
Loading
Loading
+2 −9
Original line number Diff line number Diff line
@@ -533,16 +533,9 @@ public class TestableContext extends ContextWrapper implements TestRule {
    public Statement apply(Statement base, Description description) {
        return new TestWatcher() {
            @Override
            protected void succeeded(Description description) {
            protected void finished(Description description) {
                if (mSettingsProvider != null) {
                    mSettingsProvider.clearValuesAndCheck(TestableContext.this);
                }
            }

            @Override
            protected void failed(Throwable e, Description description) {
                if (mSettingsProvider != null) {
                    mSettingsProvider.clearValuesAndCheck(TestableContext.this);
                    mSettingsProvider.unregister();
                }
            }
        }.apply(base, description);
+37 −30
Original line number Diff line number Diff line
@@ -70,6 +70,13 @@ public class TestableSettingsProvider extends MockContentProvider {
        mValues.clear();
    }

    void unregister() {
        Settings.Global.clearProviderForTest();
        Settings.Secure.clearProviderForTest();
        Settings.System.clearProviderForTest();
    }

    @Override
    public Bundle call(String method, String arg, Bundle extras) {
        // Methods are "GET_system", "GET_global", "PUT_secure", etc.
        int userId = extras.getInt(Settings.CALL_METHOD_USER_KEY, UserHandle.USER_CURRENT);
+19 −4
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ package android.testing;
import static org.junit.Assert.*;

import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.provider.Settings.Global;
import android.provider.Settings.Secure;
@@ -37,9 +38,9 @@ public class TestableSettingsProviderTest {
    public static final String NONEXISTENT_SETTING = "nonexistent_setting";
    private static final String TAG = "TestableSettingsProviderTest";
    private ContentResolver mContentResolver;
    private final Context mRealContext = InstrumentationRegistry.getContext();
    @Rule
    public final TestableContext mContext =
            new TestableContext(InstrumentationRegistry.getContext());
    public final TestableContext mContext = new TestableContext(mRealContext);

    @Before
    public void setup() {
@@ -93,11 +94,25 @@ public class TestableSettingsProviderTest {
    }

    @Test
    public void testRelease() {
    public void testClearValues() {
        // Verify different value.
        assertNull(Global.getString(mContentResolver, Global.DEVICE_PROVISIONED));
        mContext.getSettingsProvider().clearValuesAndCheck(mContext);
        // Verify actual value after release.
        // After clearing, the value should be fetched from the underlying real context.
        assertEquals("1", Global.getString(mContentResolver, Global.DEVICE_PROVISIONED));
    }

    @Test
    public void testUnregister() {
        // With the TestableSettingsProvider still in use, we should see the overridden value of
        // DEVICE_PROVISIONED.
        assertNull(Global.getString(mContentResolver, Global.DEVICE_PROVISIONED));

        mContext.getSettingsProvider().unregister();

        // After unregistering, the real value should be returned when we use the real context, even
        // though the TestableSettingsProvider still exists with the overridden value.
        assertEquals("1",
                Global.getString(mRealContext.getContentResolver(), Global.DEVICE_PROVISIONED));
    }
}