tests/utils/testutils/java/com/android/internal/util/test/FakeSettingsProviderRule.java
raw symbols SCIP| 1 | /* |
| 2 | * Copyright (C) 2020 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 | |
| 17 | package com.android.internal.util.test; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.provider.Settings; |
| 21 | import android.test.mock.MockContentResolver; |
| 22 | |
| 23 | import org.junit.rules.TestRule; |
| 24 | import org.junit.runner.Description; |
| 25 | import org.junit.runners.model.Statement; |
| 26 | |
| 27 | /** |
| 28 | * JUnit Rule helps keeping test {@link FakeSettingsProvider} clean. |
| 29 | * |
| 30 | * <p>It clears {@link FakeSettingsProvider} before and after each test. Example use: |
| 31 | * <pre class="code"><code class="java"> |
| 32 | * public class ExampleTest { |
| 33 | * |
| 34 | * @Rule public FakeSettingsProviderRule rule = FakeSettingsProvider.rule(); |
| 35 | * |
| 36 | * @Test |
| 37 | * public void shouldDoSomething() { |
| 38 | * ContextResolver cr = rule.mockContentResolver(mContext); |
| 39 | * Settings.Global.putInt(cr, "my_setting_name", 1); |
| 40 | * // Test code relying on my_setting_name value using cr |
| 41 | * } |
| 42 | * } |
| 43 | * </code></pre> |
| 44 | * |
| 45 | * @see FakeSettingsProvider |
| 46 | */ |
| 47 | public final class FakeSettingsProviderRule implements TestRule { |
| 48 | |
| 49 | /** Prevent initialization outside {@link FakeSettingsProvider}. */ |
| 50 | FakeSettingsProviderRule() { |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Creates a {@link MockContentResolver} that uses the {@link FakeSettingsProvider} as the |
| 55 | * {@link Settings#AUTHORITY} provider. |
| 56 | */ |
| 57 | public MockContentResolver mockContentResolver(Context context) { |
| 58 | MockContentResolver contentResolver = new MockContentResolver(context); |
| 59 | contentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider()); |
| 60 | return contentResolver; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public Statement apply(Statement base, Description description) { |
| 65 | return new Statement() { |
| 66 | public void evaluate() throws Throwable { |
| 67 | FakeSettingsProvider.clearSettingsProvider(); |
| 68 | try { |
| 69 | base.evaluate(); |
| 70 | } finally { |
| 71 | FakeSettingsProvider.clearSettingsProvider(); |
| 72 | } |
| 73 | } |
| 74 | }; |
| 75 | } |
| 76 | } |