• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.google.common.truth.Truth.assertThat;
19 
20 import static org.mockito.ArgumentMatchers.anyInt;
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.ArgumentMatchers.nullable;
23 import static org.mockito.Mockito.doNothing;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 
29 import android.content.Context;
30 import android.os.Bundle;
31 import android.view.Menu;
32 import android.view.MenuInflater;
33 import android.view.MenuItem;
34 
35 import androidx.preference.PreferenceScreen;
36 
37 import com.android.internal.logging.nano.MetricsProto;
38 import com.android.settings.R;
39 import com.android.settings.testutils.FakeFeatureFactory;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Answers;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 import org.robolectric.RobolectricTestRunner;
48 import org.robolectric.RuntimeEnvironment;
49 
50 @RunWith(RobolectricTestRunner.class)
51 public class PowerUsageAdvancedTest {
52     @Mock
53     private PreferenceScreen mPreferenceScreen;
54     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
55     private Menu mMenu;
56     @Mock
57     private MenuInflater mMenuInflater;
58     @Mock
59     private MenuItem mToggleAppsMenu;
60     private Context mContext;
61     private PowerUsageAdvanced mFragment;
62     private FakeFeatureFactory mFeatureFactory;
63 
64     @Before
setUp()65     public void setUp() {
66         MockitoAnnotations.initMocks(this);
67 
68         mContext = RuntimeEnvironment.application;
69         mFeatureFactory = FakeFeatureFactory.setupForTest();
70         when(mToggleAppsMenu.getItemId()).thenReturn(PowerUsageAdvanced.MENU_TOGGLE_APPS);
71 
72         mFragment = spy(new PowerUsageAdvanced());
73         mFragment.onAttach(mContext);
74     }
75 
76     @Test
testSaveInstanceState_showAllAppsRestored()77     public void testSaveInstanceState_showAllAppsRestored() {
78         Bundle bundle = new Bundle();
79         mFragment.mShowAllApps = true;
80         doReturn(mPreferenceScreen).when(mFragment).getPreferenceScreen();
81 
82         mFragment.onSaveInstanceState(bundle);
83         mFragment.restoreSavedInstance(bundle);
84 
85         assertThat(mFragment.mShowAllApps).isTrue();
86     }
87 
88     @Test
testOptionsMenu_menuAppToggle_metricEventInvoked()89     public void testOptionsMenu_menuAppToggle_metricEventInvoked() {
90         mFragment.mShowAllApps = false;
91         doNothing().when(mFragment).restartBatteryStatsLoader(anyInt());
92 
93         mFragment.onOptionsItemSelected(mToggleAppsMenu);
94 
95         verify(mFeatureFactory.metricsFeatureProvider).action(nullable(Context.class),
96                 eq(MetricsProto.MetricsEvent.ACTION_SETTINGS_MENU_BATTERY_APPS_TOGGLE), eq(true));
97     }
98 
99     @Test
testOptionsMenu_toggleAppsEnabled()100     public void testOptionsMenu_toggleAppsEnabled() {
101         when(mFeatureFactory.powerUsageFeatureProvider.isPowerAccountingToggleEnabled())
102                 .thenReturn(true);
103         mFragment.mShowAllApps = false;
104 
105         mFragment.onCreateOptionsMenu(mMenu, mMenuInflater);
106 
107         verify(mMenu).add(Menu.NONE, PowerUsageAdvanced.MENU_TOGGLE_APPS, Menu.NONE,
108                 R.string.show_all_apps);
109     }
110 }
111