• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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;
18 
19 import static android.app.slice.Slice.EXTRA_TOGGLE_STATE;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.ArgumentMatchers.anyInt;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.spy;
26 
27 import android.app.NotificationManager;
28 import android.content.Context;
29 import android.content.Intent;
30 import android.content.res.Resources;
31 import android.provider.Settings;
32 import android.support.v4.graphics.drawable.IconCompat;
33 
34 import com.android.settings.R;
35 import com.android.settings.testutils.SettingsRobolectricTestRunner;
36 import com.android.settings.testutils.SliceTester;
37 import com.android.settings.testutils.shadow.ShadowNotificationManager;
38 
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.robolectric.RuntimeEnvironment;
43 import org.robolectric.annotation.Config;
44 
45 import java.util.List;
46 
47 import androidx.slice.Slice;
48 import androidx.slice.SliceItem;
49 import androidx.slice.SliceMetadata;
50 import androidx.slice.SliceProvider;
51 import androidx.slice.core.SliceAction;
52 import androidx.slice.widget.SliceLiveData;
53 
54 @Config(shadows = ShadowNotificationManager.class)
55 @RunWith(SettingsRobolectricTestRunner.class)
56 public class ZenModeSliceBuilderTest {
57 
58     private Context mContext;
59 
60     @Before
setUp()61     public void setUp() {
62         mContext = spy(RuntimeEnvironment.application);
63 
64         // Prevent crash in SliceMetadata.
65         Resources resources = spy(mContext.getResources());
66         doReturn(60).when(resources).getDimensionPixelSize(anyInt());
67         doReturn(resources).when(mContext).getResources();
68 
69         // Set-up specs for SliceMetadata.
70         SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
71     }
72 
73     @Test
getZenModeSlice_correctSliceContent()74     public void getZenModeSlice_correctSliceContent() {
75         final Slice dndSlice = ZenModeSliceBuilder.getSlice(mContext);
76         final SliceMetadata metadata = SliceMetadata.from(mContext, dndSlice);
77 
78         final List<SliceAction> toggles = metadata.getToggles();
79         assertThat(toggles).hasSize(1);
80 
81         final SliceAction primaryAction = metadata.getPrimaryAction();
82         assertThat(primaryAction.getIcon()).isNull();
83 
84         final List<SliceItem> sliceItems = dndSlice.getItems();
85         SliceTester.assertTitle(sliceItems, mContext.getString(R.string.zen_mode_settings_title));
86     }
87 
88     @Test
handleUriChange_turnOn_zenModeTurnsOn()89     public void handleUriChange_turnOn_zenModeTurnsOn() {
90         final Intent intent = new Intent();
91         intent.putExtra(EXTRA_TOGGLE_STATE, true);
92         NotificationManager.from(mContext).setZenMode(Settings.Global.ZEN_MODE_OFF, null, "");
93 
94         ZenModeSliceBuilder.handleUriChange(mContext, intent);
95 
96         final int zenMode = NotificationManager.from(mContext).getZenMode();
97         assertThat(zenMode).isEqualTo(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS);
98     }
99 
100     @Test
handleUriChange_turnOff_zenModeTurnsOff()101     public void handleUriChange_turnOff_zenModeTurnsOff() {
102         final Intent intent = new Intent();
103         intent.putExtra(EXTRA_TOGGLE_STATE, false);
104         NotificationManager.from(mContext).setZenMode(
105                 Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, "");
106 
107         ZenModeSliceBuilder.handleUriChange(mContext, intent);
108 
109         final int zenMode = NotificationManager.from(mContext).getZenMode();
110         assertThat(zenMode).isEqualTo(Settings.Global.ZEN_MODE_OFF);
111     }
112 }
113