blob: 8ca849b8d37eee50a0143b5e4cfba94b6345967e [file] [log] [blame]
Lorenzo Colitti6d553f62016-06-05 02:20:29 +09001/*
2 * Copyright (C) 2016 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
Lorenzo Colittic8690892016-10-28 12:37:38 +090017package com.android.internal.util.test;
Lorenzo Colitti6d553f62016-06-05 02:20:29 +090018
19import android.net.Uri;
20import android.os.Bundle;
21import android.provider.Settings;
22import android.test.mock.MockContentProvider;
23import android.util.Log;
24
25import java.util.HashMap;
26
27/**
28 * Fake for system settings.
29 *
30 * To use, ensure that the Context used by the test code returns a ContentResolver that uses this
31 * provider for the Settings authority:
32 *
33 * class MyTestContext extends MockContext {
34 * ...
35 * private final MockContentResolver mContentResolver;
36 * public MyTestContext(...) {
37 * ...
38 * mContentResolver = new MockContentResolver();
39 * mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
40 * }
41 * ...
42 * @Override
43 * public ContentResolver getContentResolver() {
44 * return mContentResolver;
45 * }
46 *
47 * As long as the code under test is using the test Context, the actual code under test does not
48 * need to be modified, and can access Settings using the normal static methods:
49 *
50 * Settings.Global.getInt(cr, "my_setting", 0); // Returns 0.
51 * Settings.Global.putInt(cr, "my_setting", 5);
52 * Settings.Global.getInt(cr, "my_setting", 0); // Returns 5.
53 *
54 * Note that this class cannot be used in the same process as real settings. This is because it
55 * works by passing an alternate ContentResolver to Settings operations. Unfortunately, the Settings
56 * class only fetches the content provider from the passed-in ContentResolver the first time it's
57 * used, and after that stores it in a per-process static.
58 *
59 * TODO: evaluate implementing settings change notifications. This would require:
60 *
61 * 1. Making ContentResolver#registerContentObserver non-final and overriding it in
62 * MockContentResolver.
63 * 2. Making FakeSettingsProvider take a ContentResolver argument.
64 * 3. Calling ContentResolver#notifyChange(getUriFor(table, arg), ...) on every settings change.
65 */
66public class FakeSettingsProvider extends MockContentProvider {
67
68 private static final String TAG = FakeSettingsProvider.class.getSimpleName();
69 private static final boolean DBG = false;
70 private static final String[] TABLES = { "system", "secure", "global" };
71
72 private final HashMap<String, HashMap<String, String>> mTables = new HashMap<>();
73
74 public FakeSettingsProvider() {
75 for (int i = 0; i < TABLES.length; i++) {
76 mTables.put(TABLES[i], new HashMap<String, String>());
77 }
78 }
79
80 private Uri getUriFor(String table, String key) {
81 switch (table) {
82 case "system":
83 return Settings.System.getUriFor(key);
84 case "secure":
85 return Settings.Secure.getUriFor(key);
86 case "global":
87 return Settings.Global.getUriFor(key);
88 default:
89 throw new UnsupportedOperationException("Unknown settings table " + table);
90 }
91 }
92
93 public Bundle call(String method, String arg, Bundle extras) {
94 // Methods are "GET_system", "GET_global", "PUT_secure", etc.
95 String[] commands = method.split("_", 2);
96 String op = commands[0];
97 String table = commands[1];
98
99 Bundle out = new Bundle();
100 String value;
101 switch (op) {
102 case "GET":
103 value = mTables.get(table).get(arg);
104 if (value != null) {
105 if (DBG) {
106 Log.d(TAG, String.format("Returning fake setting %s.%s = %s",
107 table, arg, value));
108 }
109 out.putString(Settings.NameValueTable.VALUE, value);
110 }
111 break;
112 case "PUT":
113 value = extras.getString(Settings.NameValueTable.VALUE, null);
114 if (DBG) {
115 Log.d(TAG, String.format("Inserting fake setting %s.%s = %s",
116 table, arg, value));
117 }
118 if (value != null) {
119 mTables.get(table).put(arg, value);
120 } else {
121 mTables.get(table).remove(arg);
122 }
123 break;
124 default:
125 throw new UnsupportedOperationException("Unknown command " + method);
126 }
127
128 return out;
129 }
130}