1 /*
2  * Copyright (C) 2023 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.accessibility;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.widget.TextView;
25 
26 import androidx.annotation.ColorInt;
27 import androidx.preference.PreferenceViewHolder;
28 import androidx.test.core.app.ApplicationProvider;
29 
30 import com.android.settings.R;
31 import com.android.settingslib.Utils;
32 
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Spy;
38 import org.mockito.junit.MockitoJUnit;
39 import org.mockito.junit.MockitoRule;
40 import org.robolectric.RobolectricTestRunner;
41 import org.robolectric.Shadows;
42 
43 @RunWith(RobolectricTestRunner.class)
44 public class FlashNotificationsPreviewPreferenceTest {
45 
46     @Rule
47     public MockitoRule mMockitoRule = MockitoJUnit.rule();
48     @Spy
49     private final Context mContext = ApplicationProvider.getApplicationContext();
50     private FlashNotificationsPreviewPreference mFlashNotificationsPreviewPreference;
51     private PreferenceViewHolder mPreferenceViewHolder;
52 
53     @Before
setUp()54     public void setUp() {
55         mPreferenceViewHolder = PreferenceViewHolder.createInstanceForTests(
56                 LayoutInflater.from(mContext).inflate(
57                         R.layout.flash_notification_preview_preference, null));
58         mFlashNotificationsPreviewPreference = new FlashNotificationsPreviewPreference(mContext);
59     }
60 
61     @Test
setEnabled_true_verifyEnabledUi()62     public void setEnabled_true_verifyEnabledUi() {
63         @ColorInt final int textColorEnabled = ((TextView) mPreferenceViewHolder.findViewById(
64                 android.R.id.title)).getCurrentTextColor();
65 
66         mFlashNotificationsPreviewPreference.setEnabled(true);
67         mFlashNotificationsPreviewPreference.onBindViewHolder(mPreferenceViewHolder);
68 
69         final View frame = mPreferenceViewHolder.findViewById(R.id.frame);
70         final int backgroundResId = Shadows.shadowOf(frame.getBackground()).getCreatedFromResId();
71         assertThat(backgroundResId).isEqualTo(
72                 com.android.settingslib.widget.mainswitch.R.drawable.settingslib_switch_bar_bg_on);
73         final TextView title = (TextView) mPreferenceViewHolder.findViewById(android.R.id.title);
74         assertThat(title.getAlpha()).isEqualTo(1f);
75         assertThat(title.getCurrentTextColor()).isEqualTo(textColorEnabled);
76     }
77 
78     @Test
setEnabled_false_verifyDisabledUi()79     public void setEnabled_false_verifyDisabledUi() {
80         @ColorInt final int textColorDisabled = Utils.getColorAttrDefaultColor(mContext,
81                 android.R.attr.textColorPrimary);
82 
83         mFlashNotificationsPreviewPreference.setEnabled(false);
84         mFlashNotificationsPreviewPreference.onBindViewHolder(mPreferenceViewHolder);
85 
86         final View frame = mPreferenceViewHolder.findViewById(R.id.frame);
87         final int backgroundResId = Shadows.shadowOf(frame.getBackground()).getCreatedFromResId();
88         assertThat(backgroundResId).isEqualTo(R.drawable.switch_bar_bg_disabled);
89         final TextView title = (TextView) mPreferenceViewHolder.findViewById(android.R.id.title);
90         assertThat(title.getAlpha()).isEqualTo(0.38f);
91         assertThat(title.getCurrentTextColor()).isEqualTo(textColorDisabled);
92     }
93 }