• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings;
18 
19 import android.app.Activity;
20 import android.app.ActivityManager;
21 import android.app.FragmentManager;
22 import android.app.FragmentTransaction;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.graphics.Bitmap;
26 
27 import android.os.Bundle;
28 import android.view.Menu;
29 import com.android.settings.testutils.FakeFeatureFactory;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Answers;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.RuntimeEnvironment;
37 import org.robolectric.annotation.Config;
38 import org.robolectric.util.ReflectionHelpers;
39 
40 import static com.google.common.truth.Truth.assertThat;
41 import static org.mockito.Matchers.any;
42 import static org.mockito.Matchers.anyInt;
43 import static org.mockito.Mockito.doReturn;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.spy;
46 import static org.mockito.Mockito.verify;
47 import static org.mockito.Mockito.when;
48 
49 @RunWith(SettingsRobolectricTestRunner.class)
50 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
51 public class SettingsActivityTest {
52 
53     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
54     private Context mContext;
55 
56     @Mock
57     private FragmentManager mFragmentManager;
58     @Mock
59     private ActivityManager.TaskDescription mTaskDescription;
60     @Mock
61     private Bitmap mBitmap;
62     private SettingsActivity mActivity;
63 
64     private FakeFeatureFactory mFeatureFactory;
65 
66     @Before
setUp()67     public void setUp() {
68         MockitoAnnotations.initMocks(this);
69 
70         FakeFeatureFactory.setupForTest(mContext);
71         mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
72 
73         mActivity = spy(new SettingsActivity());
74         doReturn(mBitmap).when(mActivity).getBitmapFromXmlResource(anyInt());
75     }
76 
77     @Test
launchSettingFragment_nullExtraShowFragment_shouldNotCrash()78     public void launchSettingFragment_nullExtraShowFragment_shouldNotCrash()
79             throws ClassNotFoundException {
80         when(mActivity.getFragmentManager()).thenReturn(mFragmentManager);
81         when(mFragmentManager.beginTransaction()).thenReturn(mock(FragmentTransaction.class));
82 
83         doReturn(RuntimeEnvironment.application.getClassLoader()).when(mActivity).getClassLoader();
84 
85         mActivity.launchSettingFragment(null, true, mock(Intent.class));
86     }
87 
88     @Test
testSetTaskDescription_IconChanged()89     public void testSetTaskDescription_IconChanged() {
90         mActivity.setTaskDescription(mTaskDescription);
91 
92         verify(mTaskDescription).setIcon(any());
93     }
94 
95     @Test
testCreateOptionsMenu_setsUpSearch()96     public void testCreateOptionsMenu_setsUpSearch() {
97         ReflectionHelpers.setField(mActivity, "mSearchFeatureProvider",
98                 mFeatureFactory.getSearchFeatureProvider());
99         mActivity.mDisplaySearch = true;
100         mActivity.onCreateOptionsMenu(null);
101 
102         verify(mFeatureFactory.getSearchFeatureProvider()).setUpSearchMenu(any(Menu.class),
103                 any(Activity.class));
104     }
105 
106     @Test
testSaveState_DisplaySearchSaved()107     public void testSaveState_DisplaySearchSaved() {
108         mActivity.mDisplaySearch = true;
109         Bundle bundle = new Bundle();
110         mActivity.saveState(bundle);
111 
112         assertThat((boolean) bundle.get(SettingsActivity.SAVE_KEY_SHOW_SEARCH)).isTrue();
113     }
114 
115     @Test
testSaveState_EnabledHomeSaved()116     public void testSaveState_EnabledHomeSaved() {
117         mActivity.mDisplayHomeAsUpEnabled = true;
118         Bundle bundle = new Bundle();
119         mActivity.saveState(bundle);
120 
121         assertThat((boolean) bundle.get(SettingsActivity.SAVE_KEY_SHOW_HOME_AS_UP)).isTrue();
122     }
123 
124     @Test
testRestoreState_DisplaySearchRestored()125     public void testRestoreState_DisplaySearchRestored() {
126         Bundle bundle = new Bundle();
127         bundle.putBoolean(SettingsActivity.SAVE_KEY_SHOW_SEARCH, true);
128         mActivity.onRestoreInstanceState(bundle);
129 
130         assertThat(mActivity.mDisplaySearch).isTrue();
131     }
132 
133     @Test
testRestoreState_EnabledHomeRestored()134     public void testRestoreState_EnabledHomeRestored() {
135         Bundle bundle = new Bundle();
136         bundle.putBoolean(SettingsActivity.SAVE_KEY_SHOW_SEARCH, true);
137         mActivity.onRestoreInstanceState(bundle);
138 
139         assertThat(mActivity.mDisplaySearch).isTrue();
140     }
141 }
142