sdk: Remove SettingsProvider tests

Change-Id: I3abde5201b7d103b038f98c05fdf0026a7834ea8
This commit is contained in:
Michael Bestas
2024-11-03 01:14:03 +02:00
parent 756427f973
commit 5efaee2299
9 changed files with 0 additions and 1026 deletions

View File

@@ -1,38 +0,0 @@
//
// Copyright (C) 2018 The LineageOS Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
android_test {
name: "LineageSettingsProviderTests",
instrumentation_for: "LineageSettingsProvider",
srcs: ["**/*.java"],
certificate: "platform",
libs: [
"android.test.base",
"android.test.runner",
],
optimize: {
optimize: true,
proguard_flags_files: ["proguard.flags"],
},
static_libs: [
"androidx.test.rules",
"org.lineageos.platform.internal",
],
}

View File

@@ -1,34 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2015 The CyanogenMod Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.lineageos.lineagesettings.tests">
<uses-permission android:name="lineageos.permission.WRITE_SETTINGS"/>
<uses-permission android:name="lineageos.permission.WRITE_SECURE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
<uses-permission android:name="android.permission.MANAGE_USERS" />
<instrumentation
android:name="androidx.test.runner.AndroidJUnitRunner"
android:targetPackage="org.lineageos.lineagesettings.tests"
android:label="Lineage Settings Provider Tests" />
<application>
<uses-library android:name="android.test.runner" />
</application>
</manifest>

View File

@@ -1,19 +0,0 @@
## Lineage Settings Provider Tests
The tests package contains coverage for the Lineage Settings provider as well as
its public interfaces.
To run the tests (on a live device), build and install LineageSettingsProviderTests.apk
and then run:
```adb shell am instrument org.lineageos.lineagesettings.tests/androidx.test.runner.AndroidJUnitRunner```
Note: we don't use -w to wait for the results because some of the tests involve creating
and removing a guest account which causes adb connections to get reset.
View the results with:
```adb logcat | grep TestRunner```
End of the output should read something like:
```09-20 16:40:52.879 4146 4165 I TestRunner: run finished: 30 tests, 0 failed, 0 ignored```

View File

@@ -1,50 +0,0 @@
# Copyright (C) 2016 The CyanogenMod Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Don't skip non public library classes, make sure we're not keeping anything which will get mapped against api verification.
-dontskipnonpubliclibraryclasses
# Do the same with class members
-dontskipnonpubliclibraryclassmembers
# Keep test packages
-keep class androidx.** { *; }
-keep class android.test.** { *; }
-keep public class * extends androidx.** { *; }
-keep public class * extends android.test.** { *; }
-keep interface androidx.** { *; }
-keep interface android.test.** { *; }
# Keep all junit classes
-keep class junit.** { *; }
-keep class org.junit.** { *; }
-keep interface junit.** { *; }
-keep interface org.junit.** { *; }
# Keep compiled java classes from declared aidl's within the test package
-keep public class * implements android.os.IInterface { *; }
# Don't warn about the Android Support Test JUnit Runner
-dontwarn androidx.**
-dontwarn android.test.**
# Don't warn about junit
-dontwarn junit.**
-dontwarn org.junit.**
# Always process
-forceprocessing
# Make sure not to obfuscate the output
-dontobfuscate

View File

