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

Commit a216aa29 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add a way to clear cached provider for tests in Settings class."

parents f6a72c2d aa3c30df
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
@@ -1779,6 +1779,12 @@ public final class Settings {
                return mContentProvider;
            }
        }

        public void clearProviderForTest() {
            synchronized (mLock) {
                mContentProvider = null;
            }
        }
    }

    // Thread-safe.
@@ -1994,6 +2000,16 @@ public final class Settings {
                if (c != null) c.close();
            }
        }

        public void clearGenerationTrackerForTest() {
            synchronized (NameValueCache.this) {
                if (mGenerationTracker != null) {
                    mGenerationTracker.destroy();
                }
                mValues.clear();
                mGenerationTracker = null;
            }
        }
    }

    /**
@@ -2182,6 +2198,12 @@ public final class Settings {
            outKeySet.addAll(MOVED_TO_GLOBAL);
        }

        /** @hide */
        public static void clearProviderForTest() {
            sProviderHolder.clearProviderForTest();
            sNameValueCache.clearGenerationTrackerForTest();
        }

        /**
         * Look up a name in the database.
         * @param resolver to access the database with
@@ -4595,6 +4617,12 @@ public final class Settings {
            outKeySet.addAll(MOVED_TO_GLOBAL);
        }

        /** @hide */
        public static void clearProviderForTest() {
            sProviderHolder.clearProviderForTest();
            sNameValueCache.clearGenerationTrackerForTest();
        }

        /**
         * Look up a name in the database.
         * @param resolver to access the database with
@@ -10037,6 +10065,12 @@ public final class Settings {
            outKeySet.addAll(MOVED_TO_SECURE);
        }

        /** @hide */
        public static void clearProviderForTest() {
            sProviderHolder.clearProviderForTest();
            sNameValueCache.clearGenerationTrackerForTest();
        }

        /**
         * Look up a name in the database.
         * @param resolver to access the database with
+7 −1
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import android.test.mock.MockContentResolver;
import com.android.internal.util.test.FakeSettingsProvider;
import com.android.server.AppOpsService;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
@@ -59,7 +60,6 @@ import java.io.File;
 * Run: adb shell am instrument -e class com.android.server.am.CoreSettingsObserverTest -w \
 *     com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
 */
@Ignore
@SmallTest
@RunWith(AndroidJUnit4.class)
public class CoreSettingsObserverTest {
@@ -79,11 +79,17 @@ public class CoreSettingsObserverTest {

    @BeforeClass
    public static void setupOnce() {
        FakeSettingsProvider.clearSettingsProvider();
        CoreSettingsObserver.sSecureSettingToTypeMap.put(TEST_SETTING_SECURE_INT, int.class);
        CoreSettingsObserver.sGlobalSettingToTypeMap.put(TEST_SETTING_GLOBAL_FLOAT, float.class);
        CoreSettingsObserver.sSystemSettingToTypeMap.put(TEST_SETTING_SYSTEM_STRING, String.class);
    }

    @AfterClass
    public static void tearDownOnce() {
        FakeSettingsProvider.clearSettingsProvider();
    }

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
+11 −1
Original line number Diff line number Diff line
@@ -54,7 +54,8 @@ import java.util.HashMap;
 * Note that this class cannot be used in the same process as real settings. This is because it
 * works by passing an alternate ContentResolver to Settings operations. Unfortunately, the Settings
 * class only fetches the content provider from the passed-in ContentResolver the first time it's
 * used, and after that stores it in a per-process static.
 * used, and after that stores it in a per-process static. If this needs to be used in this case,
 * then call {@link #clearSettingsProvider()} before and after using this.
 *
 * TODO: evaluate implementing settings change notifications. This would require:
 *
@@ -90,6 +91,15 @@ public class FakeSettingsProvider extends MockContentProvider {
        }
    }

    /**
     * This needs to be called before and after using the FakeSettingsProvider class.
     */
    public static void clearSettingsProvider() {
        Settings.Secure.clearProviderForTest();
        Settings.Global.clearProviderForTest();
        Settings.System.clearProviderForTest();
    }

    public Bundle call(String method, String arg, Bundle extras) {
        // Methods are "GET_system", "GET_global", "PUT_secure", etc.
        String[] commands = method.split("_", 2);