1 /* 2 * Copyright (C) 2015 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.providers.settings; 18 19 import android.content.ContentResolver; 20 import android.content.ContentValues; 21 import android.content.Context; 22 import android.content.pm.UserInfo; 23 import android.database.Cursor; 24 import android.net.Uri; 25 import android.os.UserHandle; 26 import android.os.UserManager; 27 import android.provider.Settings; 28 import android.test.AndroidTestCase; 29 30 import java.util.List; 31 32 /** 33 * Base class for the SettingContentProvider tests. 34 */ 35 abstract class BaseSettingsProviderTest extends AndroidTestCase { 36 protected static final int SETTING_TYPE_GLOBAL = 1; 37 protected static final int SETTING_TYPE_SECURE = 2; 38 protected static final int SETTING_TYPE_SYSTEM = 3; 39 40 protected static final String FAKE_SETTING_NAME = "fake_setting_name"; 41 protected static final String FAKE_SETTING_NAME_1 = "fake_setting_name1"; 42 protected static final String FAKE_SETTING_NAME_2 = "fake_setting_name2"; 43 protected static final String FAKE_SETTING_VALUE = "fake_setting_value"; 44 protected static final String FAKE_SETTING_VALUE_1 = SettingsStateTest.CRAZY_STRING; 45 protected static final String FAKE_SETTING_VALUE_2 = null; 46 47 private static final String[] NAME_VALUE_COLUMNS = new String[] { 48 Settings.NameValueTable.NAME, Settings.NameValueTable.VALUE 49 }; 50 51 protected int mSecondaryUserId = UserHandle.USER_SYSTEM; 52 53 @Override 54 public void setContext(Context context) { 55 super.setContext(context); 56 57 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 58 List<UserInfo> users = userManager.getUsers(); 59 final int userCount = users.size(); 60 for (int i = 0; i < userCount; i++) { 61 UserInfo user = users.get(i); 62 if (!user.isPrimary() && !user.isManagedProfile()) { 63 mSecondaryUserId = user.id; 64 break; 65 } 66 } 67 } 68 69 protected void setStringViaFrontEndApiSetting(int type, String name, String value, int userId) { 70 ContentResolver contentResolver = getContext().getContentResolver(); 71 72 switch (type) { 73 case SETTING_TYPE_GLOBAL: { 74 Settings.Global.putStringForUser(contentResolver, name, value, userId); 75 } break; 76 77 case SETTING_TYPE_SECURE: { 78 Settings.Secure.putStringForUser(contentResolver, name, value, userId); 79 } break; 80 81 case SETTING_TYPE_SYSTEM: { 82 Settings.System.putStringForUser(contentResolver, name, value, userId); 83 } break; 84 85 default: { 86 throw new IllegalArgumentException("Invalid type: " + type); 87 } 88 } 89 } 90 91 protected String getStringViaFrontEndApiSetting(int type, String name, int userId) { 92 ContentResolver contentResolver = getContext().getContentResolver(); 93 94 switch (type) { 95 case SETTING_TYPE_GLOBAL: { 96 return Settings.Global.getStringForUser(contentResolver, name, userId); 97 } 98 99 case SETTING_TYPE_SECURE: { 100 return Settings.Secure.getStringForUser(contentResolver, name, userId); 101 } 102 103 case SETTING_TYPE_SYSTEM: { 104 return Settings.System.getStringForUser(contentResolver, name, userId); 105 } 106 107 default: { 108 throw new IllegalArgumentException("Invalid type: " + type); 109 } 110 } 111 } 112 113 protected Uri insertStringViaProviderApi(int type, String name, String value, 114 boolean withTableRowUri) { 115 Uri uri = getBaseUriForType(type); 116 if (withTableRowUri) { 117 uri = Uri.withAppendedPath(uri, name); 118 } 119 ContentValues values = new ContentValues(); 120 values.put(Settings.NameValueTable.NAME, name); 121 values.put(Settings.NameValueTable.VALUE, value); 122 123 return getContext().getContentResolver().insert(uri, values); 124 } 125 126 protected int deleteStringViaProviderApi(int type, String name) { 127 Uri uri = getBaseUriForType(type); 128 return getContext().getContentResolver().delete(uri, "name=?", new String[]{name}); 129 } 130 131 protected int updateStringViaProviderApiSetting(int type, String name, String value) { 132 Uri uri = getBaseUriForType(type); 133 ContentValues values = new ContentValues(); 134 values.put(Settings.NameValueTable.NAME, name); 135 values.put(Settings.NameValueTable.VALUE, value); 136 return getContext().getContentResolver().update(uri, values, "name=?", 137 new String[]{name}); 138 } 139 140 protected String queryStringViaProviderApi(int type, String name) { 141 return queryStringViaProviderApi(type, name, false, false); 142 } 143 144 protected String queryStringViaProviderApi(int type, String name, boolean queryStringInQuotes, 145 boolean appendNameToUri) { 146 final Uri uri; 147 final String queryString; 148 final String[] queryArgs; 149 150 if (appendNameToUri) { 151 uri = Uri.withAppendedPath(getBaseUriForType(type), name); 152 queryString = null; 153 queryArgs = null; 154 } else { 155 uri = getBaseUriForType(type); 156 queryString = queryStringInQuotes ? "(name=?)" : "name=?"; 157 queryArgs = new String[]{name}; 158 } 159 160 Cursor cursor = getContext().getContentResolver().query(uri, NAME_VALUE_COLUMNS, 161 queryString, queryArgs, null); 162 163 if (cursor == null) { 164 return null; 165 } 166 167 try { 168 if (cursor.moveToFirst()) { 169 final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE); 170 return cursor.getString(valueColumnIdx); 171 } 172 } finally { 173 cursor.close(); 174 } 175 176 return null; 177 } 178 179 protected static Uri getBaseUriForType(int type) { 180 switch (type) { 181 case SETTING_TYPE_GLOBAL: { 182 return Settings.Global.CONTENT_URI; 183 } 184 185 case SETTING_TYPE_SECURE: { 186 return Settings.Secure.CONTENT_URI; 187 } 188 189 case SETTING_TYPE_SYSTEM: { 190 return Settings.System.CONTENT_URI; 191 } 192 193 default: { 194 throw new IllegalArgumentException("Invalid type: " + type); 195 } 196 } 197 } 198 } 199