Add a way to clear cached provider for tests in Settings class.
Bug: 38437704 Bug: 38433611 Test: manual Change-Id: Id2803359644bd68a158294b6d801fc9cd316fc9f
This commit is contained in:
@@ -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
|
||||
@@ -10027,6 +10055,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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user