1 /* 2 * Copyright (C) 2020 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.accessibility; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.internal.verification.VerificationModeFactory.times; 25 26 import android.content.Context; 27 import android.view.LayoutInflater; 28 import android.view.View; 29 30 import androidx.preference.PreferenceViewHolder; 31 32 import com.android.settings.R; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.robolectric.RobolectricTestRunner; 38 import org.robolectric.RuntimeEnvironment; 39 40 /** Tests for {@link DividerSwitchPreference}. */ 41 @RunWith(RobolectricTestRunner.class) 42 public class DividerSwitchPreferenceTest { 43 private final Context mContext = RuntimeEnvironment.application; 44 private View mRootView; 45 private PreferenceViewHolder mViewHolder; 46 private DividerSwitchPreference mDividerSwitchPreference; 47 48 @Before setUp()49 public void setUp() { 50 initRootView(); 51 initPreference(); 52 } 53 54 @Test setDividerAllowedAbove_allowed_success()55 public void setDividerAllowedAbove_allowed_success() { 56 mDividerSwitchPreference.setDividerAllowedAbove(true); 57 mDividerSwitchPreference.onBindViewHolder(mViewHolder); 58 59 // One time was in parent, the other time was in child. 60 verify(mViewHolder, times(2)).setDividerAllowedAbove(true); 61 } 62 63 @Test setDividerAllowedBelow_allowed_success()64 public void setDividerAllowedBelow_allowed_success() { 65 mDividerSwitchPreference.setDividerAllowedBelow(true); 66 mDividerSwitchPreference.onBindViewHolder(mViewHolder); 67 68 // One time was in parent, the other time was in child. 69 verify(mViewHolder, times(2)).setDividerAllowedBelow(true); 70 } 71 72 @Test setSwitchVisibility_visible_success()73 public void setSwitchVisibility_visible_success() { 74 final View view = spy(new View(mContext)); 75 doReturn(view).when(mRootView).findViewById(android.R.id.widget_frame); 76 77 mDividerSwitchPreference.setSwitchVisibility(View.VISIBLE); 78 mDividerSwitchPreference.onBindViewHolder(mViewHolder); 79 80 assertThat(view.getVisibility()).isEqualTo(View.VISIBLE); 81 } 82 initRootView()83 private void initRootView() { 84 final LayoutInflater inflater = LayoutInflater.from(mContext); 85 mRootView = spy(inflater.inflate(R.layout.preference_widget_switch, /* root= */ null)); 86 mViewHolder = spy(PreferenceViewHolder.createInstanceForTests(mRootView)); 87 } 88 initPreference()89 private void initPreference() { 90 mDividerSwitchPreference = new DividerSwitchPreference(mContext); 91 mDividerSwitchPreference.setDividerAllowedAbove(false); 92 mDividerSwitchPreference.setDividerAllowedBelow(false); 93 mDividerSwitchPreference.setSwitchVisibility(View.INVISIBLE); 94 } 95 } 96