Home | History | Annotate | Download | only in notification
      1 /*
      2  * Copyright (C) 2017 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 import static org.mockito.Mockito.doReturn;
     21 import static org.mockito.Mockito.spy;
     22 import static org.mockito.Mockito.verify;
     23 import static org.mockito.Mockito.when;
     24 import static org.robolectric.RuntimeEnvironment.application;
     25 
     26 import android.app.Activity;
     27 import android.app.NotificationManager;
     28 import android.content.Context;
     29 import android.content.Intent;
     30 import android.content.res.Resources;
     31 
     32 import com.android.settings.R;
     33 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     34 import com.android.settings.testutils.shadow.SettingsShadowResources;
     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.annotation.Config;
     42 import org.robolectric.shadows.ShadowApplication;
     43 import org.robolectric.shadows.ShadowToast;
     44 
     45 @RunWith(SettingsRobolectricTestRunner.class)
     46 @Config(shadows = SettingsShadowResources.SettingsShadowTheme.class)
     47 public class ZenModeScheduleRuleSettingsTest {
     48 
     49     @Mock
     50     private Activity mActivity;
     51 
     52     @Mock
     53     private Intent mIntent;
     54 
     55     @Mock
     56     private NotificationManager mNotificationManager;
     57 
     58     private TestFragment mFragment;
     59     private Context mContext;
     60 
     61     @Before
     62     public void setUp() {
     63         MockitoAnnotations.initMocks(this);
     64 
     65         ShadowApplication shadowApplication = ShadowApplication.getInstance();
     66         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
     67         mContext = shadowApplication.getApplicationContext();
     68 
     69         mFragment = spy(new TestFragment());
     70         mFragment.onAttach(application);
     71 
     72         doReturn(mActivity).when(mFragment).getActivity();
     73 
     74         Resources res = application.getResources();
     75 
     76         doReturn(res).when(mFragment).getResources();
     77         when(mActivity.getTheme()).thenReturn(res.newTheme());
     78         when(mActivity.getIntent()).thenReturn(mIntent);
     79         when(mActivity.getResources()).thenReturn(res);
     80         when(mFragment.getContext()).thenReturn(mContext);
     81     }
     82 
     83     @Test
     84     public void onCreate_noRuleId_shouldToastAndFinishAndNoCrash() {
     85         final String expected = mContext.getString(R.string.zen_mode_rule_not_found_text);
     86 
     87         mFragment.onCreate(null);
     88 
     89         // verify the toast
     90         assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo(expected);
     91 
     92         // verify the finish
     93         verify(mActivity).finish();
     94 
     95         //should not crash
     96     }
     97 
     98     public static class TestFragment extends ZenModeScheduleRuleSettings {
     99 
    100         @Override
    101         protected Object getSystemService(final String name) {
    102             return null;
    103         }
    104     }
    105 }
    106