1 /* 2 * Copyright 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.bluetooth.pbapclient; 17 18 import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT; 19 import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED; 20 import static android.content.pm.PackageManager.DONT_KILL_APP; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import android.content.ComponentName; 25 import android.content.Context; 26 import android.content.Intent; 27 28 import androidx.test.filters.MediumTest; 29 import androidx.test.platform.app.InstrumentationRegistry; 30 import androidx.test.rule.ServiceTestRule; 31 import androidx.test.runner.AndroidJUnit4; 32 33 import org.junit.After; 34 import org.junit.Before; 35 import org.junit.Rule; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 39 @MediumTest 40 @RunWith(AndroidJUnit4.class) 41 public class AuthenticationServiceTest { 42 43 Context mTargetContext; 44 45 @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule(); 46 47 @Before setUp()48 public void setUp() { 49 mTargetContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 50 enableService(true); 51 } 52 53 @After tearDown()54 public void tearDown() { 55 enableService(false); 56 } 57 58 @Test bind()59 public void bind() throws Exception { 60 Intent intent = new Intent("android.accounts.AccountAuthenticator"); 61 intent.setClass(mTargetContext, AuthenticationService.class); 62 63 assertThat(mServiceRule.bindService(intent)).isNotNull(); 64 } 65 enableService(boolean enable)66 private void enableService(boolean enable) { 67 int enabledState = 68 enable ? COMPONENT_ENABLED_STATE_ENABLED : COMPONENT_ENABLED_STATE_DEFAULT; 69 ComponentName serviceName = new ComponentName(mTargetContext, AuthenticationService.class); 70 mTargetContext 71 .getPackageManager() 72 .setComponentEnabledSetting(serviceName, enabledState, DONT_KILL_APP); 73 } 74 } 75