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.stats;
18 
19 import static android.adservices.common.AdServicesStatusUtils.STATUS_INTERNAL_ERROR;
20 import static android.adservices.common.AdServicesStatusUtils.STATUS_SUCCESS;
21 
22 import static com.android.adservices.data.adselection.AdSelectionDatabase.DATABASE_NAME;
23 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.MISSING_END_GET_AD_SELECTION_LOGIC;
24 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.MISSING_END_GET_BUYERS_CUSTOM_AUDIENCE;
25 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.MISSING_END_PERSIST_AD_SELECTION;
26 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.MISSING_END_SCORE_ADS;
27 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.MISSING_GET_BUYERS_CUSTOM_AUDIENCE;
28 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.MISSING_PERSIST_AD_SELECTION;
29 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.MISSING_START_BIDDING_STAGE;
30 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.MISSING_START_GET_AD_SCORES;
31 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.MISSING_START_GET_AD_SELECTION_LOGIC;
32 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.MISSING_START_GET_TRUSTED_SCORING_SIGNALS;
33 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.MISSING_START_PERSIST_AD_SELECTION;
34 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.MISSING_START_RUN_AD_BIDDING;
35 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.MISSING_START_RUN_AD_SCORING;
36 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.MISSING_START_SCORE_ADS;
37 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.RATIO_OF_CAS_UNSET;
38 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.REPEATED_END_BIDDING_STAGE;
39 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.REPEATED_END_GET_AD_SCORES;
40 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.REPEATED_END_GET_AD_SELECTION_LOGIC;
41 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.REPEATED_END_GET_BUYERS_CUSTOM_AUDIENCE;
42 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.REPEATED_END_GET_TRUSTED_SCORING_SIGNALS;
43 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.REPEATED_END_PERSIST_AD_SELECTION;
44 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.REPEATED_END_RUN_AD_SCORING;
45 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.REPEATED_END_SCORE_ADS;
46 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.REPEATED_START_BIDDING_STAGE;
47 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.REPEATED_START_GET_AD_SCORES;
48 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.REPEATED_START_GET_AD_SELECTION_LOGIC;
49 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.REPEATED_START_GET_TRUSTED_SCORING_SIGNALS;
50 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.REPEATED_START_PERSIST_AD_SELECTION;
51 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.REPEATED_START_RUN_AD_BIDDING;
52 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.REPEATED_START_RUN_AD_SCORING;
53 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.REPEATED_START_SCORE_ADS;
54 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.SCRIPT_JAVASCRIPT;
55 import static com.android.adservices.service.stats.AdSelectionExecutionLogger.SCRIPT_UNSET;
56 import static com.android.adservices.service.stats.AdServicesLoggerUtil.FIELD_UNSET;
57 import static com.android.adservices.service.stats.AdsRelevanceStatusUtils.JS_RUN_STATUS_JS_REFERENCE_ERROR;
58 import static com.android.adservices.service.stats.AdsRelevanceStatusUtils.JS_RUN_STATUS_SUCCESS;
59 import static com.android.adservices.service.stats.AdsRelevanceStatusUtils.JS_RUN_STATUS_UNSET;
60 
61 import static com.google.common.truth.Truth.assertThat;
62 
63 import static org.junit.Assert.assertThrows;
64 import static org.mockito.Mockito.verify;
65 import static org.mockito.Mockito.when;
66 
67 import android.adservices.adselection.AdBiddingOutcomeFixture;
68 import android.adservices.adselection.AdSelectionConfigFixture;
69 import android.adservices.common.AdSelectionSignals;
70 import android.adservices.common.AdServicesStatusUtils;
71 import android.adservices.common.AdTechIdentifier;
72 import android.adservices.common.CallerMetadata;
73 import android.content.Context;
74 import android.net.Uri;
75 import android.util.Pair;
76 
77 import com.android.adservices.customaudience.DBCustomAudienceFixture;
78 import com.android.adservices.data.adselection.DBAdSelection;
79 import com.android.adservices.data.customaudience.DBCustomAudience;
80 import com.android.adservices.service.Flags;
81 import com.android.adservices.service.adselection.AdBiddingOutcome;
82 import com.android.adservices.shared.testing.SdkLevelSupportRule;
83 import com.android.adservices.shared.util.Clock;
84 
85 import org.junit.Before;
86 import org.junit.Rule;
87 import org.junit.Test;
88 import org.mockito.ArgumentCaptor;
89 import org.mockito.Captor;
90 import org.mockito.Mock;
91 import org.mockito.MockitoAnnotations;
92 
93 import java.io.File;
94 import java.nio.charset.StandardCharsets;
95 import java.util.Arrays;
96 import java.util.List;
97 import java.util.Objects;
98 import java.util.stream.Collectors;
99 
100 public class AdSelectionExecutionLoggerTest {
101     public static final int GET_BUYERS_CUSTOM_AUDIENCE_LATENCY_MS = 1;
102     public static final int RUN_AD_BIDDING_LATENCY_MS = 1;
103     public static final int GET_AD_SELECTION_LOGIC_LATENCY_MS = 1;
104     public static final int GET_TRUSTED_SCORING_SIGNALS_LATENCY_MS = 1;
105     public static final int SCORE_ADS_LATENCY_MS = 1;
106     public static final int PERSIST_AD_SELECTION_LATENCY_MS = 1;
107     public static final long DB_AD_SELECTION_FILE_SIZE = 10L;
108     public static final boolean IS_RMKT_ADS_WON_UNSET = false;
109     public static final int DB_AD_SELECTION_SIZE_IN_BYTES_UNSET = -1;
110     public static final boolean IS_RMKT_ADS_WON = true;
111     public static final int NUM_BUYERS_REQUESTED = 5;
112     public static final int NUM_BUYERS_FETCHED = 3;
113     private static final long BINDER_ELAPSED_TIMESTAMP = 90L;
114     public static final CallerMetadata sCallerMetadata =
115             new CallerMetadata.Builder()
116                     .setBinderElapsedTimestamp(BINDER_ELAPSED_TIMESTAMP)
117                     .build();
118     private static final int BINDER_LATENCY_MS = 2;
119     public static final long START_ELAPSED_TIMESTAMP =
120             BINDER_ELAPSED_TIMESTAMP + (long) BINDER_LATENCY_MS / 2;
121 
122     public static final long BIDDING_STAGE_START_TIMESTAMP = START_ELAPSED_TIMESTAMP + 1L;
123     public static final long GET_BUYERS_CUSTOM_AUDIENCE_END_TIMESTAMP =
124             BIDDING_STAGE_START_TIMESTAMP + GET_BUYERS_CUSTOM_AUDIENCE_LATENCY_MS;
125     public static final long RUN_AD_BIDDING_START_TIMESTAMP =
126             GET_BUYERS_CUSTOM_AUDIENCE_END_TIMESTAMP + 1L;
127     public static final long RUN_AD_BIDDING_END_TIMESTAMP =
128             RUN_AD_BIDDING_START_TIMESTAMP + RUN_AD_BIDDING_LATENCY_MS;
129     public static final long BIDDING_STAGE_END_TIMESTAMP = RUN_AD_BIDDING_END_TIMESTAMP;
130     public static final long TOTAL_BIDDING_STAGE_LATENCY_IN_MS =
131             BIDDING_STAGE_END_TIMESTAMP - BIDDING_STAGE_START_TIMESTAMP;
132     public static final long RUN_AD_SCORING_START_TIMESTAMP = RUN_AD_BIDDING_END_TIMESTAMP;
133     public static final long GET_AD_SELECTION_LOGIC_START_TIMESTAMP =
134             RUN_AD_SCORING_START_TIMESTAMP + 1L;
135     public static final long GET_AD_SELECTION_LOGIC_END_TIMESTAMP =
136             GET_AD_SELECTION_LOGIC_START_TIMESTAMP + GET_AD_SELECTION_LOGIC_LATENCY_MS;
137     public static final long GET_AD_SCORES_START_TIMESTAMP = GET_AD_SELECTION_LOGIC_END_TIMESTAMP;
138     public static final long GET_TRUSTED_SCORING_SIGNALS_START_TIMESTAMP =
139             GET_AD_SCORES_START_TIMESTAMP + 1;
140     public static final long GET_TRUSTED_SCORING_SIGNALS_END_TIMESTAMP =
141             GET_TRUSTED_SCORING_SIGNALS_START_TIMESTAMP + GET_TRUSTED_SCORING_SIGNALS_LATENCY_MS;
142     public static final long SCORE_ADS_START_TIMESTAMP =
143             GET_TRUSTED_SCORING_SIGNALS_END_TIMESTAMP + 1L;
144     public static final long SCORE_ADS_END_TIMESTAMP =
145             SCORE_ADS_START_TIMESTAMP + SCORE_ADS_LATENCY_MS;
146     public static final long GET_AD_SCORES_END_TIMESTAMP = SCORE_ADS_END_TIMESTAMP;
147     public static final int GET_AD_SCORES_LATENCY_MS =
148             (int) (GET_AD_SCORES_END_TIMESTAMP - GET_AD_SCORES_START_TIMESTAMP);
149     public static final long RUN_AD_SCORING_END_TIMESTAMP = GET_AD_SCORES_END_TIMESTAMP + 1L;
150     public static final int RUN_AD_SCORING_LATENCY_MS =
151             (int) (RUN_AD_SCORING_END_TIMESTAMP - RUN_AD_SCORING_START_TIMESTAMP);
152     public static final long PERSIST_AD_SELECTION_START_TIMESTAMP =
153             RUN_AD_SCORING_END_TIMESTAMP + 1;
154     public static final long PERSIST_AD_SELECTION_END_TIMESTAMP =
155             PERSIST_AD_SELECTION_START_TIMESTAMP + PERSIST_AD_SELECTION_LATENCY_MS;
156     public static final long STOP_ELAPSED_TIMESTAMP = PERSIST_AD_SELECTION_END_TIMESTAMP + 1;
157     public static final int RUN_AD_SELECTION_INTERNAL_FINAL_LATENCY_MS =
158             (int) (STOP_ELAPSED_TIMESTAMP - START_ELAPSED_TIMESTAMP);
159     public static final int RUN_AD_SELECTION_OVERALL_LATENCY_MS =
160             BINDER_LATENCY_MS + RUN_AD_SELECTION_INTERNAL_FINAL_LATENCY_MS;
161 
162     public static final boolean SCORE_AD_SELLER_ADDITIONAL_SIGNALS_CONTAINED_DATA_VERSION = true;
163     public static final int SCORE_AD_JS_SCRIPT_RESULT_CODE = 0;
164     private static final Uri DECISION_LOGIC_URI =
165             Uri.parse("https://developer.android.com/test/decisions_logic_uris");
166     private static final List<AdTechIdentifier> BUYERS =
167             Arrays.asList(
168                     AdSelectionConfigFixture.BUYER_1,
169                     AdSelectionConfigFixture.BUYER_2,
170                     AdSelectionConfigFixture.BUYER_3);
171     private static final List<DBCustomAudience> CUSTOM_AUDIENCES =
172             DBCustomAudienceFixture.getListOfBuyersCustomAudiences(BUYERS);
173     private static final int NUM_OF_ADS_ENTERING_BIDDING =
174             CUSTOM_AUDIENCES.stream()
175                     .map(DBCustomAudience::getAds)
176                     .filter(Objects::nonNull)
177                     .map(List::size)
178                     .reduce(0, Integer::sum);
179     private static final int NUM_OF_CAS_ENTERING_BIDDING = CUSTOM_AUDIENCES.size();
180     private static final double BID1 = 1.0;
181     private static final double BID2 = 2.0;
182     private static final double BID3 = 3.0;
183     private static final List<Pair<AdTechIdentifier, Double>> buyersAndBids =
184             Arrays.asList(
185                     Pair.create(AdSelectionConfigFixture.BUYER_1, BID1),
186                     Pair.create(AdSelectionConfigFixture.BUYER_2, BID2),
187                     Pair.create(AdSelectionConfigFixture.BUYER_3, BID3));
188     public static final List<AdBiddingOutcome> AD_BIDDING_OUTCOMES =
189             AdBiddingOutcomeFixture.getListOfAdBiddingOutcomes(buyersAndBids);
190     private static final int NUM_OF_CAS_POST_BIDDING =
191             AD_BIDDING_OUTCOMES.stream()
192                     .map(
193                             a ->
194                                     a.getCustomAudienceBiddingInfo()
195                                             .getCustomAudienceSignals()
196                                             .hashCode())
197                     .collect(Collectors.toSet())
198                     .size();
199     private static final float RATIO_OF_CAS_SELECTING_RMKT_ADS =
200             ((float) NUM_OF_CAS_POST_BIDDING) / NUM_OF_CAS_ENTERING_BIDDING;
201 
202     public static final int NUM_OF_CAS_ENTERING_SCORING =
203             AD_BIDDING_OUTCOMES.stream()
204                     .filter(Objects::nonNull)
205                     .map(AdBiddingOutcome::getCustomAudienceBiddingInfo)
206                     .filter(Objects::nonNull)
207                     .map(a -> a.getCustomAudienceSignals().hashCode())
208                     .collect(Collectors.toSet())
209                     .size();
210     public static final int NUM_OF_ADS_ENTERING_SCORING =
211             AD_BIDDING_OUTCOMES.stream()
212                     .filter(Objects::nonNull)
213                     .map(AdBiddingOutcome::getAdWithBid)
214                     .filter(Objects::nonNull)
215                     .collect(Collectors.toSet())
216                     .size();
217     private static final String SCRIPT_STRING = "The fetched script string.";
218     public static final int FETCHED_AD_SELECTION_LOGIC_SCRIPT_SIZE_IN_BYTES =
219             SCRIPT_STRING.getBytes(StandardCharsets.UTF_8).length;
220     public static final int FETCHED_TRUSTED_SCORING_SIGNALS_DATA_SIZE_IN_BYTES =
221             SCRIPT_STRING.getBytes(StandardCharsets.UTF_8).length;
222 
223     @Captor
224     ArgumentCaptor<RunAdSelectionProcessReportedStats>
225             mRunAdSelectionProcessReportedStatsArgumentCaptor;
226     @Captor
227     ArgumentCaptor<RunAdBiddingProcessReportedStats>
228             mRunAdBiddingProcessReportedStatsArgumentCaptor;
229     @Captor
230     ArgumentCaptor<RunAdScoringProcessReportedStats>
231             mRunAdScoringProcessReportedStatsArgumentCaptor;
232 
233     private String mAdSelectionLogic = SCRIPT_STRING;
234     private AdSelectionSignals mAdSelectionSignals = AdSelectionSignals.fromString(SCRIPT_STRING);
235     @Mock private Context mContextMock;
236     @Mock private File mMockDBAdSelectionFile;
237     @Mock private Clock mMockClock;
238     @Mock private DBAdSelection mMockDBAdSelection;
239     @Mock private AdServicesLogger mAdServicesLoggerMock;
240 
241     @Rule(order = 0)
242     public final SdkLevelSupportRule sdkLevel = SdkLevelSupportRule.forAtLeastS();
243 
244     private final Flags mFlags = new AdSelectionExecutionLoggerTestFlags();
245 
246     @Before
setUp()247     public void setUp() {
248         MockitoAnnotations.initMocks(this);
249         when(mMockDBAdSelection.getBiddingLogicUri()).thenReturn(DECISION_LOGIC_URI);
250     }
251 
252     @Test
testAdSelectionExecutionLogger_SuccessAdSelection()253     public void testAdSelectionExecutionLogger_SuccessAdSelection() {
254         when(mMockClock.elapsedRealtime())
255                 .thenReturn(
256                         START_ELAPSED_TIMESTAMP,
257                         BIDDING_STAGE_START_TIMESTAMP,
258                         GET_BUYERS_CUSTOM_AUDIENCE_END_TIMESTAMP,
259                         RUN_AD_BIDDING_START_TIMESTAMP,
260                         RUN_AD_BIDDING_END_TIMESTAMP,
261                         RUN_AD_SCORING_START_TIMESTAMP,
262                         GET_AD_SELECTION_LOGIC_START_TIMESTAMP,
263                         GET_AD_SELECTION_LOGIC_END_TIMESTAMP,
264                         GET_AD_SCORES_START_TIMESTAMP,
265                         GET_TRUSTED_SCORING_SIGNALS_START_TIMESTAMP,
266                         GET_TRUSTED_SCORING_SIGNALS_END_TIMESTAMP,
267                         SCORE_ADS_START_TIMESTAMP,
268                         SCORE_ADS_END_TIMESTAMP,
269                         GET_AD_SCORES_END_TIMESTAMP,
270                         RUN_AD_SCORING_END_TIMESTAMP,
271                         PERSIST_AD_SELECTION_START_TIMESTAMP,
272                         PERSIST_AD_SELECTION_END_TIMESTAMP,
273                         STOP_ELAPSED_TIMESTAMP);
274 
275         when(mContextMock.getDatabasePath(DATABASE_NAME)).thenReturn(mMockDBAdSelectionFile);
276         when(mMockDBAdSelectionFile.length()).thenReturn(DB_AD_SELECTION_FILE_SIZE);
277 
278         // Start the Ad selection execution logger and set start state of the process.
279         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
280         // Set start and end state of the subcomponent get-BUYERS-custom-audience process.
281         adSelectionExecutionLogger.startBiddingProcess(NUM_BUYERS_REQUESTED);
282         adSelectionExecutionLogger.endGetBuyersCustomAudience(NUM_BUYERS_FETCHED);
283         // Set the start and end state of the subcomponent run-ad-bidding process.
284         adSelectionExecutionLogger.startRunAdBidding(CUSTOM_AUDIENCES);
285         adSelectionExecutionLogger.endBiddingProcess(AD_BIDDING_OUTCOMES, STATUS_SUCCESS);
286         // Set the start and end states of the scoring stage.
287         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
288         adSelectionExecutionLogger.startGetAdSelectionLogic();
289         adSelectionExecutionLogger.endGetAdSelectionLogic(mAdSelectionLogic);
290         adSelectionExecutionLogger.startGetAdScores();
291         adSelectionExecutionLogger.startGetTrustedScoringSignals();
292         adSelectionExecutionLogger.endGetTrustedScoringSignals(mAdSelectionSignals);
293         adSelectionExecutionLogger.setScoreAdSellerAdditionalSignalsContainedDataVersion(
294                 SCORE_AD_SELLER_ADDITIONAL_SIGNALS_CONTAINED_DATA_VERSION);
295         adSelectionExecutionLogger.startScoreAds();
296         adSelectionExecutionLogger.setScoreAdJsScriptResultCode(JS_RUN_STATUS_SUCCESS);
297         adSelectionExecutionLogger.endScoreAds();
298         adSelectionExecutionLogger.endGetAdScores();
299         adSelectionExecutionLogger.endRunAdScoring(STATUS_SUCCESS);
300         // Set start state of the subcomponent persist-ad-selection process.
301         adSelectionExecutionLogger.startPersistAdSelection(mMockDBAdSelection);
302         // Set end state of the subcomponent persist-ad-selection process.
303         adSelectionExecutionLogger.endPersistAdSelection();
304 
305         // Close the Ad selection execution logger and log the data into the AdServicesLogger.
306         int resultCode = STATUS_SUCCESS;
307         adSelectionExecutionLogger.close(resultCode);
308 
309         // Verify the logging of the RunAdBiddingProcessReportedStats.
310         verify(mAdServicesLoggerMock)
311                 .logRunAdBiddingProcessReportedStats(
312                         mRunAdBiddingProcessReportedStatsArgumentCaptor.capture());
313         RunAdBiddingProcessReportedStats runAdBiddingProcessReportedStats =
314                 mRunAdBiddingProcessReportedStatsArgumentCaptor.getValue();
315         assertThat(runAdBiddingProcessReportedStats.getGetBuyersCustomAudienceLatencyInMills())
316                 .isEqualTo(GET_BUYERS_CUSTOM_AUDIENCE_LATENCY_MS);
317         assertThat(runAdBiddingProcessReportedStats.getGetBuyersCustomAudienceResultCode())
318                 .isEqualTo(resultCode);
319         assertThat(runAdBiddingProcessReportedStats.getNumBuyersRequested())
320                 .isEqualTo(NUM_BUYERS_REQUESTED);
321         assertThat(runAdBiddingProcessReportedStats.getNumBuyersFetched())
322                 .isEqualTo(NUM_BUYERS_FETCHED);
323         assertThat(runAdBiddingProcessReportedStats.getNumOfAdsEnteringBidding())
324                 .isEqualTo(NUM_OF_ADS_ENTERING_BIDDING);
325         assertThat(runAdBiddingProcessReportedStats.getNumOfCasEnteringBidding())
326                 .isEqualTo(NUM_OF_CAS_ENTERING_BIDDING);
327         assertThat(runAdBiddingProcessReportedStats.getNumOfCasPostBidding())
328                 .isEqualTo(NUM_OF_CAS_POST_BIDDING);
329         assertThat(runAdBiddingProcessReportedStats.getRatioOfCasSelectingRmktAds())
330                 .isEqualTo(RATIO_OF_CAS_SELECTING_RMKT_ADS);
331         assertThat(runAdBiddingProcessReportedStats.getRunAdBiddingLatencyInMillis())
332                 .isEqualTo(RUN_AD_BIDDING_LATENCY_MS);
333         assertThat(runAdBiddingProcessReportedStats.getRunAdBiddingResultCode())
334                 .isEqualTo(resultCode);
335         assertThat(runAdBiddingProcessReportedStats.getTotalAdBiddingStageLatencyInMillis())
336                 .isEqualTo(TOTAL_BIDDING_STAGE_LATENCY_IN_MS);
337 
338         // Verify the logging of the RunAdScoringProcessReportedStats.
339         verify(mAdServicesLoggerMock)
340                 .logRunAdScoringProcessReportedStats(
341                         mRunAdScoringProcessReportedStatsArgumentCaptor.capture());
342         RunAdScoringProcessReportedStats runAdScoringProcessReportedStats =
343                 mRunAdScoringProcessReportedStatsArgumentCaptor.getValue();
344         assertThat(runAdScoringProcessReportedStats.getGetAdSelectionLogicLatencyInMillis())
345                 .isEqualTo(GET_AD_SELECTION_LOGIC_LATENCY_MS);
346         assertThat(runAdScoringProcessReportedStats.getGetAdSelectionLogicResultCode())
347                 .isEqualTo(STATUS_SUCCESS);
348         assertThat(runAdScoringProcessReportedStats.getGetAdSelectionLogicScriptType())
349                 .isEqualTo(SCRIPT_JAVASCRIPT);
350         assertThat(runAdScoringProcessReportedStats.getFetchedAdSelectionLogicScriptSizeInBytes())
351                 .isEqualTo(FETCHED_AD_SELECTION_LOGIC_SCRIPT_SIZE_IN_BYTES);
352         assertThat(runAdScoringProcessReportedStats.getGetTrustedScoringSignalsLatencyInMillis())
353                 .isEqualTo(GET_TRUSTED_SCORING_SIGNALS_LATENCY_MS);
354         assertThat(runAdScoringProcessReportedStats.getGetTrustedScoringSignalsResultCode())
355                 .isEqualTo(STATUS_SUCCESS);
356         assertThat(
357                         runAdScoringProcessReportedStats
358                                 .getFetchedTrustedScoringSignalsDataSizeInBytes())
359                 .isEqualTo(FETCHED_TRUSTED_SCORING_SIGNALS_DATA_SIZE_IN_BYTES);
360 
361         assertThat(runAdScoringProcessReportedStats.getScoreAdsLatencyInMillis())
362                 .isEqualTo(SCORE_ADS_LATENCY_MS);
363         assertThat(runAdScoringProcessReportedStats.getGetAdScoresLatencyInMillis())
364                 .isEqualTo(GET_AD_SCORES_LATENCY_MS);
365         assertThat(runAdScoringProcessReportedStats.getGetAdScoresResultCode())
366                 .isEqualTo(STATUS_SUCCESS);
367         assertThat(runAdScoringProcessReportedStats.getNumOfCasEnteringScoring())
368                 .isEqualTo(NUM_OF_CAS_ENTERING_SCORING);
369         assertThat(runAdScoringProcessReportedStats.getNumOfRemarketingAdsEnteringScoring())
370                 .isEqualTo(NUM_OF_ADS_ENTERING_SCORING);
371         assertThat(runAdScoringProcessReportedStats.getNumOfContextualAdsEnteringScoring())
372                 .isEqualTo(FIELD_UNSET);
373         assertThat(runAdScoringProcessReportedStats.getRunAdScoringLatencyInMillis())
374                 .isEqualTo(RUN_AD_SCORING_LATENCY_MS);
375         assertThat(runAdScoringProcessReportedStats.getRunAdScoringResultCode())
376                 .isEqualTo(resultCode);
377         assertThat(
378                         runAdScoringProcessReportedStats
379                                 .getScoreAdSellerAdditionalSignalsContainedDataVersion())
380                 .isEqualTo(SCORE_AD_SELLER_ADDITIONAL_SIGNALS_CONTAINED_DATA_VERSION);
381         assertThat(runAdScoringProcessReportedStats.getScoreAdJsScriptResultCode())
382                 .isEqualTo(JS_RUN_STATUS_SUCCESS);
383 
384         // Verify the logging of the RunAdSelectionProcessReportedStats.
385         verify(mAdServicesLoggerMock)
386                 .logRunAdSelectionProcessReportedStats(
387                         mRunAdSelectionProcessReportedStatsArgumentCaptor.capture());
388         RunAdSelectionProcessReportedStats runAdSelectionProcessReportedStats =
389                 mRunAdSelectionProcessReportedStatsArgumentCaptor.getValue();
390         assertThat(runAdSelectionProcessReportedStats.getIsRemarketingAdsWon())
391                 .isEqualTo(IS_RMKT_ADS_WON);
392         assertThat(runAdSelectionProcessReportedStats.getDBAdSelectionSizeInBytes())
393                 .isEqualTo((int) (DB_AD_SELECTION_FILE_SIZE));
394         assertThat(runAdSelectionProcessReportedStats.getPersistAdSelectionLatencyInMillis())
395                 .isEqualTo(PERSIST_AD_SELECTION_LATENCY_MS);
396         assertThat(runAdSelectionProcessReportedStats.getPersistAdSelectionResultCode())
397                 .isEqualTo(resultCode);
398         assertThat(runAdSelectionProcessReportedStats.getRunAdSelectionLatencyInMillis())
399                 .isEqualTo(RUN_AD_SELECTION_INTERNAL_FINAL_LATENCY_MS);
400         assertThat(runAdSelectionProcessReportedStats.getRunAdSelectionResultCode())
401                 .isEqualTo(resultCode);
402     }
403 
404     @Test
testAdSelectionExecutionLogger_FailedAdSelectionDuringScoreAds()405     public void testAdSelectionExecutionLogger_FailedAdSelectionDuringScoreAds() {
406         when(mMockClock.elapsedRealtime())
407                 .thenReturn(
408                         START_ELAPSED_TIMESTAMP,
409                         BIDDING_STAGE_START_TIMESTAMP,
410                         GET_BUYERS_CUSTOM_AUDIENCE_END_TIMESTAMP,
411                         RUN_AD_BIDDING_START_TIMESTAMP,
412                         RUN_AD_BIDDING_END_TIMESTAMP,
413                         RUN_AD_SCORING_START_TIMESTAMP,
414                         GET_AD_SELECTION_LOGIC_START_TIMESTAMP,
415                         GET_AD_SELECTION_LOGIC_END_TIMESTAMP,
416                         GET_AD_SCORES_START_TIMESTAMP,
417                         GET_TRUSTED_SCORING_SIGNALS_START_TIMESTAMP,
418                         GET_TRUSTED_SCORING_SIGNALS_END_TIMESTAMP,
419                         SCORE_ADS_START_TIMESTAMP,
420                         RUN_AD_SCORING_END_TIMESTAMP,
421                         STOP_ELAPSED_TIMESTAMP);
422         // Start the Ad selection execution logger and set start state of the process.
423         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
424         // Set start and end state of the subcomponent get-BUYERS-custom-audience process.
425         adSelectionExecutionLogger.startBiddingProcess(NUM_BUYERS_REQUESTED);
426         adSelectionExecutionLogger.endGetBuyersCustomAudience(NUM_BUYERS_FETCHED);
427         // Set the start and end state of the subcomponent run-ad-bidding process.
428         adSelectionExecutionLogger.startRunAdBidding(CUSTOM_AUDIENCES);
429         adSelectionExecutionLogger.endBiddingProcess(AD_BIDDING_OUTCOMES, STATUS_SUCCESS);
430         // Set the start and end states of the scoring stage.
431         int resultCode = STATUS_INTERNAL_ERROR;
432         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
433         adSelectionExecutionLogger.startGetAdSelectionLogic();
434         adSelectionExecutionLogger.endGetAdSelectionLogic(mAdSelectionLogic);
435         adSelectionExecutionLogger.startGetAdScores();
436         adSelectionExecutionLogger.startGetTrustedScoringSignals();
437         adSelectionExecutionLogger.endGetTrustedScoringSignals(mAdSelectionSignals);
438         adSelectionExecutionLogger.setScoreAdSellerAdditionalSignalsContainedDataVersion(
439                 SCORE_AD_SELLER_ADDITIONAL_SIGNALS_CONTAINED_DATA_VERSION);
440         adSelectionExecutionLogger.startScoreAds();
441         adSelectionExecutionLogger.setScoreAdJsScriptResultCode(JS_RUN_STATUS_JS_REFERENCE_ERROR);
442         adSelectionExecutionLogger.endRunAdScoring(resultCode);
443         adSelectionExecutionLogger.close(resultCode);
444 
445         // Verify the logging of the RunAdBiddingProcessReportedStats.
446         verify(mAdServicesLoggerMock)
447                 .logRunAdBiddingProcessReportedStats(
448                         mRunAdBiddingProcessReportedStatsArgumentCaptor.capture());
449         RunAdBiddingProcessReportedStats runAdBiddingProcessReportedStats =
450                 mRunAdBiddingProcessReportedStatsArgumentCaptor.getValue();
451         assertThat(runAdBiddingProcessReportedStats.getGetBuyersCustomAudienceLatencyInMills())
452                 .isEqualTo(GET_BUYERS_CUSTOM_AUDIENCE_LATENCY_MS);
453         assertThat(runAdBiddingProcessReportedStats.getGetBuyersCustomAudienceResultCode())
454                 .isEqualTo(STATUS_SUCCESS);
455         assertThat(runAdBiddingProcessReportedStats.getNumBuyersRequested())
456                 .isEqualTo(NUM_BUYERS_REQUESTED);
457         assertThat(runAdBiddingProcessReportedStats.getNumBuyersFetched())
458                 .isEqualTo(NUM_BUYERS_FETCHED);
459         assertThat(runAdBiddingProcessReportedStats.getNumOfAdsEnteringBidding())
460                 .isEqualTo(NUM_OF_ADS_ENTERING_BIDDING);
461         assertThat(runAdBiddingProcessReportedStats.getNumOfCasEnteringBidding())
462                 .isEqualTo(NUM_OF_CAS_ENTERING_BIDDING);
463         assertThat(runAdBiddingProcessReportedStats.getNumOfCasPostBidding())
464                 .isEqualTo(NUM_OF_CAS_POST_BIDDING);
465         assertThat(runAdBiddingProcessReportedStats.getRatioOfCasSelectingRmktAds())
466                 .isEqualTo(RATIO_OF_CAS_SELECTING_RMKT_ADS);
467         assertThat(runAdBiddingProcessReportedStats.getRunAdBiddingLatencyInMillis())
468                 .isEqualTo(RUN_AD_BIDDING_LATENCY_MS);
469         assertThat(runAdBiddingProcessReportedStats.getRunAdBiddingResultCode())
470                 .isEqualTo(STATUS_SUCCESS);
471         assertThat(runAdBiddingProcessReportedStats.getTotalAdBiddingStageLatencyInMillis())
472                 .isEqualTo(TOTAL_BIDDING_STAGE_LATENCY_IN_MS);
473 
474         // Verify the logging of the RunAdScoringProcessReportedStats.
475         verify(mAdServicesLoggerMock)
476                 .logRunAdScoringProcessReportedStats(
477                         mRunAdScoringProcessReportedStatsArgumentCaptor.capture());
478         RunAdScoringProcessReportedStats runAdScoringProcessReportedStats =
479                 mRunAdScoringProcessReportedStatsArgumentCaptor.getValue();
480         assertThat(runAdScoringProcessReportedStats.getGetAdSelectionLogicLatencyInMillis())
481                 .isEqualTo(GET_AD_SELECTION_LOGIC_LATENCY_MS);
482         assertThat(runAdScoringProcessReportedStats.getGetAdSelectionLogicResultCode())
483                 .isEqualTo(STATUS_SUCCESS);
484         assertThat(runAdScoringProcessReportedStats.getGetAdSelectionLogicScriptType())
485                 .isEqualTo(SCRIPT_JAVASCRIPT);
486         assertThat(runAdScoringProcessReportedStats.getFetchedAdSelectionLogicScriptSizeInBytes())
487                 .isEqualTo(FETCHED_AD_SELECTION_LOGIC_SCRIPT_SIZE_IN_BYTES);
488 
489         assertThat(runAdScoringProcessReportedStats.getGetTrustedScoringSignalsLatencyInMillis())
490                 .isEqualTo(GET_TRUSTED_SCORING_SIGNALS_LATENCY_MS);
491         assertThat(runAdScoringProcessReportedStats.getGetTrustedScoringSignalsResultCode())
492                 .isEqualTo(STATUS_SUCCESS);
493         assertThat(
494                         runAdScoringProcessReportedStats
495                                 .getFetchedTrustedScoringSignalsDataSizeInBytes())
496                 .isEqualTo(FETCHED_TRUSTED_SCORING_SIGNALS_DATA_SIZE_IN_BYTES);
497         assertThat(runAdScoringProcessReportedStats.getScoreAdsLatencyInMillis())
498                 .isEqualTo((int) (RUN_AD_SCORING_END_TIMESTAMP - SCORE_ADS_START_TIMESTAMP));
499         assertThat(runAdScoringProcessReportedStats.getGetAdScoresLatencyInMillis())
500                 .isEqualTo((int) (RUN_AD_SCORING_END_TIMESTAMP - GET_AD_SCORES_START_TIMESTAMP));
501         assertThat(runAdScoringProcessReportedStats.getGetAdScoresResultCode())
502                 .isEqualTo(resultCode);
503         assertThat(runAdScoringProcessReportedStats.getNumOfCasEnteringScoring())
504                 .isEqualTo(NUM_OF_CAS_ENTERING_SCORING);
505         assertThat(runAdScoringProcessReportedStats.getNumOfRemarketingAdsEnteringScoring())
506                 .isEqualTo(NUM_OF_ADS_ENTERING_SCORING);
507         assertThat(runAdScoringProcessReportedStats.getNumOfContextualAdsEnteringScoring())
508                 .isEqualTo(FIELD_UNSET);
509         assertThat(runAdScoringProcessReportedStats.getRunAdScoringLatencyInMillis())
510                 .isEqualTo(RUN_AD_SCORING_LATENCY_MS);
511         assertThat(runAdScoringProcessReportedStats.getRunAdScoringResultCode())
512                 .isEqualTo(resultCode);
513         assertThat(
514                         runAdScoringProcessReportedStats
515                                 .getScoreAdSellerAdditionalSignalsContainedDataVersion())
516                 .isEqualTo(SCORE_AD_SELLER_ADDITIONAL_SIGNALS_CONTAINED_DATA_VERSION);
517         assertThat(runAdScoringProcessReportedStats.getScoreAdJsScriptResultCode())
518                 .isEqualTo(JS_RUN_STATUS_JS_REFERENCE_ERROR);
519     }
520 
521     @Test
testAdSelectionExecutionLogger_FailedAdSelectionDuringFetchTrustedScoringSignals()522     public void testAdSelectionExecutionLogger_FailedAdSelectionDuringFetchTrustedScoringSignals() {
523         when(mMockClock.elapsedRealtime())
524                 .thenReturn(
525                         START_ELAPSED_TIMESTAMP,
526                         BIDDING_STAGE_START_TIMESTAMP,
527                         GET_BUYERS_CUSTOM_AUDIENCE_END_TIMESTAMP,
528                         RUN_AD_BIDDING_START_TIMESTAMP,
529                         RUN_AD_BIDDING_END_TIMESTAMP,
530                         RUN_AD_SCORING_START_TIMESTAMP,
531                         GET_AD_SELECTION_LOGIC_START_TIMESTAMP,
532                         GET_AD_SELECTION_LOGIC_END_TIMESTAMP,
533                         GET_AD_SCORES_START_TIMESTAMP,
534                         GET_TRUSTED_SCORING_SIGNALS_START_TIMESTAMP,
535                         RUN_AD_SCORING_END_TIMESTAMP,
536                         STOP_ELAPSED_TIMESTAMP);
537         // Start the Ad selection execution logger and set start state of the process.
538         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
539         // Set start and end state of the subcomponent get-BUYERS-custom-audience process.
540         adSelectionExecutionLogger.startBiddingProcess(NUM_BUYERS_REQUESTED);
541         adSelectionExecutionLogger.endGetBuyersCustomAudience(NUM_BUYERS_FETCHED);
542         // Set the start and end state of the subcomponent run-ad-bidding process.
543         adSelectionExecutionLogger.startRunAdBidding(CUSTOM_AUDIENCES);
544         adSelectionExecutionLogger.endBiddingProcess(AD_BIDDING_OUTCOMES, STATUS_SUCCESS);
545         // Set the start and end states of the scoring stage.
546         int resultCode = STATUS_INTERNAL_ERROR;
547         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
548         adSelectionExecutionLogger.startGetAdSelectionLogic();
549         adSelectionExecutionLogger.endGetAdSelectionLogic(mAdSelectionLogic);
550         adSelectionExecutionLogger.startGetAdScores();
551         adSelectionExecutionLogger.startGetTrustedScoringSignals();
552         adSelectionExecutionLogger.endRunAdScoring(resultCode);
553 
554         adSelectionExecutionLogger.close(resultCode);
555 
556         // Verify the logging of the RunAdBiddingProcessReportedStats.
557         verify(mAdServicesLoggerMock)
558                 .logRunAdBiddingProcessReportedStats(
559                         mRunAdBiddingProcessReportedStatsArgumentCaptor.capture());
560         RunAdBiddingProcessReportedStats runAdBiddingProcessReportedStats =
561                 mRunAdBiddingProcessReportedStatsArgumentCaptor.getValue();
562         assertThat(runAdBiddingProcessReportedStats.getGetBuyersCustomAudienceLatencyInMills())
563                 .isEqualTo(GET_BUYERS_CUSTOM_AUDIENCE_LATENCY_MS);
564         assertThat(runAdBiddingProcessReportedStats.getGetBuyersCustomAudienceResultCode())
565                 .isEqualTo(STATUS_SUCCESS);
566         assertThat(runAdBiddingProcessReportedStats.getNumBuyersRequested())
567                 .isEqualTo(NUM_BUYERS_REQUESTED);
568         assertThat(runAdBiddingProcessReportedStats.getNumBuyersFetched())
569                 .isEqualTo(NUM_BUYERS_FETCHED);
570         assertThat(runAdBiddingProcessReportedStats.getNumOfAdsEnteringBidding())
571                 .isEqualTo(NUM_OF_ADS_ENTERING_BIDDING);
572         assertThat(runAdBiddingProcessReportedStats.getNumOfCasEnteringBidding())
573                 .isEqualTo(NUM_OF_CAS_ENTERING_BIDDING);
574         assertThat(runAdBiddingProcessReportedStats.getNumOfCasPostBidding())
575                 .isEqualTo(NUM_OF_CAS_POST_BIDDING);
576         assertThat(runAdBiddingProcessReportedStats.getRatioOfCasSelectingRmktAds())
577                 .isEqualTo(RATIO_OF_CAS_SELECTING_RMKT_ADS);
578         assertThat(runAdBiddingProcessReportedStats.getRunAdBiddingLatencyInMillis())
579                 .isEqualTo(RUN_AD_BIDDING_LATENCY_MS);
580         assertThat(runAdBiddingProcessReportedStats.getRunAdBiddingResultCode())
581                 .isEqualTo(STATUS_SUCCESS);
582         assertThat(runAdBiddingProcessReportedStats.getTotalAdBiddingStageLatencyInMillis())
583                 .isEqualTo(TOTAL_BIDDING_STAGE_LATENCY_IN_MS);
584 
585         // Verify the logging of the RunAdScoringProcessReportedStats.
586         verify(mAdServicesLoggerMock)
587                 .logRunAdScoringProcessReportedStats(
588                         mRunAdScoringProcessReportedStatsArgumentCaptor.capture());
589         RunAdScoringProcessReportedStats runAdScoringProcessReportedStats =
590                 mRunAdScoringProcessReportedStatsArgumentCaptor.getValue();
591         assertThat(runAdScoringProcessReportedStats.getGetAdSelectionLogicLatencyInMillis())
592                 .isEqualTo(GET_AD_SELECTION_LOGIC_LATENCY_MS);
593         assertThat(runAdScoringProcessReportedStats.getGetAdSelectionLogicResultCode())
594                 .isEqualTo(STATUS_SUCCESS);
595         assertThat(runAdScoringProcessReportedStats.getGetAdSelectionLogicScriptType())
596                 .isEqualTo(SCRIPT_JAVASCRIPT);
597         assertThat(runAdScoringProcessReportedStats.getFetchedAdSelectionLogicScriptSizeInBytes())
598                 .isEqualTo(FETCHED_AD_SELECTION_LOGIC_SCRIPT_SIZE_IN_BYTES);
599 
600         assertThat(runAdScoringProcessReportedStats.getGetTrustedScoringSignalsLatencyInMillis())
601                 .isEqualTo(
602                         (int)
603                                 (RUN_AD_SCORING_END_TIMESTAMP
604                                         - GET_TRUSTED_SCORING_SIGNALS_START_TIMESTAMP));
605         assertThat(runAdScoringProcessReportedStats.getGetTrustedScoringSignalsResultCode())
606                 .isEqualTo(resultCode);
607         assertThat(
608                         runAdScoringProcessReportedStats
609                                 .getFetchedTrustedScoringSignalsDataSizeInBytes())
610                 .isEqualTo(FIELD_UNSET);
611         assertThat(runAdScoringProcessReportedStats.getScoreAdsLatencyInMillis())
612                 .isEqualTo(FIELD_UNSET);
613         assertThat(runAdScoringProcessReportedStats.getGetAdScoresLatencyInMillis())
614                 .isEqualTo((int) (RUN_AD_SCORING_END_TIMESTAMP - GET_AD_SCORES_START_TIMESTAMP));
615         assertThat(runAdScoringProcessReportedStats.getGetAdScoresResultCode())
616                 .isEqualTo(resultCode);
617         assertThat(runAdScoringProcessReportedStats.getNumOfCasEnteringScoring())
618                 .isEqualTo(NUM_OF_CAS_ENTERING_SCORING);
619         assertThat(runAdScoringProcessReportedStats.getNumOfRemarketingAdsEnteringScoring())
620                 .isEqualTo(NUM_OF_ADS_ENTERING_SCORING);
621         assertThat(runAdScoringProcessReportedStats.getNumOfContextualAdsEnteringScoring())
622                 .isEqualTo(FIELD_UNSET);
623         assertThat(runAdScoringProcessReportedStats.getRunAdScoringLatencyInMillis())
624                 .isEqualTo(RUN_AD_SCORING_LATENCY_MS);
625         assertThat(runAdScoringProcessReportedStats.getRunAdScoringResultCode())
626                 .isEqualTo(resultCode);
627         assertThat(
628                         runAdScoringProcessReportedStats
629                                 .getScoreAdSellerAdditionalSignalsContainedDataVersion())
630                 .isEqualTo(false);
631         assertThat(runAdScoringProcessReportedStats.getScoreAdJsScriptResultCode())
632                 .isEqualTo(JS_RUN_STATUS_UNSET);
633     }
634 
635     @Test
testAdSelectionExecutionLogger_FailedAdSelectionDuringGetAdSelectionLogicScript()636     public void testAdSelectionExecutionLogger_FailedAdSelectionDuringGetAdSelectionLogicScript() {
637         when(mMockClock.elapsedRealtime())
638                 .thenReturn(
639                         START_ELAPSED_TIMESTAMP,
640                         BIDDING_STAGE_START_TIMESTAMP,
641                         GET_BUYERS_CUSTOM_AUDIENCE_END_TIMESTAMP,
642                         RUN_AD_BIDDING_START_TIMESTAMP,
643                         RUN_AD_BIDDING_END_TIMESTAMP,
644                         RUN_AD_SCORING_START_TIMESTAMP,
645                         GET_AD_SELECTION_LOGIC_START_TIMESTAMP,
646                         RUN_AD_SCORING_END_TIMESTAMP,
647                         STOP_ELAPSED_TIMESTAMP);
648         // Start the Ad selection execution logger and set start state of the process.
649         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
650         // Set start and end state of the subcomponent get-BUYERS-custom-audience process.
651         adSelectionExecutionLogger.startBiddingProcess(NUM_BUYERS_REQUESTED);
652         adSelectionExecutionLogger.endGetBuyersCustomAudience(NUM_BUYERS_FETCHED);
653         // Set the start and end state of the subcomponent run-ad-bidding process.
654         adSelectionExecutionLogger.startRunAdBidding(CUSTOM_AUDIENCES);
655         adSelectionExecutionLogger.endBiddingProcess(AD_BIDDING_OUTCOMES, STATUS_SUCCESS);
656         // Set the start and end states of the scoring stage.
657         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
658         adSelectionExecutionLogger.startGetAdSelectionLogic();
659         int resultCode = STATUS_INTERNAL_ERROR;
660         adSelectionExecutionLogger.endRunAdScoring(resultCode);
661         adSelectionExecutionLogger.close(resultCode);
662 
663         // Verify the logging of the RunAdBiddingProcessReportedStats.
664         verify(mAdServicesLoggerMock)
665                 .logRunAdBiddingProcessReportedStats(
666                         mRunAdBiddingProcessReportedStatsArgumentCaptor.capture());
667         RunAdBiddingProcessReportedStats runAdBiddingProcessReportedStats =
668                 mRunAdBiddingProcessReportedStatsArgumentCaptor.getValue();
669         assertThat(runAdBiddingProcessReportedStats.getGetBuyersCustomAudienceLatencyInMills())
670                 .isEqualTo(GET_BUYERS_CUSTOM_AUDIENCE_LATENCY_MS);
671         assertThat(runAdBiddingProcessReportedStats.getGetBuyersCustomAudienceResultCode())
672                 .isEqualTo(STATUS_SUCCESS);
673         assertThat(runAdBiddingProcessReportedStats.getNumBuyersRequested())
674                 .isEqualTo(NUM_BUYERS_REQUESTED);
675         assertThat(runAdBiddingProcessReportedStats.getNumBuyersFetched())
676                 .isEqualTo(NUM_BUYERS_FETCHED);
677         assertThat(runAdBiddingProcessReportedStats.getNumOfAdsEnteringBidding())
678                 .isEqualTo(NUM_OF_ADS_ENTERING_BIDDING);
679         assertThat(runAdBiddingProcessReportedStats.getNumOfCasEnteringBidding())
680                 .isEqualTo(NUM_OF_CAS_ENTERING_BIDDING);
681         assertThat(runAdBiddingProcessReportedStats.getNumOfCasPostBidding())
682                 .isEqualTo(NUM_OF_CAS_POST_BIDDING);
683         assertThat(runAdBiddingProcessReportedStats.getRatioOfCasSelectingRmktAds())
684                 .isEqualTo(RATIO_OF_CAS_SELECTING_RMKT_ADS);
685         assertThat(runAdBiddingProcessReportedStats.getRunAdBiddingLatencyInMillis())
686                 .isEqualTo(RUN_AD_BIDDING_LATENCY_MS);
687         assertThat(runAdBiddingProcessReportedStats.getRunAdBiddingResultCode())
688                 .isEqualTo(STATUS_SUCCESS);
689         assertThat(runAdBiddingProcessReportedStats.getTotalAdBiddingStageLatencyInMillis())
690                 .isEqualTo(TOTAL_BIDDING_STAGE_LATENCY_IN_MS);
691 
692         // Verify the logging of the RunAdScoringProcessReportedStats.
693         verify(mAdServicesLoggerMock)
694                 .logRunAdScoringProcessReportedStats(
695                         mRunAdScoringProcessReportedStatsArgumentCaptor.capture());
696         RunAdScoringProcessReportedStats runAdScoringProcessReportedStats =
697                 mRunAdScoringProcessReportedStatsArgumentCaptor.getValue();
698         assertThat(runAdScoringProcessReportedStats.getGetAdSelectionLogicLatencyInMillis())
699                 .isEqualTo(
700                         (int)
701                                 (RUN_AD_SCORING_END_TIMESTAMP
702                                         - GET_AD_SELECTION_LOGIC_START_TIMESTAMP));
703         assertThat(runAdScoringProcessReportedStats.getGetAdSelectionLogicResultCode())
704                 .isEqualTo(resultCode);
705         assertThat(runAdScoringProcessReportedStats.getGetAdSelectionLogicScriptType())
706                 .isEqualTo(SCRIPT_UNSET);
707         assertThat(runAdScoringProcessReportedStats.getFetchedAdSelectionLogicScriptSizeInBytes())
708                 .isEqualTo(FIELD_UNSET);
709         assertThat(runAdScoringProcessReportedStats.getGetTrustedScoringSignalsLatencyInMillis())
710                 .isEqualTo(FIELD_UNSET);
711         assertThat(runAdScoringProcessReportedStats.getGetTrustedScoringSignalsResultCode())
712                 .isEqualTo(FIELD_UNSET);
713         assertThat(
714                         runAdScoringProcessReportedStats
715                                 .getFetchedTrustedScoringSignalsDataSizeInBytes())
716                 .isEqualTo(FIELD_UNSET);
717         assertThat(runAdScoringProcessReportedStats.getScoreAdsLatencyInMillis())
718                 .isEqualTo(FIELD_UNSET);
719         assertThat(runAdScoringProcessReportedStats.getGetAdScoresLatencyInMillis())
720                 .isEqualTo(FIELD_UNSET);
721         assertThat(runAdScoringProcessReportedStats.getGetAdScoresResultCode())
722                 .isEqualTo(FIELD_UNSET);
723         assertThat(runAdScoringProcessReportedStats.getNumOfCasEnteringScoring())
724                 .isEqualTo(NUM_OF_CAS_ENTERING_SCORING);
725         assertThat(runAdScoringProcessReportedStats.getNumOfRemarketingAdsEnteringScoring())
726                 .isEqualTo(NUM_OF_ADS_ENTERING_SCORING);
727         assertThat(runAdScoringProcessReportedStats.getNumOfContextualAdsEnteringScoring())
728                 .isEqualTo(FIELD_UNSET);
729         assertThat(runAdScoringProcessReportedStats.getRunAdScoringLatencyInMillis())
730                 .isEqualTo(RUN_AD_SCORING_LATENCY_MS);
731         assertThat(runAdScoringProcessReportedStats.getRunAdScoringResultCode())
732                 .isEqualTo(resultCode);
733         assertThat(
734                         runAdScoringProcessReportedStats
735                                 .getScoreAdSellerAdditionalSignalsContainedDataVersion())
736                 .isEqualTo(false);
737         assertThat(runAdScoringProcessReportedStats.getScoreAdJsScriptResultCode())
738                 .isEqualTo(JS_RUN_STATUS_UNSET);
739     }
740 
741     @Test
testAdSelectionExecutionLogger_redundantStartOfGetBuyersCustomAudience()742     public void testAdSelectionExecutionLogger_redundantStartOfGetBuyersCustomAudience() {
743         when(mMockClock.elapsedRealtime())
744                 .thenReturn(START_ELAPSED_TIMESTAMP, BIDDING_STAGE_START_TIMESTAMP);
745         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
746         // Set start state of the subcomponent get-buyers-custom-audience process.
747         adSelectionExecutionLogger.startBiddingProcess(NUM_BUYERS_REQUESTED);
748 
749         Throwable throwable =
750                 assertThrows(
751                         IllegalStateException.class,
752                         () -> adSelectionExecutionLogger.startBiddingProcess(NUM_BUYERS_REQUESTED));
753         assertThat(throwable.getMessage()).contains(REPEATED_START_BIDDING_STAGE);
754     }
755 
756     @Test
testAdSelectionExecutionLogger_redundantEndOfGetBuyersCustomAudience()757     public void testAdSelectionExecutionLogger_redundantEndOfGetBuyersCustomAudience() {
758         when(mMockClock.elapsedRealtime())
759                 .thenReturn(
760                         START_ELAPSED_TIMESTAMP,
761                         BIDDING_STAGE_START_TIMESTAMP,
762                         GET_BUYERS_CUSTOM_AUDIENCE_END_TIMESTAMP);
763         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
764         adSelectionExecutionLogger.startBiddingProcess(NUM_BUYERS_REQUESTED);
765         adSelectionExecutionLogger.endGetBuyersCustomAudience(NUM_BUYERS_FETCHED);
766         Throwable throwable =
767                 assertThrows(
768                         IllegalStateException.class,
769                         () ->
770                                 adSelectionExecutionLogger.endGetBuyersCustomAudience(
771                                         NUM_BUYERS_FETCHED));
772         assertThat(throwable.getMessage()).contains(REPEATED_END_GET_BUYERS_CUSTOM_AUDIENCE);
773     }
774 
775     @Test
testAdSelectionExecutionLogger_missingStartBiddingStage()776     public void testAdSelectionExecutionLogger_missingStartBiddingStage() {
777         when(mMockClock.elapsedRealtime()).thenReturn(START_ELAPSED_TIMESTAMP);
778         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
779         Throwable throwable =
780                 assertThrows(
781                         IllegalStateException.class,
782                         () ->
783                                 adSelectionExecutionLogger.endGetBuyersCustomAudience(
784                                         NUM_BUYERS_FETCHED));
785         assertThat(throwable.getMessage()).contains(MISSING_START_BIDDING_STAGE);
786     }
787 
788     @Test
testAdSelectionExecutionLogger_missingGetBuyersCustomAudience()789     public void testAdSelectionExecutionLogger_missingGetBuyersCustomAudience() {
790         when(mMockClock.elapsedRealtime())
791                 .thenReturn(START_ELAPSED_TIMESTAMP, RUN_AD_BIDDING_START_TIMESTAMP);
792         // Start the Ad selection execution logger and set start state of the process.
793         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
794         Throwable throwable =
795                 assertThrows(
796                         IllegalStateException.class,
797                         () -> adSelectionExecutionLogger.startRunAdBidding(CUSTOM_AUDIENCES));
798         assertThat(throwable.getMessage()).contains(MISSING_GET_BUYERS_CUSTOM_AUDIENCE);
799     }
800 
801     @Test
testAdSelectionExecutionLogger_redundantStartRunAdBidding()802     public void testAdSelectionExecutionLogger_redundantStartRunAdBidding() {
803         when(mMockClock.elapsedRealtime())
804                 .thenReturn(
805                         START_ELAPSED_TIMESTAMP,
806                         BIDDING_STAGE_START_TIMESTAMP,
807                         GET_BUYERS_CUSTOM_AUDIENCE_END_TIMESTAMP,
808                         RUN_AD_BIDDING_START_TIMESTAMP);
809         // Start the Ad selection execution logger and set start state of the process.
810         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
811         adSelectionExecutionLogger.startBiddingProcess(NUM_BUYERS_REQUESTED);
812         adSelectionExecutionLogger.endGetBuyersCustomAudience(NUM_BUYERS_FETCHED);
813         adSelectionExecutionLogger.startRunAdBidding(CUSTOM_AUDIENCES);
814         Throwable throwable =
815                 assertThrows(
816                         IllegalStateException.class,
817                         () -> adSelectionExecutionLogger.startRunAdBidding(CUSTOM_AUDIENCES));
818         assertThat(throwable.getMessage()).contains(REPEATED_START_RUN_AD_BIDDING);
819     }
820 
821     @Test
testAdSelectionExecutionLogger_missingStartOfBiddingWithEndBiddingStage()822     public void testAdSelectionExecutionLogger_missingStartOfBiddingWithEndBiddingStage() {
823         when(mMockClock.elapsedRealtime()).thenReturn(START_ELAPSED_TIMESTAMP);
824         // Start the Ad selection execution logger and set start state of the process.
825         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
826         Throwable throwable =
827                 assertThrows(
828                         IllegalStateException.class,
829                         () ->
830                                 adSelectionExecutionLogger.endBiddingProcess(
831                                         AD_BIDDING_OUTCOMES, STATUS_SUCCESS));
832         assertThat(throwable.getMessage()).contains(MISSING_START_BIDDING_STAGE);
833     }
834 
835     @Test
testAdSelectionExecutionLogger_missingEndOfGetBuyersCAsWithEndRunAdBidding()836     public void testAdSelectionExecutionLogger_missingEndOfGetBuyersCAsWithEndRunAdBidding() {
837         when(mMockClock.elapsedRealtime())
838                 .thenReturn(START_ELAPSED_TIMESTAMP, BIDDING_STAGE_START_TIMESTAMP);
839         // Start the Ad selection execution logger and set start state of the process.
840         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
841         adSelectionExecutionLogger.startBiddingProcess(NUM_BUYERS_REQUESTED);
842         Throwable throwable =
843                 assertThrows(
844                         IllegalStateException.class,
845                         () ->
846                                 adSelectionExecutionLogger.endBiddingProcess(
847                                         AD_BIDDING_OUTCOMES, STATUS_SUCCESS));
848         assertThat(throwable.getMessage()).contains(MISSING_END_GET_BUYERS_CUSTOM_AUDIENCE);
849     }
850 
851     @Test
testAdSelectionExecutionLogger_missingStartOfRunAdBiddingWithEndRunAdBidding()852     public void testAdSelectionExecutionLogger_missingStartOfRunAdBiddingWithEndRunAdBidding() {
853         when(mMockClock.elapsedRealtime())
854                 .thenReturn(
855                         START_ELAPSED_TIMESTAMP,
856                         BIDDING_STAGE_START_TIMESTAMP,
857                         GET_BUYERS_CUSTOM_AUDIENCE_END_TIMESTAMP);
858         // Start the Ad selection execution logger and set start state of the process.
859         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
860         adSelectionExecutionLogger.startBiddingProcess(NUM_BUYERS_REQUESTED);
861         adSelectionExecutionLogger.endGetBuyersCustomAudience(NUM_BUYERS_FETCHED);
862         Throwable throwable =
863                 assertThrows(
864                         IllegalStateException.class,
865                         () ->
866                                 adSelectionExecutionLogger.endBiddingProcess(
867                                         AD_BIDDING_OUTCOMES, STATUS_SUCCESS));
868         assertThat(throwable.getMessage()).contains(MISSING_START_RUN_AD_BIDDING);
869     }
870 
871     @Test
testAdSelectionExecutionLogger_redundantEndRunAdBidding()872     public void testAdSelectionExecutionLogger_redundantEndRunAdBidding() {
873         when(mMockClock.elapsedRealtime())
874                 .thenReturn(
875                         START_ELAPSED_TIMESTAMP,
876                         BIDDING_STAGE_START_TIMESTAMP,
877                         GET_BUYERS_CUSTOM_AUDIENCE_END_TIMESTAMP,
878                         RUN_AD_BIDDING_START_TIMESTAMP,
879                         RUN_AD_BIDDING_END_TIMESTAMP);
880         // Start the Ad selection execution logger and set start state of the process.
881         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
882         adSelectionExecutionLogger.startBiddingProcess(NUM_BUYERS_REQUESTED);
883         adSelectionExecutionLogger.endGetBuyersCustomAudience(NUM_BUYERS_FETCHED);
884         adSelectionExecutionLogger.startRunAdBidding(CUSTOM_AUDIENCES);
885         adSelectionExecutionLogger.endBiddingProcess(AD_BIDDING_OUTCOMES, STATUS_SUCCESS);
886         Throwable throwable =
887                 assertThrows(
888                         IllegalStateException.class,
889                         () ->
890                                 adSelectionExecutionLogger.endBiddingProcess(
891                                         AD_BIDDING_OUTCOMES, STATUS_SUCCESS));
892         assertThat(throwable.getMessage()).contains(REPEATED_END_BIDDING_STAGE);
893     }
894 
895     @Test
testAdSelectionExecutionLogger_failedGetBuyersCustomAudience()896     public void testAdSelectionExecutionLogger_failedGetBuyersCustomAudience() {
897         when(mMockClock.elapsedRealtime())
898                 .thenReturn(
899                         START_ELAPSED_TIMESTAMP,
900                         BIDDING_STAGE_START_TIMESTAMP,
901                         BIDDING_STAGE_END_TIMESTAMP);
902 
903         // Start the Ad selection execution logger and set start state of the process.
904         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
905         // Set start and end state of the subcomponent get-BUYERS-custom-audience process.
906         adSelectionExecutionLogger.startBiddingProcess(NUM_BUYERS_REQUESTED);
907         int resultCode = AdServicesStatusUtils.STATUS_INTERNAL_ERROR;
908         adSelectionExecutionLogger.endBiddingProcess(null, resultCode);
909 
910         // Verify the logging of the RunAdBiddingProcessReportedStats.
911         verify(mAdServicesLoggerMock)
912                 .logRunAdBiddingProcessReportedStats(
913                         mRunAdBiddingProcessReportedStatsArgumentCaptor.capture());
914         RunAdBiddingProcessReportedStats runAdBiddingProcessReportedStats =
915                 mRunAdBiddingProcessReportedStatsArgumentCaptor.getValue();
916         assertThat(runAdBiddingProcessReportedStats.getGetBuyersCustomAudienceLatencyInMills())
917                 .isEqualTo(TOTAL_BIDDING_STAGE_LATENCY_IN_MS);
918         assertThat(runAdBiddingProcessReportedStats.getGetBuyersCustomAudienceResultCode())
919                 .isEqualTo(resultCode);
920         assertThat(runAdBiddingProcessReportedStats.getNumBuyersRequested())
921                 .isEqualTo(NUM_BUYERS_REQUESTED);
922         assertThat(runAdBiddingProcessReportedStats.getNumBuyersFetched()).isEqualTo(FIELD_UNSET);
923         assertThat(runAdBiddingProcessReportedStats.getNumOfAdsEnteringBidding())
924                 .isEqualTo(FIELD_UNSET);
925         assertThat(runAdBiddingProcessReportedStats.getNumOfCasEnteringBidding())
926                 .isEqualTo(FIELD_UNSET);
927         assertThat(runAdBiddingProcessReportedStats.getNumOfCasPostBidding())
928                 .isEqualTo(FIELD_UNSET);
929         assertThat(runAdBiddingProcessReportedStats.getRatioOfCasSelectingRmktAds())
930                 .isEqualTo(RATIO_OF_CAS_UNSET);
931         assertThat(runAdBiddingProcessReportedStats.getRunAdBiddingLatencyInMillis())
932                 .isEqualTo(FIELD_UNSET);
933         assertThat(runAdBiddingProcessReportedStats.getRunAdBiddingResultCode())
934                 .isEqualTo(FIELD_UNSET);
935         assertThat(runAdBiddingProcessReportedStats.getTotalAdBiddingStageLatencyInMillis())
936                 .isEqualTo(TOTAL_BIDDING_STAGE_LATENCY_IN_MS);
937     }
938 
939     @Test
testAdSelectionExecutionLogger_emptyFetchedCustomAudiences()940     public void testAdSelectionExecutionLogger_emptyFetchedCustomAudiences() {
941         when(mMockClock.elapsedRealtime())
942                 .thenReturn(
943                         START_ELAPSED_TIMESTAMP,
944                         BIDDING_STAGE_START_TIMESTAMP,
945                         GET_BUYERS_CUSTOM_AUDIENCE_END_TIMESTAMP,
946                         BIDDING_STAGE_END_TIMESTAMP);
947 
948         // Start the Ad selection execution logger and set start state of the process.
949         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
950         // Set start and end state of the subcomponent get-BUYERS-custom-audience process.
951         adSelectionExecutionLogger.startBiddingProcess(NUM_BUYERS_REQUESTED);
952         adSelectionExecutionLogger.endGetBuyersCustomAudience(NUM_BUYERS_FETCHED);
953         int resultCode = AdServicesStatusUtils.STATUS_INTERNAL_ERROR;
954         adSelectionExecutionLogger.endBiddingProcess(null, resultCode);
955 
956         // Verify the logging of the RunAdBiddingProcessReportedStats.
957         verify(mAdServicesLoggerMock)
958                 .logRunAdBiddingProcessReportedStats(
959                         mRunAdBiddingProcessReportedStatsArgumentCaptor.capture());
960         RunAdBiddingProcessReportedStats runAdBiddingProcessReportedStats =
961                 mRunAdBiddingProcessReportedStatsArgumentCaptor.getValue();
962         assertThat(runAdBiddingProcessReportedStats.getGetBuyersCustomAudienceLatencyInMills())
963                 .isEqualTo(GET_BUYERS_CUSTOM_AUDIENCE_LATENCY_MS);
964         assertThat(runAdBiddingProcessReportedStats.getGetBuyersCustomAudienceResultCode())
965                 .isEqualTo(STATUS_SUCCESS);
966         assertThat(runAdBiddingProcessReportedStats.getNumBuyersRequested())
967                 .isEqualTo(NUM_BUYERS_REQUESTED);
968         assertThat(runAdBiddingProcessReportedStats.getNumBuyersFetched())
969                 .isEqualTo(NUM_BUYERS_FETCHED);
970         assertThat(runAdBiddingProcessReportedStats.getNumOfAdsEnteringBidding())
971                 .isEqualTo(FIELD_UNSET);
972         assertThat(runAdBiddingProcessReportedStats.getNumOfCasEnteringBidding())
973                 .isEqualTo(FIELD_UNSET);
974         assertThat(runAdBiddingProcessReportedStats.getNumOfCasPostBidding())
975                 .isEqualTo(FIELD_UNSET);
976         assertThat(runAdBiddingProcessReportedStats.getRatioOfCasSelectingRmktAds())
977                 .isEqualTo(RATIO_OF_CAS_UNSET);
978         assertThat(runAdBiddingProcessReportedStats.getRunAdBiddingLatencyInMillis())
979                 .isEqualTo(FIELD_UNSET);
980         assertThat(runAdBiddingProcessReportedStats.getRunAdBiddingResultCode())
981                 .isEqualTo(FIELD_UNSET);
982         assertThat(runAdBiddingProcessReportedStats.getTotalAdBiddingStageLatencyInMillis())
983                 .isEqualTo(TOTAL_BIDDING_STAGE_LATENCY_IN_MS);
984     }
985 
986     @Test
testAdSelectionExecutionLogger_FailedDuringRunAdBidding()987     public void testAdSelectionExecutionLogger_FailedDuringRunAdBidding() {
988         when(mMockClock.elapsedRealtime())
989                 .thenReturn(
990                         START_ELAPSED_TIMESTAMP,
991                         BIDDING_STAGE_START_TIMESTAMP,
992                         GET_BUYERS_CUSTOM_AUDIENCE_END_TIMESTAMP,
993                         RUN_AD_BIDDING_START_TIMESTAMP,
994                         BIDDING_STAGE_END_TIMESTAMP);
995 
996         // Start the Ad selection execution logger and set start state of the process.
997         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
998         // Set start and end state of the subcomponent get-BUYERS-custom-audience process.
999         adSelectionExecutionLogger.startBiddingProcess(NUM_BUYERS_REQUESTED);
1000         adSelectionExecutionLogger.endGetBuyersCustomAudience(NUM_BUYERS_FETCHED);
1001         adSelectionExecutionLogger.startRunAdBidding(CUSTOM_AUDIENCES);
1002         int resultCode = AdServicesStatusUtils.STATUS_INTERNAL_ERROR;
1003         adSelectionExecutionLogger.endBiddingProcess(null, resultCode);
1004 
1005         // Verify the logging of the RunAdBiddingProcessReportedStats.
1006         verify(mAdServicesLoggerMock)
1007                 .logRunAdBiddingProcessReportedStats(
1008                         mRunAdBiddingProcessReportedStatsArgumentCaptor.capture());
1009         RunAdBiddingProcessReportedStats runAdBiddingProcessReportedStats =
1010                 mRunAdBiddingProcessReportedStatsArgumentCaptor.getValue();
1011         assertThat(runAdBiddingProcessReportedStats.getGetBuyersCustomAudienceLatencyInMills())
1012                 .isEqualTo(GET_BUYERS_CUSTOM_AUDIENCE_LATENCY_MS);
1013         assertThat(runAdBiddingProcessReportedStats.getGetBuyersCustomAudienceResultCode())
1014                 .isEqualTo(STATUS_SUCCESS);
1015         assertThat(runAdBiddingProcessReportedStats.getNumBuyersRequested())
1016                 .isEqualTo(NUM_BUYERS_REQUESTED);
1017         assertThat(runAdBiddingProcessReportedStats.getNumBuyersFetched())
1018                 .isEqualTo(NUM_BUYERS_FETCHED);
1019         assertThat(runAdBiddingProcessReportedStats.getNumOfAdsEnteringBidding())
1020                 .isEqualTo(NUM_OF_ADS_ENTERING_BIDDING);
1021         assertThat(runAdBiddingProcessReportedStats.getNumOfCasEnteringBidding())
1022                 .isEqualTo(NUM_OF_CAS_ENTERING_BIDDING);
1023         assertThat(runAdBiddingProcessReportedStats.getNumOfCasPostBidding())
1024                 .isEqualTo(FIELD_UNSET);
1025         assertThat(runAdBiddingProcessReportedStats.getRatioOfCasSelectingRmktAds())
1026                 .isEqualTo(RATIO_OF_CAS_UNSET);
1027         assertThat(runAdBiddingProcessReportedStats.getRunAdBiddingLatencyInMillis())
1028                 .isEqualTo((int) (BIDDING_STAGE_END_TIMESTAMP - RUN_AD_BIDDING_START_TIMESTAMP));
1029         assertThat(runAdBiddingProcessReportedStats.getRunAdBiddingResultCode())
1030                 .isEqualTo(resultCode);
1031         assertThat(runAdBiddingProcessReportedStats.getTotalAdBiddingStageLatencyInMillis())
1032                 .isEqualTo(TOTAL_BIDDING_STAGE_LATENCY_IN_MS);
1033     }
1034 
1035     @Test
testAdSelectionExecutionLogger_redundantStartRunAdScoring()1036     public void testAdSelectionExecutionLogger_redundantStartRunAdScoring() {
1037         when(mMockClock.elapsedRealtime())
1038                 .thenReturn(START_ELAPSED_TIMESTAMP, RUN_AD_SCORING_START_TIMESTAMP);
1039         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1040         // Set the start of the run-ad-scoring process.
1041         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
1042         Throwable throwable =
1043                 assertThrows(
1044                         IllegalStateException.class,
1045                         () -> adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES));
1046         assertThat(throwable.getMessage()).contains(REPEATED_START_RUN_AD_SCORING);
1047     }
1048 
1049     @Test
testAdSelectionExecutionLogger_missingStartRunAdScoring()1050     public void testAdSelectionExecutionLogger_missingStartRunAdScoring() {
1051         when(mMockClock.elapsedRealtime()).thenReturn(START_ELAPSED_TIMESTAMP);
1052         AdSelectionExecutionLogger adSelectionExecutionLogger =
1053                 new AdSelectionExecutionLogger(
1054                         sCallerMetadata, mMockClock, mContextMock, mAdServicesLoggerMock, mFlags);
1055         // Set the start of the get-ad-selection-logic process.
1056         Throwable throwable =
1057                 assertThrows(
1058                         IllegalStateException.class,
1059                         () -> adSelectionExecutionLogger.startGetAdSelectionLogic());
1060         assertThat(throwable.getMessage()).contains(MISSING_START_RUN_AD_SCORING);
1061     }
1062 
1063     @Test
testAdSelectionExecutionLogger_missingStartGetAdSelectionLogic()1064     public void testAdSelectionExecutionLogger_missingStartGetAdSelectionLogic() {
1065         when(mMockClock.elapsedRealtime())
1066                 .thenReturn(START_ELAPSED_TIMESTAMP, RUN_AD_SCORING_START_TIMESTAMP);
1067         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1068         // Set the start of the get-ad-selection-logic process.
1069         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
1070 
1071         Throwable throwable =
1072                 assertThrows(
1073                         IllegalStateException.class,
1074                         () -> adSelectionExecutionLogger.endGetAdSelectionLogic(mAdSelectionLogic));
1075         assertThat(throwable.getMessage()).contains(MISSING_START_GET_AD_SELECTION_LOGIC);
1076     }
1077 
1078     @Test
testAdSelectionExecutionLogger_redundantStartGetAdSelectionLogic()1079     public void testAdSelectionExecutionLogger_redundantStartGetAdSelectionLogic() {
1080         when(mMockClock.elapsedRealtime())
1081                 .thenReturn(
1082                         START_ELAPSED_TIMESTAMP,
1083                         RUN_AD_SCORING_START_TIMESTAMP,
1084                         GET_AD_SELECTION_LOGIC_START_TIMESTAMP);
1085         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1086         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
1087         adSelectionExecutionLogger.startGetAdSelectionLogic();
1088         Throwable throwable =
1089                 assertThrows(
1090                         IllegalStateException.class,
1091                         () -> adSelectionExecutionLogger.startGetAdSelectionLogic());
1092         assertThat(throwable.getMessage()).contains(REPEATED_START_GET_AD_SELECTION_LOGIC);
1093     }
1094 
1095     @Test
testAdSelectionExecutionLogger_missingEndGetAdSelectionLogic()1096     public void testAdSelectionExecutionLogger_missingEndGetAdSelectionLogic() {
1097         when(mMockClock.elapsedRealtime())
1098                 .thenReturn(START_ELAPSED_TIMESTAMP, RUN_AD_SCORING_START_TIMESTAMP);
1099         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1100 
1101         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
1102 
1103         Throwable throwable =
1104                 assertThrows(
1105                         IllegalStateException.class,
1106                         () -> adSelectionExecutionLogger.startGetAdScores());
1107         assertThat(throwable.getMessage()).contains(MISSING_END_GET_AD_SELECTION_LOGIC);
1108     }
1109 
1110     @Test
testAdSelectionExecutionLogger_redundantEndGetAdSelectionLogic()1111     public void testAdSelectionExecutionLogger_redundantEndGetAdSelectionLogic() {
1112         when(mMockClock.elapsedRealtime())
1113                 .thenReturn(
1114                         START_ELAPSED_TIMESTAMP,
1115                         RUN_AD_SCORING_START_TIMESTAMP,
1116                         GET_AD_SELECTION_LOGIC_START_TIMESTAMP,
1117                         GET_AD_SELECTION_LOGIC_END_TIMESTAMP);
1118         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1119 
1120         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
1121         adSelectionExecutionLogger.startGetAdSelectionLogic();
1122         adSelectionExecutionLogger.endGetAdSelectionLogic(mAdSelectionLogic);
1123 
1124         Throwable throwable =
1125                 assertThrows(
1126                         IllegalStateException.class,
1127                         () -> adSelectionExecutionLogger.endGetAdSelectionLogic(mAdSelectionLogic));
1128         assertThat(throwable.getMessage()).contains(REPEATED_END_GET_AD_SELECTION_LOGIC);
1129     }
1130 
1131     @Test
testAdSelectionExecutionLogger_missingStartGetAdScores()1132     public void testAdSelectionExecutionLogger_missingStartGetAdScores() {
1133         when(mMockClock.elapsedRealtime())
1134                 .thenReturn(
1135                         START_ELAPSED_TIMESTAMP,
1136                         RUN_AD_SCORING_START_TIMESTAMP,
1137                         GET_AD_SELECTION_LOGIC_START_TIMESTAMP,
1138                         GET_AD_SELECTION_LOGIC_END_TIMESTAMP);
1139         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1140 
1141         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
1142         adSelectionExecutionLogger.startGetAdSelectionLogic();
1143         adSelectionExecutionLogger.endGetAdSelectionLogic(mAdSelectionLogic);
1144 
1145         Throwable throwable =
1146                 assertThrows(
1147                         IllegalStateException.class,
1148                         () -> adSelectionExecutionLogger.startGetTrustedScoringSignals());
1149         assertThat(throwable.getMessage()).contains(MISSING_START_GET_AD_SCORES);
1150     }
1151 
1152     @Test
testAdSelectionExecutionLogger_redundantStartGetAdScores()1153     public void testAdSelectionExecutionLogger_redundantStartGetAdScores() {
1154         when(mMockClock.elapsedRealtime())
1155                 .thenReturn(
1156                         START_ELAPSED_TIMESTAMP,
1157                         RUN_AD_SCORING_START_TIMESTAMP,
1158                         GET_AD_SELECTION_LOGIC_START_TIMESTAMP,
1159                         GET_AD_SELECTION_LOGIC_END_TIMESTAMP,
1160                         GET_AD_SCORES_START_TIMESTAMP);
1161         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1162 
1163         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
1164         adSelectionExecutionLogger.startGetAdSelectionLogic();
1165         adSelectionExecutionLogger.endGetAdSelectionLogic(mAdSelectionLogic);
1166         adSelectionExecutionLogger.startGetAdScores();
1167 
1168         Throwable throwable =
1169                 assertThrows(
1170                         IllegalStateException.class,
1171                         () -> adSelectionExecutionLogger.startGetAdScores());
1172         assertThat(throwable.getMessage()).contains(REPEATED_START_GET_AD_SCORES);
1173     }
1174 
1175     @Test
testAdSelectionExecutionLogger_missingStartGetTrustedScoringSignals()1176     public void testAdSelectionExecutionLogger_missingStartGetTrustedScoringSignals() {
1177         when(mMockClock.elapsedRealtime())
1178                 .thenReturn(
1179                         START_ELAPSED_TIMESTAMP,
1180                         RUN_AD_SCORING_START_TIMESTAMP,
1181                         GET_AD_SELECTION_LOGIC_START_TIMESTAMP,
1182                         GET_AD_SELECTION_LOGIC_END_TIMESTAMP,
1183                         GET_AD_SCORES_START_TIMESTAMP);
1184         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1185 
1186         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
1187         adSelectionExecutionLogger.startGetAdSelectionLogic();
1188         adSelectionExecutionLogger.endGetAdSelectionLogic(mAdSelectionLogic);
1189         adSelectionExecutionLogger.startGetAdScores();
1190 
1191         Throwable throwable =
1192                 assertThrows(
1193                         IllegalStateException.class,
1194                         () ->
1195                                 adSelectionExecutionLogger.endGetTrustedScoringSignals(
1196                                         mAdSelectionSignals));
1197         assertThat(throwable.getMessage()).contains(MISSING_START_GET_TRUSTED_SCORING_SIGNALS);
1198     }
1199 
1200     @Test
testAdSelectionExecutionLogger_redundantStartGetTrustedScoringSignals()1201     public void testAdSelectionExecutionLogger_redundantStartGetTrustedScoringSignals() {
1202         when(mMockClock.elapsedRealtime())
1203                 .thenReturn(
1204                         START_ELAPSED_TIMESTAMP,
1205                         RUN_AD_SCORING_START_TIMESTAMP,
1206                         GET_AD_SELECTION_LOGIC_START_TIMESTAMP,
1207                         GET_AD_SELECTION_LOGIC_END_TIMESTAMP,
1208                         GET_AD_SCORES_START_TIMESTAMP,
1209                         GET_TRUSTED_SCORING_SIGNALS_START_TIMESTAMP);
1210         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1211 
1212         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
1213         adSelectionExecutionLogger.startGetAdSelectionLogic();
1214         adSelectionExecutionLogger.endGetAdSelectionLogic(mAdSelectionLogic);
1215         adSelectionExecutionLogger.startGetAdScores();
1216         adSelectionExecutionLogger.startGetTrustedScoringSignals();
1217         Throwable throwable =
1218                 assertThrows(
1219                         IllegalStateException.class,
1220                         () -> adSelectionExecutionLogger.startGetTrustedScoringSignals());
1221         assertThat(throwable.getMessage()).contains(REPEATED_START_GET_TRUSTED_SCORING_SIGNALS);
1222     }
1223 
1224     @Test
testAdSelectionExecutionLogger_redundantEndGetTrustedScoringSignals()1225     public void testAdSelectionExecutionLogger_redundantEndGetTrustedScoringSignals() {
1226         when(mMockClock.elapsedRealtime())
1227                 .thenReturn(
1228                         START_ELAPSED_TIMESTAMP,
1229                         RUN_AD_SCORING_START_TIMESTAMP,
1230                         GET_AD_SELECTION_LOGIC_START_TIMESTAMP,
1231                         GET_AD_SELECTION_LOGIC_END_TIMESTAMP,
1232                         GET_AD_SCORES_START_TIMESTAMP,
1233                         GET_TRUSTED_SCORING_SIGNALS_START_TIMESTAMP,
1234                         GET_TRUSTED_SCORING_SIGNALS_END_TIMESTAMP);
1235         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1236 
1237         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
1238         adSelectionExecutionLogger.startGetAdSelectionLogic();
1239         adSelectionExecutionLogger.endGetAdSelectionLogic(mAdSelectionLogic);
1240         adSelectionExecutionLogger.startGetAdScores();
1241         adSelectionExecutionLogger.startGetTrustedScoringSignals();
1242         adSelectionExecutionLogger.endGetTrustedScoringSignals(mAdSelectionSignals);
1243 
1244         Throwable throwable =
1245                 assertThrows(
1246                         IllegalStateException.class,
1247                         () ->
1248                                 adSelectionExecutionLogger.endGetTrustedScoringSignals(
1249                                         mAdSelectionSignals));
1250         assertThat(throwable.getMessage()).contains(REPEATED_END_GET_TRUSTED_SCORING_SIGNALS);
1251     }
1252 
1253     @Test
testAdSelectionExecutionLogger_redundantStartScoreAds()1254     public void testAdSelectionExecutionLogger_redundantStartScoreAds() {
1255         when(mMockClock.elapsedRealtime())
1256                 .thenReturn(
1257                         START_ELAPSED_TIMESTAMP,
1258                         RUN_AD_SCORING_START_TIMESTAMP,
1259                         GET_AD_SELECTION_LOGIC_START_TIMESTAMP,
1260                         GET_AD_SELECTION_LOGIC_END_TIMESTAMP,
1261                         GET_AD_SCORES_START_TIMESTAMP,
1262                         GET_TRUSTED_SCORING_SIGNALS_START_TIMESTAMP,
1263                         GET_TRUSTED_SCORING_SIGNALS_END_TIMESTAMP,
1264                         SCORE_ADS_START_TIMESTAMP);
1265         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1266 
1267         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
1268         adSelectionExecutionLogger.startGetAdSelectionLogic();
1269         adSelectionExecutionLogger.endGetAdSelectionLogic(mAdSelectionLogic);
1270         adSelectionExecutionLogger.startGetAdScores();
1271         adSelectionExecutionLogger.startGetTrustedScoringSignals();
1272         adSelectionExecutionLogger.endGetTrustedScoringSignals(mAdSelectionSignals);
1273         adSelectionExecutionLogger.startScoreAds();
1274 
1275         Throwable throwable =
1276                 assertThrows(
1277                         IllegalStateException.class,
1278                         () -> adSelectionExecutionLogger.startScoreAds());
1279         assertThat(throwable.getMessage()).contains(REPEATED_START_SCORE_ADS);
1280     }
1281 
1282     @Test
testAdSelectionExecutionLogger_missingStartScoreAds()1283     public void testAdSelectionExecutionLogger_missingStartScoreAds() {
1284         when(mMockClock.elapsedRealtime())
1285                 .thenReturn(
1286                         START_ELAPSED_TIMESTAMP,
1287                         RUN_AD_SCORING_START_TIMESTAMP,
1288                         GET_AD_SELECTION_LOGIC_START_TIMESTAMP,
1289                         GET_AD_SELECTION_LOGIC_END_TIMESTAMP,
1290                         GET_AD_SCORES_START_TIMESTAMP,
1291                         GET_TRUSTED_SCORING_SIGNALS_START_TIMESTAMP,
1292                         GET_TRUSTED_SCORING_SIGNALS_END_TIMESTAMP);
1293         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1294 
1295         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
1296         adSelectionExecutionLogger.startGetAdSelectionLogic();
1297         adSelectionExecutionLogger.endGetAdSelectionLogic(mAdSelectionLogic);
1298         adSelectionExecutionLogger.startGetAdScores();
1299         adSelectionExecutionLogger.startGetTrustedScoringSignals();
1300         adSelectionExecutionLogger.endGetTrustedScoringSignals(mAdSelectionSignals);
1301 
1302         Throwable throwable =
1303                 assertThrows(
1304                         IllegalStateException.class,
1305                         () -> adSelectionExecutionLogger.endScoreAds());
1306         assertThat(throwable.getMessage()).contains(MISSING_START_SCORE_ADS);
1307     }
1308 
1309     @Test
testAdSelectionExecutionLogger_redundantEndScoreAds()1310     public void testAdSelectionExecutionLogger_redundantEndScoreAds() {
1311         when(mMockClock.elapsedRealtime())
1312                 .thenReturn(
1313                         START_ELAPSED_TIMESTAMP,
1314                         RUN_AD_SCORING_START_TIMESTAMP,
1315                         GET_AD_SELECTION_LOGIC_START_TIMESTAMP,
1316                         GET_AD_SELECTION_LOGIC_END_TIMESTAMP,
1317                         GET_AD_SCORES_START_TIMESTAMP,
1318                         GET_TRUSTED_SCORING_SIGNALS_START_TIMESTAMP,
1319                         GET_TRUSTED_SCORING_SIGNALS_END_TIMESTAMP,
1320                         SCORE_ADS_START_TIMESTAMP,
1321                         SCORE_ADS_END_TIMESTAMP);
1322         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1323 
1324         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
1325         adSelectionExecutionLogger.startGetAdSelectionLogic();
1326         adSelectionExecutionLogger.endGetAdSelectionLogic(mAdSelectionLogic);
1327         adSelectionExecutionLogger.startGetAdScores();
1328         adSelectionExecutionLogger.startGetTrustedScoringSignals();
1329         adSelectionExecutionLogger.endGetTrustedScoringSignals(mAdSelectionSignals);
1330         adSelectionExecutionLogger.startScoreAds();
1331         adSelectionExecutionLogger.endScoreAds();
1332 
1333         Throwable throwable =
1334                 assertThrows(
1335                         IllegalStateException.class,
1336                         () -> adSelectionExecutionLogger.endScoreAds());
1337         assertThat(throwable.getMessage()).contains(REPEATED_END_SCORE_ADS);
1338     }
1339 
1340     @Test
testAdSelectionExecutionLogger_missingEndScoreAds()1341     public void testAdSelectionExecutionLogger_missingEndScoreAds() {
1342         when(mMockClock.elapsedRealtime())
1343                 .thenReturn(
1344                         START_ELAPSED_TIMESTAMP,
1345                         RUN_AD_SCORING_START_TIMESTAMP,
1346                         GET_AD_SELECTION_LOGIC_START_TIMESTAMP,
1347                         GET_AD_SELECTION_LOGIC_END_TIMESTAMP,
1348                         GET_AD_SCORES_START_TIMESTAMP,
1349                         GET_TRUSTED_SCORING_SIGNALS_START_TIMESTAMP,
1350                         GET_TRUSTED_SCORING_SIGNALS_END_TIMESTAMP,
1351                         SCORE_ADS_START_TIMESTAMP);
1352         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1353 
1354         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
1355         adSelectionExecutionLogger.startGetAdSelectionLogic();
1356         adSelectionExecutionLogger.endGetAdSelectionLogic(mAdSelectionLogic);
1357         adSelectionExecutionLogger.startGetAdScores();
1358         adSelectionExecutionLogger.startGetTrustedScoringSignals();
1359         adSelectionExecutionLogger.endGetTrustedScoringSignals(mAdSelectionSignals);
1360         adSelectionExecutionLogger.startScoreAds();
1361 
1362         Throwable throwable =
1363                 assertThrows(
1364                         IllegalStateException.class,
1365                         () -> adSelectionExecutionLogger.endGetAdScores());
1366         assertThat(throwable.getMessage()).contains(MISSING_END_SCORE_ADS);
1367     }
1368 
1369     @Test
testAdSelectionExecutionLogger_redundantEndGetScoreAds()1370     public void testAdSelectionExecutionLogger_redundantEndGetScoreAds() {
1371         when(mMockClock.elapsedRealtime())
1372                 .thenReturn(
1373                         START_ELAPSED_TIMESTAMP,
1374                         RUN_AD_SCORING_START_TIMESTAMP,
1375                         GET_AD_SELECTION_LOGIC_START_TIMESTAMP,
1376                         GET_AD_SELECTION_LOGIC_END_TIMESTAMP,
1377                         GET_AD_SCORES_START_TIMESTAMP,
1378                         GET_TRUSTED_SCORING_SIGNALS_START_TIMESTAMP,
1379                         GET_TRUSTED_SCORING_SIGNALS_END_TIMESTAMP,
1380                         SCORE_ADS_START_TIMESTAMP,
1381                         SCORE_ADS_END_TIMESTAMP,
1382                         GET_AD_SCORES_END_TIMESTAMP);
1383         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1384 
1385         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
1386         adSelectionExecutionLogger.startGetAdSelectionLogic();
1387         adSelectionExecutionLogger.endGetAdSelectionLogic(mAdSelectionLogic);
1388         adSelectionExecutionLogger.startGetAdScores();
1389         adSelectionExecutionLogger.startGetTrustedScoringSignals();
1390         adSelectionExecutionLogger.endGetTrustedScoringSignals(mAdSelectionSignals);
1391         adSelectionExecutionLogger.startScoreAds();
1392         adSelectionExecutionLogger.endScoreAds();
1393         adSelectionExecutionLogger.endGetAdScores();
1394 
1395         Throwable throwable =
1396                 assertThrows(
1397                         IllegalStateException.class,
1398                         () -> adSelectionExecutionLogger.endGetAdScores());
1399         assertThat(throwable.getMessage()).contains(REPEATED_END_GET_AD_SCORES);
1400     }
1401 
1402     @Test
testAdSelectionExecutionLogger_redundantEndRunAdScoring()1403     public void testAdSelectionExecutionLogger_redundantEndRunAdScoring() {
1404         when(mMockClock.elapsedRealtime())
1405                 .thenReturn(
1406                         START_ELAPSED_TIMESTAMP,
1407                         RUN_AD_SCORING_START_TIMESTAMP,
1408                         RUN_AD_SCORING_END_TIMESTAMP);
1409         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1410 
1411         adSelectionExecutionLogger.startRunAdScoring(AD_BIDDING_OUTCOMES);
1412         adSelectionExecutionLogger.endRunAdScoring(STATUS_INTERNAL_ERROR);
1413 
1414         Throwable throwable =
1415                 assertThrows(
1416                         IllegalStateException.class,
1417                         () -> adSelectionExecutionLogger.endRunAdScoring(STATUS_INTERNAL_ERROR));
1418         assertThat(throwable.getMessage()).contains(REPEATED_END_RUN_AD_SCORING);
1419     }
1420 
1421     @Test
testAdSelectionExecutionLogger_redundantStartOfPersistAdSelection()1422     public void testAdSelectionExecutionLogger_redundantStartOfPersistAdSelection() {
1423         when(mMockClock.elapsedRealtime())
1424                 .thenReturn(START_ELAPSED_TIMESTAMP, PERSIST_AD_SELECTION_START_TIMESTAMP);
1425         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1426         // Set start state of the subcomponent persist-ad-selection process.
1427         adSelectionExecutionLogger.startPersistAdSelection(mMockDBAdSelection);
1428 
1429         Throwable throwable =
1430                 assertThrows(
1431                         IllegalStateException.class,
1432                         () ->
1433                                 adSelectionExecutionLogger.startPersistAdSelection(
1434                                         mMockDBAdSelection));
1435         assertThat(throwable.getMessage()).contains(REPEATED_START_PERSIST_AD_SELECTION);
1436     }
1437 
1438     @Test
testAdSelectionExecutionLogger_redundantEndOfPersistAdSelection()1439     public void testAdSelectionExecutionLogger_redundantEndOfPersistAdSelection() {
1440         when(mMockClock.elapsedRealtime())
1441                 .thenReturn(
1442                         START_ELAPSED_TIMESTAMP,
1443                         PERSIST_AD_SELECTION_START_TIMESTAMP,
1444                         PERSIST_AD_SELECTION_END_TIMESTAMP);
1445         when(mContextMock.getDatabasePath(DATABASE_NAME)).thenReturn(mMockDBAdSelectionFile);
1446         when(mMockDBAdSelectionFile.length()).thenReturn(DB_AD_SELECTION_FILE_SIZE);
1447         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1448         // Set start and end states of the subcomponent persist-ad-selection process.
1449         adSelectionExecutionLogger.startPersistAdSelection(mMockDBAdSelection);
1450         adSelectionExecutionLogger.endPersistAdSelection();
1451 
1452         Throwable throwable =
1453                 assertThrows(
1454                         IllegalStateException.class,
1455                         adSelectionExecutionLogger::endPersistAdSelection);
1456         assertThat(throwable.getMessage()).contains(REPEATED_END_PERSIST_AD_SELECTION);
1457     }
1458 
1459     @Test
testAdSelectionExecutionLogger_missingStartOfPersistAdSelection()1460     public void testAdSelectionExecutionLogger_missingStartOfPersistAdSelection() {
1461         when(mMockClock.elapsedRealtime()).thenReturn(START_ELAPSED_TIMESTAMP);
1462         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1463         Throwable throwable =
1464                 assertThrows(
1465                         IllegalStateException.class,
1466                         adSelectionExecutionLogger::endPersistAdSelection);
1467         assertThat(throwable.getMessage()).contains(MISSING_START_PERSIST_AD_SELECTION);
1468     }
1469 
1470     @Test
testAdSelectionExecutionLogger_missingEndOfPersistAdSelection()1471     public void testAdSelectionExecutionLogger_missingEndOfPersistAdSelection() {
1472         when(mMockClock.elapsedRealtime())
1473                 .thenReturn(
1474                         START_ELAPSED_TIMESTAMP,
1475                         PERSIST_AD_SELECTION_START_TIMESTAMP,
1476                         STOP_ELAPSED_TIMESTAMP);
1477         when(mMockDBAdSelection.getBiddingLogicUri()).thenReturn(DECISION_LOGIC_URI);
1478         // Start the Ad selection execution logger and set start state of the process.
1479         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1480         // Set start state of the subcomponent persist-ad-selection process.
1481         adSelectionExecutionLogger.startPersistAdSelection(mMockDBAdSelection);
1482         // Close the Ad selection execution logger and log the data into the AdServicesLogger.
1483         Throwable throwable =
1484                 assertThrows(
1485                         IllegalStateException.class,
1486                         () -> adSelectionExecutionLogger.close(STATUS_SUCCESS));
1487         assertThat(throwable.getMessage()).contains(MISSING_END_PERSIST_AD_SELECTION);
1488     }
1489 
1490     @Test
testAdSelectionExecutionLogger_missingPersistAdSelection()1491     public void testAdSelectionExecutionLogger_missingPersistAdSelection() {
1492         when(mMockClock.elapsedRealtime())
1493                 .thenReturn(START_ELAPSED_TIMESTAMP, STOP_ELAPSED_TIMESTAMP);
1494         when(mMockDBAdSelection.getBiddingLogicUri()).thenReturn(DECISION_LOGIC_URI);
1495         // Start the Ad selection execution logger and set start state of the process.
1496         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1497         // Close the Ad selection execution logger and log the data into the AdServicesLogger.
1498         Throwable throwable =
1499                 assertThrows(
1500                         IllegalStateException.class,
1501                         () -> adSelectionExecutionLogger.close(STATUS_SUCCESS));
1502         assertThat(throwable.getMessage()).contains(MISSING_PERSIST_AD_SELECTION);
1503     }
1504 
1505     @Test
testAdSelectionExecutionLogger_RunAdSelectionFailedBeforePersistAdSelection()1506     public void testAdSelectionExecutionLogger_RunAdSelectionFailedBeforePersistAdSelection() {
1507         when(mMockClock.elapsedRealtime())
1508                 .thenReturn(START_ELAPSED_TIMESTAMP, STOP_ELAPSED_TIMESTAMP);
1509         // Start the Ad selection execution logger and set start state of the process.
1510         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1511         int resultCode = AdServicesStatusUtils.STATUS_INTERNAL_ERROR;
1512         adSelectionExecutionLogger.close(resultCode);
1513 
1514         verify(mAdServicesLoggerMock)
1515                 .logRunAdSelectionProcessReportedStats(
1516                         mRunAdSelectionProcessReportedStatsArgumentCaptor.capture());
1517         RunAdSelectionProcessReportedStats runAdSelectionProcessReportedStats =
1518                 mRunAdSelectionProcessReportedStatsArgumentCaptor.getValue();
1519         assertThat(runAdSelectionProcessReportedStats.getIsRemarketingAdsWon())
1520                 .isEqualTo(IS_RMKT_ADS_WON_UNSET);
1521         assertThat(runAdSelectionProcessReportedStats.getDBAdSelectionSizeInBytes())
1522                 .isEqualTo(DB_AD_SELECTION_SIZE_IN_BYTES_UNSET);
1523         assertThat(runAdSelectionProcessReportedStats.getPersistAdSelectionLatencyInMillis())
1524                 .isEqualTo(FIELD_UNSET);
1525         assertThat(runAdSelectionProcessReportedStats.getPersistAdSelectionResultCode())
1526                 .isEqualTo(FIELD_UNSET);
1527         assertThat(runAdSelectionProcessReportedStats.getRunAdSelectionLatencyInMillis())
1528                 .isEqualTo(RUN_AD_SELECTION_INTERNAL_FINAL_LATENCY_MS);
1529         assertThat(runAdSelectionProcessReportedStats.getRunAdSelectionResultCode())
1530                 .isEqualTo(resultCode);
1531     }
1532 
1533     @Test
testAdSelectionExecutionLogger_RunAdSelectionFailedDuringPersistAdSelection()1534     public void testAdSelectionExecutionLogger_RunAdSelectionFailedDuringPersistAdSelection() {
1535         when(mMockClock.elapsedRealtime())
1536                 .thenReturn(
1537                         START_ELAPSED_TIMESTAMP,
1538                         PERSIST_AD_SELECTION_START_TIMESTAMP,
1539                         STOP_ELAPSED_TIMESTAMP);
1540         // Start the Ad selection execution logger and set start state of the process.
1541         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1542         adSelectionExecutionLogger.startPersistAdSelection(mMockDBAdSelection);
1543 
1544         int resultCode = AdServicesStatusUtils.STATUS_INTERNAL_ERROR;
1545         adSelectionExecutionLogger.close(resultCode);
1546 
1547         verify(mAdServicesLoggerMock)
1548                 .logRunAdSelectionProcessReportedStats(
1549                         mRunAdSelectionProcessReportedStatsArgumentCaptor.capture());
1550         RunAdSelectionProcessReportedStats runAdSelectionProcessReportedStats =
1551                 mRunAdSelectionProcessReportedStatsArgumentCaptor.getValue();
1552         assertThat(runAdSelectionProcessReportedStats.getIsRemarketingAdsWon())
1553                 .isEqualTo(IS_RMKT_ADS_WON);
1554         assertThat(runAdSelectionProcessReportedStats.getDBAdSelectionSizeInBytes())
1555                 .isEqualTo(DB_AD_SELECTION_SIZE_IN_BYTES_UNSET);
1556         assertThat(runAdSelectionProcessReportedStats.getPersistAdSelectionLatencyInMillis())
1557                 .isEqualTo((int) (STOP_ELAPSED_TIMESTAMP - PERSIST_AD_SELECTION_START_TIMESTAMP));
1558         assertThat(runAdSelectionProcessReportedStats.getPersistAdSelectionResultCode())
1559                 .isEqualTo(resultCode);
1560         assertThat(runAdSelectionProcessReportedStats.getRunAdSelectionLatencyInMillis())
1561                 .isEqualTo(RUN_AD_SELECTION_INTERNAL_FINAL_LATENCY_MS);
1562         assertThat(runAdSelectionProcessReportedStats.getRunAdSelectionResultCode())
1563                 .isEqualTo(resultCode);
1564     }
1565 
1566     @Test
testRunAdSelectionLatencyCalculator_getRunAdSelectionOverallLatency()1567     public void testRunAdSelectionLatencyCalculator_getRunAdSelectionOverallLatency() {
1568         when(mMockClock.elapsedRealtime())
1569                 .thenReturn(START_ELAPSED_TIMESTAMP, STOP_ELAPSED_TIMESTAMP);
1570 
1571         // Start the Ad selection execution logger and set start state of the process.
1572         AdSelectionExecutionLogger adSelectionExecutionLogger = getAdSelectionExecutionLogger();
1573 
1574         assertThat(adSelectionExecutionLogger.getRunAdSelectionOverallLatencyInMs())
1575                 .isEqualTo(RUN_AD_SELECTION_OVERALL_LATENCY_MS);
1576     }
1577 
getAdSelectionExecutionLogger()1578     private AdSelectionExecutionLogger getAdSelectionExecutionLogger() {
1579         return new AdSelectionExecutionLogger(
1580                 sCallerMetadata, mMockClock, mContextMock, mAdServicesLoggerMock, mFlags);
1581     }
1582 
1583     private static class AdSelectionExecutionLoggerTestFlags implements Flags {
1584         @Override
getFledgeDataVersionHeaderMetricsEnabled()1585         public boolean getFledgeDataVersionHeaderMetricsEnabled() {
1586             return true;
1587         }
1588 
1589         @Override
getFledgeJsScriptResultCodeMetricsEnabled()1590         public boolean getFledgeJsScriptResultCodeMetricsEnabled() {
1591             return true;
1592         }
1593     }
1594 }
1595