1 /* 2 * Copyright (C) 2019 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.widget; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.eq; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.spy; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.content.Context; 28 import android.view.View; 29 30 import com.android.settings.R; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.RobolectricTestRunner; 37 import org.robolectric.RuntimeEnvironment; 38 39 import androidx.preference.PreferenceViewHolder; 40 41 @RunWith(RobolectricTestRunner.class) 42 public class AddPreferenceTest { 43 44 private Context mContext; 45 private PreferenceViewHolder mViewHolder; 46 private View mWidgetFrame; 47 private View mAddWidget; 48 private AddPreference mPreference; 49 50 @Before setUp()51 public void setUp() { 52 mContext = RuntimeEnvironment.application; 53 mPreference = new AddPreference(mContext, null); 54 55 final View view = spy(View.inflate(mContext, mPreference.getLayoutResource(), null)); 56 mViewHolder = PreferenceViewHolder.createInstanceForTests(view); 57 mWidgetFrame = view.findViewById(android.R.id.widget_frame); 58 mAddWidget = spy(View.inflate(mContext, mPreference.getSecondTargetResId(), null)); 59 when(mViewHolder.findViewById(mPreference.getAddWidgetResId())).thenReturn(mAddWidget); 60 } 61 62 @Test onBindViewHolder_noListener_addButtonNotVisible()63 public void onBindViewHolder_noListener_addButtonNotVisible() { 64 mPreference.onBindViewHolder(mViewHolder); 65 assertThat(mPreference.shouldHideSecondTarget()).isTrue(); 66 assertThat(mWidgetFrame.getVisibility()).isEqualTo(View.GONE); 67 } 68 69 @Test onBindViewHolder_hasListener_addButtonVisible()70 public void onBindViewHolder_hasListener_addButtonVisible() { 71 mPreference.setOnAddClickListener(p -> {}); 72 mPreference.onBindViewHolder(mViewHolder); 73 assertThat(mPreference.shouldHideSecondTarget()).isFalse(); 74 assertThat(mWidgetFrame.getVisibility()).isEqualTo(View.VISIBLE); 75 } 76 77 @Test setOnAddClickListener_listenerAddedAfterBinding_addButtonBecomesVisible()78 public void setOnAddClickListener_listenerAddedAfterBinding_addButtonBecomesVisible() { 79 mPreference.onBindViewHolder(mViewHolder); 80 assertThat(mPreference.shouldHideSecondTarget()).isTrue(); 81 assertThat(mWidgetFrame.getVisibility()).isEqualTo(View.GONE); 82 83 mPreference.setOnAddClickListener(p -> {}); 84 assertThat(mPreference.shouldHideSecondTarget()).isFalse(); 85 assertThat(mWidgetFrame.getVisibility()).isEqualTo(View.VISIBLE); 86 } 87 88 @Test setOnAddClickListener_listenerRemovedAfterBinding_addButtonNotVisible()89 public void setOnAddClickListener_listenerRemovedAfterBinding_addButtonNotVisible() { 90 mPreference.setOnAddClickListener(p -> {}); 91 92 mPreference.onBindViewHolder(mViewHolder); 93 assertThat(mPreference.shouldHideSecondTarget()).isFalse(); 94 assertThat(mWidgetFrame.getVisibility()).isEqualTo(View.VISIBLE); 95 96 mPreference.setOnAddClickListener(null); 97 assertThat(mPreference.shouldHideSecondTarget()).isTrue(); 98 assertThat(mWidgetFrame.getVisibility()).isEqualTo(View.GONE); 99 } 100 101 @Test setOnAddClickListener_listenerAddedAndRemovedAfterBinding_addButtonNotVisible()102 public void setOnAddClickListener_listenerAddedAndRemovedAfterBinding_addButtonNotVisible() { 103 mPreference.onBindViewHolder(mViewHolder); 104 assertThat(mPreference.shouldHideSecondTarget()).isTrue(); 105 assertThat(mWidgetFrame.getVisibility()).isEqualTo(View.GONE); 106 107 mPreference.setOnAddClickListener(p -> {}); 108 assertThat(mPreference.shouldHideSecondTarget()).isFalse(); 109 assertThat(mWidgetFrame.getVisibility()).isEqualTo(View.VISIBLE); 110 111 mPreference.setOnAddClickListener(null); 112 assertThat(mPreference.shouldHideSecondTarget()).isTrue(); 113 assertThat(mWidgetFrame.getVisibility()).isEqualTo(View.GONE); 114 } 115 116 @Test onClick_noListener_noCrash()117 public void onClick_noListener_noCrash() { 118 mPreference.onBindViewHolder(mViewHolder); 119 // should be no crash here 120 mPreference.onClick(mAddWidget); 121 } 122 123 @Test onClick_hasListenerBeforeBind_firesCorrectly()124 public void onClick_hasListenerBeforeBind_firesCorrectly() { 125 final AddPreference.OnAddClickListener listener = mock( 126 AddPreference.OnAddClickListener.class); 127 mPreference.setOnAddClickListener(listener); 128 129 mPreference.onBindViewHolder(mViewHolder); 130 assertThat(mWidgetFrame.getVisibility()).isEqualTo(View.VISIBLE); 131 132 mPreference.onClick(mAddWidget); 133 verify(listener).onAddClick(eq(mPreference)); 134 } 135 136 @Test onClick_listenerAddedAfterBind_firesCorrectly()137 public void onClick_listenerAddedAfterBind_firesCorrectly() { 138 mPreference.onBindViewHolder(mViewHolder); 139 assertThat(mWidgetFrame.getVisibility()).isEqualTo(View.GONE); 140 141 final AddPreference.OnAddClickListener listener = mock( 142 AddPreference.OnAddClickListener.class); 143 mPreference.setOnAddClickListener(listener); 144 assertThat(mWidgetFrame.getVisibility()).isEqualTo(View.VISIBLE); 145 146 mPreference.onClick(mAddWidget); 147 verify(listener).onAddClick(eq(mPreference)); 148 } 149 } 150