1 /*
2  * Copyright (C) 2024 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.notification.modes;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 
26 import android.app.AutomaticZenRule;
27 import android.content.Context;
28 import android.net.Uri;
29 
30 import androidx.annotation.NonNull;
31 import androidx.preference.PreferenceScreen;
32 import androidx.recyclerview.widget.RecyclerView;
33 
34 import com.android.settings.R;
35 import com.android.settings.dashboard.DashboardFragment;
36 import com.android.settingslib.widget.LayoutPreference;
37 
38 import com.google.common.collect.ImmutableList;
39 
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.ArgumentCaptor;
44 import org.robolectric.RobolectricTestRunner;
45 import org.robolectric.RuntimeEnvironment;
46 
47 @RunWith(RobolectricTestRunner.class)
48 public class ZenModeIconPickerListPreferenceControllerTest {
49 
50     private static final ZenMode ZEN_MODE = new ZenMode(
51             "mode_id",
52             new AutomaticZenRule.Builder("mode name", Uri.parse("mode")).build(),
53             /* isActive= */ false);
54 
55     private ZenModesBackend mBackend;
56     private ZenModeIconPickerListPreferenceController mController;
57     private PreferenceScreen mPreferenceScreen;
58     private RecyclerView mRecyclerView;
59 
60     @Before
setUp()61     public void setUp() {
62         Context context = RuntimeEnvironment.getApplication();
63         mBackend = mock(ZenModesBackend.class);
64 
65         DashboardFragment fragment = mock(DashboardFragment.class);
66         mController = new ZenModeIconPickerListPreferenceController(
67                 RuntimeEnvironment.getApplication(), "icon_list", fragment,
68                 new TestIconOptionsProvider(), mBackend);
69 
70         mRecyclerView = new RecyclerView(context);
71         mRecyclerView.setId(R.id.icon_list);
72         LayoutPreference layoutPreference = new LayoutPreference(context, mRecyclerView);
73         mPreferenceScreen = mock(PreferenceScreen.class);
74         when(mPreferenceScreen.findPreference(eq("icon_list"))).thenReturn(layoutPreference);
75     }
76 
77     @Test
displayPreference_loadsIcons()78     public void displayPreference_loadsIcons() {
79         mController.displayPreference(mPreferenceScreen);
80 
81         assertThat(mRecyclerView.getAdapter()).isNotNull();
82         assertThat(mRecyclerView.getAdapter().getItemCount()).isEqualTo(3);
83     }
84 
85     @Test
selectIcon_updatesMode()86     public void selectIcon_updatesMode() {
87         mController.setZenMode(ZEN_MODE);
88 
89         mController.onIconSelected(R.drawable.ic_android);
90 
91         ArgumentCaptor<ZenMode> captor = ArgumentCaptor.forClass(ZenMode.class);
92         verify(mBackend).updateMode(captor.capture());
93         assertThat(captor.getValue().getRule().getIconResId()).isEqualTo(R.drawable.ic_android);
94     }
95 
96     private static class TestIconOptionsProvider implements IconOptionsProvider {
97 
98         @Override
99         @NonNull
getIcons()100         public ImmutableList<IconInfo> getIcons() {
101             return ImmutableList.of(
102                     new IconInfo(R.drawable.ic_android, "android"),
103                     new IconInfo(R.drawable.ic_info, "info"),
104                     new IconInfo(R.drawable.ic_hearing, "hearing"));
105         }
106     }
107 }
108