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
17package com.android.volley;
18
19import android.test.suitebuilder.annotation.MediumTest;
20
21import com.android.volley.mock.MockCache;
22import com.android.volley.mock.MockNetwork;
23import com.android.volley.mock.MockRequest;
24import com.android.volley.mock.MockResponseDelivery;
25import com.android.volley.mock.WaitableQueue;
26
27import java.util.Arrays;
28
29import junit.framework.TestCase;
30
31@MediumTest
32public class NetworkDispatcherTest extends TestCase {
33    private NetworkDispatcher mDispatcher;
34    private MockResponseDelivery mDelivery;
35    private WaitableQueue mNetworkQueue;
36    private MockNetwork mNetwork;
37    private MockCache mCache;
38    private MockRequest mRequest;
39
40    private static final byte[] CANNED_DATA = "Ceci n'est pas une vraie reponse".getBytes();
41    private static final long TIMEOUT_MILLIS = 5000;
42
43    @Override
44    protected void setUp() throws Exception {
45        super.setUp();
46
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
56    @Override
57    protected void tearDown() throws Exception {
58        super.tearDown();
59        mDispatcher.quit();
60        mDispatcher.join();
61    }
62
63    public void testSuccessPostsResponse() throws Exception {
64        mNetwork.setDataToReturn(CANNED_DATA);
65        mNetwork.setNumExceptionsToThrow(0);
66        mNetworkQueue.add(mRequest);
67        mNetworkQueue.waitUntilEmpty(TIMEOUT_MILLIS);
68        assertFalse(mDelivery.postError_called);
69        assertTrue(mDelivery.postResponse_called);
70        Response<?> response = mDelivery.responsePosted;
71        assertNotNull(response);
72        assertTrue(response.isSuccess());
73        assertTrue(Arrays.equals((byte[])response.result, CANNED_DATA));
74    }
75
76    public void testExceptionPostsError() throws Exception {
77        mNetwork.setNumExceptionsToThrow(MockNetwork.ALWAYS_THROW_EXCEPTIONS);
78        mNetworkQueue.add(mRequest);
79        mNetworkQueue.waitUntilEmpty(TIMEOUT_MILLIS);
80        assertFalse(mDelivery.postResponse_called);
81        assertTrue(mDelivery.postError_called);
82    }
83
84    public void test_shouldCacheFalse() throws Exception {
85        mRequest.setShouldCache(false);
86        mNetworkQueue.add(mRequest);
87        mNetworkQueue.waitUntilEmpty(TIMEOUT_MILLIS);
88        assertFalse(mCache.putCalled);
89    }
90
91    public void test_shouldCacheTrue() throws Exception {
92        mNetwork.setDataToReturn(CANNED_DATA);
93        mRequest.setShouldCache(true);
94        mRequest.setCacheKey("bananaphone");
95        mNetworkQueue.add(mRequest);
96        mNetworkQueue.waitUntilEmpty(TIMEOUT_MILLIS);
97        assertTrue(mCache.putCalled);
98        assertNotNull(mCache.entryPut);
99        assertTrue(Arrays.equals(mCache.entryPut.data, CANNED_DATA));
100        assertEquals("bananaphone", mCache.keyPut);
101    }
102}
103