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.content.res.Configuration; 22 import android.database.Cursor; 23 import android.net.Uri; 24 import android.os.ParcelFileDescriptor; 25 import android.provider.Settings.SettingNotFoundException; 26 import android.provider.Settings.System; 27 import android.test.InstrumentationTestCase; 28 29 import java.io.FileInputStream; 30 import java.io.InputStream; 31 import java.util.Scanner; 32 33 public class Settings_SystemTest extends InstrumentationTestCase { 34 private ContentResolver cr; 35 36 private static final String INT_FIELD = "IntField"; 37 private static final String LONG_FIELD = "LongField"; 38 private static final String FLOAT_FIELD = "FloatField"; 39 private static final String STRING_FIELD = "StringField"; 40 41 @Override 42 protected void setUp() throws Exception { 43 super.setUp(); 44 45 cr = getInstrumentation().getContext().getContentResolver(); 46 assertNotNull(cr); 47 } 48 49 private void deleteTestedRows() { 50 String selection = System.NAME + "=\"" + INT_FIELD + "\""; 51 cr.delete(System.CONTENT_URI, selection, null); 52 53 selection = System.NAME + "=\"" + LONG_FIELD + "\""; 54 cr.delete(System.CONTENT_URI, selection, null); 55 56 selection = System.NAME + "=\"" + FLOAT_FIELD + "\""; 57 cr.delete(System.CONTENT_URI, selection, null); 58 59 selection = System.NAME + "=\"" + STRING_FIELD + "\""; 60 cr.delete(System.CONTENT_URI, selection, null); 61 } 62 63 private void enableAppOps() { 64 StringBuilder cmd = new StringBuilder(); 65 cmd.append("appops set "); 66 cmd.append(getInstrumentation().getContext().getPackageName()); 67 cmd.append(" android:write_settings allow"); 68 getInstrumentation().getUiAutomation().executeShellCommand(cmd.toString()); 69 70 StringBuilder query = new StringBuilder(); 71 query.append("appops get "); 72 query.append(getInstrumentation().getContext().getPackageName()); 73 query.append(" android:write_settings"); 74 String queryStr = query.toString(); 75 76 String result = "No operations."; 77 while (result.contains("No operations")) { 78 ParcelFileDescriptor pfd = getInstrumentation().getUiAutomation().executeShellCommand( 79 queryStr); 80 InputStream inputStream = new FileInputStream(pfd.getFileDescriptor()); 81 result = convertStreamToString(inputStream); 82 } 83 } 84 85 private String convertStreamToString(InputStream is) { 86 try (Scanner scanner = new Scanner(is).useDelimiter("\\A")) { 87 return scanner.hasNext() ? scanner.next() : ""; 88 } 89 } 90 91 public void testSystemSettings() throws SettingNotFoundException { 92 /** 93 * first query the exist settings in System table, and then insert five 94 * rows: an int, a long, a float, a String, and a ShowGTalkServiceStatus. 95 * Get these six rows to check whether insert succeeded and then delete them. 96 */ 97 // Precondition: these rows must not exist in the db when we begin 98 deleteTestedRows(); 99 100 // first query exist rows 101 Cursor c = cr.query(System.CONTENT_URI, null, null, null, null); 102 103 // backup fontScale 104 Configuration cfg = new Configuration(); 105 System.getConfiguration(cr, cfg); 106 float store = cfg.fontScale; 107 108 try { 109 assertNotNull(c); 110 int origCount = c.getCount(); 111 c.close(); 112 113 String stringValue = "cts"; 114 115 // insert 4 rows, and update 1 rows 116 assertTrue(System.putInt(cr, INT_FIELD, 10)); 117 assertTrue(System.putLong(cr, LONG_FIELD, 20l)); 118 assertTrue(System.putFloat(cr, FLOAT_FIELD, 30.0f)); 119 assertTrue(System.putString(cr, STRING_FIELD, stringValue)); 120 121 c = cr.query(System.CONTENT_URI, null, null, null, null); 122 assertNotNull(c); 123 assertEquals(origCount + 4, c.getCount()); 124 c.close(); 125 126 // get these rows to assert 127 assertEquals(10, System.getInt(cr, INT_FIELD)); 128 assertEquals(20l, System.getLong(cr, LONG_FIELD)); 129 assertEquals(30.0f, System.getFloat(cr, FLOAT_FIELD), 0.001); 130 131 assertEquals(stringValue, System.getString(cr, STRING_FIELD)); 132 133 // delete the tested rows again 134 deleteTestedRows(); 135 136 c = cr.query(System.CONTENT_URI, null, null, null, null); 137 assertNotNull(c); 138 assertEquals(origCount, c.getCount()); 139 140 // update fontScale row 141 cfg = new Configuration(); 142 cfg.fontScale = 1.2f; 143 assertTrue(System.putConfiguration(cr, cfg)); 144 145 System.getConfiguration(cr, cfg); 146 assertEquals(1.2f, cfg.fontScale); 147 } finally { 148 // TODO should clean up more better 149 c.close(); 150 151 // restore the fontScale 152 cfg.fontScale = store; 153 assertTrue(System.putConfiguration(cr, cfg)); 154 } 155 } 156 157 public void testGetDefaultValues() { 158 assertEquals(10, System.getInt(cr, "int", 10)); 159 assertEquals(20, System.getLong(cr, "long", 20l)); 160 assertEquals(30.0f, System.getFloat(cr, "float", 30.0f), 0.001); 161 } 162 163 public void testGetUriFor() { 164 String name = "table"; 165 166 Uri uri = System.getUriFor(name); 167 assertNotNull(uri); 168 assertEquals(Uri.withAppendedPath(System.CONTENT_URI, name), uri); 169 } 170 } 171