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.lifecycle.LiveData; 28 import androidx.slice.Slice; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 import org.robolectric.RobolectricTestRunner; 36 import org.robolectric.RuntimeEnvironment; 37 38 @RunWith(RobolectricTestRunner.class) 39 public class SlicePreferenceControllerTest { 40 private static final String KEY = "slice_preference_key"; 41 42 @Mock 43 private LiveData<Slice> mLiveData; 44 @Mock 45 private SlicePreference mSlicePreference; 46 private Context mContext; 47 private SlicePreferenceController mController; 48 private Uri mUri; 49 50 51 @Before setUp()52 public void setUp() { 53 MockitoAnnotations.initMocks(this); 54 55 mContext = spy(RuntimeEnvironment.application); 56 mController = new SlicePreferenceController(mContext, KEY); 57 mController.mLiveData = mLiveData; 58 mController.mSlicePreference = mSlicePreference; 59 mUri = Uri.EMPTY; 60 } 61 62 @Test isAvailable_uriNull_returnFalse()63 public void isAvailable_uriNull_returnFalse() { 64 assertThat(mController.isAvailable()).isFalse(); 65 } 66 67 @Test isAvailable_uriNotNull_returnTrue()68 public void isAvailable_uriNotNull_returnTrue() { 69 mController.setSliceUri(mUri); 70 assertThat(mController.isAvailable()).isTrue(); 71 } 72 73 @Test onStart_registerObserver()74 public void onStart_registerObserver() { 75 mController.onStart(); 76 verify(mLiveData).observeForever(mController); 77 } 78 79 @Test onStop_unregisterObserver()80 public void onStop_unregisterObserver() { 81 mController.onStop(); 82 verify(mLiveData).removeObserver(mController); 83 } 84 85 @Test onChanged_nullSlice_updateSlice()86 public void onChanged_nullSlice_updateSlice() { 87 mController.onChanged(null); 88 89 verify(mController.mSlicePreference).onSliceUpdated(null); 90 } 91 }