1 /*
2  * Copyright (C) 2022 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 com.android.adservices.service;
18 
19 import static com.android.adservices.service.FlagsConstants.KEY_AD_ID_FETCHER_TIMEOUT_MS;
20 import static com.android.adservices.service.FlagsConstants.KEY_DISABLE_FLEDGE_ENROLLMENT_CHECK;
21 import static com.android.adservices.service.FlagsConstants.KEY_ENFORCE_FOREGROUND_STATUS_FLEDGE_OVERRIDE;
22 import static com.android.adservices.service.FlagsConstants.KEY_ENFORCE_FOREGROUND_STATUS_FLEDGE_REPORT_IMPRESSION;
23 import static com.android.adservices.service.FlagsConstants.KEY_ENFORCE_FOREGROUND_STATUS_FLEDGE_REPORT_INTERACTION;
24 import static com.android.adservices.service.FlagsConstants.KEY_ENFORCE_FOREGROUND_STATUS_FLEDGE_RUN_AD_SELECTION;
25 import static com.android.adservices.service.FlagsConstants.KEY_FLEDGE_AD_SELECTION_CONTEXTUAL_ADS_ENABLED;
26 import static com.android.adservices.service.FlagsConstants.KEY_FLEDGE_AD_SELECTION_PREBUILT_URI_ENABLED;
27 import static com.android.adservices.service.FlagsConstants.KEY_FLEDGE_AUCTION_SERVER_KILL_SWITCH;
28 import static com.android.adservices.service.FlagsConstants.KEY_FLEDGE_CPC_BILLING_ENABLED;
29 import static com.android.adservices.service.FlagsConstants.KEY_FLEDGE_DATA_VERSION_HEADER_ENABLED;
30 import static com.android.adservices.service.FlagsConstants.KEY_FLEDGE_REGISTER_AD_BEACON_ENABLED;
31 import static com.android.adservices.service.FlagsConstants.KEY_SDK_REQUEST_PERMITS_PER_SECOND;
32 
33 import android.provider.DeviceConfig;
34 
35 /**
36  * In order to use this test fixture, make sure your UNIT test class includes a {@link
37  * TestableDeviceConfigRule} like the following:
38  *
39  * <p>{@code @Rule public final TestableDeviceConfig.TestableDeviceConfigRule deviceConfigRule = new
40  * TestableDeviceConfig.TestableDeviceConfigRule(); }
41  *
42  * <p>If you're using it on CTS tests, you need to make sure the callers have the {@code
43  * Manifest.permission.WRITE_DEVICE_CONFIG}, but a better approach would be to use {@link
44  * com.android.adservices.common.AdServicesFlagsSetterRule} instead (as that rule will take care of
45  * automatically resetting the flags to the initial value, among other features).
46  *
47  * @deprecated TODO(b/317418539) - most usages (especially on CTS) are better suited by the {@link
48  *     com.android.adservices.common.AdServicesFlagsSetterRule} JUnit rule, which provides other
49  *     advantages like properly resetting the flag at the end of the test (while tests using this
50  *     fixture need to manually use a {@code try / finally} block to do so) and supporting
51  *     annotations. So, assuming your test already uses the rule, all you need to do is to annotate
52  *     the test method with the proper annotations (like
53  *     {@code @SetFlagEnabled(FlagsConstants.MY_FEATURE_FLAG}} or
54  *     {@code @SetIntegerFlag(name=FlagsConstants.MY_CONFIG_FLAG}, value=42)} or call the {@code
55  *     setFlag()} method of the rule when the value is not static (for example, if the rule
56  *     reference is called {@code flags}, simply call {@code
57  *     flags.setFlag(FlagsConstant.MY_PGK_ALLOWLIST_FLAG, pkgNames)}).
58  */
59 @Deprecated
60 public final class PhFlagsFixture {
61 
62     /** Enables test to override the flag enabling ad selection with contextual ads */
overrideFledgeAdSelectionContextualAdsEnabled(boolean value)63     public static void overrideFledgeAdSelectionContextualAdsEnabled(boolean value) {
64         DeviceConfig.setProperty(
65                 DeviceConfig.NAMESPACE_ADSERVICES,
66                 KEY_FLEDGE_AD_SELECTION_CONTEXTUAL_ADS_ENABLED,
67                 Boolean.toString(value),
68                 false);
69     }
70 
71     /**
72      * Enables test to override the flag enabling the foreground status check for callers of the
73      * Fledge Run Ad Selection API.
74      */
overrideForegroundStatusForFledgeRunAdSelection(boolean value)75     public static void overrideForegroundStatusForFledgeRunAdSelection(boolean value) {
76         DeviceConfig.setProperty(
77                 DeviceConfig.NAMESPACE_ADSERVICES,
78                 KEY_ENFORCE_FOREGROUND_STATUS_FLEDGE_RUN_AD_SELECTION,
79                 Boolean.toString(value),
80                 false);
81     }
82 
83     /**
84      * Enables test to override the flag enabling the foreground status check for callers of the
85      * Fledge Report Impression API.
86      */
overrideForegroundStatusForFledgeReportImpression(boolean value)87     public static void overrideForegroundStatusForFledgeReportImpression(boolean value) {
88         DeviceConfig.setProperty(
89                 DeviceConfig.NAMESPACE_ADSERVICES,
90                 KEY_ENFORCE_FOREGROUND_STATUS_FLEDGE_REPORT_IMPRESSION,
91                 Boolean.toString(value),
92                 false);
93     }
94 
95     /**
96      * Enables test to override the flag enabling the foreground status check for callers of the
97      * Fledge Report Interaction API.
98      */
overrideForegroundStatusForFledgeReportInteraction(boolean value)99     public static void overrideForegroundStatusForFledgeReportInteraction(boolean value) {
100         DeviceConfig.setProperty(
101                 DeviceConfig.NAMESPACE_ADSERVICES,
102                 KEY_ENFORCE_FOREGROUND_STATUS_FLEDGE_REPORT_INTERACTION,
103                 Boolean.toString(value),
104                 false);
105     }
106 
107     /**
108      * Enables test to override the flag enabling the foreground status check for callers of the
109      * Fledge Override API.
110      */
overrideForegroundStatusForFledgeOverrides(boolean value)111     public static void overrideForegroundStatusForFledgeOverrides(boolean value) {
112         DeviceConfig.setProperty(
113                 DeviceConfig.NAMESPACE_ADSERVICES,
114                 KEY_ENFORCE_FOREGROUND_STATUS_FLEDGE_OVERRIDE,
115                 Boolean.toString(value),
116                 false);
117     }
118 
119     /**
120      * Enables test to override the flag enabling the foreground status check for callers of the
121      * Custom Audience API.
122      */
overrideForegroundStatusForFledgeCustomAudience(boolean value)123     public static void overrideForegroundStatusForFledgeCustomAudience(boolean value) {
124         DeviceConfig.setProperty(
125                 DeviceConfig.NAMESPACE_ADSERVICES,
126                 FlagsConstants.KEY_ENFORCE_FOREGROUND_STATUS_FLEDGE_CUSTOM_AUDIENCE,
127                 Boolean.toString(value),
128                 false);
129     }
130 
131     /**
132      * Enables test to override the flag enabling the enrollment check for callers of Fledge APIs.
133      *
134      * @param enable whether enable or disable the check
135      */
overrideFledgeEnrollmentCheck(boolean enable)136     public static void overrideFledgeEnrollmentCheck(boolean enable) {
137         DeviceConfig.setProperty(
138                 DeviceConfig.NAMESPACE_ADSERVICES,
139                 KEY_DISABLE_FLEDGE_ENROLLMENT_CHECK,
140                 Boolean.toString(!enable),
141                 false);
142     }
143 
144     /**
145      * Enables test to override the flag enabling Protected Audience's event-level debug reporting.
146      *
147      * @param enable whether enable or disable the check
148      */
overrideFledgeEventLevelDebugReportingEnabled(boolean enable)149     public static void overrideFledgeEventLevelDebugReportingEnabled(boolean enable) {
150         DeviceConfig.setProperty(
151                 DeviceConfig.NAMESPACE_ADSERVICES,
152                 FlagsConstants.KEY_FLEDGE_EVENT_LEVEL_DEBUG_REPORTING_ENABLED,
153                 Boolean.toString(enable),
154                 false);
155     }
156 
overrideFledgeEventLevelDebugReportSendImmediately(boolean enable)157     public static void overrideFledgeEventLevelDebugReportSendImmediately(boolean enable) {
158         DeviceConfig.setProperty(
159                 DeviceConfig.NAMESPACE_ADSERVICES,
160                 FlagsConstants.KEY_FLEDGE_EVENT_LEVEL_DEBUG_REPORT_SEND_IMMEDIATELY,
161                 Boolean.toString(enable),
162                 false);
163     }
164 
overrideFledgeEventLevelDebugReportingBatchDelay(int batchDelayInSeconds)165     public static void overrideFledgeEventLevelDebugReportingBatchDelay(int batchDelayInSeconds) {
166         DeviceConfig.setProperty(
167                 DeviceConfig.NAMESPACE_ADSERVICES,
168                 FlagsConstants.KEY_FLEDGE_EVENT_LEVEL_DEBUG_REPORTING_BATCH_DELAY_SECONDS,
169                 Integer.toString(batchDelayInSeconds),
170                 false);
171     }
172 
overrideFledgeEventLevelDebugReportingMaxItemsPerBatch( int maxItemsPerBatch)173     public static void overrideFledgeEventLevelDebugReportingMaxItemsPerBatch(
174             int maxItemsPerBatch) {
175         DeviceConfig.setProperty(
176                 DeviceConfig.NAMESPACE_ADSERVICES,
177                 FlagsConstants.KEY_FLEDGE_EVENT_LEVEL_DEBUG_REPORTING_MAX_ITEMS_PER_BATCH,
178                 Integer.toString(maxItemsPerBatch),
179                 false);
180     }
181 
overrideFledgeDebugReportSenderJobNetworkConnectionTimeoutMs( int phOverrideValue)182     public static void overrideFledgeDebugReportSenderJobNetworkConnectionTimeoutMs(
183             int phOverrideValue) {
184         DeviceConfig.setProperty(
185                 DeviceConfig.NAMESPACE_ADSERVICES,
186                 FlagsConstants.KEY_FLEDGE_DEBUG_REPORTI_SENDER_JOB_NETWORK_CONNECT_TIMEOUT_MS,
187                 Integer.toString(phOverrideValue),
188                 false);
189     }
190 
overrideFledgeDebugReportSenderJobNetworkReadTimeoutMs(int phOverrideValue)191     public static void overrideFledgeDebugReportSenderJobNetworkReadTimeoutMs(int phOverrideValue) {
192         DeviceConfig.setProperty(
193                 DeviceConfig.NAMESPACE_ADSERVICES,
194                 FlagsConstants.KEY_FLEDGE_DEBUG_REPORTI_SENDER_JOB_NETWORK_READ_TIMEOUT_MS,
195                 Integer.toString(phOverrideValue),
196                 false);
197     }
198 
overrideFledgeDebugReportSenderJobMaxRuntimeMs(long phOverrideValue)199     public static void overrideFledgeDebugReportSenderJobMaxRuntimeMs(long phOverrideValue) {
200         DeviceConfig.setProperty(
201                 DeviceConfig.NAMESPACE_ADSERVICES,
202                 FlagsConstants.KEY_FLEDGE_DEBUG_REPORTI_SENDER_JOB_MAX_TIMEOUT_MS,
203                 Long.toString(phOverrideValue),
204                 false);
205     }
206 
overrideFledgeDebugReportSenderJobPeriodicMs(long phOverrideValue)207     public static void overrideFledgeDebugReportSenderJobPeriodicMs(long phOverrideValue) {
208         DeviceConfig.setProperty(
209                 DeviceConfig.NAMESPACE_ADSERVICES,
210                 FlagsConstants.KEY_FLEDGE_DEBUG_REPORT_SENDER_JOB_PERIOD_MS,
211                 Long.toString(phOverrideValue),
212                 false);
213     }
214 
overrideFledgeDebugReportSenderJobFlexMs(long phOverrideValue)215     public static void overrideFledgeDebugReportSenderJobFlexMs(long phOverrideValue) {
216         DeviceConfig.setProperty(
217                 DeviceConfig.NAMESPACE_ADSERVICES,
218                 FlagsConstants.KEY_FLEDGE_DEBUG_REPORT_SENDER_JOB_FLEX_MS,
219                 Long.toString(phOverrideValue),
220                 false);
221     }
222 
overrideEnforceIsolateMaxHeapSize(boolean value)223     public static void overrideEnforceIsolateMaxHeapSize(boolean value) {
224         DeviceConfig.setProperty(
225                 DeviceConfig.NAMESPACE_ADSERVICES,
226                 FlagsConstants.KEY_ENFORCE_ISOLATE_MAX_HEAP_SIZE,
227                 Boolean.toString(value),
228                 false);
229     }
230 
overrideIsolateMaxHeapSizeBytes(long value)231     public static void overrideIsolateMaxHeapSizeBytes(long value) {
232         DeviceConfig.setProperty(
233                 DeviceConfig.NAMESPACE_ADSERVICES,
234                 FlagsConstants.KEY_ISOLATE_MAX_HEAP_SIZE_BYTES,
235                 Long.toString(value),
236                 false);
237     }
238 
239     /** Enables or disables the HTTP cache */
overrideHttpClientCacheEnabled(boolean value)240     public static void overrideHttpClientCacheEnabled(boolean value) {
241         DeviceConfig.setProperty(
242                 DeviceConfig.NAMESPACE_ADSERVICES,
243                 FlagsConstants.KEY_FLEDGE_HTTP_CACHE_ENABLE,
244                 Boolean.toString(value),
245                 false);
246     }
247 
overrideSdkRequestPermitsPerSecond(int value)248     public static void overrideSdkRequestPermitsPerSecond(int value) {
249         DeviceConfig.setProperty(
250                 DeviceConfig.NAMESPACE_ADSERVICES,
251                 KEY_SDK_REQUEST_PERMITS_PER_SECOND,
252                 Integer.toString(value),
253                 true);
254     }
255 
256     /** Overrides whether the {@code registerAdBeacon} feature is enabled. */
overrideFledgeRegisterAdBeaconEnabled(boolean value)257     public static void overrideFledgeRegisterAdBeaconEnabled(boolean value) {
258         DeviceConfig.setProperty(
259                 DeviceConfig.NAMESPACE_ADSERVICES,
260                 KEY_FLEDGE_REGISTER_AD_BEACON_ENABLED,
261                 Boolean.toString(value),
262                 false);
263     }
264 
265     /** Overrides whether the CPC billing feature is enabled. */
overrideFledgeCpcBillingEnabled(boolean value)266     public static void overrideFledgeCpcBillingEnabled(boolean value) {
267         DeviceConfig.setProperty(
268                 DeviceConfig.NAMESPACE_ADSERVICES,
269                 KEY_FLEDGE_CPC_BILLING_ENABLED,
270                 Boolean.toString(value),
271                 false);
272     }
273 
274     /** Overrides whether the CPC billing feature is enabled. */
overrideFledgeDataVersionHeaderEnabled(boolean value)275     public static void overrideFledgeDataVersionHeaderEnabled(boolean value) {
276         DeviceConfig.setProperty(
277                 DeviceConfig.NAMESPACE_ADSERVICES,
278                 KEY_FLEDGE_DATA_VERSION_HEADER_ENABLED,
279                 Boolean.toString(value),
280                 false);
281     }
282 
283     /** Overrides whether the {@code prebuilt Uri} feature is enabled. */
overrideFledgeAdSelectionPrebuiltUriEnabled(boolean value)284     public static void overrideFledgeAdSelectionPrebuiltUriEnabled(boolean value) {
285         DeviceConfig.setProperty(
286                 DeviceConfig.NAMESPACE_ADSERVICES,
287                 KEY_FLEDGE_AD_SELECTION_PREBUILT_URI_ENABLED,
288                 Boolean.toString(value),
289                 false);
290     }
291 
292     /** Overrides whether the auction server APIs are enabled. */
overrideFledgeAdSelectionAuctionServerApisEnabled(boolean value)293     public static void overrideFledgeAdSelectionAuctionServerApisEnabled(boolean value) {
294         DeviceConfig.setProperty(
295                 DeviceConfig.NAMESPACE_ADSERVICES,
296                 KEY_FLEDGE_AUCTION_SERVER_KILL_SWITCH,
297                 Boolean.toString(value),
298                 false);
299     }
300 
301     /** Overrides the timeouts for on-device auctions. */
overrideFledgeOnDeviceAdSelectionTimeouts( long biddingTimeoutPerCaMs, long scoringTimeoutMs, long overallTimeoutMs)302     public static void overrideFledgeOnDeviceAdSelectionTimeouts(
303             long biddingTimeoutPerCaMs, long scoringTimeoutMs, long overallTimeoutMs) {
304         DeviceConfig.setProperty(
305                 DeviceConfig.NAMESPACE_ADSERVICES,
306                 FlagsConstants.KEY_FLEDGE_AD_SELECTION_BIDDING_TIMEOUT_PER_CA_MS,
307                 Long.toString(biddingTimeoutPerCaMs),
308                 false);
309         DeviceConfig.setProperty(
310                 DeviceConfig.NAMESPACE_ADSERVICES,
311                 FlagsConstants.KEY_FLEDGE_AD_SELECTION_SCORING_TIMEOUT_MS,
312                 Long.toString(scoringTimeoutMs),
313                 false);
314         DeviceConfig.setProperty(
315                 DeviceConfig.NAMESPACE_ADSERVICES,
316                 FlagsConstants.KEY_FLEDGE_AD_SELECTION_OVERALL_TIMEOUT_MS,
317                 Long.toString(overallTimeoutMs),
318                 false);
319     }
320 
321     /** Overrides timeout for {@link com.android.adservices.service.adselection.AdIdFetcher}. */
overrideAdIdFetcherTimeoutMs(long timeoutMs)322     public static void overrideAdIdFetcherTimeoutMs(long timeoutMs) {
323         DeviceConfig.setProperty(
324                 DeviceConfig.NAMESPACE_ADSERVICES,
325                 KEY_AD_ID_FETCHER_TIMEOUT_MS,
326                 Long.toString(timeoutMs),
327                 false);
328     }
329 
330     // DO NOT ADD MORE METHODS HERE, THIS CLASS IS DEPRECATED.
331     //
332     // If you still need a new method instead of using AdServicesFlagsSetterRule or other infra,
333     // please add a TODO(b/317418539) comment on such method explaining why (and include a
334     // Bug: 317418539 line in the CL that adds it).
335 }
336