jackqdyulei | 1369bf6 | 2017-08-09 09:57:05 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.settingslib.graph; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import android.content.Context; |
| 22 | import android.graphics.drawable.BitmapDrawable; |
| 23 | import android.graphics.drawable.VectorDrawable; |
| 24 | |
| 25 | import com.android.settingslib.R; |
| 26 | import com.android.settingslib.SettingLibRobolectricTestRunner; |
| 27 | import com.android.settingslib.TestConfig; |
| 28 | import com.android.settingslib.testutils.shadow.SettingsLibShadowResources; |
| 29 | |
| 30 | import org.junit.Before; |
| 31 | import org.junit.Test; |
| 32 | import org.junit.runner.RunWith; |
| 33 | import org.robolectric.RuntimeEnvironment; |
| 34 | import org.robolectric.annotation.Config; |
| 35 | |
| 36 | @RunWith(SettingLibRobolectricTestRunner.class) |
| 37 | @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, |
| 38 | shadows = SettingsLibShadowResources.class) |
| 39 | public class BluetoothDeviceLayerDrawableTest { |
| 40 | private static final int RES_ID = R.drawable.ic_bt_cellphone; |
| 41 | private static final int BATTERY_LEVEL = 15; |
jackqdyulei | 98b7fdb | 2017-09-06 16:38:23 -0700 | [diff] [blame] | 42 | private static final float BATTERY_ICON_SCALE = 0.75f; |
| 43 | private static final int BATTERY_ICON_PADDING_TOP = 6; |
jackqdyulei | 1369bf6 | 2017-08-09 09:57:05 -0700 | [diff] [blame] | 44 | private static final float TOLERANCE = 0.001f; |
| 45 | |
| 46 | private Context mContext; |
| 47 | |
| 48 | @Before |
| 49 | public void setUp() { |
| 50 | mContext = RuntimeEnvironment.application; |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | public void testCreateLayerDrawable_configCorrect() { |
| 55 | BluetoothDeviceLayerDrawable drawable = BluetoothDeviceLayerDrawable.createLayerDrawable( |
| 56 | mContext, RES_ID, BATTERY_LEVEL); |
| 57 | |
| 58 | assertThat(drawable.getDrawable(0)).isInstanceOf(VectorDrawable.class); |
jackqdyulei | 98b7fdb | 2017-09-06 16:38:23 -0700 | [diff] [blame] | 59 | assertThat(drawable.getDrawable(1)).isInstanceOf( |
| 60 | BluetoothDeviceLayerDrawable.BatteryMeterDrawable.class); |
| 61 | assertThat(drawable.getLayerInsetStart(1)).isEqualTo( |
| 62 | drawable.getDrawable(0).getIntrinsicWidth()); |
| 63 | assertThat(drawable.getLayerInsetTop(1)).isEqualTo(0); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void testCreateLayerDrawable_withIconScale_configCorrect() { |
| 68 | BluetoothDeviceLayerDrawable drawable = BluetoothDeviceLayerDrawable.createLayerDrawable( |
| 69 | mContext, RES_ID, BATTERY_LEVEL, BATTERY_ICON_SCALE); |
| 70 | |
| 71 | assertThat(drawable.getDrawable(0)).isInstanceOf(VectorDrawable.class); |
| 72 | assertThat(drawable.getDrawable(1)).isInstanceOf( |
| 73 | BluetoothDeviceLayerDrawable.BatteryMeterDrawable.class); |
| 74 | assertThat(drawable.getLayerInsetStart(1)).isEqualTo( |
| 75 | drawable.getDrawable(0).getIntrinsicWidth()); |
| 76 | assertThat(drawable.getLayerInsetTop(1)).isEqualTo(BATTERY_ICON_PADDING_TOP); |
jackqdyulei | 1369bf6 | 2017-08-09 09:57:05 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Test |
| 80 | public void testBatteryMeterDrawable_configCorrect() { |
| 81 | BluetoothDeviceLayerDrawable.BatteryMeterDrawable batteryDrawable = |
| 82 | new BluetoothDeviceLayerDrawable.BatteryMeterDrawable(mContext, |
| 83 | R.color.meter_background_color, BATTERY_LEVEL); |
| 84 | |
jackqdyulei | 98b7fdb | 2017-09-06 16:38:23 -0700 | [diff] [blame] | 85 | assertThat(batteryDrawable.getAspectRatio()).isWithin(TOLERANCE).of(0.35f); |
jackqdyulei | 1369bf6 | 2017-08-09 09:57:05 -0700 | [diff] [blame] | 86 | assertThat(batteryDrawable.getRadiusRatio()).isWithin(TOLERANCE).of(0f); |
| 87 | assertThat(batteryDrawable.getBatteryLevel()).isEqualTo(BATTERY_LEVEL); |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void testConstantState_returnTwinBluetoothLayerDrawable() { |
| 92 | BluetoothDeviceLayerDrawable drawable = BluetoothDeviceLayerDrawable.createLayerDrawable( |
| 93 | mContext, RES_ID, BATTERY_LEVEL); |
| 94 | |
| 95 | BluetoothDeviceLayerDrawable twinDrawable = |
| 96 | (BluetoothDeviceLayerDrawable) drawable.getConstantState().newDrawable(); |
| 97 | |
| 98 | assertThat(twinDrawable.getDrawable(0)).isEqualTo(drawable.getDrawable(0)); |
| 99 | assertThat(twinDrawable.getDrawable(1)).isEqualTo(drawable.getDrawable(1)); |
| 100 | assertThat(twinDrawable.getLayerInsetTop(1)).isEqualTo( |
| 101 | drawable.getLayerInsetTop(1)); |
| 102 | } |
jackqdyulei | b4f8a0b | 2017-09-21 15:25:15 -0700 | [diff] [blame] | 103 | |
| 104 | @Test |
| 105 | public void testCreateLayerDrawable_bluetoothDrawable_hasCorrectFrameColor() { |
| 106 | BluetoothDeviceLayerDrawable drawable = BluetoothDeviceLayerDrawable.createLayerDrawable( |
| 107 | mContext, RES_ID, BATTERY_LEVEL); |
| 108 | BluetoothDeviceLayerDrawable.BatteryMeterDrawable batteryMeterDrawable = |
| 109 | (BluetoothDeviceLayerDrawable.BatteryMeterDrawable) drawable.getDrawable(1); |
| 110 | |
| 111 | assertThat(batteryMeterDrawable.mFrameColor).isEqualTo( |
| 112 | mContext.getColor(R.color.meter_background_color)); |
| 113 | } |
jackqdyulei | 1369bf6 | 2017-08-09 09:57:05 -0700 | [diff] [blame] | 114 | } |