Fan Zhang | 035ff93 | 2017-03-22 10:54:03 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.settingslib; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.support.v7.preference.PreferenceViewHolder; |
| 21 | import android.view.View; |
| 22 | |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.junit.runner.RunWith; |
| 26 | import org.mockito.Mock; |
| 27 | import org.mockito.MockitoAnnotations; |
| 28 | import org.robolectric.RuntimeEnvironment; |
| 29 | import org.robolectric.annotation.Config; |
| 30 | |
| 31 | import static com.google.common.truth.Truth.assertThat; |
| 32 | import static org.mockito.Mockito.doReturn; |
| 33 | import static org.mockito.Mockito.mock; |
| 34 | import static org.mockito.Mockito.spy; |
| 35 | import static org.mockito.Mockito.verify; |
| 36 | import static org.mockito.Mockito.when; |
| 37 | |
| 38 | @RunWith(SettingLibRobolectricTestRunner.class) |
| 39 | @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) |
| 40 | public class TwoTargetPreferenceTest { |
| 41 | |
| 42 | private PreferenceViewHolder mViewHolder; |
| 43 | @Mock |
| 44 | private View mDivider; |
| 45 | @Mock |
| 46 | private View mWidgetFrame; |
| 47 | private TwoTargetPreference mPreference; |
| 48 | private Context mContext; |
| 49 | |
| 50 | @Before |
| 51 | public void setUp() { |
| 52 | MockitoAnnotations.initMocks(this); |
| 53 | mContext = RuntimeEnvironment.application; |
| 54 | mPreference = spy(new TwoTargetPreference(mContext)); |
Filip Pavlis | 81d5351 | 2017-04-05 10:56:38 +0100 | [diff] [blame] | 55 | mViewHolder = PreferenceViewHolder.createInstanceForTests(mock(View.class)); |
Fan Zhang | 035ff93 | 2017-03-22 10:54:03 -0700 | [diff] [blame] | 56 | when(mViewHolder.findViewById(R.id.two_target_divider)) |
| 57 | .thenReturn(mDivider); |
| 58 | when(mViewHolder.findViewById(android.R.id.widget_frame)) |
| 59 | .thenReturn(mWidgetFrame); |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void bind_noSecondTarget_shouldNotDrawDivider() { |
| 64 | assertThat(mPreference.shouldHideSecondTarget()).isTrue(); |
| 65 | |
| 66 | mPreference.onBindViewHolder(mViewHolder); |
| 67 | |
| 68 | verify(mDivider).setVisibility(View.GONE); |
| 69 | verify(mWidgetFrame).setVisibility(View.GONE); |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void bind_hasSecondTarget_shouldNotDrawDivider() { |
| 74 | doReturn(false).when(mPreference).shouldHideSecondTarget(); |
| 75 | |
| 76 | mPreference.onBindViewHolder(mViewHolder); |
| 77 | |
| 78 | verify(mDivider).setVisibility(View.VISIBLE); |
| 79 | verify(mWidgetFrame).setVisibility(View.VISIBLE); |
| 80 | } |
| 81 | } |