• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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.volley;
18 
19 import com.android.volley.mock.MockCache;
20 import com.android.volley.mock.MockNetwork;
21 import com.android.volley.mock.MockRequest;
22 import com.android.volley.mock.MockResponseDelivery;
23 import com.android.volley.mock.WaitableQueue;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.robolectric.RobolectricTestRunner;
29 
30 import java.util.Arrays;
31 
32 import static org.junit.Assert.*;
33 
34 @RunWith(RobolectricTestRunner.class)
35 public class NetworkDispatcherTest {
36     private NetworkDispatcher mDispatcher;
37     private MockResponseDelivery mDelivery;
38     private WaitableQueue mNetworkQueue;
39     private MockNetwork mNetwork;
40     private MockCache mCache;
41     private MockRequest mRequest;
42 
43     private static final byte[] CANNED_DATA = "Ceci n'est pas une vraie reponse".getBytes();
44     private static final long TIMEOUT_MILLIS = 5000;
45 
setUp()46     @Before public void setUp() throws Exception {
47         mDelivery = new MockResponseDelivery();
48         mNetworkQueue = new WaitableQueue();
49         mNetwork = new MockNetwork();
50         mCache = new MockCache();
51         mRequest = new MockRequest();
52         mDispatcher = new NetworkDispatcher(mNetworkQueue, mNetwork, mCache, mDelivery);
53         mDispatcher.start();
54     }
55 
tearDown()56     @After public void tearDown() throws Exception {
57         mDispatcher.quit();
58         mDispatcher.join();
59     }
60 
successPostsResponse()61     @Test public void successPostsResponse() throws Exception {
62         mNetwork.setDataToReturn(CANNED_DATA);
63         mNetwork.setNumExceptionsToThrow(0);
64         mNetworkQueue.add(mRequest);
65         mNetworkQueue.waitUntilEmpty(TIMEOUT_MILLIS);
66         assertFalse(mDelivery.postError_called);
67         assertTrue(mDelivery.postResponse_called);
68         Response<?> response = mDelivery.responsePosted;
69         assertNotNull(response);
70         assertTrue(response.isSuccess());
71         assertTrue(Arrays.equals((byte[])response.result, CANNED_DATA));
72     }
73 
exceptionPostsError()74     @Test public void exceptionPostsError() throws Exception {
75         mNetwork.setNumExceptionsToThrow(MockNetwork.ALWAYS_THROW_EXCEPTIONS);
76         mNetworkQueue.add(mRequest);
77         mNetworkQueue.waitUntilEmpty(TIMEOUT_MILLIS);
78         assertFalse(mDelivery.postResponse_called);
79         assertTrue(mDelivery.postError_called);
80     }
81 
shouldCacheFalse()82     @Test public void shouldCacheFalse() throws Exception {
83         mRequest.setShouldCache(false);
84         mNetworkQueue.add(mRequest);
85         mNetworkQueue.waitUntilEmpty(TIMEOUT_MILLIS);
86         assertFalse(mCache.putCalled);
87     }
88 
shouldCacheTrue()89     @Test public void shouldCacheTrue() throws Exception {
90         mNetwork.setDataToReturn(CANNED_DATA);
91         mRequest.setShouldCache(true);
92         mRequest.setCacheKey("bananaphone");
93         mNetworkQueue.add(mRequest);
94         mNetworkQueue.waitUntilEmpty(TIMEOUT_MILLIS);
95         assertTrue(mCache.putCalled);
96         assertNotNull(mCache.entryPut);
97         assertTrue(Arrays.equals(mCache.entryPut.data, CANNED_DATA));
98         assertEquals("bananaphone", mCache.keyPut);
99     }
100 }
101