1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.adservices.tests.extdata;
17 
18 import static android.adservices.extdata.AdServicesExtDataParams.BOOLEAN_FALSE;
19 import static android.adservices.extdata.AdServicesExtDataParams.BOOLEAN_TRUE;
20 import static android.adservices.extdata.AdServicesExtDataParams.STATE_UNKNOWN;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import android.adservices.extdata.AdServicesExtDataParams;
25 import android.adservices.extdata.AdServicesExtDataStorageService;
26 import android.adservices.extdata.GetAdServicesExtDataResult;
27 import android.adservices.extdata.IAdServicesExtDataStorageService;
28 import android.adservices.extdata.IGetAdServicesExtDataCallback;
29 import android.content.Intent;
30 import android.os.IBinder;
31 
32 import com.android.adservices.shared.testing.concurrency.FailableOnResultSyncCallback;
33 
34 import org.junit.Test;
35 
36 public final class AdServicesExtDataServiceCtsTest {
37     /** Fake AdServicesExtDataStorageService implementation for testing. */
38     private static final class AdServicesExtDataStorageServiceTestProxy
39             extends AdServicesExtDataStorageService {
40 
41         private AdServicesExtDataParams mParamsStorage = null;
42 
onGetAdServicesExtData()43         public AdServicesExtDataParams onGetAdServicesExtData() {
44             return mParamsStorage;
45         }
46 
onPutAdServicesExtData( AdServicesExtDataParams adServicesExtDataParams, int[] adServicesExtDataFields)47         public void onPutAdServicesExtData(
48                 AdServicesExtDataParams adServicesExtDataParams, int[] adServicesExtDataFields) {
49             // For testing, ignore adServicesExtDataFields. Update entire object.
50             mParamsStorage = adServicesExtDataParams;
51         }
52     }
53 
54     @Test
testAdServicesExtDataStorageService()55     public void testAdServicesExtDataStorageService() throws Exception {
56         // Check service connection
57         AdServicesExtDataStorageServiceTestProxy proxy =
58                 new AdServicesExtDataStorageServiceTestProxy();
59         Intent intent = new Intent();
60         intent.setAction(AdServicesExtDataStorageService.SERVICE_INTERFACE);
61         IBinder remoteObject = proxy.onBind(intent);
62         assertThat(remoteObject).isNotNull();
63 
64         IAdServicesExtDataStorageService service =
65                 IAdServicesExtDataStorageService.Stub.asInterface(remoteObject);
66         assertThat(service).isNotNull();
67 
68         // Update AdExt data
69         AdServicesExtDataParams paramsToUpdate =
70                 new AdServicesExtDataParams(
71                         /*isNotificationDisplayed=*/ BOOLEAN_TRUE,
72                         /*isMeasurementConsented=*/ BOOLEAN_FALSE,
73                         /*isU18Account=*/ BOOLEAN_TRUE,
74                         /*isAdultAccount=*/ BOOLEAN_FALSE,
75                         /*manualInteractionWithConsentStatus=*/ STATE_UNKNOWN,
76                         /*measurementRollbackApexVersion=*/ 200L);
77 
78         SyncAdExtTestCallback putReceiver = new SyncAdExtTestCallback();
79         service.putAdServicesExtData(
80                 paramsToUpdate,
81                 new int[] {}, // Fake service implementation ignores this parameter.
82                 putReceiver);
83 
84         putReceiver.assertResultReceived();
85 
86         // Get updated AdExt data
87         SyncAdExtTestCallback getReceiver = new SyncAdExtTestCallback();
88         service.getAdServicesExtData(getReceiver);
89 
90         GetAdServicesExtDataResult result = getReceiver.assertResultReceived();
91         assertThat(result).isNotNull();
92         assertThat(result.getAdServicesExtDataParams()).isEqualTo(paramsToUpdate);
93     }
94 
95     private static final class SyncAdExtTestCallback
96             extends FailableOnResultSyncCallback<GetAdServicesExtDataResult, String>
97             implements IGetAdServicesExtDataCallback {
98         @Override
onError(String msg)99         public void onError(String msg) {
100             onFailure(msg);
101         }
102     }
103 }
104