diff --git a/android/app/src/com/android/bluetooth/pbapclient/PbapClientConnectionHandler.java b/android/app/src/com/android/bluetooth/pbapclient/PbapClientConnectionHandler.java index 5032bcb7218d3158dd5c971832d51710a3218b05..dcfd9de194c6e2fea2249742a715aafadfce96d7 100644 --- a/android/app/src/com/android/bluetooth/pbapclient/PbapClientConnectionHandler.java +++ b/android/app/src/com/android/bluetooth/pbapclient/PbapClientConnectionHandler.java @@ -247,7 +247,7 @@ class PbapClientConnectionHandler extends Handler { break; case MSG_DOWNLOAD: - mAccountCreated = addAccount(mAccount); + mAccountCreated = addAccount(); if (!mAccountCreated) { Log.e(TAG, "Account creation failed."); return; @@ -456,8 +456,8 @@ class PbapClientConnectionHandler extends Handler { } @VisibleForTesting - boolean addAccount(Account account) { - if (mAccountManager.addAccountExplicitly(account, null, null)) { + boolean addAccount() { + if (mAccountManager.addAccountExplicitly(mAccount, null, null)) { if (DBG) { Log.d(TAG, "Added account " + mAccount); } diff --git a/android/app/tests/unit/src/com/android/bluetooth/pbapclient/AuthenticationServiceTest.java b/android/app/tests/unit/src/com/android/bluetooth/pbapclient/AuthenticationServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..e07a9c8abb5ef36a39682c1b5fc0a47c4ea0818a --- /dev/null +++ b/android/app/tests/unit/src/com/android/bluetooth/pbapclient/AuthenticationServiceTest.java @@ -0,0 +1,75 @@ +/* + * Copyright 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.bluetooth.pbapclient; + +import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT; +import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED; +import static android.content.pm.PackageManager.DONT_KILL_APP; + +import static com.google.common.truth.Truth.assertThat; + +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; + +import androidx.test.filters.MediumTest; +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.rule.ServiceTestRule; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@MediumTest +@RunWith(AndroidJUnit4.class) +public class AuthenticationServiceTest { + + Context mTargetContext; + + @Rule + public final ServiceTestRule mServiceRule = new ServiceTestRule(); + + @Before + public void setUp() { + mTargetContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + enableService(true); + } + + @After + public void tearDown() { + enableService(false); + } + + @Test + public void bind() throws Exception { + Intent intent = new Intent("android.accounts.AccountAuthenticator"); + intent.setClass(mTargetContext, AuthenticationService.class); + + assertThat(mServiceRule.bindService(intent)).isNotNull(); + } + + private void enableService(boolean enable) { + int enabledState = enable ? COMPONENT_ENABLED_STATE_ENABLED + : COMPONENT_ENABLED_STATE_DEFAULT; + ComponentName serviceName = new ComponentName( + mTargetContext, AuthenticationService.class); + mTargetContext.getPackageManager().setComponentEnabledSetting( + serviceName, enabledState, DONT_KILL_APP); + } +} diff --git a/android/app/tests/unit/src/com/android/bluetooth/pbapclient/PbapClientConnectionHandlerTest.java b/android/app/tests/unit/src/com/android/bluetooth/pbapclient/PbapClientConnectionHandlerTest.java index 9ef08a6d920fe691a0027fba6d2a050c321bac8d..1b4f5aa059583f313411a8738d6ff6cba9f284b6 100644 --- a/android/app/tests/unit/src/com/android/bluetooth/pbapclient/PbapClientConnectionHandlerTest.java +++ b/android/app/tests/unit/src/com/android/bluetooth/pbapclient/PbapClientConnectionHandlerTest.java @@ -34,7 +34,6 @@ import android.content.Context; import android.content.ContextWrapper; import android.os.HandlerThread; import android.os.Looper; -import android.util.Log; import androidx.test.filters.SmallTest; import androidx.test.platform.app.InstrumentationRegistry; @@ -54,8 +53,6 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import java.util.HashMap; - @SmallTest @RunWith(AndroidJUnit4.class) public class PbapClientConnectionHandlerTest {