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 android.profiling.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.fail;
22 
23 import android.os.Flags;
24 import android.os.IBinder;
25 import android.os.ProfilingFrameworkInitializer;
26 import android.os.ProfilingServiceManager;
27 import android.os.ProfilingServiceManager.ServiceNotFoundException;
28 import android.os.ProfilingServiceManager.ServiceRegisterer;
29 import android.platform.test.annotations.RequiresFlagsEnabled;
30 
31 import androidx.test.runner.AndroidJUnit4;
32 
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 /**
37  *
38  * Tests defined in this class are expected to test the implementation of the
39  * ProfilingServiceManager APIs.
40  *
41  */
42 @RunWith(AndroidJUnit4.class)
43 public class ProfilingServiceManagerTest {
44 
45     /**
46      * Tests that the ProfilingServiceManager.getProfilingServiceRegisterer()
47      * and the ServiceRegisterer's .get and .getOrThrow methods return results.
48      */
49     @Test
50     @RequiresFlagsEnabled(Flags.FLAG_TELEMETRY_APIS_FRAMEWORK_INITIALIZATION)
testProfilingServiceRegisterer()51     public void testProfilingServiceRegisterer() {
52         ProfilingServiceManager serviceManager =
53                 ProfilingFrameworkInitializer.getProfilingServiceManager();
54 
55         ServiceRegisterer serviceRegisterer =
56                 serviceManager.getProfilingServiceRegisterer();
57         assertNotNull(serviceRegisterer);
58 
59         IBinder serviceBinder = serviceRegisterer.get();
60         assertNotNull(serviceBinder);
61 
62         serviceBinder = null;
63         try {
64             serviceBinder = serviceRegisterer.getOrThrow();
65             assertNotNull(serviceBinder);
66         } catch (ServiceNotFoundException exception) {
67             fail("ServiceNotFoundException should not be thrown "
68                     + "since the service should exist in this test");
69         }
70     }
71 
72     /** Tests that the ProfilingServiceManagerServiceNotFoundException works as expected. */
73     @Test
74     @RequiresFlagsEnabled(Flags.FLAG_TELEMETRY_APIS_FRAMEWORK_INITIALIZATION)
testProfilingServiceNotFoundException()75     public void testProfilingServiceNotFoundException() {
76         String name = "test-profiling-service";
77         ServiceNotFoundException testException =
78                 new ServiceNotFoundException(name);
79 
80         try {
81             throw testException;
82         } catch (ServiceNotFoundException exception) {
83             assertEquals("No service published for: " + name,
84                     exception.getMessage());
85         }
86     }
87 }
88