blob: adec40267b970eb8625065aa692a53c8fc20e28e [file] [log] [blame]
jackqdyulei1369bf62017-08-09 09:57:05 -07001/*
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
17package com.android.settingslib.graph;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import android.content.Context;
22import android.graphics.drawable.BitmapDrawable;
23import android.graphics.drawable.VectorDrawable;
24
25import com.android.settingslib.R;
26import com.android.settingslib.SettingLibRobolectricTestRunner;
27import com.android.settingslib.TestConfig;
28import com.android.settingslib.testutils.shadow.SettingsLibShadowResources;
29
30import org.junit.Before;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33import org.robolectric.RuntimeEnvironment;
34import org.robolectric.annotation.Config;
35
36@RunWith(SettingLibRobolectricTestRunner.class)
37@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
38 shadows = SettingsLibShadowResources.class)
39public class BluetoothDeviceLayerDrawableTest {
40 private static final int RES_ID = R.drawable.ic_bt_cellphone;
41 private static final int BATTERY_LEVEL = 15;
jackqdyulei98b7fdb2017-09-06 16:38:23 -070042 private static final float BATTERY_ICON_SCALE = 0.75f;
43 private static final int BATTERY_ICON_PADDING_TOP = 6;
jackqdyulei1369bf62017-08-09 09:57:05 -070044 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);
jackqdyulei98b7fdb2017-09-06 16:38:23 -070059 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);
jackqdyulei1369bf62017-08-09 09:57:05 -070077 }
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
jackqdyulei98b7fdb2017-09-06 16:38:23 -070085 assertThat(batteryDrawable.getAspectRatio()).isWithin(TOLERANCE).of(0.35f);
jackqdyulei1369bf62017-08-09 09:57:05 -070086 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 }
jackqdyuleib4f8a0b2017-09-21 15:25:15 -0700103
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 }
jackqdyulei1369bf62017-08-09 09:57:05 -0700114}