Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21481 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.MockRequest;
21
import com.android.volley.mock.MockResponseDelivery;
22
import com.android.volley.mock.WaitableQueue;
23
import com.android.volley.utils.CacheTestUtils;
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 static org.junit.Assert.assertFalse;
32
import static org.junit.Assert.assertNull;
33
import static org.junit.Assert.assertSame;
34
import static org.junit.Assert.assertTrue;
35
 
36
@RunWith(RobolectricTestRunner.class)
37
@SuppressWarnings("rawtypes")
38
public class CacheDispatcherTest {
39
    private CacheDispatcher mDispatcher;
40
    private WaitableQueue mCacheQueue;
41
    private WaitableQueue mNetworkQueue;
42
    private MockCache mCache;
43
    private MockResponseDelivery mDelivery;
44
    private MockRequest mRequest;
45
 
46
    private static final long TIMEOUT_MILLIS = 5000;
47
 
48
    @Before public void setUp() throws Exception {
49
        mCacheQueue = new WaitableQueue();
50
        mNetworkQueue = new WaitableQueue();
51
        mCache = new MockCache();
52
        mDelivery = new MockResponseDelivery();
53
 
54
        mRequest = new MockRequest();
55
 
56
        mDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);
57
        mDispatcher.start();
58
    }
59
 
60
    @After public void tearDown() throws Exception {
61
        mDispatcher.quit();
62
        mDispatcher.join();
63
    }
64
 
65
    // A cancelled request should not be processed at all.
66
    @Test public void cancelledRequest() throws Exception {
67
        mRequest.cancel();
68
        mCacheQueue.add(mRequest);
69
        mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
70
        assertFalse(mCache.getCalled);
71
        assertFalse(mDelivery.wasEitherResponseCalled());
72
    }
73
 
74
    // A cache miss does not post a response and puts the request on the network queue.
75
    @Test public void cacheMiss() throws Exception {
76
        mCacheQueue.add(mRequest);
77
        mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
78
        assertFalse(mDelivery.wasEitherResponseCalled());
79
        assertTrue(mNetworkQueue.size() > 0);
80
        Request request = mNetworkQueue.take();
81
        assertNull(request.getCacheEntry());
82
    }
83
 
84
    // A non-expired cache hit posts a response and does not queue to the network.
85
    @Test public void nonExpiredCacheHit() throws Exception {
86
        Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, false);
87
        mCache.setEntryToReturn(entry);
88
        mCacheQueue.add(mRequest);
89
        mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
90
        assertTrue(mDelivery.postResponse_called);
91
        assertFalse(mDelivery.postError_called);
92
    }
93
 
94
    // A soft-expired cache hit posts a response and queues to the network.
95
    @Test public void softExpiredCacheHit() throws Exception {
96
        Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, true);
97
        mCache.setEntryToReturn(entry);
98
        mCacheQueue.add(mRequest);
99
        mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
100
        assertTrue(mDelivery.postResponse_called);
101
        assertFalse(mDelivery.postError_called);
102
        assertTrue(mNetworkQueue.size() > 0);
103
        Request request = mNetworkQueue.take();
104
        assertSame(entry, request.getCacheEntry());
105
    }
106
 
107
    // An expired cache hit does not post a response and queues to the network.
108
    @Test public void expiredCacheHit() throws Exception {
109
        Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, true, true);
110
        mCache.setEntryToReturn(entry);
111
        mCacheQueue.add(mRequest);
112
        mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
113
        assertFalse(mDelivery.wasEitherResponseCalled());
114
        assertTrue(mNetworkQueue.size() > 0);
115
        Request request = mNetworkQueue.take();
116
        assertSame(entry, request.getCacheEntry());
117
    }
118
}