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 junit.framework.Assert.assertEquals; 20 21 import static org.junit.Assert.assertTrue; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.when; 24 25 import android.app.AutomaticZenRule; 26 import android.app.NotificationManager; 27 import android.content.Context; 28 29 import androidx.preference.PreferenceScreen; 30 31 import com.android.settingslib.core.lifecycle.Lifecycle; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.robolectric.RobolectricTestRunner; 39 import org.robolectric.RuntimeEnvironment; 40 import org.robolectric.shadows.ShadowApplication; 41 import org.robolectric.util.ReflectionHelpers; 42 43 @RunWith(RobolectricTestRunner.class) 44 public class ZenRulePreferenceControllerTest { 45 46 @Mock 47 private ZenModeBackend mBackend; 48 @Mock 49 private NotificationManager mNotificationManager; 50 @Mock 51 private ZenCustomRadioButtonPreference mockPref; 52 @Mock 53 private PreferenceScreen mScreen; 54 55 private Context mContext; 56 private TestablePreferenceController mController; 57 58 @Before setup()59 public void setup() { 60 MockitoAnnotations.initMocks(this); 61 ShadowApplication shadowApplication = ShadowApplication.getInstance(); 62 shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager); 63 64 mContext = RuntimeEnvironment.application; 65 mController = new TestablePreferenceController(mContext,"test", mock(Lifecycle.class)); 66 ReflectionHelpers.setField(mController, "mBackend", mBackend); 67 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref); 68 mController.displayPreference(mScreen); 69 } 70 71 @Test onResumeTest()72 public void onResumeTest() { 73 final String id = "testid"; 74 final AutomaticZenRule rule = new AutomaticZenRule("test", null, null, 75 null, null, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); 76 77 assertTrue(mController.mRule == null); 78 assertTrue(mController.mId == null); 79 80 mController.onResume(rule, id); 81 82 assertEquals(mController.mId, id); 83 assertEquals(mController.mRule, rule); 84 } 85 86 class TestablePreferenceController extends AbstractZenCustomRulePreferenceController { TestablePreferenceController(Context context, String key, Lifecycle lifecycle)87 TestablePreferenceController(Context context, String key, 88 Lifecycle lifecycle) { 89 super(context, key, lifecycle); 90 } 91 } 92 } 93