aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/com/android/volley/toolbox/AndroidAuthenticatorTest.java107
-rw-r--r--src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java41
-rw-r--r--src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java22
-rw-r--r--src/test/java/com/android/volley/toolbox/ImageLoaderTest.java80
-rw-r--r--src/test/java/com/android/volley/toolbox/ImageRequestTest.java86
-rw-r--r--src/test/java/com/android/volley/toolbox/NetworkImageViewTest.java3
-rw-r--r--src/test/java/com/android/volley/utils/CacheTestUtils.java2
7 files changed, 323 insertions, 18 deletions
diff --git a/src/test/java/com/android/volley/toolbox/AndroidAuthenticatorTest.java b/src/test/java/com/android/volley/toolbox/AndroidAuthenticatorTest.java
new file mode 100644
index 0000000..e878658
--- /dev/null
+++ b/src/test/java/com/android/volley/toolbox/AndroidAuthenticatorTest.java
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2015 The Android Open Source 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 com.android.volley.toolbox;
+
+import android.accounts.Account;
+import android.accounts.AccountManager;
+import android.accounts.AccountManagerFuture;
+import android.accounts.AuthenticatorException;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import com.android.volley.AuthFailureError;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+
+import static org.mockito.Mockito.*;
+
+@RunWith(RobolectricTestRunner.class)
+public class AndroidAuthenticatorTest {
+ private AccountManager mAccountManager;
+ private Account mAccount;
+ private AccountManagerFuture<Bundle> mFuture;
+ private AndroidAuthenticator mAuthenticator;
+
+ @Before
+ public void setUp() {
+ mAccountManager = mock(AccountManager.class);
+ mFuture = mock(AccountManagerFuture.class);
+ mAccount = new Account("coolperson", "cooltype");
+ mAuthenticator = new AndroidAuthenticator(mAccountManager, mAccount, "cooltype", false);
+ }
+
+ @Test(expected = AuthFailureError.class)
+ public void failedGetAuthToken() throws Exception {
+ when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture);
+ when(mFuture.getResult()).thenThrow(new AuthenticatorException("sadness!"));
+ mAuthenticator.getAuthToken();
+ }
+
+ @Test(expected = AuthFailureError.class)
+ public void resultContainsIntent() throws Exception {
+ Intent intent = new Intent();
+ Bundle bundle = new Bundle();
+ bundle.putParcelable(AccountManager.KEY_INTENT, intent);
+ when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture);
+ when(mFuture.getResult()).thenReturn(bundle);
+ when(mFuture.isDone()).thenReturn(true);
+ when(mFuture.isCancelled()).thenReturn(false);
+ mAuthenticator.getAuthToken();
+ }
+
+ @Test(expected = AuthFailureError.class)
+ public void missingAuthToken() throws Exception {
+ Bundle bundle = new Bundle();
+ when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture);
+ when(mFuture.getResult()).thenReturn(bundle);
+ when(mFuture.isDone()).thenReturn(true);
+ when(mFuture.isCancelled()).thenReturn(false);
+ mAuthenticator.getAuthToken();
+ }
+
+ @Test
+ public void invalidateAuthToken() throws Exception {
+ mAuthenticator.invalidateAuthToken("monkey");
+ verify(mAccountManager).invalidateAuthToken("cooltype", "monkey");
+ }
+
+ @Test
+ public void goodToken() throws Exception {
+ Bundle bundle = new Bundle();
+ bundle.putString(AccountManager.KEY_AUTHTOKEN, "monkey");
+ when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture);
+ when(mFuture.getResult()).thenReturn(bundle);
+ when(mFuture.isDone()).thenReturn(true);
+ when(mFuture.isCancelled()).thenReturn(false);
+ Assert.assertEquals("monkey", mAuthenticator.getAuthToken());
+ }
+
+ @Test
+ public void publicMethods() throws Exception {
+ // Catch-all test to find API-breaking changes.
+ Context context = mock(Context.class);
+ new AndroidAuthenticator(context, mAccount, "cooltype");
+ new AndroidAuthenticator(context, mAccount, "cooltype", true);
+ Assert.assertSame(mAccount, mAuthenticator.getAccount());
+ }
+}
diff --git a/src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java b/src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java
index 4b2955d..d9d49e9 100644
--- a/src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java
+++ b/src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java
@@ -34,6 +34,7 @@ public class DiskBasedCacheTest {
Cache.Entry e = new Cache.Entry();
e.data = new byte[8];
e.serverDate = 1234567L;
+ e.lastModified = 13572468L;
e.ttl = 9876543L;
e.softTtl = 8765432L;
e.etag = "etag";
@@ -48,6 +49,7 @@ public class DiskBasedCacheTest {
assertEquals(first.key, second.key);
assertEquals(first.serverDate, second.serverDate);
+ assertEquals(first.lastModified, second.lastModified);
assertEquals(first.ttl, second.ttl);
assertEquals(first.softTtl, second.softTtl);
assertEquals(first.etag, second.etag);
@@ -121,4 +123,43 @@ public class DiskBasedCacheTest {
assertEquals(DiskBasedCache.readStringStringMap(bais), emptyKey);
assertEquals(DiskBasedCache.readStringStringMap(bais), emptyValue);
}
+
+ // Test deserializing the old format into the new one.
+ public void testCacheHeaderSerializationOldToNewFormat() throws Exception {
+
+ final int CACHE_MAGIC = 0x20140623;
+ final String key = "key";
+ final String etag = "etag";
+ final long serverDate = 1234567890l;
+ final long ttl = 1357924680l;
+ final long softTtl = 2468013579l;
+
+ Map<String, String> responseHeaders = new HashMap<String, String>();
+ responseHeaders.put("first", "thing");
+ responseHeaders.put("second", "item");
+
+ // write old sytle header (without lastModified)
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ DiskBasedCache.writeInt(baos, CACHE_MAGIC);
+ DiskBasedCache.writeString(baos, key);
+ DiskBasedCache.writeString(baos, etag == null ? "" : etag);
+ DiskBasedCache.writeLong(baos, serverDate);
+ DiskBasedCache.writeLong(baos, ttl);
+ DiskBasedCache.writeLong(baos, softTtl);
+ DiskBasedCache.writeStringStringMap(responseHeaders, baos);
+
+ // read / test new style header (with lastModified)
+ ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+ CacheHeader cacheHeader = CacheHeader.readHeader(bais);
+
+ assertEquals(cacheHeader.key, key);
+ assertEquals(cacheHeader.etag, etag);
+ assertEquals(cacheHeader.serverDate, serverDate);
+ assertEquals(cacheHeader.ttl, ttl);
+ assertEquals(cacheHeader.softTtl, softTtl);
+ assertEquals(cacheHeader.responseHeaders, responseHeaders);
+
+ // the old format doesn't know lastModified
+ assertEquals(cacheHeader.lastModified, 0);
+ }
}
diff --git a/src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java b/src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java
index ae8257a..f9230c6 100644
--- a/src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java
+++ b/src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java
@@ -40,6 +40,7 @@ public class HttpHeaderParserTest {
private static long ONE_MINUTE_MILLIS = 1000L * 60;
private static long ONE_HOUR_MILLIS = 1000L * 60 * 60;
+ private static long ONE_DAY_MILLIS = ONE_HOUR_MILLIS * 24;
private NetworkResponse response;
private Map<String, String> headers;
@@ -55,6 +56,7 @@ public class HttpHeaderParserTest {
assertNotNull(entry);
assertNull(entry.etag);
assertEquals(0, entry.serverDate);
+ assertEquals(0, entry.lastModified);
assertEquals(0, entry.ttl);
assertEquals(0, entry.softTtl);
}
@@ -82,6 +84,7 @@ public class HttpHeaderParserTest {
@Test public void parseCacheHeaders_normalExpire() {
long now = System.currentTimeMillis();
headers.put("Date", rfc1123Date(now));
+ headers.put("Last-Modified", rfc1123Date(now - ONE_DAY_MILLIS));
headers.put("Expires", rfc1123Date(now + ONE_HOUR_MILLIS));
Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);
@@ -89,6 +92,7 @@ public class HttpHeaderParserTest {
assertNotNull(entry);
assertNull(entry.etag);
assertEqualsWithin(entry.serverDate, now, ONE_MINUTE_MILLIS);
+ assertEqualsWithin(entry.lastModified, (now - ONE_DAY_MILLIS), ONE_MINUTE_MILLIS);
assertTrue(entry.softTtl >= (now + ONE_HOUR_MILLIS));
assertTrue(entry.ttl == entry.softTtl);
}
@@ -135,6 +139,24 @@ public class HttpHeaderParserTest {
assertEquals(entry.softTtl, entry.ttl);
}
+ @Test public void testParseCacheHeaders_staleWhileRevalidate() {
+ long now = System.currentTimeMillis();
+ headers.put("Date", rfc1123Date(now));
+ headers.put("Expires", rfc1123Date(now + ONE_HOUR_MILLIS));
+
+ // - max-age (entry.softTtl) indicates that the asset is fresh for 1 day
+ // - stale-while-revalidate (entry.ttl) indicates that the asset may
+ // continue to be served stale for up to additional 7 days
+ headers.put("Cache-Control", "max-age=86400, stale-while-revalidate=604800");
+
+ Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);
+
+ assertNotNull(entry);
+ assertNull(entry.etag);
+ assertEqualsWithin(now + 24 * ONE_HOUR_MILLIS, entry.softTtl, ONE_MINUTE_MILLIS);
+ assertEqualsWithin(now + 8 * 24 * ONE_HOUR_MILLIS, entry.ttl, ONE_MINUTE_MILLIS);
+ }
+
@Test public void parseCacheHeaders_cacheControlNoCache() {
long now = System.currentTimeMillis();
headers.put("Date", rfc1123Date(now));
diff --git a/src/test/java/com/android/volley/toolbox/ImageLoaderTest.java b/src/test/java/com/android/volley/toolbox/ImageLoaderTest.java
new file mode 100644
index 0000000..47c55a6
--- /dev/null
+++ b/src/test/java/com/android/volley/toolbox/ImageLoaderTest.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2015 The Android Open Source 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 com.android.volley.toolbox;
+
+import android.graphics.Bitmap;
+import com.android.volley.Request;
+import com.android.volley.RequestQueue;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+
+import static org.mockito.Mockito.*;
+
+@RunWith(RobolectricTestRunner.class)
+public class ImageLoaderTest {
+ private RequestQueue mRequestQueue;
+ private ImageLoader.ImageCache mImageCache;
+ private ImageLoader mImageLoader;
+
+ @Before
+ public void setUp() {
+ mRequestQueue = mock(RequestQueue.class);
+ mImageCache = mock(ImageLoader.ImageCache.class);
+ mImageLoader = new ImageLoader(mRequestQueue, mImageCache);
+ }
+
+ @Test
+ public void isCachedChecksCache() throws Exception {
+ when(mImageCache.getBitmap(anyString())).thenReturn(null);
+ Assert.assertFalse(mImageLoader.isCached("http://foo", 0, 0));
+ }
+
+ @Test
+ public void getWithCacheHit() throws Exception {
+ Bitmap bitmap = Bitmap.createBitmap(1, 1, null);
+ ImageLoader.ImageListener listener = mock(ImageLoader.ImageListener.class);
+ when(mImageCache.getBitmap(anyString())).thenReturn(bitmap);
+ ImageLoader.ImageContainer ic = mImageLoader.get("http://foo", listener);
+ Assert.assertSame(bitmap, ic.getBitmap());
+ verify(listener).onResponse(ic, true);
+ }
+
+ @Test
+ public void getWithCacheMiss() throws Exception {
+ when(mImageCache.getBitmap(anyString())).thenReturn(null);
+ ImageLoader.ImageListener listener = mock(ImageLoader.ImageListener.class);
+ // Ask for the image to be loaded.
+ mImageLoader.get("http://foo", listener);
+ // Second pass to test deduping logic.
+ mImageLoader.get("http://foo", listener);
+ // Response callback should be called both times.
+ verify(listener, times(2)).onResponse(any(ImageLoader.ImageContainer.class), eq(true));
+ // But request should be enqueued only once.
+ verify(mRequestQueue, times(1)).add(any(Request.class));
+ }
+
+ @Test
+ public void publicMethods() throws Exception {
+ // Catch API breaking changes.
+ ImageLoader.getImageListener(null, -1, -1);
+ mImageLoader.setBatchedResponseDelay(1000);
+ }
+}
+
diff --git a/src/test/java/com/android/volley/toolbox/ImageRequestTest.java b/src/test/java/com/android/volley/toolbox/ImageRequestTest.java
index 2f4495a..bd98e7d 100644
--- a/src/test/java/com/android/volley/toolbox/ImageRequestTest.java
+++ b/src/test/java/com/android/volley/toolbox/ImageRequestTest.java
@@ -18,7 +18,7 @@ package com.android.volley.toolbox;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
-
+import android.widget.ImageView.ScaleType;
import com.android.volley.NetworkResponse;
import com.android.volley.Response;
import org.junit.Test;
@@ -47,30 +47,84 @@ public class ImageRequestTest {
ShadowBitmapFactory.provideWidthAndHeightHints("fake", 1024, 500);
NetworkResponse jpeg = new NetworkResponse(jpegBytes);
- // No resize
- verifyResize(jpeg, 0, 0, 1024, 500);
+ // Scale the image uniformly (maintain the image's aspect ratio) so that
+ // both dimensions (width and height) of the image will be equal to or
+ // less than the corresponding dimension of the view.
+ ScaleType scalteType = ScaleType.CENTER_INSIDE;
// Exact sizes
- verifyResize(jpeg, 512, 250, 512, 250); // exactly half
- verifyResize(jpeg, 511, 249, 509, 249); // just under half
- verifyResize(jpeg, 1080, 500, 1024, 500); // larger
- verifyResize(jpeg, 500, 500, 500, 244); // keep same ratio
+ verifyResize(jpeg, 512, 250, scalteType, 512, 250); // exactly half
+ verifyResize(jpeg, 511, 249, scalteType, 509, 249); // just under half
+ verifyResize(jpeg, 1080, 500, scalteType, 1024, 500); // larger
+ verifyResize(jpeg, 500, 500, scalteType, 500, 244); // keep same ratio
// Specify only width, preserve aspect ratio
- verifyResize(jpeg, 512, 0, 512, 250);
- verifyResize(jpeg, 800, 0, 800, 390);
- verifyResize(jpeg, 1024, 0, 1024, 500);
+ verifyResize(jpeg, 512, 0, scalteType, 512, 250);
+ verifyResize(jpeg, 800, 0, scalteType, 800, 390);
+ verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);
// Specify only height, preserve aspect ratio
- verifyResize(jpeg, 0, 250, 512, 250);
- verifyResize(jpeg, 0, 391, 800, 391);
- verifyResize(jpeg, 0, 500, 1024, 500);
+ verifyResize(jpeg, 0, 250, scalteType, 512, 250);
+ verifyResize(jpeg, 0, 391, scalteType, 800, 391);
+ verifyResize(jpeg, 0, 500, scalteType, 1024, 500);
+
+ // No resize
+ verifyResize(jpeg, 0, 0, scalteType, 1024, 500);
+
+
+ // Scale the image uniformly (maintain the image's aspect ratio) so that
+ // both dimensions (width and height) of the image will be equal to or
+ // larger than the corresponding dimension of the view.
+ scalteType = ScaleType.CENTER_CROP;
+
+ // Exact sizes
+ verifyResize(jpeg, 512, 250, scalteType, 512, 250);
+ verifyResize(jpeg, 511, 249, scalteType, 511, 249);
+ verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
+ verifyResize(jpeg, 500, 500, scalteType, 1024, 500);
+
+ // Specify only width
+ verifyResize(jpeg, 512, 0, scalteType, 512, 250);
+ verifyResize(jpeg, 800, 0, scalteType, 800, 390);
+ verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);
+
+ // Specify only height
+ verifyResize(jpeg, 0, 250, scalteType, 512, 250);
+ verifyResize(jpeg, 0, 391, scalteType, 800, 391);
+ verifyResize(jpeg, 0, 500, scalteType, 1024, 500);
+
+ // No resize
+ verifyResize(jpeg, 0, 0, scalteType, 1024, 500);
+
+
+ // Scale in X and Y independently, so that src matches dst exactly. This
+ // may change the aspect ratio of the src.
+ scalteType = ScaleType.FIT_XY;
+
+ // Exact sizes
+ verifyResize(jpeg, 512, 250, scalteType, 512, 250);
+ verifyResize(jpeg, 511, 249, scalteType, 511, 249);
+ verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
+ verifyResize(jpeg, 500, 500, scalteType, 500, 500);
+
+ // Specify only width
+ verifyResize(jpeg, 512, 0, scalteType, 512, 500);
+ verifyResize(jpeg, 800, 0, scalteType, 800, 500);
+ verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);
+
+ // Specify only height
+ verifyResize(jpeg, 0, 250, scalteType, 1024, 250);
+ verifyResize(jpeg, 0, 391, scalteType, 1024, 391);
+ verifyResize(jpeg, 0, 500, scalteType, 1024, 500);
+
+ // No resize
+ verifyResize(jpeg, 0, 0, scalteType, 1024, 500);
}
private void verifyResize(NetworkResponse networkResponse, int maxWidth, int maxHeight,
- int expectedWidth, int expectedHeight) {
- ImageRequest request = new ImageRequest(
- "", null, maxWidth, maxHeight, Config.RGB_565, null);
+ ScaleType scaleType, int expectedWidth, int expectedHeight) {
+ ImageRequest request = new ImageRequest("", null, maxWidth, maxHeight, scaleType,
+ Config.RGB_565, null);
Response<Bitmap> response = request.parseNetworkResponse(networkResponse);
assertNotNull(response);
assertTrue(response.isSuccess());
diff --git a/src/test/java/com/android/volley/toolbox/NetworkImageViewTest.java b/src/test/java/com/android/volley/toolbox/NetworkImageViewTest.java
index 48c81b6..bc2cc29 100644
--- a/src/test/java/com/android/volley/toolbox/NetworkImageViewTest.java
+++ b/src/test/java/com/android/volley/toolbox/NetworkImageViewTest.java
@@ -1,6 +1,7 @@
package com.android.volley.toolbox;
import android.view.ViewGroup.LayoutParams;
+import android.widget.ImageView.ScaleType;
import org.junit.Before;
import org.junit.Test;
@@ -43,7 +44,7 @@ public class NetworkImageViewTest {
public int lastMaxHeight;
public ImageContainer get(String requestUrl, ImageListener imageListener, int maxWidth,
- int maxHeight) {
+ int maxHeight, ScaleType scaleType) {
lastRequestUrl = requestUrl;
lastMaxWidth = maxWidth;
lastMaxHeight = maxHeight;
diff --git a/src/test/java/com/android/volley/utils/CacheTestUtils.java b/src/test/java/com/android/volley/utils/CacheTestUtils.java
index cd2b8e7..898d055 100644
--- a/src/test/java/com/android/volley/utils/CacheTestUtils.java
+++ b/src/test/java/com/android/volley/utils/CacheTestUtils.java
@@ -24,7 +24,7 @@ public class CacheTestUtils {
entry.data = new byte[random.nextInt(1024)];
}
entry.etag = String.valueOf(random.nextLong());
- entry.serverDate = random.nextLong();
+ entry.lastModified = random.nextLong();
entry.ttl = isExpired ? 0 : Long.MAX_VALUE;
entry.softTtl = needsRefresh ? 0 : Long.MAX_VALUE;
return entry;