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.settingslib;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.ImageView;
25 
26 import androidx.preference.PreferenceViewHolder;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.robolectric.RobolectricTestRunner;
32 import org.robolectric.RuntimeEnvironment;
33 
34 @RunWith(RobolectricTestRunner.class)
35 public class RestrictedSwitchPreferenceTest {
36 
37     private static final int SIZE = 50;
38 
39     private RestrictedSwitchPreference mPreference;
40     private Context mContext;
41     private PreferenceViewHolder mViewHolder;
42     private View mRootView;
43     private ImageView mImageView;
44 
45     @Before
setUp()46     public void setUp() {
47         mContext = RuntimeEnvironment.application;
48         mPreference = new RestrictedSwitchPreference(mContext);
49         mRootView = View.inflate(mContext, R.layout.restricted_switch_preference,
50                 null /* parent */);
51         mViewHolder = PreferenceViewHolder.createInstanceForTests(mRootView);
52         mImageView = (ImageView) mViewHolder.findViewById(android.R.id.icon);
53     }
54 
55     @Test
onBindViewHolder_setIconSize_shouldHaveCorrectLayoutParam()56     public void onBindViewHolder_setIconSize_shouldHaveCorrectLayoutParam() {
57         mPreference.setIconSize(SIZE);
58 
59         mPreference.onBindViewHolder(mViewHolder);
60 
61         assertThat(mImageView.getLayoutParams().height).isEqualTo(SIZE);
62         assertThat(mImageView.getLayoutParams().width).isEqualTo(SIZE);
63     }
64 
65     @Test
onBindViewHolder_notSetIconSize_shouldHaveCorrectLayoutParam()66     public void onBindViewHolder_notSetIconSize_shouldHaveCorrectLayoutParam() {
67         mPreference.onBindViewHolder(mViewHolder);
68 
69         assertThat(mImageView.getLayoutParams().height).isEqualTo(
70                 ViewGroup.LayoutParams.WRAP_CONTENT);
71         assertThat(mImageView.getLayoutParams().width).isEqualTo(
72                 ViewGroup.LayoutParams.WRAP_CONTENT);
73     }
74 }
75