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.dream; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.when; 24 25 import android.content.ComponentName; 26 import android.content.Context; 27 28 import com.android.settings.widget.GearPreference; 29 import com.android.settingslib.dream.DreamBackend; 30 import com.android.settingslib.dream.DreamBackend.DreamInfo; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.Answers; 36 import org.mockito.ArgumentCaptor; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.RobolectricTestRunner; 40 import org.robolectric.util.ReflectionHelpers; 41 42 import java.util.ArrayList; 43 import java.util.Collections; 44 45 @RunWith(RobolectricTestRunner.class) 46 public class CurrentDreamPreferenceControllerTest { 47 48 private CurrentDreamPreferenceController mController; 49 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 50 private DreamBackend mBackend; 51 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 52 private Context mContext; 53 @Mock 54 private DreamInfo mDreamInfo; 55 56 @Before setup()57 public void setup() { 58 MockitoAnnotations.initMocks(this); 59 60 mController = new CurrentDreamPreferenceController(mContext, "test"); 61 ReflectionHelpers.setField(mController, "mBackend", mBackend); 62 } 63 64 @Test isDisabledIfNoDreamsAvailable()65 public void isDisabledIfNoDreamsAvailable() { 66 when(mBackend.getDreamInfos()).thenReturn(new ArrayList<>(0)); 67 68 assertThat(mController.isAvailable()).isFalse(); 69 } 70 71 @Test isEnabledIfDreamsAvailable()72 public void isEnabledIfDreamsAvailable() { 73 when(mBackend.getDreamInfos()).thenReturn(Collections.singletonList(mDreamInfo)); 74 75 assertThat(mController.isAvailable()).isTrue(); 76 } 77 78 @Test gearShowsIfActiveDreamInfoHasOptions()79 public void gearShowsIfActiveDreamInfoHasOptions() { 80 mDreamInfo.settingsComponentName = mock(ComponentName.class); 81 mDreamInfo.isActive = true; 82 83 when(mBackend.getDreamInfos()).thenReturn(Collections.singletonList(mDreamInfo)); 84 85 GearPreference mockPref = mock(GearPreference.class); 86 ArgumentCaptor<GearPreference.OnGearClickListener> captor = 87 ArgumentCaptor.forClass(GearPreference.OnGearClickListener.class); 88 89 // verify that updateState sets a non-null gear click listener 90 mController.updateState(mockPref); 91 verify(mockPref).setOnGearClickListener(captor.capture()); 92 captor.getAllValues().forEach(listener -> assertThat(listener).isNotNull()); 93 } 94 95 @Test gearHidesIfActiveDreamInfoHasNoOptions()96 public void gearHidesIfActiveDreamInfoHasNoOptions() { 97 mDreamInfo.settingsComponentName = null; 98 mDreamInfo.isActive = true; 99 100 when(mBackend.getDreamInfos()).thenReturn(Collections.singletonList(mDreamInfo)); 101 102 GearPreference mockPref = mock(GearPreference.class); 103 ArgumentCaptor<GearPreference.OnGearClickListener> captor = 104 ArgumentCaptor.forClass(GearPreference.OnGearClickListener.class); 105 106 // setting a null onGearClickListener removes the gear from view 107 mController.updateState(mockPref); 108 verify(mockPref).setOnGearClickListener(captor.capture()); 109 captor.getAllValues().forEach(listener -> assertThat(listener).isNull()); 110 } 111 } 112