Home | History | Annotate | Download | only in systemui
      1 /*
      2  * Copyright (C) 2016 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.systemui;
     18 
     19 import com.android.settingslib.graph.BatteryMeterDrawableBase;
     20 
     21 import static org.mockito.Mockito.any;
     22 import static org.mockito.Mockito.anyFloat;
     23 import static org.mockito.Mockito.anyString;
     24 import static org.mockito.Mockito.atLeastOnce;
     25 import static org.mockito.Mockito.eq;
     26 import static org.mockito.Mockito.mock;
     27 import static org.mockito.Mockito.never;
     28 import static org.mockito.Mockito.times;
     29 import static org.mockito.Mockito.verify;
     30 
     31 import android.content.res.Resources;
     32 import android.graphics.Canvas;
     33 import android.support.test.runner.AndroidJUnit4;
     34 import android.test.suitebuilder.annotation.SmallTest;
     35 
     36 import org.junit.Before;
     37 import org.junit.Test;
     38 import org.junit.runner.RunWith;
     39 
     40 @SmallTest
     41 @RunWith(AndroidJUnit4.class)
     42 public class BatteryMeterDrawableTest extends SysuiTestCase {
     43 
     44     private Resources mResources;
     45     private BatteryMeterDrawableBase mBatteryMeter;
     46 
     47     @Before
     48     public void setUp() throws Exception {
     49         mResources = mContext.getResources();
     50         mBatteryMeter = new BatteryMeterDrawableBase(mContext, 0);
     51     }
     52 
     53     @Test
     54     public void testDrawImageButNoTextIfPluggedIn() {
     55         mBatteryMeter.setBatteryLevel(0);
     56         mBatteryMeter.setCharging(true);
     57         final Canvas canvas = mock(Canvas.class);
     58         mBatteryMeter.draw(canvas);
     59         verify(canvas, atLeastOnce()).drawPath(any(), any());
     60         verify(canvas, never()).drawText(anyString(), anyFloat(), anyFloat(), any());
     61     }
     62 
     63     @Test
     64     public void testDrawTextIfNotPluggedIn() {
     65         mBatteryMeter.setBatteryLevel(0);
     66         mBatteryMeter.setCharging(false);
     67         final Canvas canvas = mock(Canvas.class);
     68         mBatteryMeter.draw(canvas);
     69         verify(canvas, times(1)).drawText(anyString(), anyFloat(), anyFloat(), any());
     70     }
     71 
     72     @Test
     73     public void testDrawNoTextIfPowerSaveEnabled() {
     74         mBatteryMeter.setBatteryLevel(0);
     75         mBatteryMeter.setCharging(false);
     76         mBatteryMeter.setPowerSave(true);
     77         final Canvas canvas = mock(Canvas.class);
     78         mBatteryMeter.draw(canvas);
     79         verify(canvas, never()).drawText(anyString(), anyFloat(), anyFloat(), any());
     80     }
     81 
     82     @Test
     83     public void testDrawTextWarningAtCriticalLevel() {
     84         int criticalLevel = mResources.getInteger(
     85                 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
     86         mBatteryMeter.setBatteryLevel(criticalLevel);
     87         mBatteryMeter.setCharging(false);
     88         final Canvas canvas = mock(Canvas.class);
     89         mBatteryMeter.draw(canvas);
     90         String warningString = mResources.getString(R.string.battery_meter_very_low_overlay_symbol);
     91         verify(canvas, times(1)).drawText(eq(warningString), anyFloat(), anyFloat(), any());
     92     }
     93 
     94     @Test
     95     public void testDrawTextNoWarningAboveCriticalLevel() {
     96         int criticalLevel = mResources.getInteger(
     97                 com.android.internal.R.integer.config_criticalBatteryWarningLevel);
     98         mBatteryMeter.setBatteryLevel(criticalLevel + 1);
     99         mBatteryMeter.setCharging(false);
    100         final Canvas canvas = mock(Canvas.class);
    101         mBatteryMeter.draw(canvas);
    102         String warningString = mResources.getString(R.string.battery_meter_very_low_overlay_symbol);
    103         verify(canvas, never()).drawText(eq(warningString), anyFloat(), anyFloat(), any());
    104     }
    105 }
    106