Home | History | Annotate | Download | only in fuelgauge
      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 package com.android.settings.fuelgauge;
     17 
     18 import static org.mockito.Matchers.any;
     19 import static org.mockito.Matchers.anyInt;
     20 import static org.mockito.Mockito.doReturn;
     21 import static org.mockito.Mockito.never;
     22 import static org.mockito.Mockito.spy;
     23 import static org.mockito.Mockito.verify;
     24 
     25 import android.app.LoaderManager;
     26 import android.content.Context;
     27 import android.os.Bundle;
     28 
     29 import com.android.internal.os.BatteryStatsHelper;
     30 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     31 import com.android.settings.testutils.shadow.ShadowDashboardFragment;
     32 import com.android.settingslib.core.AbstractPreferenceController;
     33 
     34 import org.junit.Before;
     35 import org.junit.Test;
     36 import org.junit.runner.RunWith;
     37 import org.mockito.Mock;
     38 import org.mockito.MockitoAnnotations;
     39 import org.robolectric.annotation.Config;
     40 
     41 import java.util.List;
     42 
     43 @RunWith(SettingsRobolectricTestRunner.class)
     44 @Config(shadows = ShadowDashboardFragment.class)
     45 public class PowerUsageBaseTest {
     46 
     47     @Mock
     48     private BatteryStatsHelper mBatteryStatsHelper;
     49     @Mock
     50     private LoaderManager mLoaderManager;
     51     private TestFragment mFragment;
     52 
     53     @Before
     54     public void setUp() {
     55         MockitoAnnotations.initMocks(this);
     56 
     57         mFragment = spy(new TestFragment());
     58         mFragment.setBatteryStatsHelper(mBatteryStatsHelper);
     59         doReturn(mLoaderManager).when(mFragment).getLoaderManager();
     60     }
     61 
     62     @Test
     63     public void testOnCreate_batteryStatsLoaderNotInvoked() {
     64         mFragment.onCreate(null);
     65 
     66         verify(mLoaderManager, never()).initLoader(anyInt(), any(Bundle.class), any());
     67     }
     68 
     69     public static class TestFragment extends PowerUsageBase {
     70 
     71         @Override
     72         public int getMetricsCategory() {
     73             return 0;
     74         }
     75 
     76         @Override
     77         protected void refreshUi(int refreshType) {
     78             // Do nothing
     79         }
     80 
     81         @Override
     82         protected String getLogTag() {
     83             return null;
     84         }
     85 
     86         @Override
     87         protected int getPreferenceScreenResId() {
     88             return 0;
     89         }
     90 
     91         @Override
     92         protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
     93             return null;
     94         }
     95 
     96         private void setBatteryStatsHelper(BatteryStatsHelper batteryStatsHelper) {
     97             mStatsHelper = batteryStatsHelper;
     98         }
     99     }
    100 }
    101