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.development; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.support.v7.preference.Preference; 22 import android.support.v7.preference.PreferenceManager; 23 import android.support.v7.preference.PreferenceScreen; 24 25 import com.android.settings.SettingsRobolectricTestRunner; 26 import com.android.settings.TestConfig; 27 import com.android.settings.testutils.FakeFeatureFactory; 28 import com.android.settingslib.drawer.CategoryKey; 29 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.annotation.Config; 37 import org.robolectric.shadows.ShadowApplication; 38 39 import java.util.ArrayList; 40 import java.util.List; 41 42 import static org.mockito.Answers.RETURNS_DEEP_STUBS; 43 import static org.mockito.Matchers.any; 44 import static org.mockito.Mockito.doReturn; 45 import static org.mockito.Mockito.spy; 46 import static org.mockito.Mockito.times; 47 import static org.mockito.Mockito.verify; 48 import static org.mockito.Mockito.when; 49 50 @RunWith(SettingsRobolectricTestRunner.class) 51 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 52 public class DevelopmentSettingsTest { 53 54 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 55 private Context mContext; 56 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 57 private Activity mActivity; 58 @Mock(answer = RETURNS_DEEP_STUBS) 59 private PreferenceScreen mScreen; 60 @Mock 61 private PreferenceManager mPreferenceManager; 62 63 private FakeFeatureFactory mFeatureFactory; 64 private DevelopmentSettings mSettings; 65 66 @Before setUp()67 public void setUp() { 68 MockitoAnnotations.initMocks(this); 69 FakeFeatureFactory.setupForTest(mContext); 70 mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); 71 mSettings = spy(new DevelopmentSettings()); 72 } 73 74 @Test addDashboardCategoryPreference_shouldAddToScreen()75 public void addDashboardCategoryPreference_shouldAddToScreen() { 76 final List<Preference> preferences = new ArrayList<>(); 77 preferences.add(new Preference(ShadowApplication.getInstance().getApplicationContext())); 78 preferences.add(new Preference(ShadowApplication.getInstance().getApplicationContext())); 79 doReturn(mScreen).when(mSettings).getPreferenceScreen(); 80 doReturn(mPreferenceManager).when(mSettings).getPreferenceManager(); 81 doReturn(mActivity).when(mSettings).getActivity(); 82 when(mPreferenceManager.getContext()).thenReturn(mContext); 83 when(mFeatureFactory.dashboardFeatureProvider.getPreferencesForCategory( 84 mActivity, mContext, mSettings.getMetricsCategory(), 85 CategoryKey.CATEGORY_SYSTEM_DEVELOPMENT)) 86 .thenReturn(preferences); 87 88 mSettings.onAttach(mContext); 89 mSettings.addDashboardCategoryPreferences(); 90 91 verify(mScreen, times(2)).addPreference(any(Preference.class)); 92 } 93 } 94