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.cts;
18 
19 
20 import android.content.ContentResolver;
21 import android.database.Cursor;
22 import android.net.Uri;
23 import android.provider.Settings;
24 import android.provider.Settings.NameValueTable;
25 import android.test.AndroidTestCase;
26 
27 public class Settings_NameValueTableTest extends AndroidTestCase {
testPutString()28     public void testPutString() {
29         ContentResolver cr = mContext.getContentResolver();
30         Uri uri = Settings.System.CONTENT_URI;
31         String name = "name1";
32         String value = "value1";
33 
34         // before putString
35         Cursor c = cr.query(uri, null, null, null, null);
36         try {
37             assertNotNull(c);
38             int origCount = c.getCount();
39             c.close();
40 
41             MyNameValueTable.putString(cr, uri, name, value);
42             c = cr.query(uri, null, null, null, null);
43             assertNotNull(c);
44             assertEquals(origCount + 1, c.getCount());
45             c.close();
46 
47             // query this row
48             String selection = NameValueTable.NAME + "=\"" + name + "\"";
49             c = cr.query(uri, null, selection, null, null);
50             assertNotNull(c);
51             assertEquals(1, c.getCount());
52             c.moveToFirst();
53             assertEquals("name1", c.getString(c.getColumnIndexOrThrow(NameValueTable.NAME)));
54             assertEquals("value1", c.getString(c.getColumnIndexOrThrow(NameValueTable.VALUE)));
55             c.close();
56 
57             // delete this row
58             cr.delete(uri, selection, null);
59             c = cr.query(uri, null, null, null, null);
60             assertNotNull(c);
61             assertEquals(origCount, c.getCount());
62         } finally {
63             // TODO should clean up more better
64             c.close();
65         }
66     }
67 
testGetUriFor()68     public void testGetUriFor() {
69         Uri uri = Uri.parse("content://authority/path");
70         String name = "table";
71 
72         Uri res = NameValueTable.getUriFor(uri, name);
73         assertNotNull(res);
74         assertEquals(Uri.withAppendedPath(uri, name), res);
75     }
76 
77     private static class MyNameValueTable extends NameValueTable {
putString(ContentResolver resolver, Uri uri, String name, String value)78         protected static boolean putString(ContentResolver resolver, Uri uri, String name,
79                 String value) {
80             return NameValueTable.putString(resolver, uri, name, value);
81         }
82     }
83 }
84