@SmallTest @MediumTest @LargeTest public void testCacheRoundtrip() { ArrayList<String> permissions = Utility.arrayList("stream_publish", "go_outside_and_play"); String token = "AnImaginaryTokenValue"; Date later = TestUtils.nowPlusSeconds(60); Date earlier = TestUtils.nowPlusSeconds(-60); SharedPreferencesTokenCachingStrategy cache = new SharedPreferencesTokenCachingStrategy(getContext()); cache.clear(); Bundle bundle = new Bundle(); TokenCachingStrategy.putToken(bundle, token); TokenCachingStrategy.putExpirationDate(bundle, later); TokenCachingStrategy.putSource(bundle, AccessTokenSource.FACEBOOK_APPLICATION_NATIVE); TokenCachingStrategy.putLastRefreshDate(bundle, earlier); TokenCachingStrategy.putPermissions(bundle, permissions); cache.save(bundle); bundle = cache.load(); AccessToken accessToken = AccessToken.createFromCache(bundle); TestUtils.assertSamePermissions(permissions, accessToken); assertEquals(token, accessToken.getToken()); assertEquals(AccessTokenSource.FACEBOOK_APPLICATION_NATIVE, accessToken.getSource()); assertTrue(!accessToken.isInvalid()); Bundle cachedBundle = accessToken.toCacheBundle(); TestUtils.assertEqualContents(bundle, cachedBundle); }
@SmallTest @MediumTest @LargeTest public void testMultipleCaches() { Bundle bundle1 = new Bundle(), bundle2 = new Bundle(); bundle1.putInt(INT_KEY, 10); bundle1.putString(STRING_KEY, "ABC"); bundle2.putInt(INT_KEY, 100); bundle2.putString(STRING_KEY, "xyz"); ensureApplicationContext(); SharedPreferencesTokenCachingStrategy cache1 = new SharedPreferencesTokenCachingStrategy(getContext()); SharedPreferencesTokenCachingStrategy cache2 = new SharedPreferencesTokenCachingStrategy(getContext(), "CustomCache"); cache1.save(bundle1); cache2.save(bundle2); // Get new references to make sure we are getting persisted data. // Reverse the cache references for fun. cache1 = new SharedPreferencesTokenCachingStrategy(getContext(), "CustomCache"); cache2 = new SharedPreferencesTokenCachingStrategy(getContext()); Bundle newBundle1 = cache1.load(), newBundle2 = cache2.load(); Assert.assertEquals(bundle2.getInt(INT_KEY), newBundle1.getInt(INT_KEY)); Assert.assertEquals(bundle2.getString(STRING_KEY), newBundle1.getString(STRING_KEY)); Assert.assertEquals(bundle1.getInt(INT_KEY), newBundle2.getInt(INT_KEY)); Assert.assertEquals(bundle1.getString(STRING_KEY), newBundle2.getString(STRING_KEY)); }
@SmallTest @MediumTest @LargeTest public void testAllTypes() { Bundle originalBundle = new Bundle(); putBoolean(BOOLEAN_KEY, originalBundle); putBooleanArray(BOOLEAN_ARRAY_KEY, originalBundle); putByte(BYTE_KEY, originalBundle); putByteArray(BYTE_ARRAY_KEY, originalBundle); putShort(SHORT_KEY, originalBundle); putShortArray(SHORT_ARRAY_KEY, originalBundle); putInt(INT_KEY, originalBundle); putIntArray(INT_ARRAY_KEY, originalBundle); putLong(LONG_KEY, originalBundle); putLongArray(LONG_ARRAY_KEY, originalBundle); putFloat(FLOAT_KEY, originalBundle); putFloatArray(FLOAT_ARRAY_KEY, originalBundle); putDouble(DOUBLE_KEY, originalBundle); putDoubleArray(DOUBLE_ARRAY_KEY, originalBundle); putChar(CHAR_KEY, originalBundle); putCharArray(CHAR_ARRAY_KEY, originalBundle); putString(STRING_KEY, originalBundle); putStringList(STRING_LIST_KEY, originalBundle); originalBundle.putSerializable(SERIALIZABLE_KEY, AccessTokenSource.FACEBOOK_APPLICATION_WEB); ensureApplicationContext(); SharedPreferencesTokenCachingStrategy cache = new SharedPreferencesTokenCachingStrategy(getContext()); cache.save(originalBundle); SharedPreferencesTokenCachingStrategy cache2 = new SharedPreferencesTokenCachingStrategy(getContext()); Bundle cachedBundle = cache2.load(); Assert.assertEquals( originalBundle.getBoolean(BOOLEAN_KEY), cachedBundle.getBoolean(BOOLEAN_KEY)); assertArrayEquals( originalBundle.getBooleanArray(BOOLEAN_ARRAY_KEY), cachedBundle.getBooleanArray(BOOLEAN_ARRAY_KEY)); Assert.assertEquals(originalBundle.getByte(BYTE_KEY), cachedBundle.getByte(BYTE_KEY)); assertArrayEquals( originalBundle.getByteArray(BYTE_ARRAY_KEY), cachedBundle.getByteArray(BYTE_ARRAY_KEY)); Assert.assertEquals(originalBundle.getShort(SHORT_KEY), cachedBundle.getShort(SHORT_KEY)); assertArrayEquals( originalBundle.getShortArray(SHORT_ARRAY_KEY), cachedBundle.getShortArray(SHORT_ARRAY_KEY)); Assert.assertEquals(originalBundle.getInt(INT_KEY), cachedBundle.getInt(INT_KEY)); assertArrayEquals( originalBundle.getIntArray(INT_ARRAY_KEY), cachedBundle.getIntArray(INT_ARRAY_KEY)); Assert.assertEquals(originalBundle.getLong(LONG_KEY), cachedBundle.getLong(LONG_KEY)); assertArrayEquals( originalBundle.getLongArray(LONG_ARRAY_KEY), cachedBundle.getLongArray(LONG_ARRAY_KEY)); Assert.assertEquals(originalBundle.getFloat(FLOAT_KEY), cachedBundle.getFloat(FLOAT_KEY)); assertArrayEquals( originalBundle.getFloatArray(FLOAT_ARRAY_KEY), cachedBundle.getFloatArray(FLOAT_ARRAY_KEY)); Assert.assertEquals(originalBundle.getDouble(DOUBLE_KEY), cachedBundle.getDouble(DOUBLE_KEY)); assertArrayEquals( originalBundle.getDoubleArray(DOUBLE_ARRAY_KEY), cachedBundle.getDoubleArray(DOUBLE_ARRAY_KEY)); Assert.assertEquals(originalBundle.getChar(CHAR_KEY), cachedBundle.getChar(CHAR_KEY)); assertArrayEquals( originalBundle.getCharArray(CHAR_ARRAY_KEY), cachedBundle.getCharArray(CHAR_ARRAY_KEY)); Assert.assertEquals(originalBundle.getString(STRING_KEY), cachedBundle.getString(STRING_KEY)); assertListEquals( originalBundle.getStringArrayList(STRING_LIST_KEY), cachedBundle.getStringArrayList(STRING_LIST_KEY)); Assert.assertEquals( originalBundle.getSerializable(SERIALIZABLE_KEY), cachedBundle.getSerializable(SERIALIZABLE_KEY)); }