@@ -1,127 +0,0 @@
/**
* Copyright (c) 2016, The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lineageos.lineagesettings.tests;
import android.content.ContentResolver;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import lineageos.providers.LineageSettings;
public class LineageSettingsGlobalTests extends AndroidTestCase {
private ContentResolver mContentResolver;
private static final String UNREALISTIC_SETTING = "_______UNREAL_______";
@Override
protected void setUp() throws Exception {
super.setUp();
mContentResolver = mContext.getContentResolver();
}
@SmallTest
public void testFloat() {
final float expectedFloatValue = 1.0f;
LineageSettings.Global.putFloat(mContentResolver,
LineageSettings.Global.__MAGICAL_TEST_PASSING_ENABLER, expectedFloatValue);
try {
float actualValue = LineageSettings.Global.getFloat(mContentResolver,
LineageSettings.Global.__MAGICAL_TEST_PASSING_ENABLER);
assertEquals(expectedFloatValue, actualValue);
} catch (LineageSettings.LineageSettingNotFoundException e) {
throw new AssertionError(e);
}
}
@SmallTest
public void testFloatWithDefault() {
final float expectedDefaultFloatValue = 1.5f;
float actualValue = LineageSettings.Global.getFloat(mContentResolver,
UNREALISTIC_SETTING, expectedDefaultFloatValue);
assertEquals(expectedDefaultFloatValue, actualValue);
}
@SmallTest
public void testInt() {
final int expectedIntValue = 2;
LineageSettings.Global.putInt(mContentResolver,
LineageSettings.Global.__MAGICAL_TEST_PASSING_ENABLER, expectedIntValue);
try {
int actualValue = LineageSettings.Global.getInt(mContentResolver,
LineageSettings.Global.__MAGICAL_TEST_PASSING_ENABLER);
assertEquals(expectedIntValue, actualValue);
} catch (LineageSettings.LineageSettingNotFoundException e) {
throw new AssertionError(e);
}
}
@SmallTest
public void testIntWithDefault() {
final int expectedDefaultIntValue = 11;
int actualValue = LineageSettings.Global.getInt(mContentResolver,
UNREALISTIC_SETTING, expectedDefaultIntValue);
assertEquals(expectedDefaultIntValue, actualValue);
}
@SmallTest
public void testLong() {
final long expectedLongValue = 3l;
LineageSettings.Global.putLong(mContentResolver,
LineageSettings.Global.__MAGICAL_TEST_PASSING_ENABLER, expectedLongValue);
try {
long actualValue = LineageSettings.Global.getLong(mContentResolver,
LineageSettings.Global.__MAGICAL_TEST_PASSING_ENABLER);
assertEquals(expectedLongValue, actualValue);
} catch (LineageSettings.LineageSettingNotFoundException e) {
throw new AssertionError(e);
}
}
@SmallTest
public void testLongWithDefault() {
final long expectedDefaultLongValue = 17l;
long actualValue = LineageSettings.Global.getLong(mContentResolver,
UNREALISTIC_SETTING, expectedDefaultLongValue);
assertEquals(expectedDefaultLongValue, actualValue);
}
@SmallTest
public void testString() {
final String expectedStringValue = "4";
LineageSettings.Global.putString(mContentResolver,
LineageSettings.Global.__MAGICAL_TEST_PASSING_ENABLER, expectedStringValue);
String actualValue = LineageSettings.Global.getString(mContentResolver,
LineageSettings.Global.__MAGICAL_TEST_PASSING_ENABLER);
assertEquals(expectedStringValue, actualValue);
}
@SmallTest
public void testGetUri() {
final Uri expectedUri = Uri.withAppendedPath(LineageSettings.Global.CONTENT_URI,
LineageSettings.Global.__MAGICAL_TEST_PASSING_ENABLER);
final Uri actualUri = LineageSettings.Global.getUriFor(
LineageSettings.Global.__MAGICAL_TEST_PASSING_ENABLER);
assertEquals(expectedUri, actualUri);
}
}

View File

@@ -1,246 +0,0 @@
/**
* Copyright (C) 2016 The CyanogenMod Project
* 2021 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lineageos.lineagesettings.tests;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.UserHandle;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.text.TextUtils;
import android.util.TypedValue;
import lineageos.providers.LineageSettings;
import org.lineageos.lineagesettings.LineageDatabaseHelper;
import org.lineageos.lineagesettings.LineageSettingsProvider;
import java.util.ArrayList;
/**
* Created by adnan on 1/25/16.
*/
public class LineageSettingsProviderDefaultsTest extends AndroidTestCase {
private ContentResolver mContentResolver;
private boolean mHasMigratedSettings;
private Resources mRemoteResources;
// These data structures are set up in a way that is easier for manual input of new defaults
private static ArrayList<Setting> SYSTEM_SETTINGS_DEFAULTS = new ArrayList<Setting>();
private static ArrayList<Setting> SECURE_SETTINGS_DEFAULTS = new ArrayList<Setting>();
private static ArrayList<Setting> GLOBAL_SETTINGS_DEFAULTS = new ArrayList<Setting>();
//SYSTEM
static {
SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
LineageSettings.System.STATUS_BAR_QUICK_QS_PULLDOWN,
"R.integer.def_qs_quick_pulldown"));
SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
LineageSettings.System.NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL,
"R.integer.def_notification_brightness_level"));
SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
LineageSettings.System.SYSTEM_PROFILES_ENABLED,
"R.bool.def_profiles_enabled"));
SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
LineageSettings.System.NOTIFICATION_LIGHT_PULSE_CUSTOM_ENABLE,
"R.bool.def_notification_pulse_custom_enable"));
SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
LineageSettings.System.SWAP_VOLUME_KEYS_ON_ROTATION,
"R.bool.def_swap_volume_keys_on_rotation"));
SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
LineageSettings.System.NOTIFICATION_LIGHT_PULSE_CUSTOM_VALUES,
"R.string.def_notification_pulse_custom_value"));
SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
LineageSettings.System.STATUS_BAR_BATTERY_STYLE,
"R.integer.def_battery_style"));
SYSTEM_SETTINGS_DEFAULTS.add(new Setting(
LineageSettings.System.FORCE_SHOW_NAVBAR,
"R.integer.def_force_show_navbar"));
}
//SECURE
static {
SECURE_SETTINGS_DEFAULTS.add(new Setting(
LineageSettings.Secure.STATS_COLLECTION,
"R.bool.def_stats_collection"));
SECURE_SETTINGS_DEFAULTS.add(new Setting(
LineageSettings.Secure.LOCKSCREEN_VISUALIZER_ENABLED,
"R.bool.def_lockscreen_visualizer"));
}
//GLOBAL
/*
static {
GLOBAL_SETTINGS_DEFAULTS.add(new Setting(
LineageSettings.Global.WAKE_WHEN_PLUGGED_OR_UNPLUGGED,
false));
}
*/
@Override
protected void setUp() throws Exception {
super.setUp();
mContentResolver = getContext().getContentResolver();
mHasMigratedSettings = getContext().getSharedPreferences(LineageSettingsProvider.TAG,
Context.MODE_PRIVATE).getBoolean(LineageSettingsProvider.PREF_HAS_MIGRATED_LINEAGE_SETTINGS,
false);
mRemoteResources = getRemoteResources("org.lineageos.lineagesettings");
}
@SmallTest
public void testVerifySystemSettingsDefault() {
if (verifyNotMigratedSettings()) {
for (Setting setting : SYSTEM_SETTINGS_DEFAULTS) {
verifyDefaultSettingForTable(setting, LineageDatabaseHelper.LineageTableNames.TABLE_SYSTEM);
}
}
}
@SmallTest
public void testVerifySecureSettingsDefaults() {
if (verifyNotMigratedSettings()) {
for (Setting setting : SECURE_SETTINGS_DEFAULTS) {
verifyDefaultSettingForTable(setting, LineageDatabaseHelper.LineageTableNames.TABLE_SECURE);
}
}
}
@SmallTest
public void testVerifyGlobalSettingsDefaults() {
if (verifyNotMigratedSettings()) {
for (Setting setting : GLOBAL_SETTINGS_DEFAULTS) {
verifyDefaultSettingForTable(setting, LineageDatabaseHelper.LineageTableNames.TABLE_GLOBAL);
}
}
}
private boolean verifyNotMigratedSettings() {
return !mHasMigratedSettings;
}
private void verifyDefaultSettingForTable(Setting setting, String table) {
TypedValue value = new TypedValue();
try {
int identifier = mRemoteResources.getIdentifier(
setting.mDefResName, setting.mType, "org.lineageos.lineagesettings");
mRemoteResources.getValue(identifier, value, true);
} catch (Resources.NotFoundException e) {
// Resource not found, can't verify because it probably wasn't loaded in
throw new AssertionError("Unable to find resource for " + setting.mKey);
}
try {
switch (value.type) {
case TypedValue.TYPE_INT_DEC:
int actualValue = getIntForTable(setting, table);
try {
assertEquals(value.data, actualValue);
} catch (AssertionError e) {
throw new AssertionError("Compared value of " + setting.mKey + " expected "
+ value.data + " got " + actualValue);
}
break;
case TypedValue.TYPE_INT_BOOLEAN:
int actualBooleanValue = getIntForTable(setting, table);
try {
//This is gross
//Boolean can be "true" as long as it isn't 0
if (value.data != 0) {
value.data = 1;
}
assertEquals(value.data, actualBooleanValue);
} catch (AssertionError e) {
throw new AssertionError("Compared value of " + setting.mKey + " expected "
+ value.data + " got " + actualBooleanValue);
}
break;
case TypedValue.TYPE_STRING:
if (!TextUtils.isEmpty(value.string)) {
//This should really be done as a parameterized test
String actualStringValue = getStringForTable(setting, table);
try {
assertEquals(value.string, actualStringValue);
} catch (AssertionError e) {
throw new AssertionError("Compared value of " + setting.mKey
+ " expected " + value.string + " got " + actualStringValue);
}
}
break;
case TypedValue.TYPE_NULL:
break;
}
} catch (LineageSettings.LineageSettingNotFoundException e) {
e.printStackTrace();
throw new AssertionError("Setting " + setting.mKey + " not found!");
}
}
private int getIntForTable(Setting setting, String table)
throws LineageSettings.LineageSettingNotFoundException {
switch (table) {
case LineageDatabaseHelper.LineageTableNames.TABLE_SYSTEM:
return LineageSettings.System.getIntForUser(mContentResolver, setting.mKey,
UserHandle.USER_SYSTEM);
case LineageDatabaseHelper.LineageTableNames.TABLE_SECURE:
return LineageSettings.Secure.getIntForUser(mContentResolver, setting.mKey,
UserHandle.USER_SYSTEM);
case LineageDatabaseHelper.LineageTableNames.TABLE_GLOBAL:
return LineageSettings.Global.getIntForUser(mContentResolver, setting.mKey,
UserHandle.USER_SYSTEM);
default:
throw new AssertionError("Invalid or empty table!");
}
}
private String getStringForTable(Setting setting, String table)
throws LineageSettings.LineageSettingNotFoundException {
switch (table) {
case LineageDatabaseHelper.LineageTableNames.TABLE_SYSTEM:
return LineageSettings.System.getStringForUser(mContentResolver, setting.mKey,
UserHandle.USER_SYSTEM);
case LineageDatabaseHelper.LineageTableNames.TABLE_SECURE:
return LineageSettings.Secure.getStringForUser(mContentResolver, setting.mKey,
UserHandle.USER_SYSTEM);
case LineageDatabaseHelper.LineageTableNames.TABLE_GLOBAL:
return LineageSettings.Global.getStringForUser(mContentResolver, setting.mKey,
UserHandle.USER_SYSTEM);
default:
throw new AssertionError("Invalid or empty table!");
}
}
private static class Setting {
public String mKey;
public String mDefResName;
public String mType;
public Setting(String key, String defResId) {
mKey = key;
String[] parts = defResId.split("\\.");
mType = parts[1];
mDefResName = parts[2];
}
}
private Resources getRemoteResources(String packageName)
throws PackageManager.NameNotFoundException {
PackageManager packageManager = mContext.getPackageManager();
return packageManager.getResourcesForApplication(packageName);
}
}

