2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.settings.core;
18 import static com.google.common.truth.Truth.assertThat;
20 import android.content.Context;
22 import androidx.preference.SwitchPreference;
24 import com.android.settings.slices.SliceData;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.robolectric.RobolectricTestRunner;
30 import org.robolectric.RuntimeEnvironment;
32 @RunWith(RobolectricTestRunner.class)
33 public class TogglePreferenceControllerTest {
35 private FakeToggle mToggleController;
37 private Context mContext;
38 private SwitchPreference mPreference;
42 mContext = RuntimeEnvironment.application;
43 mPreference = new SwitchPreference(mContext);
44 mToggleController = new FakeToggle(mContext, "key");
48 public void testSetsPreferenceValue_setsChecked() {
49 mToggleController.setChecked(true);
50 mPreference.setChecked(false);
52 mToggleController.updateState(mPreference);
54 assertThat(mPreference.isChecked()).isTrue();
58 public void testSetsPreferenceValue_setsNotChecked() {
59 mToggleController.setChecked(false);
60 mPreference.setChecked(true);
62 mToggleController.updateState(mPreference);
64 assertThat(mPreference.isChecked()).isFalse();
68 public void testUpdatesPreferenceOnChange_turnsOn() {
69 boolean newValue = true;
70 mToggleController.setChecked(!newValue);
72 mToggleController.onPreferenceChange(mPreference, newValue);
74 assertThat(mToggleController.isChecked()).isEqualTo(newValue);
78 public void testUpdatesPreferenceOnChange_turnsOff() {
79 boolean newValue = false;
80 mToggleController.setChecked(!newValue);
82 mToggleController.onPreferenceChange(mPreference, newValue);
84 assertThat(mToggleController.isChecked()).isEqualTo(newValue);
88 public void testSliceType_returnsSliceType() {
89 assertThat(mToggleController.getSliceType()).isEqualTo(
90 SliceData.SliceType.SWITCH);
94 public void isSliceable_returnTrue() {
95 assertThat(mToggleController.isSliceable()).isTrue();
99 public void isPublicSlice_returnFalse() {
100 assertThat(mToggleController.isPublicSlice()).isFalse();
103 private static class FakeToggle extends TogglePreferenceController {
105 private boolean checkedFlag;
107 private FakeToggle(Context context, String preferenceKey) {
108 super(context, preferenceKey);
112 public boolean isChecked() {
117 public boolean setChecked(boolean isChecked) {
118 checkedFlag = isChecked;
123 public int getAvailabilityStatus() {