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 org.mockito.ArgumentMatchers.anyInt;
20 import static org.mockito.Mockito.spy;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 import android.util.TypedValue;
26 import android.view.LayoutInflater;
27 import android.widget.ImageView;
28 
29 import androidx.preference.PreferenceViewHolder;
30 import androidx.test.core.app.ApplicationProvider;
31 import androidx.test.ext.junit.runners.AndroidJUnit4;
32 
33 import com.android.settings.testutils.ResourcesUtils;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 
41 /** Unittest for MutableGearPreference */
42 @RunWith(AndroidJUnit4.class)
43 public class MutableGearPreferenceTest {
44     @Mock
45     private MutableGearPreference.OnGearClickListener mOnGearClickListener;
46 
47     private Context mContext = ApplicationProvider.getApplicationContext();
48     private MutableGearPreference mMutableGearPreference;
49     private PreferenceViewHolder mViewHolder;
50     private ImageView mGearView;
51 
52     @Before
setUp()53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55         mMutableGearPreference =
56                 new MutableGearPreference(mContext, null);
57         int layoutId =
58                 ResourcesUtils.getResourcesId(mContext, "layout", "preference_widget_gear");
59         PreferenceViewHolder holder =
60                 PreferenceViewHolder.createInstanceForTests(
61                         LayoutInflater.from(ApplicationProvider.getApplicationContext())
62                                 .inflate(layoutId, null));
63         mViewHolder = spy(holder);
64         mGearView = spy(new ImageView(mContext, null));
65         int gearId = ResourcesUtils.getResourcesId(mContext, "id", "settings_button");
66         when(mViewHolder.findViewById(gearId)).thenReturn(mGearView);
67     }
68 
69     @Test
onBindViewHolder_gearChangeAlpha()70     public void onBindViewHolder_gearChangeAlpha() {
71         mMutableGearPreference.setGearEnabled(false);
72         mMutableGearPreference.setOnGearClickListener(mOnGearClickListener);
73 
74         mMutableGearPreference.onBindViewHolder(mViewHolder);
75 
76         verify(mGearView).setImageAlpha(anyInt());
77     }
78 
getDisabledAlphaValue(Context context)79     private static int getDisabledAlphaValue(Context context) {
80         TypedValue value = new TypedValue();
81         context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, value, true);
82         return (int) (value.getFloat() * 255);
83     }
84 }
85