View File

@@ -1,258 +0,0 @@
/**
* Copyright (c) 2015, The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lineageos.lineagesettings.tests;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.UserInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.text.TextUtils;
import lineageos.providers.LineageSettings;
import java.util.LinkedHashMap;
import java.util.Map;
public class LineageSettingsProviderTest extends AndroidTestCase {
private static final String TAG = "LineageSettingsProviderTest";
private static final LinkedHashMap<String, String> sMap = new LinkedHashMap<String, String>();
static {
sMap.put("testKey1", "value1");
sMap.put("testKey2", "value2");
sMap.put("testKey3", "value3");
}
private static final String[] PROJECTIONS = new String[] { Settings.NameValueTable.NAME,
Settings.NameValueTable.VALUE };
private ContentResolver mContentResolver;
private UserManager mUserManager;
private UserInfo mGuest;
@Override
public void setUp() {
mContentResolver = mContext.getContentResolver();
mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
}
@Override
public void tearDown() {
if (mGuest != null) {
mUserManager.removeUser(mGuest.id);
}
}
@MediumTest
public void testMigrateLineageSettingsForOtherUser() {
// Make sure there's an owner
assertTrue(findUser(mUserManager, UserHandle.USER_SYSTEM));
mGuest = mUserManager.createGuest(mContext, "GuestUser1");
assertNotNull(mGuest);
testMigrateSettingsForUser(mGuest.id);
}
private void testMigrateSettingsForUser(int userId) {
// Setup values in Settings
/*final String expectedPullDownValue = "testQuickPullDownValue";
Settings.System.putStringForUser(mContentResolver,
LineageSettingsProvider.LegacyLineageSettings.STATUS_BAR_QUICK_QS_PULLDOWN,
expectedPullDownValue, userId);
final int expectedKeyboardBrightness = 4;
Settings.Secure.putIntForUser(mContentResolver,
LineageSettingsProvider.LegacyLineageSettings.KEYBOARD_BRIGHTNESS,
expectedKeyboardBrightness, userId);
Bundle arg = new Bundle();
arg.putInt(LineageSettings.CALL_METHOD_USER_KEY, userId);
IContentProvider contentProvider = mContentResolver.acquireProvider(
LineageSettings.AUTHORITY);
try{
// Trigger migrate settings for guest
contentProvider.call(mContentResolver.getPackageName(),
LineageSettings.CALL_METHOD_MIGRATE_SETTINGS_FOR_USER, null, arg);
} catch (RemoteException ex) {
fail("Failed to trigger settings migration due to RemoteException");
}
// Check values
final String actualPullDownValue = LineageSettings.System.getStringForUser(mContentResolver,
LineageSettings.System.QS_QUICK_PULLDOWN, userId);
assertEquals(expectedPullDownValue, actualPullDownValue);
final int actualKeyboardBrightness = LineageSettings.Secure.getIntForUser(mContentResolver,
LineageSettings.Secure.KEYBOARD_BRIGHTNESS, -1, userId);
assertEquals(expectedKeyboardBrightness, actualKeyboardBrightness);*/
}
private boolean findUser(UserManager userManager, int userHandle) {
for (UserInfo user : userManager.getUsers()) {
if (user.id == userHandle) {
return true;
}
}
return false;
}
@MediumTest
public void testBulkInsertSuccess() {
ContentValues[] contentValues = new ContentValues[sMap.size()];
String[] keyValues = new String[sMap.size()];
int count = 0;
for (Map.Entry<String, String> kVPair : sMap.entrySet()) {
ContentValues contentValue = new ContentValues();
final String key = kVPair.getKey();
contentValue.put(Settings.NameValueTable.NAME, key);
keyValues[count] = key;
contentValue.put(Settings.NameValueTable.VALUE, kVPair.getValue());
contentValues[count++] = contentValue;
}
testBulkInsertForUri(LineageSettings.System.CONTENT_URI, contentValues, keyValues);
testBulkInsertForUri(LineageSettings.Secure.CONTENT_URI, contentValues, keyValues);
testBulkInsertForUri(LineageSettings.Global.CONTENT_URI, contentValues, keyValues);
}
private void testBulkInsertForUri(Uri uri, ContentValues[] contentValues, String[] keyValues) {
int rowsInserted = mContentResolver.bulkInsert(uri, contentValues);
assertEquals(sMap.size(), rowsInserted);
final String placeholderSymbol = "?";
String[] placeholders = new String[contentValues.length];
for (int i = 0; i < placeholders.length; i++) {
placeholders[i] = placeholderSymbol;
}
final String placeholdersString = TextUtils.join(",", placeholders);
Cursor queryCursor = mContentResolver.query(uri, PROJECTIONS,
Settings.NameValueTable.NAME + " IN (" + placeholdersString + ")", keyValues,
null);
assertEquals(contentValues.length, queryCursor.getCount());
try {
while (queryCursor.moveToNext()) {
assertEquals(PROJECTIONS.length, queryCursor.getColumnCount());
String actualKey = queryCursor.getString(0);
assertTrue(sMap.containsKey(actualKey));
assertEquals(sMap.get(actualKey), queryCursor.getString(1));
}
}
finally {
queryCursor.close();
}
// TODO: Find a better way to cleanup database/use ProviderTestCase2 without process crash
for (String key : sMap.keySet()) {
mContentResolver.delete(uri, Settings.NameValueTable.NAME + " = ?",
new String[]{ key });
}
}
@MediumTest
public void testInsertUpdateDeleteSuccess() {
//testInsertUpdateDeleteForUri(LineageSettings.System.CONTENT_URI);
testInsertUpdateDeleteForUri(LineageSettings.Secure.CONTENT_URI);
testInsertUpdateDeleteForUri(LineageSettings.Global.CONTENT_URI);
}
private void testInsertUpdateDeleteForUri(Uri uri) {
String key = "key";
String value1 = "value1";
String value2 = "value2";
// test insert
ContentValues contentValue = new ContentValues();
contentValue.put(Settings.NameValueTable.NAME, key);
contentValue.put(Settings.NameValueTable.VALUE, value1);
Uri expectedUri = uri.withAppendedPath(uri, key);
Uri returnUri = mContentResolver.insert(uri, contentValue);
assertEquals(expectedUri, returnUri);
Cursor queryCursor = null;
try {
// check insert
queryCursor = mContentResolver.query(uri, PROJECTIONS, Settings.NameValueTable.NAME +
" = ?", new String[]{ key }, null);
assertEquals(1, queryCursor.getCount());
assertExpectedKeyValuePair(queryCursor, key, value1);
// check insert with returned uri
queryCursor = mContentResolver.query(returnUri, PROJECTIONS, null, null, null);
assertEquals(1, queryCursor.getCount());
assertExpectedKeyValuePair(queryCursor, key, value1);
// test update
contentValue.clear();
contentValue.put(Settings.NameValueTable.VALUE, value2);
int rowsAffected = mContentResolver.update(uri, contentValue,
Settings.NameValueTable.NAME + " = ?", new String[]{ key });
assertEquals(1, rowsAffected);
// check update
queryCursor = mContentResolver.query(uri, PROJECTIONS, Settings.NameValueTable.NAME +
" = ?", new String[]{ key }, null);
assertEquals(1, queryCursor.getCount());
assertExpectedKeyValuePair(queryCursor, key, value2);
// test delete
rowsAffected = mContentResolver.delete(uri, Settings.NameValueTable.NAME + " = ?",
new String[]{ key });
assertEquals(1, rowsAffected);
// check delete
queryCursor = mContentResolver.query(uri, PROJECTIONS, Settings.NameValueTable.NAME +
" = ?", new String[]{ key }, null);
assertEquals(0, queryCursor.getCount());
} finally {
if (queryCursor != null) {
queryCursor.close();
}
}
}
private void assertExpectedKeyValuePair(Cursor cursor, String expectedKey,
String expectedValue) {
cursor.moveToNext();
assertEquals(PROJECTIONS.length, cursor.getColumnCount());
String actualKey = cursor.getString(0);
assertEquals(expectedKey, actualKey);
assertEquals(expectedValue, cursor.getString(1));
}
}

