| 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 | 14 |
| 15 import java.io.IOException; |
| 16 import java.util.concurrent.Executor; |
| 17 import java.util.concurrent.Executors; |
| 18 |
| 15 /** | 19 /** |
| 16 * Tests that {@link CronetUrlRequest#getStatus} works as expected. | 20 * Tests that {@link CronetUrlRequest#getStatus} works as expected. |
| 17 */ | 21 */ |
| 18 public class GetStatusTest extends CronetTestBase { | 22 public class GetStatusTest extends CronetTestBase { |
| 19 private CronetTestFramework mTestFramework; | 23 private CronetTestFramework mTestFramework; |
| 20 | 24 |
| 21 private static class TestStatusListener extends StatusListener { | 25 private static class TestStatusListener extends StatusListener { |
| 22 boolean mOnStatusCalled = false; | 26 boolean mOnStatusCalled = false; |
| 23 int mStatus = Integer.MAX_VALUE; | 27 int mStatus = Integer.MAX_VALUE; |
| 24 private final ConditionVariable mBlock = new ConditionVariable(); | 28 private final ConditionVariable mBlock = new ConditionVariable(); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 try { | 134 try { |
| 131 Status.convertLoadState(16); | 135 Status.convertLoadState(16); |
| 132 fail(); | 136 fail(); |
| 133 } catch (AssertionError e) { | 137 } catch (AssertionError e) { |
| 134 // Expected. | 138 // Expected. |
| 135 } catch (IllegalArgumentException e) { | 139 } catch (IllegalArgumentException e) { |
| 136 // If assertions are disabled, an IllegalArgumentException should be
thrown. | 140 // If assertions are disabled, an IllegalArgumentException should be
thrown. |
| 137 assertEquals("No request status found.", e.getMessage()); | 141 assertEquals("No request status found.", e.getMessage()); |
| 138 } | 142 } |
| 139 } | 143 } |
| 144 |
| 145 @SmallTest |
| 146 @Feature({"Cronet"}) |
| 147 // Regression test for crbug.com/606872. |
| 148 @OnlyRunNativeCronet |
| 149 public void testGetStatusForUpload() throws Exception { |
| 150 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 151 UrlRequest.Builder builder = new UrlRequest.Builder(NativeTestServer.get
EchoBodyURL(), |
| 152 callback, callback.getExecutor(), mTestFramework.mCronetEngine); |
| 153 |
| 154 final ConditionVariable block = new ConditionVariable(); |
| 155 // Use a separate executor for UploadDataProvider so the upload can be |
| 156 // stalled while getStatus gets processed. |
| 157 Executor uploadProviderExecutor = Executors.newSingleThreadExecutor(); |
| 158 TestUploadDataProvider dataProvider = new TestUploadDataProvider( |
| 159 TestUploadDataProvider.SuccessCallbackMode.SYNC, uploadProviderE
xecutor) { |
| 160 @Override |
| 161 public long getLength() throws IOException { |
| 162 // Pause the data provider. |
| 163 block.block(); |
| 164 block.close(); |
| 165 return super.getLength(); |
| 166 } |
| 167 }; |
| 168 dataProvider.addRead("test".getBytes()); |
| 169 builder.setUploadDataProvider(dataProvider, uploadProviderExecutor); |
| 170 builder.addHeader("Content-Type", "useless/string"); |
| 171 UrlRequest urlRequest = builder.build(); |
| 172 TestStatusListener statusListener = new TestStatusListener(); |
| 173 urlRequest.start(); |
| 174 // Call getStatus() immediately after start(), which will post |
| 175 // startInternal() to the upload provider's executor because there is an |
| 176 // upload. When CronetUrlRequestAdapter::GetStatusOnNetworkThread is |
| 177 // executed, the |url_request_| is null. |
| 178 urlRequest.getStatus(statusListener); |
| 179 statusListener.waitUntilOnStatusCalled(); |
| 180 assertTrue(statusListener.mOnStatusCalled); |
| 181 // The request should be in IDLE state because GetStatusOnNetworkThread |
| 182 // is called before |url_request_| is initialized and started. |
| 183 assertEquals(Status.IDLE, statusListener.mStatus); |
| 184 // Resume the UploadDataProvider. |
| 185 block.open(); |
| 186 |
| 187 // Make sure the request is successful and there is no crash. |
| 188 callback.blockForDone(); |
| 189 dataProvider.assertClosed(); |
| 190 |
| 191 assertEquals(4, dataProvider.getUploadedLength()); |
| 192 assertEquals(1, dataProvider.getNumReadCalls()); |
| 193 assertEquals(0, dataProvider.getNumRewindCalls()); |
| 194 |
| 195 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 196 assertEquals("test", callback.mResponseAsString); |
| 197 } |
| 140 } | 198 } |
| OLD | NEW |