1 /*
2  * Copyright (C) 2008 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 android.provider;
18 
19 import android.content.ContentResolver;
20 import android.content.ContentUris;
21 import android.content.ContentValues;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.pm.PackageManager;
25 import android.content.pm.ResolveInfo;
26 import android.content.pm.UserInfo;
27 import android.database.Cursor;
28 import android.net.Uri;
29 import android.os.UserHandle;
30 import android.os.UserManager;
31 import android.provider.Settings;
32 import android.test.AndroidTestCase;
33 import android.test.suitebuilder.annotation.MediumTest;
34 import android.test.suitebuilder.annotation.SmallTest;
35 
36 import java.util.List;
37 
38 /** Unit test for SettingsProvider. */
39 public class SettingsProviderTest extends AndroidTestCase {
40     @MediumTest
testNameValueCache()41     public void testNameValueCache() {
42         ContentResolver r = getContext().getContentResolver();
43         Settings.Secure.putString(r, "test_service", "Value");
44         assertEquals("Value", Settings.Secure.getString(r, "test_service"));
45 
46         // Make sure the value can be overwritten.
47         Settings.Secure.putString(r, "test_service", "New");
48         assertEquals("New", Settings.Secure.getString(r, "test_service"));
49 
50         // Also that delete works.
51         assertEquals(1, r.delete(Settings.Secure.getUriFor("test_service"), null, null));
52         assertEquals(null, Settings.Secure.getString(r, "test_service"));
53 
54         // Try all the same things in the System table
55         Settings.System.putString(r, "test_setting", "Value");
56         assertEquals("Value", Settings.System.getString(r, "test_setting"));
57 
58         Settings.System.putString(r, "test_setting", "New");
59         assertEquals("New", Settings.System.getString(r, "test_setting"));
60 
61         assertEquals(1, r.delete(Settings.System.getUriFor("test_setting"), null, null));
62         assertEquals(null, Settings.System.getString(r, "test_setting"));
63     }
64 
65     @MediumTest
testRowNameContentUri()66     public void testRowNameContentUri() {
67         ContentResolver r = getContext().getContentResolver();
68 
69         assertEquals("content://settings/system/test_setting",
70                 Settings.System.getUriFor("test_setting").toString());
71         assertEquals("content://settings/secure/test_service",
72                 Settings.Secure.getUriFor("test_service").toString());
73 
74         // These tables use the row name (not ID) as their content URI.
75         Uri tables[] = { Settings.System.CONTENT_URI, Settings.Secure.CONTENT_URI };
76         for (Uri table : tables) {
77             ContentValues v = new ContentValues();
78             v.put(Settings.System.NAME, "test_key");
79             v.put(Settings.System.VALUE, "Test");
80             Uri uri = r.insert(table, v);
81             assertEquals(table.toString() + "/test_key", uri.toString());
82 
83             // Query with a specific URI and no WHERE clause succeeds.
84             Cursor c = r.query(uri, null, null, null, null);
85             try {
86                 assertTrue(c.moveToNext());
87                 assertEquals("test_key", c.getString(c.getColumnIndex(Settings.System.NAME)));
88                 assertEquals("Test", c.getString(c.getColumnIndex(Settings.System.VALUE)));
89                 assertFalse(c.moveToNext());
90             } finally {
91                 c.close();
92             }
93 
94             // Query with a specific URI and a WHERE clause fails.
95             try {
96                 r.query(uri, null, "1", null, null);
97                 fail("UnsupportedOperationException expected");
98             } catch (UnsupportedOperationException e) {
99                 if (!e.toString().contains("WHERE clause")) throw e;
100             }
101 
102             // Query with a tablewide URI and a WHERE clause succeeds.
103             c = r.query(table, null, "name='test_key'", null, null);
104             try {
105                 assertTrue(c.moveToNext());
106                 assertEquals("test_key", c.getString(c.getColumnIndex(Settings.System.NAME)));
107                 assertEquals("Test", c.getString(c.getColumnIndex(Settings.System.VALUE)));
108                 assertFalse(c.moveToNext());
109             } finally {
110                 c.close();
111             }
112 
113             v = new ContentValues();
114             v.put(Settings.System.VALUE, "Toast");
115             assertEquals(1, r.update(uri, v, null, null));
116 
117             c = r.query(uri, null, null, null, null);
118             try {
119                 assertTrue(c.moveToNext());
120                 assertEquals("test_key", c.getString(c.getColumnIndex(Settings.System.NAME)));
121                 assertEquals("Toast", c.getString(c.getColumnIndex(Settings.System.VALUE)));
122                 assertFalse(c.moveToNext());
123             } finally {
124                 c.close();
125             }
126 
127             assertEquals(1, r.delete(uri, null, null));
128         }
129 
130         assertEquals(null, Settings.System.getString(r, "test_key"));
131         assertEquals(null, Settings.Secure.getString(r, "test_key"));
132     }
133 
134     @MediumTest
testSettingsChangeForOtherUser()135     public void testSettingsChangeForOtherUser() {
136         UserManager um = (UserManager) getContext().getSystemService(Context.USER_SERVICE);
137         ContentResolver r = getContext().getContentResolver();
138 
139         // Make sure there's an owner
140         assertTrue(findUser(um, UserHandle.USER_OWNER));
141 
142         // create a new user to use for testing
143         UserInfo otherUser = um.createUser("TestUser1", UserInfo.FLAG_GUEST);
144         assertTrue(otherUser != null);
145         try {
146             assertNotSame("Current calling user id should not be the new guest user",
147                     otherUser.id, UserHandle.getCallingUserId());
148 
149             Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "gps");
150             Settings.Secure.putStringForUser(r,
151                     Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "network", otherUser.id);
152 
153             assertEquals("gps",
154                     Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED));
155             assertEquals("network", Settings.Secure.getStringForUser(
156                     r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, otherUser.id));
157 
158             assertNotSame("Current calling user id should not be the new guest user",
159                     otherUser.id, UserHandle.getCallingUserId());
160             Settings.Secure.setLocationProviderEnabledForUser(r, "network", false, otherUser.id);
161             assertEquals("", Settings.Secure.getStringForUser(
162                     r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, otherUser.id));
163 
164         } finally {
165             // Tidy up
166             um.removeUser(otherUser.id);
167         }
168     }
169 
170     @MediumTest
testRowNumberContentUri()171     public void testRowNumberContentUri() {
172         ContentResolver r = getContext().getContentResolver();
173 
174         // The bookmarks table (and everything else) uses standard row number content URIs.
175         Uri uri = Settings.Bookmarks.add(r, new Intent("TEST"),
176                 "Test Title", "Test Folder", '*', 123);
177 
178         assertTrue(ContentUris.parseId(uri) > 0);
179 
180         assertEquals("TEST", Settings.Bookmarks.getIntentForShortcut(r, '*').getAction());
181 
182         ContentValues v = new ContentValues();
183         v.put(Settings.Bookmarks.INTENT, "#Intent;action=TOAST;end");
184         assertEquals(1, r.update(uri, v, null, null));
185 
186         assertEquals("TOAST", Settings.Bookmarks.getIntentForShortcut(r, '*').getAction());
187 
188         assertEquals(1, r.delete(uri, null, null));
189 
190         assertEquals(null, Settings.Bookmarks.getIntentForShortcut(r, '*'));
191     }
192 
193     @MediumTest
testParseProviderList()194     public void testParseProviderList() {
195         ContentResolver r = getContext().getContentResolver();
196 
197         // Make sure we get out what we put in.
198         Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
199                 "test1,test2,test3");
200         assertEquals(Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED),
201                 "test1,test2,test3");
202 
203         // Test adding a value
204         Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
205                 "");
206         Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "+test1");
207         assertEquals("test1",
208                 Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED));
209 
210         // Test adding a second value
211         Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "+test2");
212         assertEquals("test1,test2",
213                 Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED));
214 
215         // Test adding a third value
216         Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "+test3");
217         assertEquals("test1,test2,test3",
218                 Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED));
219 
220         // Test deleting the first value in a 3 item list
221         Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "-test1");
222         assertEquals("test2,test3",
223                 Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED));
224 
225         // Test deleting the middle value in a 3 item list
226         Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
227                 "test1,test2,test3");
228         Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "-test2");
229         assertEquals("test1,test3",
230                 Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED));
231 
232         // Test deleting the last value in a 3 item list
233         Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
234                 "test1,test2,test3");
235         Settings.Secure.putString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "-test3");
236         assertEquals("test1,test2",
237                 Settings.Secure.getString(r, Settings.Secure.LOCATION_PROVIDERS_ALLOWED));
238      }
239 
findUser(UserManager um, int userHandle)240     private boolean findUser(UserManager um, int userHandle) {
241         for (UserInfo user : um.getUsers()) {
242             if (user.id == userHandle) {
243                 return true;
244             }
245         }
246         return false;
247     }
248 
249     @MediumTest
testPerUserSettings()250     public void testPerUserSettings() {
251         UserManager um = (UserManager) getContext().getSystemService(Context.USER_SERVICE);
252         ContentResolver r = getContext().getContentResolver();
253 
254         // Make sure there's an owner
255         assertTrue(findUser(um, UserHandle.USER_OWNER));
256 
257         // create a new user to use for testing
258         UserInfo user = um.createUser("TestUser1", UserInfo.FLAG_GUEST);
259         assertTrue(user != null);
260 
261         try {
262             // Write some settings for that user as well as the current user
263             final String TEST_KEY = "test_setting";
264             final int SELF_VALUE = 40;
265             final int OTHER_VALUE = 27;
266 
267             Settings.System.putInt(r, TEST_KEY, SELF_VALUE);
268             Settings.System.putIntForUser(r, TEST_KEY, OTHER_VALUE, user.id);
269 
270             // Verify that they read back as intended
271             int myValue = Settings.System.getInt(r, TEST_KEY, 0);
272             int otherValue = Settings.System.getIntForUser(r, TEST_KEY, 0, user.id);
273             assertTrue("Running as user " + UserHandle.myUserId()
274                     + " and reading/writing as user " + user.id
275                     + ", expected to read " + SELF_VALUE + " but got " + myValue,
276                     myValue == SELF_VALUE);
277             assertTrue("Running as user " + UserHandle.myUserId()
278                     + " and reading/writing as user " + user.id
279                     + ", expected to read " + OTHER_VALUE + " but got " + otherValue,
280                     otherValue == OTHER_VALUE);
281         } finally {
282             // Tidy up
283             um.removeUser(user.id);
284         }
285     }
286 
287      @SmallTest
testSettings()288      public void testSettings() {
289         assertCanBeHandled(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
290         assertCanBeHandled(new Intent(Settings.ACTION_ADD_ACCOUNT));
291         assertCanBeHandled(new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS));
292         assertCanBeHandled(new Intent(Settings.ACTION_APN_SETTINGS));
293         assertCanBeHandled(new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
294                 .setData(Uri.parse("package:" + getContext().getPackageName())));
295         assertCanBeHandled(new Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS));
296         assertCanBeHandled(new Intent(Settings.ACTION_APPLICATION_SETTINGS));
297         assertCanBeHandled(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
298         assertCanBeHandled(new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS));
299         assertCanBeHandled(new Intent(Settings.ACTION_DATE_SETTINGS));
300         assertCanBeHandled(new Intent(Settings.ACTION_DEVICE_INFO_SETTINGS));
301         assertCanBeHandled(new Intent(Settings.ACTION_DISPLAY_SETTINGS));
302         assertCanBeHandled(new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS));
303         assertCanBeHandled(new Intent(Settings.ACTION_INTERNAL_STORAGE_SETTINGS));
304         assertCanBeHandled(new Intent(Settings.ACTION_LOCALE_SETTINGS));
305         assertCanBeHandled(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
306         assertCanBeHandled(new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
307         assertCanBeHandled(new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS));
308         assertCanBeHandled(new Intent(Settings.ACTION_MEMORY_CARD_SETTINGS));
309         assertCanBeHandled(new Intent(Settings.ACTION_NETWORK_OPERATOR_SETTINGS));
310         assertCanBeHandled(new Intent(Settings.ACTION_PRIVACY_SETTINGS));
311         assertCanBeHandled(new Intent(Settings.ACTION_QUICK_LAUNCH_SETTINGS));
312         assertCanBeHandled(new Intent(Settings.ACTION_SEARCH_SETTINGS));
313         assertCanBeHandled(new Intent(Settings.ACTION_SECURITY_SETTINGS));
314         assertCanBeHandled(new Intent(Settings.ACTION_SETTINGS));
315         assertCanBeHandled(new Intent(Settings.ACTION_SOUND_SETTINGS));
316         assertCanBeHandled(new Intent(Settings.ACTION_SYNC_SETTINGS));
317         assertCanBeHandled(new Intent(Settings.ACTION_SYSTEM_UPDATE_SETTINGS));
318         assertCanBeHandled(new Intent(Settings.ACTION_USER_DICTIONARY_SETTINGS));
319         assertCanBeHandled(new Intent(Settings.ACTION_WIFI_IP_SETTINGS));
320         assertCanBeHandled(new Intent(Settings.ACTION_WIFI_SETTINGS));
321         assertCanBeHandled(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
322     }
323 
assertCanBeHandled(final Intent intent)324     private void assertCanBeHandled(final Intent intent) {
325         PackageManager packageManager = mContext.getPackageManager();
326         List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, 0);
327         assertNotNull(resolveInfoList);
328         // one or more activity can handle this intent.
329         assertTrue(resolveInfoList.size() > 0);
330     }
331 }
332