blob: 2113d479b9892b3ae95e339174da6bad89e3414f [file] [log] [blame]
jackqdyuleid94a9382017-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;
jackqdyuleid94a9382017-08-09 09:57:05 -070022import android.graphics.drawable.VectorDrawable;
23
24import com.android.settingslib.R;
jackqdyuleid94a9382017-08-09 09:57:05 -070025
26import org.junit.Before;
27import org.junit.Test;
28import org.junit.runner.RunWith;
James Lemieuxec3ad9e2018-11-28 17:49:14 -080029import org.robolectric.RobolectricTestRunner;
jackqdyuleid94a9382017-08-09 09:57:05 -070030import org.robolectric.RuntimeEnvironment;
jackqdyuleid94a9382017-08-09 09:57:05 -070031
James Lemieuxec3ad9e2018-11-28 17:49:14 -080032@RunWith(RobolectricTestRunner.class)
jackqdyuleid94a9382017-08-09 09:57:05 -070033public class BluetoothDeviceLayerDrawableTest {
Amin Shaikhf3a94272019-01-29 18:22:14 -050034 private static final int RES_ID = com.android.internal.R.drawable.ic_phone;
jackqdyuleid94a9382017-08-09 09:57:05 -070035 private static final int BATTERY_LEVEL = 15;
jackqdyulei98b7fdb2017-09-06 16:38:23 -070036 private static final float BATTERY_ICON_SCALE = 0.75f;
37 private static final int BATTERY_ICON_PADDING_TOP = 6;
jackqdyuleid94a9382017-08-09 09:57:05 -070038 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);
jackqdyulei98b7fdb2017-09-06 16:38:23 -070053 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);
jackqdyuleid94a9382017-08-09 09:57:05 -070071 }
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
jackqdyulei98b7fdb2017-09-06 16:38:23 -070079 assertThat(batteryDrawable.getAspectRatio()).isWithin(TOLERANCE).of(0.35f);
jackqdyuleid94a9382017-08-09 09:57:05 -070080 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 Zhangafa36932018-10-11 14:44:48 -070092 assertThat(twinDrawable.getDrawable(0)).isNotNull();
93 assertThat(twinDrawable.getDrawable(1)).isNotNull();
94 assertThat(twinDrawable.getLayerInsetTop(1)).isEqualTo(drawable.getLayerInsetTop(1));
jackqdyuleid94a9382017-08-09 09:57:05 -070095 }
jackqdyuleib4f8a0b2017-09-21 15:25:15 -070096
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 }
jackqdyuleid94a9382017-08-09 09:57:05 -0700107}