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.Request.Priority;
20import org.junit.Test;
21import org.junit.runner.RunWith;
22import org.robolectric.RobolectricTestRunner;
23
24import static org.junit.Assert.*;
25
26@RunWith(RobolectricTestRunner.class)
27public class RequestTest {
28
29    @Test public void compareTo() {
30        int sequence = 0;
31        TestRequest low = new TestRequest(Priority.LOW);
32        low.setSequence(sequence++);
33        TestRequest low2 = new TestRequest(Priority.LOW);
34        low2.setSequence(sequence++);
35        TestRequest high = new TestRequest(Priority.HIGH);
36        high.setSequence(sequence++);
37        TestRequest immediate = new TestRequest(Priority.IMMEDIATE);
38        immediate.setSequence(sequence++);
39
40        // "Low" should sort higher because it's really processing order.
41        assertTrue(low.compareTo(high) > 0);
42        assertTrue(high.compareTo(low) < 0);
43        assertTrue(low.compareTo(low2) < 0);
44        assertTrue(low.compareTo(immediate) > 0);
45        assertTrue(immediate.compareTo(high) < 0);
46    }
47
48    private class TestRequest extends Request<Object> {
49        private Priority mPriority = Priority.NORMAL;
50        public TestRequest(Priority priority) {
51            super(Request.Method.GET, "", null);
52            mPriority = priority;
53        }
54
55        @Override
56        public Priority getPriority() {
57            return mPriority;
58        }
59
60        @Override
61        protected void deliverResponse(Object response) {
62        }
63
64        @Override
65        protected Response<Object> parseNetworkResponse(NetworkResponse response) {
66            return null;
67        }
68    }
69
70    @Test public void urlParsing() {
71        UrlParseRequest nullUrl = new UrlParseRequest(null);
72        assertEquals(0, nullUrl.getTrafficStatsTag());
73        UrlParseRequest emptyUrl = new UrlParseRequest("");
74        assertEquals(0, emptyUrl.getTrafficStatsTag());
75        UrlParseRequest noHost = new UrlParseRequest("http:///");
76        assertEquals(0, noHost.getTrafficStatsTag());
77        UrlParseRequest badProtocol = new UrlParseRequest("bad:http://foo");
78        assertEquals(0, badProtocol.getTrafficStatsTag());
79        UrlParseRequest goodProtocol = new UrlParseRequest("http://foo");
80        assertFalse(0 == goodProtocol.getTrafficStatsTag());
81    }
82
83    private class UrlParseRequest extends Request<Object> {
84        public UrlParseRequest(String url) {
85            super(Request.Method.GET, url, null);
86        }
87
88        @Override
89        protected void deliverResponse(Object response) {
90        }
91
92        @Override
93        protected Response<Object> parseNetworkResponse(NetworkResponse response) {
94            return null;
95        }
96    }
97}
98