fix: 首次提交
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settingslib.graph;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class BatteryMeterDrawableBaseTest {
|
||||
private static final int CRITICAL_LEVEL = 5;
|
||||
private static final int PADDING = 5;
|
||||
private static final int HEIGHT = 80;
|
||||
private static final int WIDTH = 40;
|
||||
@Mock
|
||||
private Canvas mCanvas;
|
||||
private Context mContext;
|
||||
private BatteryMeterDrawableBase mBatteryMeterDrawableBase;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mBatteryMeterDrawableBase = spy(new BatteryMeterDrawableBase(mContext, 0 /* frameColor */));
|
||||
ReflectionHelpers.setField(mBatteryMeterDrawableBase, "mCriticalLevel", CRITICAL_LEVEL);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDraw_hasPaddingAndBounds_drawWarningInCorrectPosition() {
|
||||
mBatteryMeterDrawableBase.setPadding(PADDING, PADDING, PADDING, PADDING);
|
||||
mBatteryMeterDrawableBase.setBounds(0, 0, WIDTH + 2 * PADDING, HEIGHT + 2 * PADDING);
|
||||
mBatteryMeterDrawableBase.setBatteryLevel(3);
|
||||
|
||||
mBatteryMeterDrawableBase.draw(mCanvas);
|
||||
|
||||
// WIDTH * 0.5 + PADDING = 25
|
||||
// (HEIGHT + TEXT_HEIGHT) * 0.48 + PADDING = 43.3999998
|
||||
verify(mCanvas).drawText(eq("!"), eq(25f), eq(43.399998f), any(Paint.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDraw_hasPaddingAndBounds_drawBatteryLevelInCorrectPosition() {
|
||||
mBatteryMeterDrawableBase.setPadding(PADDING, PADDING, PADDING, PADDING);
|
||||
mBatteryMeterDrawableBase.setBounds(0, 0, WIDTH + 2 * PADDING, HEIGHT + 2 * PADDING);
|
||||
mBatteryMeterDrawableBase.setBatteryLevel(20);
|
||||
mBatteryMeterDrawableBase.setShowPercent(true);
|
||||
|
||||
mBatteryMeterDrawableBase.draw(mCanvas);
|
||||
|
||||
// WIDTH * 0.5 + PADDING = 25
|
||||
// (HEIGHT + TEXT_HEIGHT) * 0.47 + PADDING = 42.6
|
||||
verify(mCanvas).drawText(eq("20"), eq(25f), eq(42.6f), any(Paint.class));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settingslib.graph;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.VectorDrawable;
|
||||
|
||||
import com.android.settingslib.R;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class BluetoothDeviceLayerDrawableTest {
|
||||
private static final int RES_ID = com.android.internal.R.drawable.ic_phone;
|
||||
private static final int BATTERY_LEVEL = 15;
|
||||
private static final float BATTERY_ICON_SCALE = 0.75f;
|
||||
private static final int BATTERY_ICON_PADDING_TOP = 6;
|
||||
private static final float TOLERANCE = 0.001f;
|
||||
|
||||
private Context mContext;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = RuntimeEnvironment.application;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateLayerDrawable_configCorrect() {
|
||||
BluetoothDeviceLayerDrawable drawable = BluetoothDeviceLayerDrawable.createLayerDrawable(
|
||||
mContext, RES_ID, BATTERY_LEVEL);
|
||||
|
||||
assertThat(drawable.getDrawable(0)).isInstanceOf(VectorDrawable.class);
|
||||
assertThat(drawable.getDrawable(1)).isInstanceOf(
|
||||
BluetoothDeviceLayerDrawable.BatteryMeterDrawable.class);
|
||||
assertThat(drawable.getLayerInsetStart(1)).isEqualTo(
|
||||
drawable.getDrawable(0).getIntrinsicWidth());
|
||||
assertThat(drawable.getLayerInsetTop(1)).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateLayerDrawable_withIconScale_configCorrect() {
|
||||
BluetoothDeviceLayerDrawable drawable = BluetoothDeviceLayerDrawable.createLayerDrawable(
|
||||
mContext, RES_ID, BATTERY_LEVEL, BATTERY_ICON_SCALE);
|
||||
|
||||
assertThat(drawable.getDrawable(0)).isInstanceOf(VectorDrawable.class);
|
||||
assertThat(drawable.getDrawable(1)).isInstanceOf(
|
||||
BluetoothDeviceLayerDrawable.BatteryMeterDrawable.class);
|
||||
assertThat(drawable.getLayerInsetStart(1)).isEqualTo(
|
||||
drawable.getDrawable(0).getIntrinsicWidth());
|
||||
assertThat(drawable.getLayerInsetTop(1)).isEqualTo(BATTERY_ICON_PADDING_TOP);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBatteryMeterDrawable_configCorrect() {
|
||||
BluetoothDeviceLayerDrawable.BatteryMeterDrawable batteryDrawable =
|
||||
new BluetoothDeviceLayerDrawable.BatteryMeterDrawable(mContext,
|
||||
R.color.meter_background_color, BATTERY_LEVEL);
|
||||
|
||||
assertThat(batteryDrawable.getAspectRatio()).isWithin(TOLERANCE).of(0.35f);
|
||||
assertThat(batteryDrawable.getRadiusRatio()).isWithin(TOLERANCE).of(0f);
|
||||
assertThat(batteryDrawable.getBatteryLevel()).isEqualTo(BATTERY_LEVEL);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstantState_returnTwinBluetoothLayerDrawable() {
|
||||
BluetoothDeviceLayerDrawable drawable = BluetoothDeviceLayerDrawable.createLayerDrawable(
|
||||
mContext, RES_ID, BATTERY_LEVEL);
|
||||
|
||||
BluetoothDeviceLayerDrawable twinDrawable =
|
||||
(BluetoothDeviceLayerDrawable) drawable.getConstantState().newDrawable();
|
||||
|
||||
assertThat(twinDrawable.getDrawable(0)).isNotNull();
|
||||
assertThat(twinDrawable.getDrawable(1)).isNotNull();
|
||||
assertThat(twinDrawable.getLayerInsetTop(1)).isEqualTo(drawable.getLayerInsetTop(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateLayerDrawable_bluetoothDrawable_hasCorrectFrameColor() {
|
||||
BluetoothDeviceLayerDrawable drawable = BluetoothDeviceLayerDrawable.createLayerDrawable(
|
||||
mContext, RES_ID, BATTERY_LEVEL);
|
||||
BluetoothDeviceLayerDrawable.BatteryMeterDrawable batteryMeterDrawable =
|
||||
(BluetoothDeviceLayerDrawable.BatteryMeterDrawable) drawable.getDrawable(1);
|
||||
|
||||
assertThat(batteryMeterDrawable.mFrameColor).isEqualTo(
|
||||
mContext.getColor(R.color.meter_background_color));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user