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