• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.unit_tests;
18 
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;
28 
29 /** Unit test for SettingsProvider. */
30 public class SettingsProviderTest extends AndroidTestCase {
31     @MediumTest
testNameValueCache()32     public void testNameValueCache() {
33         ContentResolver r = getContext().getContentResolver();
34         Settings.Gservices.putString(r, "test_service", "Value");
35         assertEquals("Value", Settings.Gservices.getString(r, "test_service"));
36 
37         // Make sure the value can be overwritten.
38         Settings.Gservices.putString(r, "test_service", "New");
39         assertEquals("New", Settings.Gservices.getString(r, "test_service"));
40 
41         // Also that delete works.
42         assertEquals(1, r.delete(Settings.Gservices.getUriFor("test_service"), null, null));
43         assertEquals(null, Settings.Gservices.getString(r, "test_service"));
44 
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"));
48 
49         Settings.System.putString(r, "test_setting", "New");
50         assertEquals("New", Settings.System.getString(r, "test_setting"));
51 
52         assertEquals(1, r.delete(Settings.System.getUriFor("test_setting"), null, null));
53         assertEquals(null, Settings.System.getString(r, "test_setting"));
54     }
55 
56     @MediumTest
testRowNameContentUri()57     public void testRowNameContentUri() {
58         ContentResolver r = getContext().getContentResolver();
59 
60         assertEquals("content://settings/system/test_setting",
61                 Settings.System.getUriFor("test_setting").toString());
62         assertEquals("content://settings/gservices/test_service",
63                 Settings.Gservices.getUriFor("test_service").toString());
64 
65         // These tables use the row name (not ID) as their content URI.
66         Uri tables[] = { Settings.System.CONTENT_URI, Settings.Gservices.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());
73 
74             // Query with a specific URI and no WHERE clause succeeds.
75             Cursor c = r.query(uri, null, null, null, null);
76             try {
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());
81             } finally {
82                 c.close();
83             }
84 
85             // Query with a specific URI and a WHERE clause fails.
86             try {
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;
91             }
92 
93             // Query with a tablewide URI and a WHERE clause succeeds.
94             c = r.query(table, null, "name='test_key'", null, null);
95             try {
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());
100             } finally {
101                 c.close();
102             }
103 
104             v = new ContentValues();
105             v.put(Settings.System.VALUE, "Toast");
106             assertEquals(1, r.update(uri, v, null, null));
107 
108             c = r.query(uri, null, null, null, null);
109             try {
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());
114             } finally {
115                 c.close();
116             }
117 
118             assertEquals(1, r.delete(uri, null, null));
119         }
120 
121         assertEquals(null, Settings.System.getString(r, "test_key"));
122         assertEquals(null, Settings.Gservices.getString(r, "test_key"));
123     }
124 
125     @MediumTest
testRowNumberContentUri()126     public void testRowNumberContentUri() {
127         ContentResolver r = getContext().getContentResolver();
128 
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);
132 
133         assertTrue(ContentUris.parseId(uri) > 0);
134 
135         assertEquals("TEST", Settings.Bookmarks.getIntentForShortcut(r, '*').getAction());
136 
137         ContentValues v = new ContentValues();
138         v.put(Settings.Bookmarks.INTENT, "#Intent;action=TOAST;end");
139         assertEquals(1, r.update(uri, v, null, null));
140 
141         assertEquals("TOAST", Settings.Bookmarks.getIntentForShortcut(r, '*').getAction());
142 
143         assertEquals(1, r.delete(uri, null, null));
144 
145         assertEquals(null, Settings.Bookmarks.getIntentForShortcut(r, '*'));
146     }
147 }
148