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.slices; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.verify; 23 24 import android.content.Context; 25 import android.net.Uri; 26 27 import androidx.annotation.NonNull; 28 import androidx.lifecycle.LiveData; 29 import androidx.lifecycle.Observer; 30 import androidx.slice.Slice; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 import org.robolectric.RobolectricTestRunner; 38 import org.robolectric.RuntimeEnvironment; 39 40 @RunWith(RobolectricTestRunner.class) 41 public class SlicePreferenceControllerTest { 42 private static final String KEY = "slice_preference_key"; 43 44 @Mock 45 private LiveData<Slice> mLiveData; 46 @Mock 47 private SlicePreference mSlicePreference; 48 49 private Context mContext; 50 private SlicePreferenceController mController; 51 private Uri mUri; 52 53 @Before setUp()54 public void setUp() { 55 MockitoAnnotations.initMocks(this); 56 57 mContext = spy(RuntimeEnvironment.application); 58 mController = new SlicePreferenceController(mContext, KEY); 59 mController.mLiveData = mLiveData; 60 mController.mSlicePreference = mSlicePreference; 61 mUri = Uri.EMPTY; 62 } 63 64 @Test isAvailable_uriNull_returnFalse()65 public void isAvailable_uriNull_returnFalse() { 66 assertThat(mController.isAvailable()).isFalse(); 67 } 68 69 @Test isAvailable_uriNotNull_returnTrue()70 public void isAvailable_uriNotNull_returnTrue() { 71 mController.setSliceUri(mUri); 72 assertThat(mController.isAvailable()).isTrue(); 73 } 74 75 @Test onStart_registerObserver()76 public void onStart_registerObserver() { 77 mController.onStart(); 78 verify(mLiveData).observeForever(mController); 79 } 80 81 @Test onStop_unregisterObserver()82 public void onStop_unregisterObserver() { 83 mController.onStart(); 84 85 mController.onStop(); 86 verify(mLiveData).removeObserver(mController); 87 } 88 89 @Test onStop_unregisterObserverAndHasSecurityException_noCrash()90 public void onStop_unregisterObserverAndHasSecurityException_noCrash() { 91 LiveData<Slice> liveData = new LiveData<Slice>() { 92 @Override 93 public void removeObserver(@NonNull Observer<? super Slice> observer) { 94 super.removeObserver(observer); 95 throw new SecurityException("SecurityException Test"); 96 } 97 }; 98 mController.mLiveData = liveData; 99 mController.onStart(); 100 101 mController.onStop(); 102 } 103 104 @Test onChanged_nullSlice_updateSlice()105 public void onChanged_nullSlice_updateSlice() { 106 mController.onChanged(null); 107 108 verify(mController.mSlicePreference).onSliceUpdated(null); 109 } 110 }