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 package com.android.adservices.tests.cts.topics.testapp1;
17 
18 import android.adservices.clients.topics.AdvertisingTopicsClient;
19 import android.adservices.topics.GetTopicsResponse;
20 import android.adservices.topics.Topic;
21 import android.annotation.NonNull;
22 import android.app.Activity;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.os.Bundle;
26 import android.util.Log;
27 
28 import java.util.List;
29 import java.util.concurrent.Executor;
30 import java.util.concurrent.Executors;
31 
32 /**
33  * This test app is used for the CTS test of App Update flow in Topics API. It calls Topics API and
34  * send the response to CTS test suite app by explicit broadcast.
35  */
36 public class MainActivity extends Activity {
37     @SuppressWarnings("unused")
38     private static final String TAG = "testapp1";
39 
40     private static final String LOG_TAG = "adservices";
41     private static final Executor CALLBACK_EXECUTOR = Executors.newCachedThreadPool();
42 
43     // Broadcast sent from test apps to test suite to pass GetTopicsResponse. Use two types of
44     // broadcast to make the result more explicit.
45     private static final String TOPIC_RESPONSE_BROADCAST_KEY = "topicIds";
46     // This test app will send this broadcast to the CTS test suite if it receives non-empty topics.
47     private static final String NON_EMPTY_TOPIC_RESPONSE_BROADCAST =
48             "com.android.adservices.tests.cts.topics.NON_EMPTY_TOPIC_RESPONSE";
49     // This test app will send this broadcast to the CTS test suite if it receives empty topics.
50     private static final String EMPTY_TOPIC_RESPONSE_BROADCAST =
51             "com.android.adservices.tests.cts.topics.EMPTY_TOPIC_RESPONSE";
52     private static final String SDK_NAME = "sdk";
53 
54     private Context mContext;
55 
56     @Override
onCreate(Bundle savedInstanceState)57     protected void onCreate(Bundle savedInstanceState) {
58         super.onCreate(savedInstanceState);
59         mContext = getApplicationContext();
60 
61         try {
62             // Call Topics API via sdk.
63             GetTopicsResponse getTopicsResponse = callTopicsApi(SDK_NAME);
64             sendGetTopicsResponseBroadcast(getTopicsResponse);
65         } catch (Exception e) {
66             Log.e(LOG_TAG, "getTopics() call failed, please check: " + e);
67         }
68 
69         // Kill this test app
70         finish();
71     }
72 
73     // Send Broadcast to main test suite to pass the GetTopicsResponse
74     @SuppressWarnings("NewApi")
sendGetTopicsResponseBroadcast(@onNull GetTopicsResponse getTopicsResponse)75     private void sendGetTopicsResponseBroadcast(@NonNull GetTopicsResponse getTopicsResponse) {
76         List<Topic> topics = getTopicsResponse.getTopics();
77         Log.v(LOG_TAG, "Test app gets topics: " + topics);
78 
79         // Put returned topic ids into extra to pass them to CTS test suite app
80         Intent sendGetTopicsResponseBroadcast = new Intent();
81         if (topics.isEmpty()) {
82             sendGetTopicsResponseBroadcast.setAction(EMPTY_TOPIC_RESPONSE_BROADCAST);
83         } else {
84             sendGetTopicsResponseBroadcast.setAction(NON_EMPTY_TOPIC_RESPONSE_BROADCAST);
85             int[] topicIds = topics.stream().mapToInt(Topic::getTopicId).toArray();
86             sendGetTopicsResponseBroadcast.putExtra(TOPIC_RESPONSE_BROADCAST_KEY, topicIds);
87         }
88 
89         mContext.sendBroadcast(sendGetTopicsResponseBroadcast);
90     }
91 
callTopicsApi(@onNull String sdk)92     private GetTopicsResponse callTopicsApi(@NonNull String sdk) throws Exception {
93         AdvertisingTopicsClient.Builder builder =
94                 new AdvertisingTopicsClient.Builder()
95                         .setContext(mContext)
96                         .setExecutor(CALLBACK_EXECUTOR);
97         if (!sdk.isEmpty()) {
98             builder.setSdkName(sdk);
99         }
100 
101         return builder.build().getTopics().get();
102     }
103 }
104