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