• 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 com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 import static org.robolectric.RuntimeEnvironment.application;
26 
27 import android.app.NotificationManager;
28 import android.content.Context;
29 import android.content.Intent;
30 import android.content.res.Resources;
31 
32 import androidx.fragment.app.FragmentActivity;
33 
34 import com.android.settings.R;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.robolectric.RobolectricTestRunner;
42 import org.robolectric.RuntimeEnvironment;
43 import org.robolectric.shadows.ShadowApplication;
44 import org.robolectric.shadows.ShadowToast;
45 
46 import java.util.ArrayList;
47 import java.util.List;
48 
49 @RunWith(RobolectricTestRunner.class)
50 public class ZenModeEventRuleSettingsTest {
51 
52     @Mock
53     private FragmentActivity mActivity;
54 
55     @Mock
56     private Intent mIntent;
57 
58     @Mock
59     private NotificationManager mNotificationManager;
60 
61     private TestFragment mFragment;
62     private Context mContext;
63 
64     @Before
setUp()65     public void setUp() {
66         MockitoAnnotations.initMocks(this);
67 
68         ShadowApplication shadowApplication = ShadowApplication.getInstance();
69         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
70         mContext = RuntimeEnvironment.application;
71 
72         mFragment = spy(new TestFragment());
73         mFragment.onAttach(application);
74 
75         doReturn(mActivity).when(mFragment).getActivity();
76 
77         Resources res = application.getResources();
78 
79         doReturn(res).when(mFragment).getResources();
80         when(mActivity.getTheme()).thenReturn(res.newTheme());
81         when(mActivity.getIntent()).thenReturn(mIntent);
82         when(mActivity.getResources()).thenReturn(res);
83         when(mFragment.getContext()).thenReturn(mContext);
84     }
85 
86     @Test
onCreate_noRuleId_shouldToastAndFinishAndNoCrash()87     public void onCreate_noRuleId_shouldToastAndFinishAndNoCrash() {
88         final String expected = mContext.getString(R.string.zen_mode_rule_not_found_text);
89 
90         mFragment.onCreate(null);
91 
92         // verify the toast
93         assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo(expected);
94 
95         // verify the finish
96         verify(mActivity).finish();
97 
98         //should not crash
99     }
100 
101     @Test
testNoDuplicateCalendars()102     public void testNoDuplicateCalendars() {
103         List<ZenModeEventRuleSettings.CalendarInfo> calendarsList = new ArrayList<>();
104         mFragment.addCalendar(1234, "calName", 1, calendarsList);
105         mFragment.addCalendar(1234, "calName", 2, calendarsList);
106         mFragment.addCalendar(1234, "calName", 3, calendarsList);
107         assertThat(calendarsList.size()).isEqualTo(1);
108     }
109 
110     private static class TestFragment extends ZenModeEventRuleSettings {
111 
112         @Override
getSystemService(final String name)113         protected Object getSystemService(final String name) {
114             return null;
115         }
116     }
117 }
118