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