Home | History | Annotate | Download | only in batterytip
      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.settings.fuelgauge.batterytip;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 import static org.mockito.Matchers.any;
     21 import static org.mockito.Mockito.doReturn;
     22 import static org.mockito.Mockito.spy;
     23 
     24 import android.content.Context;
     25 import android.content.Intent;
     26 import android.os.PowerManager;
     27 
     28 import com.android.internal.os.BatteryStatsHelper;
     29 import com.android.settings.fuelgauge.BatteryInfo;
     30 import com.android.settings.fuelgauge.BatteryUtils;
     31 import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
     32 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     33 
     34 import org.junit.Before;
     35 import org.junit.Test;
     36 import org.junit.runner.RunWith;
     37 import org.mockito.Answers;
     38 import org.mockito.Mock;
     39 import org.mockito.MockitoAnnotations;
     40 import org.robolectric.RuntimeEnvironment;
     41 
     42 import java.util.List;
     43 
     44 @RunWith(SettingsRobolectricTestRunner.class)
     45 public class BatteryTipLoaderTest {
     46 
     47     private static final int[] TIP_ORDER = {
     48             BatteryTip.TipType.APP_RESTRICTION,
     49             BatteryTip.TipType.BATTERY_SAVER,
     50             BatteryTip.TipType.HIGH_DEVICE_USAGE,
     51             BatteryTip.TipType.LOW_BATTERY,
     52             BatteryTip.TipType.SUMMARY,
     53             BatteryTip.TipType.SMART_BATTERY_MANAGER};
     54     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     55     private BatteryStatsHelper mBatteryStatsHelper;
     56     @Mock
     57     private PowerManager mPowerManager;
     58     @Mock
     59     private Intent mIntent;
     60     @Mock
     61     private BatteryUtils mBatteryUtils;
     62     @Mock
     63     private BatteryInfo mBatteryInfo;
     64     private Context mContext;
     65     private BatteryTipLoader mBatteryTipLoader;
     66 
     67     @Before
     68     public void setUp() {
     69         MockitoAnnotations.initMocks(this);
     70 
     71         mContext = spy(RuntimeEnvironment.application);
     72         doReturn(mContext).when(mContext).getApplicationContext();
     73         doReturn(mPowerManager).when(mContext).getSystemService(Context.POWER_SERVICE);
     74         doReturn(mIntent).when(mContext).registerReceiver(any(), any());
     75         doReturn(mBatteryInfo).when(mBatteryUtils).getBatteryInfo(any(), any());
     76         mBatteryTipLoader = new BatteryTipLoader(mContext, mBatteryStatsHelper);
     77         mBatteryTipLoader.mBatteryUtils = mBatteryUtils;
     78     }
     79 
     80     @Test
     81     public void testLoadBackground_containsAllTipsWithOrder() {
     82         final List<BatteryTip> batteryTips = mBatteryTipLoader.loadInBackground();
     83 
     84         assertThat(batteryTips.size()).isEqualTo(TIP_ORDER.length);
     85         for (int i = 0, size = batteryTips.size(); i < size; i++) {
     86             assertThat(batteryTips.get(i).getType()).isEqualTo(TIP_ORDER[i]);
     87         }
     88     }
     89 }
     90