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.VectorDrawable;
23 
24 import com.android.settingslib.R;
25 
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.robolectric.RobolectricTestRunner;
30 import org.robolectric.RuntimeEnvironment;
31 
32 @RunWith(RobolectricTestRunner.class)
33 public class BluetoothDeviceLayerDrawableTest {
34     private static final int RES_ID = com.android.internal.R.drawable.ic_phone;
35     private static final int BATTERY_LEVEL = 15;
36     private static final float BATTERY_ICON_SCALE = 0.75f;
37     private static final int BATTERY_ICON_PADDING_TOP = 6;
38     private static final float TOLERANCE = 0.001f;
39 
40     private Context mContext;
41 
42     @Before
setUp()43     public void setUp() {
44         mContext = RuntimeEnvironment.application;
45     }
46 
47     @Test
testCreateLayerDrawable_configCorrect()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);
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
testCreateLayerDrawable_withIconScale_configCorrect()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);
71     }
72 
73     @Test
testBatteryMeterDrawable_configCorrect()74     public void testBatteryMeterDrawable_configCorrect() {
75         BluetoothDeviceLayerDrawable.BatteryMeterDrawable batteryDrawable =
76                 new BluetoothDeviceLayerDrawable.BatteryMeterDrawable(mContext,
77                         R.color.meter_background_color, BATTERY_LEVEL);
78 
79         assertThat(batteryDrawable.getAspectRatio()).isWithin(TOLERANCE).of(0.35f);
80         assertThat(batteryDrawable.getRadiusRatio()).isWithin(TOLERANCE).of(0f);
81         assertThat(batteryDrawable.getBatteryLevel()).isEqualTo(BATTERY_LEVEL);
82     }
83 
84     @Test
testConstantState_returnTwinBluetoothLayerDrawable()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 
92         assertThat(twinDrawable.getDrawable(0)).isNotNull();
93         assertThat(twinDrawable.getDrawable(1)).isNotNull();
94         assertThat(twinDrawable.getLayerInsetTop(1)).isEqualTo(drawable.getLayerInsetTop(1));
95     }
96 
97     @Test
testCreateLayerDrawable_bluetoothDrawable_hasCorrectFrameColor()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     }
107 }
108