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.tests.providers.sdk1;
18 
19 import android.adservices.clients.topics.AdvertisingTopicsClient;
20 import android.adservices.topics.GetTopicsResponse;
21 import android.adservices.topics.Topic;
22 import android.app.sdksandbox.LoadSdkException;
23 import android.app.sdksandbox.SandboxedSdk;
24 import android.app.sdksandbox.SandboxedSdkProvider;
25 import android.content.Context;
26 import android.os.Binder;
27 import android.os.Bundle;
28 import android.util.Log;
29 import android.view.View;
30 
31 import com.google.common.collect.ImmutableSet;
32 
33 import java.util.concurrent.Executor;
34 import java.util.concurrent.Executors;
35 
36 public class Sdk1 extends SandboxedSdkProvider {
37     private static final String TAG = "Sdk1";
38     private static final Executor CALLBACK_EXECUTOR = Executors.newCachedThreadPool();
39 
40     // Override the Epoch Job Period to this value to speed up the epoch computation.
41     private static final long TEST_EPOCH_JOB_PERIOD_MS = 6000;
42 
43     // Expected Taxonomy version and Model version. This should be changed along with corresponding
44     // model and taxonomy change.
45     private static final long TAXONOMY_VERSION = 2L;
46     private static final long MODEL_VERSION = 5L;
47 
48     // Set of classification topics for the Test App. The returned topic should be one of these
49     // Topics.
50     private static final ImmutableSet<Integer> TOPIC_ID_SET =
51             ImmutableSet.of(10175, 10147, 10254, 10333, 10253);
52 
53     @Override
onLoadSdk(Bundle params)54     public SandboxedSdk onLoadSdk(Bundle params) throws LoadSdkException {
55         try {
56             // Make the second call to the Topics API. Since we called the Topics API in the
57             // previous epoch, we should have some returned topic now.
58             GetTopicsResponse response = callTopicsApi();
59             if (response.getTopics().isEmpty()) {
60                 // Trigger the error callback to tell the Test App that we did not receive
61                 // any topic. This will tell the Test App to fail the test.
62                 Log.e(TAG, "Failed. No topics!");
63                 throw new LoadSdkException(new Exception("Failed. No topics!"), new Bundle());
64             } else {
65                 // Verify the returned Topic.
66                 Topic topic = response.getTopics().get(0);
67                 boolean correctResult =
68                         TOPIC_ID_SET.contains(topic.getTopicId())
69                                 && topic.getTaxonomyVersion() == TAXONOMY_VERSION
70                                 && topic.getModelVersion() == MODEL_VERSION;
71                 if (correctResult) {
72                     // Return a response to tell the Test App that the second Topics
73                     // API call got back some topic which is expected. This will tell the Test
74                     // App to pass the test.
75                     Log.i(TAG, "Got correct returned topic: " + topic.getTopicId());
76                     return new SandboxedSdk(new Binder());
77                 } else {
78                     // Throw an exception to tell the test app that we received
79                     // a wrong topic. This will tell the Test App to fail the test.
80                     String errorMessage =
81                             String.format(
82                                     "Got incorrect returned topic: %d, taxonomy version: %d, model"
83                                             + " version: %d",
84                                     topic.getTopicId(),
85                                     topic.getTaxonomyVersion(),
86                                     topic.getModelVersion());
87                     Log.e(TAG, errorMessage);
88                     throw new LoadSdkException(new Exception(errorMessage), new Bundle());
89                 }
90             }
91         } catch (Exception e) {
92             Log.e(TAG, e.getMessage());
93             // Throw an exception to tell the Test App that some errors occurred so
94             // that it will fail the test.
95             throw new LoadSdkException(e, new Bundle());
96         }
97     }
98 
99     @Override
getView(Context windowContext, Bundle params, int width, int height)100     public View getView(Context windowContext, Bundle params, int width, int height) {
101         return null;
102     }
103 
callTopicsApi()104     private GetTopicsResponse callTopicsApi() throws Exception {
105         AdvertisingTopicsClient advertisingTopicsClient =
106                 new AdvertisingTopicsClient.Builder()
107                         .setContext(getContext())
108                         .setExecutor(CALLBACK_EXECUTOR)
109                         .build();
110 
111         return advertisingTopicsClient.getTopics().get();
112     }
113 }
114