jackqdyulei | d94a938 | 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; |
jackqdyulei | d94a938 | 2017-08-09 09:57:05 -0700 | [diff] [blame] | 22 | import android.graphics.drawable.VectorDrawable; |
| 23 | |
| 24 | import com.android.settingslib.R; |
jackqdyulei | d94a938 | 2017-08-09 09:57:05 -0700 | [diff] [blame] | 25 | |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 29 | import org.robolectric.RobolectricTestRunner; |
jackqdyulei | d94a938 | 2017-08-09 09:57:05 -0700 | [diff] [blame] | 30 | import org.robolectric.RuntimeEnvironment; |
jackqdyulei | d94a938 | 2017-08-09 09:57:05 -0700 | [diff] [blame] | 31 | |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 32 | @RunWith(RobolectricTestRunner.class) |
jackqdyulei | d94a938 | 2017-08-09 09:57:05 -0700 | [diff] [blame] | 33 | public class BluetoothDeviceLayerDrawableTest { |
Amin Shaikh | f3a9427 | 2019-01-29 18:22:14 -0500 | [diff] [blame] | 34 | private static final int RES_ID = com.android.internal.R.drawable.ic_phone; |
jackqdyulei | d94a938 | 2017-08-09 09:57:05 -0700 | [diff] [blame] | 35 | private static final int BATTERY_LEVEL = 15; |
jackqdyulei | 98b7fdb | 2017-09-06 16:38:23 -0700 | [diff] [blame] | 36 | private static final float BATTERY_ICON_SCALE = 0.75f; |
| 37 | private static final int BATTERY_ICON_PADDING_TOP = 6; |
jackqdyulei | d94a938 | 2017-08-09 09:57:05 -0700 | [diff] [blame] | 38 | private static final float TOLERANCE = 0.001f; |
| 39 | |
| 40 | private Context mContext; |
| 41 | |
| 42 | @Before |
| 43 | public void setUp() { |
| 44 | mContext = RuntimeEnvironment.application; |
| 45 | } |
| 46 | |
| 47 | @Test |
| 48 | public void testCreateLayerDrawable_configCorrect() { |
| 49 | BluetoothDeviceLayerDrawable drawable = BluetoothDeviceLayerDrawable.createLayerDrawable( |
| 50 | mContext, RES_ID, BATTERY_LEVEL); |
| 51 | |
| 52 | assertThat(drawable.getDrawable(0)).isInstanceOf(VectorDrawable.class); |
jackqdyulei | 98b7fdb | 2017-09-06 16:38:23 -0700 | [diff] [blame] | 53 | assertThat(drawable.getDrawable(1)).isInstanceOf( |
| 54 | BluetoothDeviceLayerDrawable.BatteryMeterDrawable.class); |
| 55 | assertThat(drawable.getLayerInsetStart(1)).isEqualTo( |
| 56 | drawable.getDrawable(0).getIntrinsicWidth()); |
| 57 | assertThat(drawable.getLayerInsetTop(1)).isEqualTo(0); |
| 58 | } |
| 59 | |
| 60 | @Test |
| 61 | public void testCreateLayerDrawable_withIconScale_configCorrect() { |
| 62 | BluetoothDeviceLayerDrawable drawable = BluetoothDeviceLayerDrawable.createLayerDrawable( |
| 63 | mContext, RES_ID, BATTERY_LEVEL, BATTERY_ICON_SCALE); |
| 64 | |
| 65 | assertThat(drawable.getDrawable(0)).isInstanceOf(VectorDrawable.class); |
| 66 | assertThat(drawable.getDrawable(1)).isInstanceOf( |
| 67 | BluetoothDeviceLayerDrawable.BatteryMeterDrawable.class); |
| 68 | assertThat(drawable.getLayerInsetStart(1)).isEqualTo( |
| 69 | drawable.getDrawable(0).getIntrinsicWidth()); |
| 70 | assertThat(drawable.getLayerInsetTop(1)).isEqualTo(BATTERY_ICON_PADDING_TOP); |
jackqdyulei | d94a938 | 2017-08-09 09:57:05 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testBatteryMeterDrawable_configCorrect() { |
| 75 | BluetoothDeviceLayerDrawable.BatteryMeterDrawable batteryDrawable = |
| 76 | new BluetoothDeviceLayerDrawable.BatteryMeterDrawable(mContext, |
| 77 | R.color.meter_background_color, BATTERY_LEVEL); |
| 78 | |
jackqdyulei | 98b7fdb | 2017-09-06 16:38:23 -0700 | [diff] [blame] | 79 | assertThat(batteryDrawable.getAspectRatio()).isWithin(TOLERANCE).of(0.35f); |
jackqdyulei | d94a938 | 2017-08-09 09:57:05 -0700 | [diff] [blame] | 80 | assertThat(batteryDrawable.getRadiusRatio()).isWithin(TOLERANCE).of(0f); |
| 81 | assertThat(batteryDrawable.getBatteryLevel()).isEqualTo(BATTERY_LEVEL); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testConstantState_returnTwinBluetoothLayerDrawable() { |
| 86 | BluetoothDeviceLayerDrawable drawable = BluetoothDeviceLayerDrawable.createLayerDrawable( |
| 87 | mContext, RES_ID, BATTERY_LEVEL); |
| 88 | |
| 89 | BluetoothDeviceLayerDrawable twinDrawable = |
| 90 | (BluetoothDeviceLayerDrawable) drawable.getConstantState().newDrawable(); |
| 91 | |
Fan Zhang | afa3693 | 2018-10-11 14:44:48 -0700 | [diff] [blame] | 92 | assertThat(twinDrawable.getDrawable(0)).isNotNull(); |
| 93 | assertThat(twinDrawable.getDrawable(1)).isNotNull(); |
| 94 | assertThat(twinDrawable.getLayerInsetTop(1)).isEqualTo(drawable.getLayerInsetTop(1)); |
jackqdyulei | d94a938 | 2017-08-09 09:57:05 -0700 | [diff] [blame] | 95 | } |
jackqdyulei | b4f8a0b | 2017-09-21 15:25:15 -0700 | [diff] [blame] | 96 | |
| 97 | @Test |
| 98 | public void testCreateLayerDrawable_bluetoothDrawable_hasCorrectFrameColor() { |
| 99 | BluetoothDeviceLayerDrawable drawable = BluetoothDeviceLayerDrawable.createLayerDrawable( |
| 100 | mContext, RES_ID, BATTERY_LEVEL); |
| 101 | BluetoothDeviceLayerDrawable.BatteryMeterDrawable batteryMeterDrawable = |
| 102 | (BluetoothDeviceLayerDrawable.BatteryMeterDrawable) drawable.getDrawable(1); |
| 103 | |
| 104 | assertThat(batteryMeterDrawable.mFrameColor).isEqualTo( |
| 105 | mContext.getColor(R.color.meter_background_color)); |
| 106 | } |
jackqdyulei | d94a938 | 2017-08-09 09:57:05 -0700 | [diff] [blame] | 107 | } |