2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.provider;
19 import android.content.ContentResolver;
20 import android.content.ContentUris;
21 import android.content.ContentValues;
22 import android.content.Intent;
23 import android.database.Cursor;
24 import android.net.Uri;
25 import android.provider.Settings;
26 import android.test.AndroidTestCase;
27 import android.test.suitebuilder.annotation.MediumTest;
29 /** Unit test for SettingsProvider. */
30 public class SettingsProviderTest extends AndroidTestCase {
32 public void testNameValueCache() {
33 ContentResolver r = getContext().getContentResolver();
34 Settings.Secure.putString(r, "test_service", "Value");
35 assertEquals("Value", Settings.Secure.getString(r, "test_service"));
37 // Make sure the value can be overwritten.
38 Settings.Secure.putString(r, "test_service", "New");
39 assertEquals("New", Settings.Secure.getString(r, "test_service"));
41 // Also that delete works.
42 assertEquals(1, r.delete(Settings.Secure.getUriFor("test_service"), null, null));
43 assertEquals(null, Settings.Secure.getString(r, "test_service"));
45 // Try all the same things in the System table
46 Settings.System.putString(r, "test_setting", "Value");
47 assertEquals("Value", Settings.System.getString(r, "test_setting"));
49 Settings.System.putString(r, "test_setting", "New");
50 assertEquals("New", Settings.System.getString(r, "test_setting"));
52 assertEquals(1, r.delete(Settings.System.getUriFor("test_setting"), null, null));
53 assertEquals(null, Settings.System.getString(r, "test_setting"));
57 public void testRowNameContentUri() {
58 ContentResolver r = getContext().getContentResolver();
60 assertEquals("content://settings/system/test_setting",
61 Settings.System.getUriFor("test_setting").toString());
62 assertEquals("content://settings/secure/test_service",
63 Settings.Secure.getUriFor("test_service").toString());
65 // These tables use the row name (not ID) as their content URI.
66 Uri tables[] = { Settings.System.CONTENT_URI, Settings.Secure.CONTENT_URI };
67 for (Uri table : tables) {
68 ContentValues v = new ContentValues();
69 v.put(Settings.System.NAME, "test_key");
70 v.put(Settings.System.VALUE, "Test");
71 Uri uri = r.insert(table, v);
72 assertEquals(table.toString() + "/test_key", uri.toString());
74 // Query with a specific URI and no WHERE clause succeeds.
75 Cursor c = r.query(uri, null, null, null, null);
77 assertTrue(c.moveToNext());
78 assertEquals("test_key", c.getString(c.getColumnIndex(Settings.System.NAME)));
79 assertEquals("Test", c.getString(c.getColumnIndex(Settings.System.VALUE)));
80 assertFalse(c.moveToNext());
85 // Query with a specific URI and a WHERE clause fails.
87 r.query(uri, null, "1", null, null);
88 fail("UnsupportedOperationException expected");
89 } catch (UnsupportedOperationException e) {
90 if (!e.toString().contains("WHERE clause")) throw e;
93 // Query with a tablewide URI and a WHERE clause succeeds.
94 c = r.query(table, null, "name='test_key'", null, null);
96 assertTrue(c.moveToNext());
97 assertEquals("test_key", c.getString(c.getColumnIndex(Settings.System.NAME)));
98 assertEquals("Test", c.getString(c.getColumnIndex(Settings.System.VALUE)));
99 assertFalse(c.moveToNext());
104 v = new ContentValues();
105 v.put(Settings.System.VALUE, "Toast");
106 assertEquals(1, r.update(uri, v, null, null));
108 c = r.query(uri, null, null, null, null);
110 assertTrue(c.moveToNext());
111 assertEquals("test_key", c.getString(c.getColumnIndex(Settings.System.NAME)));
112 assertEquals("Toast", c.getString(c.getColumnIndex(Settings.System.VALUE)));
113 assertFalse(c.moveToNext());
118 assertEquals(1, r.delete(uri, null, null));
121 assertEquals(null, Settings.System.getString(r, "test_key"));
122 assertEquals(null, Settings.Secure.getString(r, "test_key"));
126 public void testRowNumberContentUri() {
127 ContentResolver r = getContext().getContentResolver();
129 // The bookmarks table (and everything else) uses standard row number content URIs.
130 Uri uri = Settings.Bookmarks.add(r, new Intent("TEST"),
131 "Test Title", "Test Folder", '*', 123);
133 assertTrue(ContentUris.parseId(uri) > 0);
135 assertEquals("TEST", Settings.Bookmarks.getIntentForShortcut(r, '*').getAction());
137 ContentValues v = new ContentValues();
138 v.put(Settings.Bookmarks.INTENT, "#Intent;action=TOAST;end");
139 assertEquals(1, r.update(uri, v, null, null));
141 assertEquals("TOAST", Settings.Bookmarks.getIntentForShortcut(r, '*').getAction());
143 assertEquals(1, r.delete(uri, null, null));
145 assertEquals(null, Settings.Bookmarks.getIntentForShortcut(r, '*'));