blob: 505d7a6c5a06857e7e220365a143ebe4fbec7e95 [file] [log] [blame]
Fan Zhang035ff932017-03-22 10:54:03 -07001/*
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.settingslib;
18
19import android.content.Context;
20import android.support.v7.preference.PreferenceViewHolder;
21import android.view.View;
22
23import org.junit.Before;
24import org.junit.Test;
25import org.junit.runner.RunWith;
26import org.mockito.Mock;
27import org.mockito.MockitoAnnotations;
28import org.robolectric.RuntimeEnvironment;
29import org.robolectric.annotation.Config;
30
31import static com.google.common.truth.Truth.assertThat;
32import static org.mockito.Mockito.doReturn;
33import static org.mockito.Mockito.mock;
34import static org.mockito.Mockito.spy;
35import static org.mockito.Mockito.verify;
36import static org.mockito.Mockito.when;
37
38@RunWith(SettingLibRobolectricTestRunner.class)
39@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
40public 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 Pavlis81d53512017-04-05 10:56:38 +010055 mViewHolder = PreferenceViewHolder.createInstanceForTests(mock(View.class));
Fan Zhang035ff932017-03-22 10:54:03 -070056 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}