• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.adservices.tests.cts.measurement;
18 
19 import static com.android.adservices.shared.testing.AndroidSdk.RVC;
20 
21 import static com.google.common.truth.Truth.assertWithMessage;
22 
23 import android.adservices.exceptions.AdServicesException;
24 import android.adservices.measurement.DeletionRequest;
25 import android.adservices.measurement.MeasurementManager;
26 import android.adservices.measurement.SourceRegistrationRequest;
27 import android.adservices.measurement.WebSourceParams;
28 import android.adservices.measurement.WebSourceRegistrationRequest;
29 import android.adservices.measurement.WebTriggerParams;
30 import android.adservices.measurement.WebTriggerRegistrationRequest;
31 import android.net.Uri;
32 import android.view.InputEvent;
33 import android.view.KeyEvent;
34 
35 import com.android.adservices.common.AdServicesOutcomeReceiverForTests;
36 import com.android.adservices.common.AdservicesTestHelper;
37 import com.android.adservices.common.WebUtil;
38 import com.android.adservices.shared.testing.annotations.RequiresSdkRange;
39 import com.android.adservices.shared.testing.concurrency.DeviceSideConcurrencyHelper;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 
44 import java.util.Collections;
45 import java.util.concurrent.Executor;
46 import java.util.concurrent.Executors;
47 
48 @RequiresSdkRange(atMost = RVC)
49 public final class MeasurementManagerRvcCtsTest extends CtsMeasurementEndToEndTestCase {
50 
51     private static final Executor CALLBACK_EXECUTOR = Executors.newCachedThreadPool();
52 
53     // Note: The source and trigger registration used here must match one of those in
54     // {@link PreEnrolledAdTechForTest}.
55     private static final Uri SOURCE_REGISTRATION_URI = WebUtil.validUri("https://test.test/source");
56     private static final Uri TRIGGER_REGISTRATION_URI =
57             WebUtil.validUri("https://test.test/trigger");
58     private static final Uri DESTINATION = WebUtil.validUri("http://trigger-origin.test");
59     private static final Uri OS_DESTINATION = Uri.parse("android-app://com.os.destination");
60     private static final Uri WEB_DESTINATION = WebUtil.validUri("http://web-destination.test");
61     private static final InputEvent INPUT_EVENT =
62             new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_1);
63 
64     private MeasurementManager mMeasurementManager;
65 
66     @Before
setup()67     public void setup() throws Exception {
68         // Kill adservices process to avoid interfering from other tests.
69         AdservicesTestHelper.killAdservicesProcess(mContext);
70         mMeasurementManager = MeasurementManager.get(mContext);
71         assertWithMessage("MeasurementManager.get(%s)", mContext)
72                 .that(mMeasurementManager)
73                 .isNotNull();
74 
75         DeviceSideConcurrencyHelper.sleep(
76                 1000, "Cool-off rate limiter in case it was initialized by another test");
77     }
78 
79     @Test
testRegisterSourceUri_onR_invokesCallbackOnError()80     public void testRegisterSourceUri_onR_invokesCallbackOnError() throws Exception {
81         AdServicesOutcomeReceiverForTests callback = new AdServicesOutcomeReceiverForTests();
82 
83         mMeasurementManager.registerSource(
84                 Uri.parse("https://registration-source"),
85                 /* inputEvent= */ null,
86                 CALLBACK_EXECUTOR,
87                 callback);
88 
89         callback.assertFailure(AdServicesException.class);
90     }
91 
92     @Test
testRegisterSourceRequest_onR_invokesCallbackOnError()93     public void testRegisterSourceRequest_onR_invokesCallbackOnError() throws Exception {
94         SourceRegistrationRequest request = createSourceRegistrationRequest();
95         AdServicesOutcomeReceiverForTests callback = new AdServicesOutcomeReceiverForTests();
96 
97         mMeasurementManager.registerSource(request, CALLBACK_EXECUTOR, callback);
98 
99         callback.assertFailure(AdServicesException.class);
100     }
101 
102     @Test
testRegisterWebSource_onR_invokesCallbackOnError()103     public void testRegisterWebSource_onR_invokesCallbackOnError() throws Exception {
104         AdServicesOutcomeReceiverForTests callback = new AdServicesOutcomeReceiverForTests();
105 
106         WebSourceParams webSourceParams =
107                 new WebSourceParams.Builder(SOURCE_REGISTRATION_URI)
108                         .setDebugKeyAllowed(false)
109                         .build();
110         WebSourceRegistrationRequest webSourceRegistrationRequest =
111                 new WebSourceRegistrationRequest.Builder(
112                                 Collections.singletonList(webSourceParams), SOURCE_REGISTRATION_URI)
113                         .setInputEvent(null)
114                         .setAppDestination(OS_DESTINATION)
115                         .setWebDestination(WEB_DESTINATION)
116                         .setVerifiedDestination(null)
117                         .build();
118 
119         mMeasurementManager.registerWebSource(
120                 webSourceRegistrationRequest, CALLBACK_EXECUTOR, callback);
121 
122         callback.assertFailure(AdServicesException.class);
123     }
124 
125     @Test
testRegisterWebTrigger_onR_invokesCallbackOnError()126     public void testRegisterWebTrigger_onR_invokesCallbackOnError() throws Exception {
127         AdServicesOutcomeReceiverForTests callback = new AdServicesOutcomeReceiverForTests();
128 
129         WebTriggerParams webTriggerParams =
130                 new WebTriggerParams.Builder(TRIGGER_REGISTRATION_URI).build();
131         WebTriggerRegistrationRequest webTriggerRegistrationRequest =
132                 new WebTriggerRegistrationRequest.Builder(
133                                 Collections.singletonList(webTriggerParams), DESTINATION)
134                         .build();
135 
136         mMeasurementManager.registerWebTrigger(
137                 webTriggerRegistrationRequest, CALLBACK_EXECUTOR, callback);
138 
139         callback.assertFailure(AdServicesException.class);
140     }
141 
142     @Test
testRegisterTrigger_onR_invokesCallbackOnError()143     public void testRegisterTrigger_onR_invokesCallbackOnError() throws Exception {
144         AdServicesOutcomeReceiverForTests callback = new AdServicesOutcomeReceiverForTests();
145 
146         mMeasurementManager.registerTrigger(
147                 Uri.parse("https://registration-trigger"), CALLBACK_EXECUTOR, callback);
148 
149         callback.assertFailure(AdServicesException.class);
150     }
151 
152     @Test
testDeleteRegistrations_onR_invokesCallbackOnError()153     public void testDeleteRegistrations_onR_invokesCallbackOnError() throws Exception {
154         DeletionRequest request = new DeletionRequest.Builder().build();
155         AdServicesOutcomeReceiverForTests callback = new AdServicesOutcomeReceiverForTests();
156 
157         mMeasurementManager.deleteRegistrations(request, CALLBACK_EXECUTOR, callback);
158 
159         callback.assertFailure(AdServicesException.class);
160     }
161 
162     @Test
testGetMeasurementApiStatus_onR_invokesCallbackOnError()163     public void testGetMeasurementApiStatus_onR_invokesCallbackOnError() throws Exception {
164         AdServicesOutcomeReceiverForTests callback = new AdServicesOutcomeReceiverForTests();
165 
166         mMeasurementManager.getMeasurementApiStatus(CALLBACK_EXECUTOR, callback);
167 
168         callback.assertFailure(AdServicesException.class);
169     }
170 
createSourceRegistrationRequest()171     private SourceRegistrationRequest createSourceRegistrationRequest() {
172         return new SourceRegistrationRequest.Builder(
173                         Collections.singletonList(SOURCE_REGISTRATION_URI))
174                 .setInputEvent(INPUT_EVENT)
175                 .build();
176     }
177 }
178