1 /* 2 * Copyright (C) 2023 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 17 package android.net.http.cts 18 19 import android.content.Context 20 import android.net.http.CallbackException 21 import android.net.http.HttpEngine 22 import android.net.http.cts.util.HttpCtsTestServer 23 import android.net.http.cts.util.TestUrlRequestCallback 24 import android.net.http.cts.util.TestUrlRequestCallback.FailureType 25 import android.net.http.cts.util.TestUrlRequestCallback.ResponseStep 26 import android.os.Build 27 import androidx.test.core.app.ApplicationProvider 28 import com.android.testutils.DevSdkIgnoreRule 29 import com.android.testutils.DevSdkIgnoreRunner 30 import kotlin.test.Test 31 import kotlin.test.assertEquals 32 import kotlin.test.assertIs 33 import kotlin.test.assertSame 34 import kotlin.test.assertTrue 35 import org.junit.runner.RunWith 36 37 @RunWith(DevSdkIgnoreRunner::class) 38 @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R) 39 class CallbackExceptionTest { 40 41 @Test testCallbackException_returnsInputParametersnull42 fun testCallbackException_returnsInputParameters() { 43 val message = "failed" 44 val cause = Throwable("exception") 45 val callbackException = object : CallbackException(message, cause) {} 46 47 assertEquals(message, callbackException.message) 48 assertSame(cause, callbackException.cause) 49 } 50 51 @Test testCallbackException_thrownFromUrlRequestnull52 fun testCallbackException_thrownFromUrlRequest() { 53 val context: Context = ApplicationProvider.getApplicationContext() 54 val server = HttpCtsTestServer(context) 55 val httpEngine = HttpEngine.Builder(context).build() 56 val callback = TestUrlRequestCallback() 57 callback.setFailure(FailureType.THROW_SYNC, ResponseStep.ON_RESPONSE_STARTED) 58 val request = httpEngine 59 .newUrlRequestBuilder(server.successUrl, callback.executor, callback) 60 .build() 61 62 request.start() 63 callback.blockForDone() 64 65 assertTrue(request.isDone) 66 assertIs<CallbackException>(callback.mError) 67 server.shutdown() 68 httpEngine.shutdown() 69 } 70 } 71