| 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.ShadowSystemClock;
|
|
|
20 |
import com.android.volley.toolbox.NoCache;
|
|
|
21 |
import com.android.volley.utils.ImmediateResponseDelivery;
|
|
|
22 |
|
|
|
23 |
import org.junit.Before;
|
|
|
24 |
import org.junit.Test;
|
|
|
25 |
import org.junit.runner.RunWith;
|
|
|
26 |
import org.mockito.Mock;
|
|
|
27 |
import org.robolectric.RobolectricTestRunner;
|
|
|
28 |
import org.robolectric.annotation.Config;
|
|
|
29 |
|
|
|
30 |
import static org.mockito.Mockito.mock;
|
|
|
31 |
import static org.mockito.Mockito.never;
|
|
|
32 |
import static org.mockito.Mockito.verify;
|
|
|
33 |
import static org.mockito.Mockito.when;
|
|
|
34 |
import static org.mockito.MockitoAnnotations.initMocks;
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Unit tests for RequestQueue, with all dependencies mocked out
|
|
|
38 |
*/
|
|
|
39 |
@RunWith(RobolectricTestRunner.class)
|
|
|
40 |
@Config(shadows = {ShadowSystemClock.class})
|
|
|
41 |
public class RequestQueueTest {
|
|
|
42 |
|
|
|
43 |
private ResponseDelivery mDelivery;
|
|
|
44 |
@Mock private Network mMockNetwork;
|
|
|
45 |
|
|
|
46 |
@Before public void setUp() throws Exception {
|
|
|
47 |
mDelivery = new ImmediateResponseDelivery();
|
|
|
48 |
initMocks(this);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
@Test public void cancelAll_onlyCorrectTag() throws Exception {
|
|
|
52 |
RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 0, mDelivery);
|
|
|
53 |
Object tagA = new Object();
|
|
|
54 |
Object tagB = new Object();
|
|
|
55 |
Request req1 = mock(Request.class);
|
|
|
56 |
when(req1.getTag()).thenReturn(tagA);
|
|
|
57 |
Request req2 = mock(Request.class);
|
|
|
58 |
when(req2.getTag()).thenReturn(tagB);
|
|
|
59 |
Request req3 = mock(Request.class);
|
|
|
60 |
when(req3.getTag()).thenReturn(tagA);
|
|
|
61 |
Request req4 = mock(Request.class);
|
|
|
62 |
when(req4.getTag()).thenReturn(tagA);
|
|
|
63 |
|
|
|
64 |
queue.add(req1); // A
|
|
|
65 |
queue.add(req2); // B
|
|
|
66 |
queue.add(req3); // A
|
|
|
67 |
queue.cancelAll(tagA);
|
|
|
68 |
queue.add(req4); // A
|
|
|
69 |
|
|
|
70 |
verify(req1).cancel(); // A cancelled
|
|
|
71 |
verify(req3).cancel(); // A cancelled
|
|
|
72 |
verify(req2, never()).cancel(); // B not cancelled
|
|
|
73 |
verify(req4, never()).cancel(); // A added after cancel not cancelled
|
|
|
74 |
}
|
|
|
75 |
}
|