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 17package com.android.settings.accessibility; 18 19import static org.mockito.Mockito.mock; 20import static org.mockito.Mockito.spy; 21import static org.mockito.Mockito.verify; 22 23import android.os.Bundle; 24import android.support.annotation.XmlRes; 25import android.view.LayoutInflater; 26import android.view.View; 27import android.view.ViewGroup; 28 29import com.android.settings.R; 30import com.android.settings.testutils.SettingsRobolectricTestRunner; 31import com.android.settings.testutils.shadow.SettingsShadowResources; 32import com.android.settings.widget.SwitchBar; 33 34import org.junit.Test; 35import org.junit.runner.RunWith; 36import org.robolectric.annotation.Config; 37import org.robolectric.util.FragmentTestUtil; 38 39@RunWith(SettingsRobolectricTestRunner.class) 40@Config(shadows = SettingsShadowResources.SettingsShadowTheme.class) 41public class ToggleFeaturePreferenceFragmentTest { 42 43 private ToggleFeaturePreferenceFragmentTestable mFragment; 44 45 @Test 46 public void createFragment_shouldOnlyAddPreferencesOnce() { 47 mFragment = spy(new ToggleFeaturePreferenceFragmentTestable()); 48 FragmentTestUtil.startFragment(mFragment); 49 50 // execute exactly once 51 verify(mFragment).addPreferencesFromResource(R.xml.placeholder_prefs); 52 } 53 54 public static class ToggleFeaturePreferenceFragmentTestable 55 extends ToggleFeaturePreferenceFragment { 56 57 @Override 58 protected void onPreferenceToggled(String preferenceKey, boolean enabled) {} 59 60 @Override 61 public int getMetricsCategory() { 62 return 0; 63 } 64 65 @Override 66 public int getPreferenceScreenResId() { 67 return R.xml.placeholder_prefs; 68 } 69 70 @Override 71 public View onCreateView(LayoutInflater inflater, ViewGroup container, 72 Bundle savedInstanceState) { 73 return mock(View.class); 74 } 75 76 @Override 77 public void addPreferencesFromResource(@XmlRes int preferencesResId) { 78 // do nothing 79 } 80 81 @Override 82 public void onViewCreated(View view, Bundle savedInstanceState) { 83 // do nothing 84 } 85 86 @Override 87 public void onActivityCreated(Bundle savedInstanceState) { 88 mSwitchBar = mock(SwitchBar.class); 89 super.onActivityCreated(savedInstanceState); 90 } 91 } 92} 93