• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.server;
18 
19 import static android.service.watchdog.ExplicitHealthCheckService.EXTRA_HEALTH_CHECK_PASSED_PACKAGE;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Mockito.spy;
24 
25 import android.content.Intent;
26 import android.os.IBinder;
27 import android.os.RemoteCallback;
28 import android.service.watchdog.ExplicitHealthCheckService;
29 import android.service.watchdog.IExplicitHealthCheckService;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 
34 import java.util.concurrent.CountDownLatch;
35 
36 public class ExplicitHealthCheckServiceTest {
37 
38     private ExplicitHealthCheckService mExplicitHealthCheckService;
39     private static final String PACKAGE_NAME = "com.test.package";
40 
41     @Before
setup()42     public void setup() throws Exception {
43         mExplicitHealthCheckService = spy(ExplicitHealthCheckService.class);
44     }
45 
46     /**
47      * Test to verify that the correct information is sent in the callback when a package has
48      * passed an explicit health check.
49      */
50     @Test
testNotifyHealthCheckPassed()51     public void testNotifyHealthCheckPassed() throws Exception {
52         IBinder binder = mExplicitHealthCheckService.onBind(new Intent());
53         CountDownLatch countDownLatch = new CountDownLatch(1);
54         RemoteCallback callback = new RemoteCallback(result -> {
55             assertThat(result.getString(EXTRA_HEALTH_CHECK_PASSED_PACKAGE))
56                     .isEqualTo(PACKAGE_NAME);
57             countDownLatch.countDown();
58         });
59         IExplicitHealthCheckService.Stub.asInterface(binder).setCallback(callback);
60         mExplicitHealthCheckService.notifyHealthCheckPassed(PACKAGE_NAME);
61         countDownLatch.await();
62     }
63 }
64