Merge "Rewrite dream settings."

This commit is contained in:
Lucas Silva
2022-01-19 15:03:40 +00:00
committed by Android (Google) Code Review
16 changed files with 500 additions and 561 deletions

View File

@@ -1,92 +0,0 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.dream;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.os.UserManager;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.dream.DreamBackend;
import com.android.settingslib.dream.DreamBackend.DreamInfo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.util.ReflectionHelpers;
import java.util.Collections;
@RunWith(RobolectricTestRunner.class)
public class CurrentDreamPickerTest {
private static String COMPONENT_KEY = "mocked_component_name_string";
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private DreamBackend mBackend;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Activity mActivity;
@Mock
private UserManager mUserManager;
private CurrentDreamPicker mPicker;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
FakeFeatureFactory.setupForTest();
mPicker = new CurrentDreamPicker();
mPicker.onAttach(mActivity);
ReflectionHelpers.setField(mPicker, "mBackend", mBackend);
}
@Test
public void getDefaultShouldReturnActiveDream() {
ComponentName mockComponentName = mock(ComponentName.class);
when(mockComponentName.flattenToString()).thenReturn(COMPONENT_KEY);
when(mBackend.getActiveDream()).thenReturn(mockComponentName);
assertThat(mPicker.getDefaultKey()).isEqualTo(COMPONENT_KEY);
}
@Test
public void setDefaultShouldUpdateActiveDream() {
DreamInfo mockInfo = mock(DreamInfo.class);
ComponentName mockName = mock(ComponentName.class);
mockInfo.componentName = mockName;
when(mockName.flattenToString()).thenReturn(COMPONENT_KEY);
when(mBackend.getDreamInfos()).thenReturn(Collections.singletonList(mockInfo));
mPicker.setDefaultKey(COMPONENT_KEY);
verify(mBackend).setActiveDream(mockName);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 The Android Open Source Project
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,94 +18,92 @@ package com.android.settings.dream;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.ComponentName;
import android.content.Context;
import android.widget.Button;
import com.android.settings.widget.GearPreference;
import androidx.preference.PreferenceScreen;
import androidx.recyclerview.widget.RecyclerView;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settingslib.dream.DreamBackend;
import com.android.settingslib.dream.DreamBackend.DreamInfo;
import com.android.settingslib.widget.LayoutPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList;
import java.util.Collections;
@RunWith(RobolectricTestRunner.class)
public class CurrentDreamPreferenceControllerTest {
private CurrentDreamPreferenceController mController;
public class DreamPickerControllerTest {
private DreamPickerController mController;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private DreamBackend mBackend;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock
private DreamInfo mDreamInfo;
private PreferenceScreen mScreen;
private LayoutPreference mPreference;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mContext = ApplicationProvider.getApplicationContext();
mController = new CurrentDreamPreferenceController(mContext, "test");
ReflectionHelpers.setField(mController, "mBackend", mBackend);
mPreference = new LayoutPreference(mContext, R.layout.dream_picker_layout);
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
mController = new DreamPickerController(
mContext,
/* preferenceKey= */ "test",
mBackend);
mController.displayPreference(mScreen);
}
@Test
public void isDisabledIfNoDreamsAvailable() {
when(mBackend.getDreamInfos()).thenReturn(new ArrayList<>(0));
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isEnabledIfDreamsAvailable() {
when(mBackend.getDreamInfos()).thenReturn(Collections.singletonList(mDreamInfo));
when(mBackend.getDreamInfos()).thenReturn(Collections.singletonList(new DreamInfo()));
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void gearShowsIfActiveDreamInfoHasOptions() {
mDreamInfo.settingsComponentName = mock(ComponentName.class);
mDreamInfo.isActive = true;
public void testDreamDisplayedInList() {
when(mBackend.getDreamInfos()).thenReturn(Collections.singletonList(new DreamInfo()));
mController.updateState(mPreference);
when(mBackend.getDreamInfos()).thenReturn(Collections.singletonList(mDreamInfo));
GearPreference mockPref = mock(GearPreference.class);
ArgumentCaptor<GearPreference.OnGearClickListener> captor =
ArgumentCaptor.forClass(GearPreference.OnGearClickListener.class);
// verify that updateState sets a non-null gear click listener
mController.updateState(mockPref);
verify(mockPref).setOnGearClickListener(captor.capture());
captor.getAllValues().forEach(listener -> assertThat(listener).isNotNull());
RecyclerView view = mPreference.findViewById(R.id.dream_list);
assertThat(view.getAdapter().getItemCount()).isEqualTo(1);
}
@Test
public void gearHidesIfActiveDreamInfoHasNoOptions() {
mDreamInfo.settingsComponentName = null;
mDreamInfo.isActive = true;
public void testPreviewButton() {
final DreamInfo mockDreamInfo = new DreamInfo();
mockDreamInfo.componentName = new ComponentName("package", "class");
mockDreamInfo.isActive = true;
when(mBackend.getDreamInfos()).thenReturn(Collections.singletonList(mDreamInfo));
when(mBackend.getDreamInfos()).thenReturn(Collections.singletonList(mockDreamInfo));
mController.updateState(mPreference);
GearPreference mockPref = mock(GearPreference.class);
ArgumentCaptor<GearPreference.OnGearClickListener> captor =
ArgumentCaptor.forClass(GearPreference.OnGearClickListener.class);
// setting a null onGearClickListener removes the gear from view
mController.updateState(mockPref);
verify(mockPref).setOnGearClickListener(captor.capture());
captor.getAllValues().forEach(listener -> assertThat(listener).isNull());
Button view = mPreference.findViewById(R.id.preview_button);
view.performClick();
verify(mBackend).preview(mockDreamInfo);
}
}

View File

@@ -1,87 +0,0 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.dream;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.widget.Button;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settingslib.dream.DreamBackend;
import com.android.settingslib.widget.LayoutPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class StartNowPreferenceControllerTest {
private StartNowPreferenceController mController;
private Context mContext;
@Mock
private PreferenceScreen mScreen;
@Mock
private LayoutPreference mLayoutPref;
@Mock
private Button mButton;
@Mock
private DreamBackend mBackend;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mController = new StartNowPreferenceController(mContext, "key");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mLayoutPref);
when(mLayoutPref.findViewById(R.id.dream_start_now_button)).thenReturn(mButton);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
}
@Test
public void updateState_neverDreaming_buttonShouldDidabled() {
when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.NEVER);
mController.displayPreference(mScreen);
mController.updateState(mLayoutPref);
verify(mButton).setEnabled(false);
}
@Test
public void updateState_dreamIsAvailable_buttonShouldEnabled() {
when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.EITHER);
mController.displayPreference(mScreen);
mController.updateState(mLayoutPref);
verify(mButton).setEnabled(true);
}
}