1 /* 2 * Copyright (C) 2022 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.ArgumentMatchers.anyString; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 26 import androidx.preference.PreferenceScreen; 27 import androidx.recyclerview.widget.RecyclerView; 28 import androidx.test.core.app.ApplicationProvider; 29 30 import com.android.settings.R; 31 import com.android.settingslib.dream.DreamBackend; 32 import com.android.settingslib.dream.DreamBackend.DreamInfo; 33 import com.android.settingslib.widget.LayoutPreference; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Answers; 39 import org.mockito.Mock; 40 import org.mockito.MockitoAnnotations; 41 import org.robolectric.RobolectricTestRunner; 42 43 import java.util.ArrayList; 44 import java.util.Collections; 45 46 @RunWith(RobolectricTestRunner.class) 47 public class DreamPickerControllerTest { 48 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 49 private DreamBackend mBackend; 50 private Context mContext; 51 @Mock 52 private PreferenceScreen mScreen; 53 private LayoutPreference mPreference; 54 55 @Before setup()56 public void setup() { 57 MockitoAnnotations.initMocks(this); 58 mContext = ApplicationProvider.getApplicationContext(); 59 60 mPreference = new LayoutPreference(mContext, R.layout.dream_picker_layout); 61 when(mScreen.findPreference(anyString())).thenReturn(mPreference); 62 } 63 buildController()64 private DreamPickerController buildController() { 65 final DreamPickerController controller = new DreamPickerController(mContext, mBackend); 66 controller.displayPreference(mScreen); 67 return controller; 68 } 69 70 @Test isDisabledIfNoDreamsAvailable()71 public void isDisabledIfNoDreamsAvailable() { 72 when(mBackend.getDreamInfos()).thenReturn(new ArrayList<>(0)); 73 final DreamPickerController controller = buildController(); 74 assertThat(controller.isAvailable()).isFalse(); 75 } 76 77 @Test isEnabledIfDreamsAvailable()78 public void isEnabledIfDreamsAvailable() { 79 when(mBackend.getDreamInfos()).thenReturn(Collections.singletonList(new DreamInfo())); 80 final DreamPickerController controller = buildController(); 81 assertThat(controller.isAvailable()).isTrue(); 82 } 83 84 @Test testDreamDisplayedInList()85 public void testDreamDisplayedInList() { 86 when(mBackend.getDreamInfos()).thenReturn(Collections.singletonList(new DreamInfo())); 87 final DreamPickerController controller = buildController(); 88 controller.updateState(mPreference); 89 90 RecyclerView view = mPreference.findViewById(R.id.dream_list); 91 assertThat(view.getAdapter().getItemCount()).isEqualTo(1); 92 } 93 } 94