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