OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net; | 5 package org.chromium.net; |
6 | 6 |
7 import android.os.ConditionVariable; | 7 import android.os.ConditionVariable; |
8 import android.test.suitebuilder.annotation.SmallTest; | 8 import android.test.suitebuilder.annotation.SmallTest; |
9 | 9 |
10 import org.chromium.base.test.util.Feature; | 10 import org.chromium.base.test.util.Feature; |
11 import org.chromium.net.TestUrlRequestCallback.ResponseStep; | 11 import org.chromium.net.TestUrlRequestCallback.ResponseStep; |
12 import org.chromium.net.UrlRequest.Status; | 12 import org.chromium.net.UrlRequest.Status; |
13 import org.chromium.net.UrlRequest.StatusListener; | 13 import org.chromium.net.UrlRequest.StatusListener; |
14 import org.chromium.net.impl.LoadState; | |
15 import org.chromium.net.impl.UrlRequestBase; | |
14 | 16 |
15 import java.io.IOException; | 17 import java.io.IOException; |
16 import java.util.concurrent.Executor; | 18 import java.util.concurrent.Executor; |
17 import java.util.concurrent.Executors; | 19 import java.util.concurrent.Executors; |
18 | 20 |
19 /** | 21 /** |
20 * Tests that {@link CronetUrlRequest#getStatus} works as expected. | 22 * Tests that {@link org.chromium.net.impl.CronetUrlRequest#getStatus(StatusList ener)} works as |
23 * expected. | |
21 */ | 24 */ |
22 public class GetStatusTest extends CronetTestBase { | 25 public class GetStatusTest extends CronetTestBase { |
23 private CronetTestFramework mTestFramework; | 26 private CronetTestFramework mTestFramework; |
24 | 27 |
25 private static class TestStatusListener extends StatusListener { | 28 private static class TestStatusListener extends UrlRequest.StatusListener { |
pauljensen
2016/10/03 15:22:38
why this change? this file imports StatusListener
kapishnikov
2016/10/03 23:49:29
Fixed.
| |
26 boolean mOnStatusCalled = false; | 29 boolean mOnStatusCalled = false; |
27 int mStatus = Integer.MAX_VALUE; | 30 int mStatus = Integer.MAX_VALUE; |
28 private final ConditionVariable mBlock = new ConditionVariable(); | 31 private final ConditionVariable mBlock = new ConditionVariable(); |
29 | 32 |
30 @Override | 33 @Override |
31 public void onStatus(int status) { | 34 public void onStatus(int status) { |
32 mOnStatusCalled = true; | 35 mOnStatusCalled = true; |
33 mStatus = status; | 36 mStatus = status; |
34 mBlock.open(); | 37 mBlock.open(); |
35 } | 38 } |
(...skipping 16 matching lines...) Expand all Loading... | |
52 mTestFramework.mCronetEngine.shutdown(); | 55 mTestFramework.mCronetEngine.shutdown(); |
53 super.tearDown(); | 56 super.tearDown(); |
54 } | 57 } |
55 | 58 |
56 @SmallTest | 59 @SmallTest |
57 @Feature({"Cronet"}) | 60 @Feature({"Cronet"}) |
58 public void testSimpleGet() throws Exception { | 61 public void testSimpleGet() throws Exception { |
59 String url = NativeTestServer.getEchoMethodURL(); | 62 String url = NativeTestServer.getEchoMethodURL(); |
60 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 63 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
61 callback.setAutoAdvance(false); | 64 callback.setAutoAdvance(false); |
62 UrlRequest.Builder builder = new UrlRequest.Builder( | 65 UrlRequest.Builder builder = mTestFramework.mCronetEngine.newUrlRequestB uilder( |
63 url, callback, callback.getExecutor(), mTestFramework.mCronetEng ine); | 66 url, callback, callback.getExecutor()); |
64 UrlRequest urlRequest = builder.build(); | 67 UrlRequest urlRequest = builder.build(); |
65 // Calling before request is started should give Status.INVALID, | 68 // Calling before request is started should give Status.INVALID, |
66 // since the native adapter is not created. | 69 // since the native adapter is not created. |
67 TestStatusListener statusListener0 = new TestStatusListener(); | 70 TestStatusListener statusListener0 = new TestStatusListener(); |
68 urlRequest.getStatus(statusListener0); | 71 urlRequest.getStatus(statusListener0); |
69 statusListener0.waitUntilOnStatusCalled(); | 72 statusListener0.waitUntilOnStatusCalled(); |
70 assertTrue(statusListener0.mOnStatusCalled); | 73 assertTrue(statusListener0.mOnStatusCalled); |
71 assertEquals(Status.INVALID, statusListener0.mStatus); | 74 assertEquals(Status.INVALID, statusListener0.mStatus); |
72 | 75 |
73 urlRequest.start(); | 76 urlRequest.start(); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 assertEquals(Status.INVALID, statusListener3.mStatus); | 111 assertEquals(Status.INVALID, statusListener3.mStatus); |
109 | 112 |
110 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 113 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
111 assertEquals("GET", callback.mResponseAsString); | 114 assertEquals("GET", callback.mResponseAsString); |
112 } | 115 } |
113 | 116 |
114 @SmallTest | 117 @SmallTest |
115 @Feature({"Cronet"}) | 118 @Feature({"Cronet"}) |
116 public void testInvalidLoadState() throws Exception { | 119 public void testInvalidLoadState() throws Exception { |
117 try { | 120 try { |
118 Status.convertLoadState(LoadState.WAITING_FOR_APPCACHE); | 121 UrlRequestBase.StatusConverter.convertLoadState(LoadState.WAITING_FO R_APPCACHE); |
119 fail(); | 122 fail(); |
120 } catch (IllegalArgumentException e) { | 123 } catch (IllegalArgumentException e) { |
121 // Expected because LoadState.WAITING_FOR_APPCACHE is not mapped. | 124 // Expected because LoadState.WAITING_FOR_APPCACHE is not mapped. |
122 } | 125 } |
123 | 126 |
124 try { | 127 try { |
125 Status.convertLoadState(-1); | 128 UrlRequestBase.StatusConverter.convertLoadState(-1); |
126 fail(); | 129 fail(); |
127 } catch (AssertionError e) { | 130 } catch (AssertionError e) { |
128 // Expected. | 131 // Expected. |
129 } catch (IllegalArgumentException e) { | 132 } catch (IllegalArgumentException e) { |
130 // If assertions are disabled, an IllegalArgumentException should be thrown. | 133 // If assertions are disabled, an IllegalArgumentException should be thrown. |
131 assertEquals("No request status found.", e.getMessage()); | 134 assertEquals("No request status found.", e.getMessage()); |
132 } | 135 } |
133 | 136 |
134 try { | 137 try { |
135 Status.convertLoadState(16); | 138 UrlRequestBase.StatusConverter.convertLoadState(16); |
136 fail(); | 139 fail(); |
137 } catch (AssertionError e) { | 140 } catch (AssertionError e) { |
138 // Expected. | 141 // Expected. |
139 } catch (IllegalArgumentException e) { | 142 } catch (IllegalArgumentException e) { |
140 // If assertions are disabled, an IllegalArgumentException should be thrown. | 143 // If assertions are disabled, an IllegalArgumentException should be thrown. |
141 assertEquals("No request status found.", e.getMessage()); | 144 assertEquals("No request status found.", e.getMessage()); |
142 } | 145 } |
143 } | 146 } |
144 | 147 |
145 @SmallTest | 148 @SmallTest |
146 @Feature({"Cronet"}) | 149 @Feature({"Cronet"}) |
147 // Regression test for crbug.com/606872. | 150 // Regression test for crbug.com/606872. |
148 @OnlyRunNativeCronet | 151 @OnlyRunNativeCronet |
149 public void testGetStatusForUpload() throws Exception { | 152 public void testGetStatusForUpload() throws Exception { |
150 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 153 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
151 UrlRequest.Builder builder = new UrlRequest.Builder(NativeTestServer.get EchoBodyURL(), | 154 UrlRequest.Builder builder = mTestFramework.mCronetEngine.newUrlRequestB uilder( |
152 callback, callback.getExecutor(), mTestFramework.mCronetEngine); | 155 NativeTestServer.getEchoBodyURL(), callback, callback.getExecuto r()); |
153 | 156 |
154 final ConditionVariable block = new ConditionVariable(); | 157 final ConditionVariable block = new ConditionVariable(); |
155 // Use a separate executor for UploadDataProvider so the upload can be | 158 // Use a separate executor for UploadDataProvider so the upload can be |
156 // stalled while getStatus gets processed. | 159 // stalled while getStatus gets processed. |
157 Executor uploadProviderExecutor = Executors.newSingleThreadExecutor(); | 160 Executor uploadProviderExecutor = Executors.newSingleThreadExecutor(); |
158 TestUploadDataProvider dataProvider = new TestUploadDataProvider( | 161 TestUploadDataProvider dataProvider = new TestUploadDataProvider( |
159 TestUploadDataProvider.SuccessCallbackMode.SYNC, uploadProviderE xecutor) { | 162 TestUploadDataProvider.SuccessCallbackMode.SYNC, uploadProviderE xecutor) { |
160 @Override | 163 @Override |
161 public long getLength() throws IOException { | 164 public long getLength() throws IOException { |
162 // Pause the data provider. | 165 // Pause the data provider. |
(...skipping 26 matching lines...) Expand all Loading... | |
189 dataProvider.assertClosed(); | 192 dataProvider.assertClosed(); |
190 | 193 |
191 assertEquals(4, dataProvider.getUploadedLength()); | 194 assertEquals(4, dataProvider.getUploadedLength()); |
192 assertEquals(1, dataProvider.getNumReadCalls()); | 195 assertEquals(1, dataProvider.getNumReadCalls()); |
193 assertEquals(0, dataProvider.getNumRewindCalls()); | 196 assertEquals(0, dataProvider.getNumRewindCalls()); |
194 | 197 |
195 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 198 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
196 assertEquals("test", callback.mResponseAsString); | 199 assertEquals("test", callback.mResponseAsString); |
197 } | 200 } |
198 } | 201 } |
OLD | NEW |