1 /* 2 * Copyright (C) 2021 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.Mockito.spy; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.view.LayoutInflater; 26 import android.view.View; 27 28 import androidx.preference.PreferenceViewHolder; 29 import androidx.test.core.app.ApplicationProvider; 30 import androidx.test.ext.junit.runners.AndroidJUnit4; 31 32 import com.android.settings.testutils.ResourcesUtils; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 40 /** Unittest for GearPreference */ 41 @RunWith(AndroidJUnit4.class) 42 public class GearPreferenceTest { 43 @Mock 44 private GearPreference.OnGearClickListener mOnGearClickListener; 45 46 private Context mContext = ApplicationProvider.getApplicationContext(); 47 private GearPreference mGearPreference; 48 private PreferenceViewHolder mViewHolder; 49 private View mGearView; 50 51 @Before setUp()52 public void setUp() { 53 MockitoAnnotations.initMocks(this); 54 mGearPreference = 55 new GearPreference(mContext, null); 56 int layoutId = ResourcesUtils.getResourcesId(mContext, "layout", "preference_widget_gear"); 57 PreferenceViewHolder holder = 58 PreferenceViewHolder.createInstanceForTests( 59 LayoutInflater.from(ApplicationProvider.getApplicationContext()) 60 .inflate(layoutId, null)); 61 mViewHolder = spy(holder); 62 mGearView = new View(mContext, null); 63 int gearId = ResourcesUtils.getResourcesId(mContext, "id", "settings_button"); 64 when(mViewHolder.findViewById(gearId)).thenReturn(mGearView); 65 } 66 67 @Test onBindViewHolder_gearIsVisible()68 public void onBindViewHolder_gearIsVisible() { 69 mGearPreference.setOnGearClickListener(mOnGearClickListener); 70 71 mGearPreference.onBindViewHolder(mViewHolder); 72 73 assertThat(mGearView.getVisibility()).isEqualTo(View.VISIBLE); 74 } 75 76 @Test onBindViewHolder_gearIsGone()77 public void onBindViewHolder_gearIsGone() { 78 mGearPreference.setOnGearClickListener(null); 79 80 mGearPreference.onBindViewHolder(mViewHolder); 81 82 assertThat(mGearView.getVisibility()).isEqualTo(View.GONE); 83 } 84 } 85