|
| 1 | +package com.unity3d.ads.test.unit; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.SharedPreferences; |
| 5 | +import android.support.test.InstrumentationRegistry; |
| 6 | +import android.support.test.runner.AndroidJUnit4; |
| 7 | + |
| 8 | +import com.unity3d.ads.preferences.AndroidPreferences; |
| 9 | +import com.unity3d.ads.properties.ClientProperties; |
| 10 | + |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | +import org.junit.runner.RunWith; |
| 14 | + |
| 15 | +import static org.junit.Assert.assertEquals; |
| 16 | +import static org.junit.Assert.assertFalse; |
| 17 | +import static org.junit.Assert.assertNull; |
| 18 | +import static org.junit.Assert.assertTrue; |
| 19 | + |
| 20 | +@RunWith(AndroidJUnit4.class) |
| 21 | +public class PreferencesTest { |
| 22 | + String testSettings = "unity.test.preferences"; |
| 23 | + String nonExistingSettings = "unity.test.does.not.exist"; |
| 24 | + String stringKey = "test.string"; |
| 25 | + String intKey = "test.int"; |
| 26 | + String longKey = "test.long"; |
| 27 | + String boolKey = "test.boolean"; |
| 28 | + String floatKey = "test.float"; |
| 29 | + String nonExistingKey = "non.existing.key"; |
| 30 | + |
| 31 | + @Before |
| 32 | + public void setup() { |
| 33 | + ClientProperties.setApplicationContext(InstrumentationRegistry.getTargetContext()); |
| 34 | + |
| 35 | + deleteKey(stringKey); |
| 36 | + deleteKey(intKey); |
| 37 | + deleteKey(longKey); |
| 38 | + deleteKey(boolKey); |
| 39 | + deleteKey(floatKey); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testPreferencesStringGetter() { |
| 44 | + String testValue = "testString"; |
| 45 | + |
| 46 | + assertFalse("Preferences contained a key before key was written", AndroidPreferences.hasKey(testSettings, stringKey)); |
| 47 | + |
| 48 | + SharedPreferences.Editor editor = getEditor(); |
| 49 | + editor.putString(stringKey, testValue); |
| 50 | + editor.commit(); |
| 51 | + |
| 52 | + assertTrue("Preferences did not contain previously committed value", AndroidPreferences.hasKey(testSettings, stringKey)); |
| 53 | + assertEquals("Proper string value was not read from SharedPreferences", AndroidPreferences.getString(testSettings, stringKey), testValue); |
| 54 | + assertNull("Non-null value returned for a non-existing key", AndroidPreferences.getString(testSettings, nonExistingKey)); |
| 55 | + assertNull("Non-null value returned for a non-existing settings", AndroidPreferences.getString(nonExistingSettings, stringKey)); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testPreferencesIntGetter() { |
| 60 | + int testValue = 12345; |
| 61 | + |
| 62 | + assertFalse("Preferences contained a key before key was written", AndroidPreferences.hasKey(testSettings, intKey)); |
| 63 | + |
| 64 | + SharedPreferences.Editor editor = getEditor(); |
| 65 | + editor.putInt(intKey, testValue); |
| 66 | + editor.commit(); |
| 67 | + |
| 68 | + assertTrue("Preferences did not contain previously committed value", AndroidPreferences.hasKey(testSettings, intKey)); |
| 69 | + assertEquals("Proper int value was not read from SharedPreferences", AndroidPreferences.getInteger(testSettings, intKey), Integer.valueOf(testValue)); |
| 70 | + assertNull("Non-null value returned for a non-existing key", AndroidPreferences.getInteger(testSettings, nonExistingKey)); |
| 71 | + assertNull("Non-null value returned for a non-existing settings", AndroidPreferences.getInteger(nonExistingSettings, intKey)); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testPreferencesLongGetter() { |
| 76 | + long testValue = 12345678; |
| 77 | + |
| 78 | + assertFalse("Preferences contained a key before key was written", AndroidPreferences.hasKey(testSettings, longKey)); |
| 79 | + |
| 80 | + SharedPreferences.Editor editor = getEditor(); |
| 81 | + editor.putLong(longKey, testValue); |
| 82 | + editor.commit(); |
| 83 | + |
| 84 | + assertTrue("Preferences did not contain previously committed value", AndroidPreferences.hasKey(testSettings, longKey)); |
| 85 | + assertEquals("Proper long value was not read from SharedPreferences", AndroidPreferences.getLong(testSettings, longKey), Long.valueOf(testValue)); |
| 86 | + assertNull("Non-null value returned for a non-existing key", AndroidPreferences.getLong(testSettings, nonExistingKey)); |
| 87 | + assertNull("Non-null value returned for a non-existing settings", AndroidPreferences.getLong(nonExistingSettings, longKey)); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testPreferencesBooleanGetter() { |
| 92 | + boolean testValue = true; |
| 93 | + |
| 94 | + assertFalse("Preferences contained a key before key was written", AndroidPreferences.hasKey(testSettings, boolKey)); |
| 95 | + |
| 96 | + SharedPreferences.Editor editor = getEditor(); |
| 97 | + editor.putBoolean(boolKey, testValue); |
| 98 | + editor.commit(); |
| 99 | + |
| 100 | + assertTrue("Preferences did not contain previously committed value", AndroidPreferences.hasKey(testSettings, boolKey)); |
| 101 | + assertEquals("Proper boolean value was not read from SharedPreferences", AndroidPreferences.getBoolean(testSettings, boolKey), Boolean.valueOf(testValue)); |
| 102 | + assertNull("Non-null value returned for a non-existing key", AndroidPreferences.getBoolean(testSettings, nonExistingKey)); |
| 103 | + assertNull("Non-null value returned for a non-existing settings", AndroidPreferences.getBoolean(nonExistingSettings, boolKey)); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testPreferencesFloatGetter() { |
| 108 | + float testValue = 1.2345f; |
| 109 | + |
| 110 | + assertFalse("Preferences contained a key before key was written", AndroidPreferences.hasKey(testSettings, floatKey)); |
| 111 | + |
| 112 | + SharedPreferences.Editor editor = getEditor(); |
| 113 | + editor.putFloat(floatKey, testValue); |
| 114 | + editor.commit(); |
| 115 | + |
| 116 | + assertTrue("Preferences did not contain previously committed value", AndroidPreferences.hasKey(testSettings, floatKey)); |
| 117 | + assertEquals("Proper float value was not read from SharedPreferences", AndroidPreferences.getFloat(testSettings, floatKey), Float.valueOf(testValue)); |
| 118 | + assertNull("Non-null value returned for a non-existing key", AndroidPreferences.getFloat(testSettings, nonExistingKey)); |
| 119 | + assertNull("Non-null value returned for a non-existing settings", AndroidPreferences.getFloat(nonExistingSettings, floatKey)); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testPreferencesStringSetter() { |
| 124 | + String testValue = "testString"; |
| 125 | + |
| 126 | + assertFalse("Preferences contained a key before key was written", AndroidPreferences.hasKey(testSettings, stringKey)); |
| 127 | + |
| 128 | + AndroidPreferences.setString(testSettings, stringKey, testValue); |
| 129 | + |
| 130 | + assertTrue("Preferences did not contain previously committed value", AndroidPreferences.hasKey(testSettings, stringKey)); |
| 131 | + assertEquals("Proper string value was not read from SharedPreferences", getPrefs().getString(stringKey, ""), testValue); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void testPreferencesIntSetter() { |
| 136 | + int testValue = 12345; |
| 137 | + |
| 138 | + assertFalse("Preferences contained a key before key was written", AndroidPreferences.hasKey(testSettings, intKey)); |
| 139 | + |
| 140 | + AndroidPreferences.setInteger(testSettings, intKey, testValue); |
| 141 | + |
| 142 | + assertTrue("Preferences did not contain previously committed value", AndroidPreferences.hasKey(testSettings, intKey)); |
| 143 | + assertEquals("Proper int value was not read from SharedPreferences", (Integer)getPrefs().getInt(intKey, -1), Integer.valueOf(testValue)); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testPreferencesLongSetter() { |
| 148 | + long testValue = 12345678; |
| 149 | + |
| 150 | + assertFalse("Preferences contained a key before key was written", AndroidPreferences.hasKey(testSettings, longKey)); |
| 151 | + |
| 152 | + AndroidPreferences.setLong(testSettings, longKey, testValue); |
| 153 | + |
| 154 | + assertTrue("Preferences did not contain previously committed value", AndroidPreferences.hasKey(testSettings, longKey)); |
| 155 | + assertEquals("Proper long value was not read from SharedPreferences", (Long)getPrefs().getLong(longKey, -1), Long.valueOf(testValue)); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testPreferencesBooleanSetter() { |
| 160 | + boolean testValue = true; |
| 161 | + |
| 162 | + assertFalse("Preferences contained a key before key was written", AndroidPreferences.hasKey(testSettings, boolKey)); |
| 163 | + |
| 164 | + AndroidPreferences.setBoolean(testSettings, boolKey, testValue); |
| 165 | + |
| 166 | + assertTrue("Preferences did not contain previously committed value", AndroidPreferences.hasKey(testSettings, boolKey)); |
| 167 | + assertEquals("Proper boolean value was not read from SharedPreferences", getPrefs().getBoolean(boolKey, false), Boolean.valueOf(testValue)); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + public void testPreferencesFloatSetter() { |
| 172 | + float testValue = 1.2345f; |
| 173 | + |
| 174 | + assertFalse("Preferences contained a key before key was written", AndroidPreferences.hasKey(testSettings, floatKey)); |
| 175 | + |
| 176 | + AndroidPreferences.setFloat(testSettings, floatKey, new Double(testValue)); |
| 177 | + |
| 178 | + assertTrue("Preferences did not contain previously committed value", AndroidPreferences.hasKey(testSettings, floatKey)); |
| 179 | + assertEquals("Proper float value was not read from SharedPreferences", (Float)getPrefs().getFloat(floatKey, Float.NaN), Float.valueOf(testValue)); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void testPreferencesTypeErrors() { |
| 184 | + SharedPreferences.Editor editor = getEditor(); |
| 185 | + editor.putString(stringKey, "testString"); |
| 186 | + editor.putInt(intKey, 12345); |
| 187 | + editor.commit(); |
| 188 | + |
| 189 | + assertNull("Type mismatch did not return null", AndroidPreferences.getString(testSettings, intKey)); |
| 190 | + assertNull("Type mismatch did not return null", AndroidPreferences.getInteger(testSettings, stringKey)); |
| 191 | + assertNull("Type mismatch did not return null", AndroidPreferences.getLong(testSettings, stringKey)); |
| 192 | + assertNull("Type mismatch did not return null", AndroidPreferences.getBoolean(testSettings, stringKey)); |
| 193 | + assertNull("Type mismatch did not return null", AndroidPreferences.getFloat(testSettings, stringKey)); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + public void testPreferencesRemoveKey() { |
| 198 | + SharedPreferences.Editor editor = getEditor(); |
| 199 | + editor.putString(stringKey, "testString"); |
| 200 | + editor.commit(); |
| 201 | + |
| 202 | + assertTrue("Preferences did not contain test string value", AndroidPreferences.hasKey(testSettings, stringKey)); |
| 203 | + |
| 204 | + AndroidPreferences.removeKey(testSettings, stringKey); |
| 205 | + |
| 206 | + assertFalse("Preferences has a key after is was removed", AndroidPreferences.hasKey(testSettings, stringKey)); |
| 207 | + } |
| 208 | + |
| 209 | + private SharedPreferences getPrefs() { |
| 210 | + return InstrumentationRegistry.getTargetContext().getSharedPreferences(testSettings, Context.MODE_PRIVATE); |
| 211 | + } |
| 212 | + |
| 213 | + private SharedPreferences.Editor getEditor() { |
| 214 | + return InstrumentationRegistry.getTargetContext().getSharedPreferences(testSettings, Context.MODE_PRIVATE).edit(); |
| 215 | + } |
| 216 | + |
| 217 | + private void deleteKey(String key) { |
| 218 | + SharedPreferences prefs = getPrefs(); |
| 219 | + if(prefs != null) { |
| 220 | + if(prefs.contains(key)) { |
| 221 | + SharedPreferences.Editor editor = getEditor(); |
| 222 | + editor.remove(key); |
| 223 | + editor.commit(); |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | +} |
0 commit comments