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
17package android.preference;
18
19
20import static org.mockito.Mockito.verify;
21import static org.mockito.Mockito.when;
22
23import android.content.Context;
24import android.graphics.Color;
25import android.graphics.drawable.ColorDrawable;
26import android.support.test.InstrumentationRegistry;
27import android.support.test.filters.SmallTest;
28import android.support.test.runner.AndroidJUnit4;
29import android.view.View;
30import android.view.ViewGroup;
31import android.widget.ImageView;
32
33import org.junit.Before;
34import org.junit.Test;
35import org.junit.runner.RunWith;
36import org.mockito.Mock;
37import org.mockito.MockitoAnnotations;
38
39@RunWith(AndroidJUnit4.class)
40@SmallTest
41public class PreferenceIconSpaceTest {
42
43    private TestPreference mPreference;
44
45    @Mock
46    private ViewGroup mViewGroup;
47    @Mock
48    private ImageView mIconView;
49    @Mock
50    private View mImageFrame;
51
52    @Before
53    public void setUp() {
54        MockitoAnnotations.initMocks(this);
55        when(mViewGroup.findViewById(com.android.internal.R.id.icon_frame))
56                .thenReturn(mImageFrame);
57        when(mViewGroup.findViewById(com.android.internal.R.id.icon))
58                .thenReturn(mIconView);
59
60        mPreference = new TestPreference(InstrumentationRegistry.getTargetContext());
61    }
62
63    @Test
64    public void bindView_iconSpaceReserved_shouldReserveIconSpace() {
65        mPreference.setIconSpaceReserved(true);
66        mPreference.bindView(mViewGroup);
67
68        verify(mIconView).setVisibility(View.INVISIBLE);
69        verify(mImageFrame).setVisibility(View.INVISIBLE);
70    }
71
72    @Test
73    public void bindView_iconSpaceNotReserved_shouldNotReserveIconSpace() {
74        mPreference.setIconSpaceReserved(false);
75        mPreference.bindView(mViewGroup);
76
77        verify(mIconView).setVisibility(View.GONE);
78        verify(mImageFrame).setVisibility(View.GONE);
79    }
80
81    @Test
82    public void bindView_hasIcon_shouldDisplayIcon() {
83        mPreference.setIcon(new ColorDrawable(Color.BLACK));
84        mPreference.bindView(mViewGroup);
85
86        verify(mIconView).setVisibility(View.VISIBLE);
87        verify(mImageFrame).setVisibility(View.VISIBLE);
88    }
89
90    private static class TestPreference extends Preference {
91
92        public TestPreference(Context context) {
93            super(context);
94        }
95
96        public void bindView(View view) {
97            onBindView(view);
98        }
99    }
100}
101