1 /* 2 * Copyright (C) 2017 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.settings.bluetooth; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import android.bluetooth.BluetoothDevice; 21 import android.content.Context; 22 import android.graphics.drawable.Drawable; 23 24 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 25 import com.android.settings.R; 26 import com.android.settings.testutils.SettingsRobolectricTestRunner; 27 import com.android.settings.TestConfig; 28 import com.android.settings.core.instrumentation.MetricsFeatureProvider; 29 import com.android.settings.testutils.FakeFeatureFactory; 30 import com.android.settings.testutils.shadow.SettingsShadowResources; 31 import com.android.settingslib.bluetooth.LocalBluetoothManager; 32 import com.android.settingslib.graph.BluetoothDeviceLayerDrawable; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Answers; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.RuntimeEnvironment; 41 import org.robolectric.annotation.Config; 42 43 import static org.mockito.Matchers.anyInt; 44 import static org.mockito.Matchers.anyString; 45 import static org.mockito.Matchers.eq; 46 import static org.mockito.Mockito.mock; 47 import static org.mockito.Mockito.verify; 48 import static org.mockito.Mockito.when; 49 50 @RunWith(SettingsRobolectricTestRunner.class) 51 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, 52 shadows = SettingsShadowResources.class) 53 public class UtilsTest { 54 55 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 56 private Context mContext; 57 @Mock 58 private LocalBluetoothManager mLocalBluetoothManager; 59 60 private FakeFeatureFactory mFakeFeatureFactory; 61 private MetricsFeatureProvider mMetricsFeatureProvider; 62 63 @Before 64 public void setUp() { 65 MockitoAnnotations.initMocks(this); 66 FakeFeatureFactory.setupForTest(mContext); 67 mFakeFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); 68 mMetricsFeatureProvider = mFakeFeatureFactory.getMetricsFeatureProvider(); 69 } 70 71 @Test 72 public void testShowConnectingError_shouldLogBluetoothConnectError() { 73 when(mContext.getString(anyInt(), anyString())).thenReturn("testMessage"); 74 Utils.showConnectingError(mContext, "testName", mock(LocalBluetoothManager.class)); 75 76 verify(mMetricsFeatureProvider).visible(eq(mContext), anyInt(), 77 eq(MetricsEvent.ACTION_SETTINGS_BLUETOOTH_CONNECT_ERROR)); 78 } 79 80 @Test 81 public void testGetBluetoothDrawable_noBatteryLevel_returnSimpleDrawable() { 82 final Drawable drawable = Utils.getBluetoothDrawable(RuntimeEnvironment.application, 83 R.drawable.ic_bt_laptop, BluetoothDevice.BATTERY_LEVEL_UNKNOWN, 1 /* iconScale */); 84 85 assertThat(drawable).isNotInstanceOf(BluetoothDeviceLayerDrawable.class); 86 } 87 88 @Test 89 public void testGetBluetoothDrawable_hasBatteryLevel_returnLayerDrawable() { 90 final Drawable drawable = Utils.getBluetoothDrawable(RuntimeEnvironment.application, 91 R.drawable.ic_bt_laptop, 10 /* batteryLevel */, 1 /* iconScale */); 92 93 assertThat(drawable).isInstanceOf(BluetoothDeviceLayerDrawable.class); 94 } 95 } 96