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.bluetooth; 18 19 import static org.mockito.ArgumentMatchers.any; 20 import static org.mockito.Mockito.spy; 21 import static org.mockito.Mockito.times; 22 import static org.mockito.Mockito.verify; 23 24 import android.content.Context; 25 26 import androidx.preference.PreferenceFragmentCompat; 27 import androidx.preference.PreferenceScreen; 28 29 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 30 import com.android.settingslib.core.lifecycle.Lifecycle; 31 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.robolectric.RobolectricTestRunner; 35 36 @RunWith(RobolectricTestRunner.class) 37 public class BluetoothDetailsControllerEventsTest extends BluetoothDetailsControllerTestBase { 38 39 @Test pauseResumeEvents()40 public void pauseResumeEvents() { 41 TestController controller = 42 spy(new TestController(mContext, mFragment, mCachedDevice, mLifecycle)); 43 verify(mLifecycle).addObserver(any(BluetoothDetailsController.class)); 44 45 showScreen(controller); 46 verify(mCachedDevice, times(1)).registerCallback(controller); 47 verify(controller, times(1)).refresh(); 48 49 controller.onPause(); 50 verify(controller, times(1)).refresh(); 51 verify(mCachedDevice).unregisterCallback(controller); 52 53 controller.onResume(); 54 verify(controller, times(2)).refresh(); 55 verify(mCachedDevice, times(2)).registerCallback(controller); 56 57 // The init function should only have been called once 58 verify(controller, times(1)).init(mScreen); 59 } 60 61 private static class TestController extends BluetoothDetailsController { TestController(Context context, PreferenceFragmentCompat fragment, CachedBluetoothDevice device, Lifecycle lifecycle)62 private TestController(Context context, PreferenceFragmentCompat fragment, 63 CachedBluetoothDevice device, Lifecycle lifecycle) { 64 super(context, fragment, device, lifecycle); 65 } 66 67 @Override getPreferenceKey()68 public String getPreferenceKey() { 69 return null; 70 } 71 72 @Override init(PreferenceScreen screen)73 protected void init(PreferenceScreen screen) {} 74 75 @Override refresh()76 protected void refresh() {} 77 } 78 } 79