View File

@@ -1,127 +0,0 @@
/**
* Copyright (c) 2016, The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lineageos.lineagesettings.tests;
import android.content.ContentResolver;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import lineageos.providers.LineageSettings;
public class LineageSettingsSecureTests extends AndroidTestCase {
private ContentResolver mContentResolver;
private static final String UNREALISTIC_SETTING = "_______UNREAL_______";
@Override
protected void setUp() throws Exception {
super.setUp();
mContentResolver = mContext.getContentResolver();
}
@SmallTest
public void testFloat() {
final float expectedFloatValue = 1.0f;
LineageSettings.Secure.putFloat(mContentResolver,
LineageSettings.Secure.__MAGICAL_TEST_PASSING_ENABLER, expectedFloatValue);
try {
float actualValue = LineageSettings.Secure.getFloat(mContentResolver,
LineageSettings.Secure.__MAGICAL_TEST_PASSING_ENABLER);
assertEquals(expectedFloatValue, actualValue);
} catch (LineageSettings.LineageSettingNotFoundException e) {
throw new AssertionError(e);
}
}
@SmallTest
public void testFloatWithDefault() {
final float expectedDefaultFloatValue = 1.5f;
float actualValue = LineageSettings.Secure.getFloat(mContentResolver,
UNREALISTIC_SETTING, expectedDefaultFloatValue);
assertEquals(expectedDefaultFloatValue, actualValue);
}
@SmallTest
public void testInt() {
final int expectedIntValue = 2;
LineageSettings.Secure.putInt(mContentResolver,
LineageSettings.Secure.__MAGICAL_TEST_PASSING_ENABLER, expectedIntValue);
try {
int actualValue = LineageSettings.Secure.getInt(mContentResolver,
LineageSettings.Secure.__MAGICAL_TEST_PASSING_ENABLER);
assertEquals(expectedIntValue, actualValue);
} catch (LineageSettings.LineageSettingNotFoundException e) {
throw new AssertionError(e);
}
}
@SmallTest
public void testIntWithDefault() {
final int expectedDefaultIntValue = 11;
int actualValue = LineageSettings.Secure.getInt(mContentResolver,
UNREALISTIC_SETTING, expectedDefaultIntValue);
assertEquals(expectedDefaultIntValue, actualValue);
}
@SmallTest
public void testLong() {
final long expectedLongValue = 3l;
LineageSettings.Secure.putLong(mContentResolver,
LineageSettings.Secure.__MAGICAL_TEST_PASSING_ENABLER, expectedLongValue);
try {
long actualValue = LineageSettings.Secure.getLong(mContentResolver,
LineageSettings.Secure.__MAGICAL_TEST_PASSING_ENABLER);
assertEquals(expectedLongValue, actualValue);
} catch (LineageSettings.LineageSettingNotFoundException e) {
throw new AssertionError(e);
}
}
@SmallTest
public void testLongWithDefault() {
final long expectedDefaultLongValue = 17l;
long actualValue = LineageSettings.Secure.getLong(mContentResolver,
UNREALISTIC_SETTING, expectedDefaultLongValue);
assertEquals(expectedDefaultLongValue, actualValue);
}
@SmallTest
public void testString() {
final String expectedStringValue = "4";
LineageSettings.Secure.putString(mContentResolver,
LineageSettings.Secure.__MAGICAL_TEST_PASSING_ENABLER, expectedStringValue);
String actualValue = LineageSettings.Secure.getString(mContentResolver,
LineageSettings.Secure.__MAGICAL_TEST_PASSING_ENABLER);
assertEquals(expectedStringValue, actualValue);
}
@SmallTest
public void testGetUri() {
final Uri expectedUri = Uri.withAppendedPath(LineageSettings.Secure.CONTENT_URI,
LineageSettings.Secure.__MAGICAL_TEST_PASSING_ENABLER);
final Uri actualUri = LineageSettings.Secure.getUriFor(
LineageSettings.Secure.__MAGICAL_TEST_PASSING_ENABLER);
assertEquals(expectedUri, actualUri);
}
}

View File

@@ -1,127 +0,0 @@
/**
* Copyright (c) 2016, The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lineageos.lineagesettings.tests;
import android.content.ContentResolver;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import lineageos.providers.LineageSettings;
public class LineageSettingsSystemTests extends AndroidTestCase {
private ContentResolver mContentResolver;
private static final String UNREALISTIC_SETTING = "_______UNREAL_______";
@Override
protected void setUp() throws Exception {
super.setUp();
mContentResolver = mContext.getContentResolver();
}
@SmallTest
public void testFloat() {
final float expectedFloatValue = 1.0f;
LineageSettings.System.putFloat(mContentResolver,
LineageSettings.System.__MAGICAL_TEST_PASSING_ENABLER, expectedFloatValue);
try {
float actualValue = LineageSettings.System.getFloat(mContentResolver,
LineageSettings.System.__MAGICAL_TEST_PASSING_ENABLER);
assertEquals(expectedFloatValue, actualValue);
} catch (LineageSettings.LineageSettingNotFoundException e) {
throw new AssertionError(e);
}
}
@SmallTest
public void testFloatWithDefault() {
final float expectedDefaultFloatValue = 1.5f;
float actualValue = LineageSettings.System.getFloat(mContentResolver,
UNREALISTIC_SETTING, expectedDefaultFloatValue);
assertEquals(expectedDefaultFloatValue, actualValue);
}
@SmallTest
public void testInt() {
final int expectedIntValue = 2;
LineageSettings.System.putInt(mContentResolver,
LineageSettings.System.__MAGICAL_TEST_PASSING_ENABLER, expectedIntValue);
try {
int actualValue = LineageSettings.System.getInt(mContentResolver,
LineageSettings.System.__MAGICAL_TEST_PASSING_ENABLER);
assertEquals(expectedIntValue, actualValue);
} catch (LineageSettings.LineageSettingNotFoundException e) {
throw new AssertionError(e);
}
}
@SmallTest
public void testIntWithDefault() {
final int expectedDefaultIntValue = 11;
int actualValue = LineageSettings.System.getInt(mContentResolver,
UNREALISTIC_SETTING, expectedDefaultIntValue);
assertEquals(expectedDefaultIntValue, actualValue);
}
@SmallTest
public void testLong() {
final long expectedLongValue = 3l;
LineageSettings.System.putLong(mContentResolver,
LineageSettings.System.__MAGICAL_TEST_PASSING_ENABLER, expectedLongValue);
try {
long actualValue = LineageSettings.System.getLong(mContentResolver,
LineageSettings.System.__MAGICAL_TEST_PASSING_ENABLER);
assertEquals(expectedLongValue, actualValue);
} catch (LineageSettings.LineageSettingNotFoundException e) {
throw new AssertionError(e);
}
}
@SmallTest
public void testLongWithDefault() {
final long expectedDefaultLongValue = 17l;
long actualValue = LineageSettings.System.getLong(mContentResolver,
UNREALISTIC_SETTING, expectedDefaultLongValue);
assertEquals(expectedDefaultLongValue, actualValue);
}
@SmallTest
public void testString() {
final String expectedStringValue = "4";
LineageSettings.System.putString(mContentResolver,
LineageSettings.System.__MAGICAL_TEST_PASSING_ENABLER, expectedStringValue);
String actualValue = LineageSettings.System.getString(mContentResolver,
LineageSettings.System.__MAGICAL_TEST_PASSING_ENABLER);
assertEquals(expectedStringValue, actualValue);
}
@SmallTest
public void testGetUri() {
final Uri expectedUri = Uri.withAppendedPath(LineageSettings.System.CONTENT_URI,
LineageSettings.System.__MAGICAL_TEST_PASSING_ENABLER);
final Uri actualUri = LineageSettings.System.getUriFor(
LineageSettings.System.__MAGICAL_TEST_PASSING_ENABLER);
assertEquals(expectedUri, actualUri);
}
}