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