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