1 /* 2 * Copyright (C) 2024 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.adservices.debuggablects; 18 19 import static android.adservices.common.AdServicesStatusUtils.ILLEGAL_STATE_BACKGROUND_CALLER_ERROR_MESSAGE; 20 import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; 21 22 import static com.android.adservices.service.FlagsConstants.KEY_FOREGROUND_STATUS_LEVEL; 23 24 import android.adservices.utils.FledgeScenarioTest; 25 import android.adservices.utils.ScenarioDispatcher; 26 import android.adservices.utils.ScenarioDispatcherFactory; 27 28 import com.android.adservices.shared.testing.annotations.RequiresSdkLevelAtLeastT; 29 import com.android.adservices.shared.testing.annotations.SetIntegerFlag; 30 31 import com.google.common.truth.Truth; 32 33 import org.junit.After; 34 import org.junit.Assert; 35 import org.junit.Before; 36 import org.junit.Test; 37 38 import java.util.concurrent.ExecutionException; 39 40 @SetIntegerFlag(name = KEY_FOREGROUND_STATUS_LEVEL, value = IMPORTANCE_FOREGROUND) 41 @RequiresSdkLevelAtLeastT(reason = "No foreground check in S-") 42 public class FledgeApiCallFromBackgroundTest extends FledgeScenarioTest { 43 private ScenarioDispatcher mDispatcher; 44 45 @Before setup()46 public void setup() throws Exception { 47 mDispatcher = 48 setupDispatcher( 49 ScenarioDispatcherFactory.createFromScenarioFileWithRandomPrefix( 50 "scenarios/remarketing-cuj-default.json")); 51 } 52 53 @After teardown()54 public void teardown() throws Exception { 55 Truth.assertThat(mDispatcher.getCalledPaths()).isEmpty(); 56 } 57 58 /** CUJ 054: Calling select ads API from background will fail. */ 59 @Test testRunAdSelectionFromBackground()60 public void testRunAdSelectionFromBackground() { 61 Exception e = 62 Assert.assertThrows( 63 ExecutionException.class, 64 () -> 65 doSelectAds( 66 makeAdSelectionConfig( 67 mDispatcher.getBaseAddressWithPrefix()))); 68 Assert.assertTrue(e.getCause() instanceof IllegalStateException); 69 Assert.assertEquals( 70 ILLEGAL_STATE_BACKGROUND_CALLER_ERROR_MESSAGE, e.getCause().getMessage()); 71 } 72 } 73