blob: 13056cf677d6444c25e575a39d7abe2d2c786b6f [file] [log] [blame]
Jason Monkf06a3172017-04-25 16:30:53 -04001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package android.testing;
16
17import android.content.ContentProviderClient;
18import android.content.Context;
19import android.os.Bundle;
20import android.os.RemoteException;
21import android.provider.Settings;
22import android.test.mock.MockContentProvider;
23import android.util.Log;
24
25import java.util.HashMap;
26
27import static org.junit.Assert.*;
28
29/**
30 * Allows calls to android.provider.Settings to be tested easier.
31 *
32 * This provides a simple copy-on-write implementation of settings that gets cleared
33 * at the end of each test.
34 */
35public class TestableSettingsProvider extends MockContentProvider {
36
37 private static final String TAG = "TestableSettingsProvider";
38 private static final boolean DEBUG = false;
39 private static final String MY_UNIQUE_KEY = "Key_" + TestableSettingsProvider.class.getName();
40 private static TestableSettingsProvider sInstance;
41
42 private final ContentProviderClient mSettings;
43
44 private final HashMap<String, String> mValues = new HashMap<>();
45
46 private TestableSettingsProvider(ContentProviderClient settings) {
47 mSettings = settings;
48 }
49
50 void clearValuesAndCheck(Context context) {
51 mValues.put(key("global", MY_UNIQUE_KEY), MY_UNIQUE_KEY);
52 mValues.put(key("secure", MY_UNIQUE_KEY), MY_UNIQUE_KEY);
53 mValues.put(key("system", MY_UNIQUE_KEY), MY_UNIQUE_KEY);
54
55 // Verify that if any test is using TestableContext, they all have the correct settings
56 // provider.
57 assertEquals("Incorrect settings provider, test using incorrect Context?", MY_UNIQUE_KEY,
58 Settings.Global.getString(context.getContentResolver(), MY_UNIQUE_KEY));
59 assertEquals("Incorrect settings provider, test using incorrect Context?", MY_UNIQUE_KEY,
60 Settings.Secure.getString(context.getContentResolver(), MY_UNIQUE_KEY));
61 assertEquals("Incorrect settings provider, test using incorrect Context?", MY_UNIQUE_KEY,
62 Settings.System.getString(context.getContentResolver(), MY_UNIQUE_KEY));
63
64 mValues.clear();
65 }
66
67 public Bundle call(String method, String arg, Bundle extras) {
68 // Methods are "GET_system", "GET_global", "PUT_secure", etc.
69 final String[] commands = method.split("_", 2);
70 final String op = commands[0];
71 final String table = commands[1];
72
73 String k = key(table, arg);
74 String value;
75 Bundle out = new Bundle();
76 switch (op) {
77 case "GET":
78 if (mValues.containsKey(k)) {
79 value = mValues.get(k);
80 if (value != null) {
81 out.putString(Settings.NameValueTable.VALUE, value);
82 }
83 } else {
84 // Fall through to real settings.
85 try {
86 if (DEBUG) Log.d(TAG, "Falling through to real settings " + method);
87 // TODO: Add our own version of caching to handle this.
88 Bundle call = mSettings.call(method, arg, extras);
89 call.remove(Settings.CALL_METHOD_TRACK_GENERATION_KEY);
90 return call;
91 } catch (RemoteException e) {
92 throw new RuntimeException(e);
93 }
94 }
95 break;
96 case "PUT":
97 value = extras.getString(Settings.NameValueTable.VALUE, null);
98 mValues.put(k, value);
99 break;
100 default:
101 throw new UnsupportedOperationException("Unknown command " + method);
102 }
103 return out;
104 }
105
106 private static String key(String table, String key) {
107 return table + "_" + key;
108 }
109
110 /**
111 * Since the settings provider is cached inside android.provider.Settings, this must
112 * be gotten statically to ensure there is only one instance referenced.
113 */
114 static TestableSettingsProvider getFakeSettingsProvider(ContentProviderClient settings) {
115 if (sInstance == null) {
116 sInstance = new TestableSettingsProvider(settings);
117 }
118 return sInstance;
119 }
120}