• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.adservices.service.CommonDebugFlagsConstants.KEY_ADSERVICES_SHELL_COMMAND_ENABLED;
20 import static com.android.adservices.service.DebugFlagsConstants.KEY_CONSENT_NOTIFICATION_DEBUG_MODE;
21 import static com.android.adservices.service.DebugFlagsConstants.KEY_FLEDGE_IS_CUSTOM_AUDIENCE_CLI_ENABLED;
22 import static com.android.adservices.service.FlagsConstants.KEY_DISABLE_FLEDGE_ENROLLMENT_CHECK;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import android.adservices.common.AdData;
27 import android.adservices.common.AdSelectionSignals;
28 import android.adservices.common.AdTechIdentifier;
29 import android.adservices.customaudience.CustomAudience;
30 import android.adservices.utils.ScenarioDispatcher;
31 import android.adservices.utils.ScenarioDispatcherFactory;
32 import android.adservices.utils.Scenarios;
33 import android.net.Uri;
34 
35 import com.android.adservices.common.AdServicesShellCommandHelper;
36 import com.android.adservices.shared.testing.annotations.EnableDebugFlag;
37 import com.android.adservices.shared.testing.annotations.SetFlagEnabled;
38 
39 import org.json.JSONException;
40 import org.json.JSONObject;
41 import org.junit.Test;
42 
43 import java.util.List;
44 
45 @SetFlagEnabled(KEY_DISABLE_FLEDGE_ENROLLMENT_CHECK)
46 @EnableDebugFlag(KEY_ADSERVICES_SHELL_COMMAND_ENABLED)
47 @EnableDebugFlag(KEY_FLEDGE_IS_CUSTOM_AUDIENCE_CLI_ENABLED)
48 @EnableDebugFlag(KEY_CONSENT_NOTIFICATION_DEBUG_MODE)
49 public final class CustomAudienceShellCommandsScenarioTest extends FledgeDebuggableScenarioTest {
50     private final AdServicesShellCommandHelper mShellCommandHelper =
51             new AdServicesShellCommandHelper();
52 
53     @Test
testRun_refreshCustomAudiences_verifyCustomAudienceChanged()54     public void testRun_refreshCustomAudiences_verifyCustomAudienceChanged() throws Exception {
55         ScenarioDispatcher dispatcher =
56                 setupDispatcher(
57                         ScenarioDispatcherFactory.createFromScenarioFileWithRandomPrefix(
58                                 "scenarios/remarketing-cuj-refresh-ca.json"));
59         joinCustomAudience(SHOES_CA);
60         AdTechIdentifier adTechIdentifier =
61                 AdTechIdentifier.fromString(dispatcher.getBaseAddressWithPrefix().getHost());
62         String baseAddressWithPrefix = dispatcher.getBaseAddressWithPrefix().toString();
63 
64         CustomAudience customAudienceBefore = getCustomAudience(adTechIdentifier);
65         mShellCommandHelper.runCommand(
66                 "custom-audience refresh --owner %s --buyer %s --name %s",
67                 mPackageName, adTechIdentifier, SHOES_CA);
68         CustomAudience customAudienceAfter = getCustomAudience(adTechIdentifier);
69 
70         assertThat(customAudienceBefore).isNotEqualTo(customAudienceAfter);
71         assertThat(customAudienceAfter.getTrustedBiddingData().getTrustedBiddingUri())
72                 .isEqualTo(Uri.parse(baseAddressWithPrefix + Scenarios.BIDDING_SIGNALS_PATH));
73         assertThat(customAudienceAfter.getTrustedBiddingData().getTrustedBiddingKeys())
74                 .isEqualTo(List.of("key1", "key2"));
75         assertThat(customAudienceAfter.getUserBiddingSignals())
76                 .isEqualTo(
77                         AdSelectionSignals.fromString(
78                                 "{\"valid\":true,\"arbitrary\":\"yes\"}", true));
79 
80         assertThat(customAudienceAfter.getAds())
81                 .isEqualTo(
82                         List.of(
83                                 new AdData.Builder()
84                                         .setRenderUri(
85                                                 Uri.parse(
86                                                         baseAddressWithPrefix
87                                                                 + Scenarios.AD_RENDER_1))
88                                         .setMetadata("{\"valid\":1}")
89                                         .build(),
90                                 new AdData.Builder()
91                                         .setRenderUri(
92                                                 Uri.parse(
93                                                         baseAddressWithPrefix
94                                                                 + Scenarios.AD_RENDER_2))
95                                         .setMetadata("{\"valid\":2}")
96                                         .build()));
97         assertThat(dispatcher.getCalledPaths())
98                 .containsAtLeastElementsIn(dispatcher.getVerifyCalledPaths());
99         leaveCustomAudience(SHOES_CA);
100     }
101 
getCustomAudience(AdTechIdentifier adTechIdentifier)102     private CustomAudience getCustomAudience(AdTechIdentifier adTechIdentifier)
103             throws JSONException {
104         return CustomAudienceShellCommandHelper.fromJson(
105                 new JSONObject(
106                         mShellCommandHelper.runCommand(
107                                 "custom-audience view --owner %s --buyer %s --name %s",
108                                 mPackageName, adTechIdentifier, SHOES_CA)));
109     }
110 }
111