1 // Copyright 2021 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package com.google.android.downloader; 16 17 import static com.google.common.util.concurrent.MoreExecutors.listeningDecorator; 18 import static java.util.concurrent.Executors.newSingleThreadExecutor; 19 20 import com.google.common.util.concurrent.ListeningExecutorService; 21 import java.util.concurrent.Executors; 22 import okhttp3.Dispatcher; 23 import okhttp3.OkHttpClient; 24 import org.junit.After; 25 import org.junit.Before; 26 import org.junit.Rule; 27 import org.junit.Test; 28 import org.junit.rules.TemporaryFolder; 29 import org.junit.runner.RunWith; 30 import org.junit.runners.JUnit4; 31 32 /** Unit tests for OkHttp3UrlEngine. */ 33 @RunWith(JUnit4.class) 34 public class OkHttp3UrlEngineTest { 35 @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); 36 37 private MockWebServerUrlEngineTestHelper testHelper; 38 private OkHttp3UrlEngine engine; 39 private TestingExecutorService dispatchExecutorService; 40 private ListeningExecutorService transferExecutorService; 41 42 @Before setUp()43 public void setUp() { 44 dispatchExecutorService = new TestingExecutorService(Executors.newSingleThreadExecutor()); 45 testHelper = new MockWebServerUrlEngineTestHelper(temporaryFolder, dispatchExecutorService); 46 transferExecutorService = listeningDecorator(newSingleThreadExecutor()); 47 // Note: The OkHttpClient dispatcher uses the TestingExecutorService (which waits to execute 48 // tasks) in order to ensure that OkHttp requests are executed in an asynchronous manner, so 49 // that we can properly test request cancellation. 50 OkHttpClient client = 51 new OkHttpClient.Builder().dispatcher(new Dispatcher(dispatchExecutorService)).build(); 52 engine = new OkHttp3UrlEngine(client, transferExecutorService); 53 } 54 55 @After tearDown()56 public void tearDown() throws Exception { 57 testHelper.tearDown(); 58 transferExecutorService.shutdown(); 59 dispatchExecutorService.shutdown(); 60 } 61 62 @Test executeRequest_normalResponse_succeeds()63 public void executeRequest_normalResponse_succeeds() throws Exception { 64 testHelper.executeRequest_normalResponse_succeeds(engine); 65 } 66 67 @Test executeRequest_responseThrottled_succeeds()68 public void executeRequest_responseThrottled_succeeds() throws Exception { 69 testHelper.executeRequest_responseThrottled_succeeds(engine); 70 } 71 72 @Test executeRequest_largeResponse_succeeds()73 public void executeRequest_largeResponse_succeeds() throws Exception { 74 testHelper.executeRequest_largeResponse_succeeds(engine, 1024 * 1024); 75 } 76 77 @Test executeRequest_closeBeforeWrite_failsAborted()78 public void executeRequest_closeBeforeWrite_failsAborted() throws Exception { 79 testHelper.executeRequest_closeBeforeWrite_failsAborted(engine); 80 } 81 82 @Test executeRequest_serverError_failsInternalError()83 public void executeRequest_serverError_failsInternalError() throws Exception { 84 testHelper.executeRequest_serverError_failsInternalError(engine); 85 } 86 87 @Test executeRequest_networkError_failsInternalError()88 public void executeRequest_networkError_failsInternalError() throws Exception { 89 testHelper.executeRequest_networkError_failsInternalError(engine); 90 } 91 92 @Test executeRequest_writeError_failsInternalError()93 public void executeRequest_writeError_failsInternalError() throws Exception { 94 testHelper.executeRequest_writeError_failsInternalError(engine); 95 } 96 97 @Test executeRequest_requestCanceled_requestNeverSent()98 public void executeRequest_requestCanceled_requestNeverSent() throws Exception { 99 testHelper.executeRequest_requestCanceled_requestNeverSent(engine); 100 } 101 102 @Test executeRequest_invalidUrl_failsInvalidArgument()103 public void executeRequest_invalidUrl_failsInvalidArgument() { 104 testHelper.executeRequest_invalidUrl_failsInvalidArgument(engine); 105 } 106 } 107