Add video to each gesture preference screen.

- Refactor GesturePreference to a generic VideoPreference.
- The old video_preference.xml is only for magnification video, so
  renamed.
- And use VideoPreference in gesture setting pages.
- Refactor common logic into GesturePreferenceController.

Bug: 32637613
Test: RunSettingsRoboTests

Change-Id: I58580b01a32873cb32c5dc5bf2ec021d5b1400cc
This commit is contained in:
Fan Zhang
2016-11-09 11:35:10 -08:00
parent 0d38cea6c9
commit 33b0d91d74
31 changed files with 724 additions and 466 deletions

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2016 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.
-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/video_background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/accessibility_screen_magnification_background"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:importantForAccessibility="noHideDescendants" />
<VideoView
android:id="@+id/video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/video_background"
android:layout_centerHorizontal="true"
android:importantForAccessibility="noHideDescendants" />
</RelativeLayout>

View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2016 The Android Open Source Project <!--
Copyright (C) 2016 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -12,27 +13,45 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
--> -->
<RelativeLayout <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gesture_animation_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:background="@color/gestures_setting_background_color"
android:clipToPadding="false"
android:gravity="center"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:orientation="horizontal">
<com.android.settings.widget.AspectRatioFrameLayout
android:layout_width="240dp"
android:layout_height="wrap_content"
android:padding="@dimen/gesture_animation_padding">
<TextureView
android:id="@+id/video_texture_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"/>
<ImageView <ImageView
android:id="@+id/video_background" android:id="@+id/video_preview_image"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:src="@drawable/accessibility_screen_magnification_background" android:scaleType="fitXY"/>
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:importantForAccessibility="noHideDescendants" />
<VideoView <ImageView
android:id="@+id/video" android:id="@+id/video_play_button"
android:layout_width="wrap_content" android:layout_width="@dimen/gestures_play_button_size"
android:layout_height="wrap_content" android:layout_height="@dimen/gestures_play_button_size"
android:layout_alignTop="@id/video_background" android:src="@drawable/ic_gesture_play_button"
android:layout_centerHorizontal="true" android:gravity="center"
android:importantForAccessibility="noHideDescendants" /> android:layout_gravity="center"/>
</com.android.settings.widget.AspectRatioFrameLayout>
</LinearLayout>
</RelativeLayout>

View File

@@ -142,6 +142,11 @@
<attr name="preview" format="reference" /> <attr name="preview" format="reference" />
</declare-styleable> </declare-styleable>
<declare-styleable name="VideoPreference">
<attr name="animation" format="reference" />
<attr name="preview" format="reference" />
</declare-styleable>
<!-- For AspectRatioFrameLayout --> <!-- For AspectRatioFrameLayout -->
<declare-styleable name="AspectRatioFrameLayout"> <declare-styleable name="AspectRatioFrameLayout">
<attr name="aspectRatio" format="float" /> <attr name="aspectRatio" format="float" />

View File

@@ -15,7 +15,13 @@
limitations under the License. limitations under the License.
--> -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.android.settings.widget.VideoPreference
android:key="gesture_double_tap_power_video"
app:animation="@raw/gesture_double_tap"
app:preview="@drawable/gesture_double_tap"/>
<SwitchPreference <SwitchPreference
android:key="gesture_double_tap_power" android:key="gesture_double_tap_power"

View File

@@ -15,7 +15,13 @@
limitations under the License. limitations under the License.
--> -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.android.settings.widget.VideoPreference
android:key="gesture_double_tap_screen_video"
app:animation="@raw/gesture_ambient_tap"
app:preview="@drawable/gesture_ambient_tap"/>
<SwitchPreference <SwitchPreference
android:key="gesture_double_tap_screen" android:key="gesture_double_tap_screen"

View File

@@ -15,7 +15,13 @@
limitations under the License. limitations under the License.
--> -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.android.settings.widget.VideoPreference
android:key="gesture_double_twist_video"
app:animation="@raw/gesture_twist"
app:preview="@drawable/gesture_twist"/>
<SwitchPreference <SwitchPreference
android:key="gesture_double_twist" android:key="gesture_double_twist"

View File

@@ -15,7 +15,13 @@
limitations under the License. limitations under the License.
--> -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.android.settings.widget.VideoPreference
android:key="gesture_pick_up_video"
app:animation="@raw/gesture_ambient_lift"
app:preview="@drawable/gesture_ambient_lift"/>
<SwitchPreference <SwitchPreference
android:key="gesture_pick_up" android:key="gesture_pick_up"

View File

@@ -15,7 +15,13 @@
limitations under the License. limitations under the License.
--> -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.android.settings.widget.VideoPreference
android:key="gesture_swipe_down_fingerprint_video"
app:animation="@raw/gesture_fingerprint_swipe"
app:preview="@drawable/gesture_fingerprint_swipe"/>
<SwitchPreference <SwitchPreference
android:key="gesture_swipe_down_fingerprint" android:key="gesture_swipe_down_fingerprint"

View File

@@ -120,7 +120,7 @@ public class ToggleScreenMagnificationPreferenceFragment extends ToggleFeaturePr
mVideoPreference = new VideoPreference(getPrefContext()); mVideoPreference = new VideoPreference(getPrefContext());
mVideoPreference.setSelectable(false); mVideoPreference.setSelectable(false);
mVideoPreference.setPersistent(false); mVideoPreference.setPersistent(false);
mVideoPreference.setLayoutResource(R.layout.video_preference); mVideoPreference.setLayoutResource(R.layout.magnification_video_preference);
final PreferenceScreen preferenceScreen = getPreferenceManager().getPreferenceScreen(); final PreferenceScreen preferenceScreen = getPreferenceManager().getPreferenceScreen();
preferenceScreen.setOrderingAsAdded(false); preferenceScreen.setOrderingAsAdded(false);

View File

@@ -60,6 +60,13 @@ public abstract class ObservablePreferenceFragment extends PreferenceFragment {
super.onStart(); super.onStart();
} }
@CallSuper
@Override
public void onStop() {
mLifecycle.onStop();
super.onStop();
}
@CallSuper @CallSuper
@Override @Override
public void onResume() { public void onResume() {

View File

@@ -19,17 +19,16 @@ package com.android.settings.gestures;
import android.content.Context; import android.content.Context;
import android.provider.Settings; import android.provider.Settings;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.TwoStatePreference;
import com.android.settings.core.PreferenceController; import com.android.settings.core.lifecycle.Lifecycle;
public class DoubleTapPowerPreferenceController extends PreferenceController public class DoubleTapPowerPreferenceController extends GesturePreferenceController {
implements Preference.OnPreferenceChangeListener {
private static final String PREF_KEY_VIDEO = "gesture_double_tap_power_video";
private static final String PREF_KEY_DOUBLE_TAP_POWER = "gesture_double_tap_power"; private static final String PREF_KEY_DOUBLE_TAP_POWER = "gesture_double_tap_power";
public DoubleTapPowerPreferenceController(Context context) { public DoubleTapPowerPreferenceController(Context context, Lifecycle lifecycle) {
super(context); super(context, lifecycle);
} }
@Override @Override
@@ -39,8 +38,8 @@ public class DoubleTapPowerPreferenceController extends PreferenceController
} }
@Override @Override
public boolean handlePreferenceTreeClick(Preference preference) { protected String getVideoPrefKey() {
return false; return PREF_KEY_VIDEO;
} }
@Override @Override
@@ -48,20 +47,6 @@ public class DoubleTapPowerPreferenceController extends PreferenceController
return PREF_KEY_DOUBLE_TAP_POWER; return PREF_KEY_DOUBLE_TAP_POWER;
} }
@Override
public void updateState(Preference preference) {
final boolean isEnabled = isDoubleTapEnabled();
if (preference != null) {
if (preference instanceof TwoStatePreference) {
((TwoStatePreference) preference).setChecked(isEnabled);
} else {
preference.setSummary(isEnabled
? com.android.settings.R.string.gesture_setting_on
: com.android.settings.R.string.gesture_setting_off);
}
}
}
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
boolean enabled = (boolean) newValue; boolean enabled = (boolean) newValue;
@@ -70,7 +55,8 @@ public class DoubleTapPowerPreferenceController extends PreferenceController
return true; return true;
} }
private boolean isDoubleTapEnabled() { @Override
protected boolean isSwitchPrefEnabled() {
final int cameraDisabled = Settings.Secure.getInt(mContext.getContentResolver(), final int cameraDisabled = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 0); Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 0);
return cameraDisabled == 0; return cameraDisabled == 0;

View File

@@ -52,7 +52,7 @@ public class DoubleTapPowerSettings extends DashboardFragment {
@Override @Override
protected List<PreferenceController> getPreferenceControllers(Context context) { protected List<PreferenceController> getPreferenceControllers(Context context) {
final List<PreferenceController> controllers = new ArrayList<>(); final List<PreferenceController> controllers = new ArrayList<>();
controllers.add(new DoubleTapPowerPreferenceController(context)); controllers.add(new DoubleTapPowerPreferenceController(context, getLifecycle()));
return controllers; return controllers;
} }
} }

View File

@@ -20,23 +20,22 @@ import android.annotation.UserIdInt;
import android.content.Context; import android.content.Context;
import android.provider.Settings; import android.provider.Settings;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.TwoStatePreference;
import com.android.internal.hardware.AmbientDisplayConfiguration; import com.android.internal.hardware.AmbientDisplayConfiguration;
import com.android.settings.core.PreferenceController; import com.android.settings.core.lifecycle.Lifecycle;
public class DoubleTapScreenPreferenceController extends PreferenceController public class DoubleTapScreenPreferenceController extends GesturePreferenceController {
implements Preference.OnPreferenceChangeListener {
private static final String PREF_KEY_VIDEO = "gesture_double_tap_screen_video";
private static final String PREF_KEY_DOUBLE_TAP_SCREEN = "gesture_double_tap_screen"; private static final String PREF_KEY_DOUBLE_TAP_SCREEN = "gesture_double_tap_screen";
private final AmbientDisplayConfiguration mAmbientConfig; private final AmbientDisplayConfiguration mAmbientConfig;
@UserIdInt @UserIdInt
private final int mUserId; private final int mUserId;
public DoubleTapScreenPreferenceController(Context context, AmbientDisplayConfiguration config, public DoubleTapScreenPreferenceController(Context context, Lifecycle lifecycle,
@UserIdInt int userId) { AmbientDisplayConfiguration config, @UserIdInt int userId) {
super(context); super(context, lifecycle);
mAmbientConfig = config; mAmbientConfig = config;
mUserId = userId; mUserId = userId;
} }
@@ -46,25 +45,6 @@ public class DoubleTapScreenPreferenceController extends PreferenceController
return mAmbientConfig.pulseOnDoubleTapAvailable(); return mAmbientConfig.pulseOnDoubleTapAvailable();
} }
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
return false;
}
@Override
public void updateState(Preference preference) {
final boolean isEnabled = mAmbientConfig.pulseOnDoubleTapEnabled(mUserId);
if (preference != null) {
if (preference instanceof TwoStatePreference) {
((TwoStatePreference) preference).setChecked(isEnabled);
} else {
preference.setSummary(isEnabled
? com.android.settings.R.string.gesture_setting_on
: com.android.settings.R.string.gesture_setting_off);
}
}
}
@Override @Override
public String getPreferenceKey() { public String getPreferenceKey() {
return PREF_KEY_DOUBLE_TAP_SCREEN; return PREF_KEY_DOUBLE_TAP_SCREEN;
@@ -77,4 +57,14 @@ public class DoubleTapScreenPreferenceController extends PreferenceController
Settings.Secure.DOZE_PULSE_ON_DOUBLE_TAP, enabled ? 1 : 0); Settings.Secure.DOZE_PULSE_ON_DOUBLE_TAP, enabled ? 1 : 0);
return true; return true;
} }
@Override
protected String getVideoPrefKey() {
return PREF_KEY_VIDEO;
}
@Override
protected boolean isSwitchPrefEnabled() {
return mAmbientConfig.pulseOnDoubleTapEnabled(mUserId);
}
} }

View File

@@ -54,8 +54,8 @@ public class DoubleTapScreenSettings extends DashboardFragment {
@Override @Override
protected List<PreferenceController> getPreferenceControllers(Context context) { protected List<PreferenceController> getPreferenceControllers(Context context) {
final List<PreferenceController> controllers = new ArrayList<>(); final List<PreferenceController> controllers = new ArrayList<>();
controllers.add(new DoubleTapScreenPreferenceController( controllers.add(new DoubleTapScreenPreferenceController(context, getLifecycle(),
context, new AmbientDisplayConfiguration(context), UserHandle.myUserId())); new AmbientDisplayConfiguration(context), UserHandle.myUserId()));
return controllers; return controllers;
} }
} }

View File

@@ -52,7 +52,7 @@ public class DoubleTwistGestureSettings extends DashboardFragment {
@Override @Override
protected List<PreferenceController> getPreferenceControllers(Context context) { protected List<PreferenceController> getPreferenceControllers(Context context) {
final List<PreferenceController> controllers = new ArrayList<>(); final List<PreferenceController> controllers = new ArrayList<>();
controllers.add(new DoubleTwistPreferenceController(context)); controllers.add(new DoubleTwistPreferenceController(context, getLifecycle()));
return controllers; return controllers;
} }
} }

View File

@@ -22,19 +22,18 @@ import android.hardware.Sensor;
import android.hardware.SensorManager; import android.hardware.SensorManager;
import android.provider.Settings; import android.provider.Settings;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.TwoStatePreference;
import android.text.TextUtils; import android.text.TextUtils;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.PreferenceController; import com.android.settings.core.lifecycle.Lifecycle;
public class DoubleTwistPreferenceController extends PreferenceController public class DoubleTwistPreferenceController extends GesturePreferenceController {
implements Preference.OnPreferenceChangeListener {
private static final String PREF_KEY_VIDEO = "gesture_double_twist_video";
private static final String PREF_KEY_DOUBLE_TWIST = "gesture_double_twist"; private static final String PREF_KEY_DOUBLE_TWIST = "gesture_double_twist";
public DoubleTwistPreferenceController(Context context) { public DoubleTwistPreferenceController(Context context, Lifecycle lifecycle) {
super(context); super(context, lifecycle);
} }
@Override @Override
@@ -44,22 +43,8 @@ public class DoubleTwistPreferenceController extends PreferenceController
} }
@Override @Override
public void updateState(Preference preference) { protected String getVideoPrefKey() {
final boolean isEnabled = isDoubleTwistEnabled(); return PREF_KEY_VIDEO;
if (preference != null) {
if (preference instanceof TwoStatePreference) {
((TwoStatePreference) preference).setChecked(isEnabled);
} else {
preference.setSummary(isEnabled
? com.android.settings.R.string.gesture_setting_on
: com.android.settings.R.string.gesture_setting_off);
}
}
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
return false;
} }
@Override @Override
@@ -67,7 +52,16 @@ public class DoubleTwistPreferenceController extends PreferenceController
return PREF_KEY_DOUBLE_TWIST; return PREF_KEY_DOUBLE_TWIST;
} }
private boolean isDoubleTwistEnabled() { @Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean enabled = (boolean) newValue;
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, enabled ? 1 : 0);
return true;
}
@Override
protected boolean isSwitchPrefEnabled() {
final int doubleTwistEnabled = Settings.Secure.getInt(mContext.getContentResolver(), final int doubleTwistEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 1); Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 1);
return doubleTwistEnabled != 0; return doubleTwistEnabled != 0;
@@ -88,12 +82,4 @@ public class DoubleTwistPreferenceController extends PreferenceController
} }
return false; return false;
} }
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean enabled = (boolean) newValue;
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, enabled ? 1 : 0);
return true;
}
} }

View File

@@ -0,0 +1,89 @@
/*
* Copyright (C) 2016 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.gestures;
import android.content.Context;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.support.v7.preference.TwoStatePreference;
import com.android.settings.R;
import com.android.settings.core.PreferenceController;
import com.android.settings.core.lifecycle.Lifecycle;
import com.android.settings.core.lifecycle.LifecycleObserver;
import com.android.settings.core.lifecycle.events.OnStart;
import com.android.settings.core.lifecycle.events.OnStop;
import com.android.settings.widget.VideoPreference;
public abstract class GesturePreferenceController extends PreferenceController
implements Preference.OnPreferenceChangeListener, LifecycleObserver, OnStart, OnStop {
private VideoPreference mVideoPreference;
public GesturePreferenceController(Context context, Lifecycle lifecycle) {
super(context);
if (lifecycle != null) {
lifecycle.addObserver(this);
}
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
if (isAvailable()) {
mVideoPreference = (VideoPreference) screen.findPreference(getVideoPrefKey());
}
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
final boolean isEnabled = isSwitchPrefEnabled();
if (preference != null) {
if (preference instanceof TwoStatePreference) {
((TwoStatePreference) preference).setChecked(isEnabled);
} else {
preference.setSummary(isEnabled
? R.string.gesture_setting_on
: R.string.gesture_setting_off);
}
}
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
return false;
}
@Override
public void onStop() {
if (mVideoPreference != null) {
mVideoPreference.onViewInvisible();
}
}
@Override
public void onStart() {
if (mVideoPreference != null) {
mVideoPreference.onViewVisible();
}
}
protected abstract String getVideoPrefKey();
protected abstract boolean isSwitchPrefEnabled();
}

View File

@@ -30,6 +30,7 @@ import com.android.internal.hardware.AmbientDisplayConfiguration;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.PreferenceController; import com.android.settings.core.PreferenceController;
import com.android.settings.core.lifecycle.Lifecycle;
import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable; import com.android.settings.search.Indexable;
@@ -56,13 +57,14 @@ public class GestureSettings extends DashboardFragment {
protected List<PreferenceController> getPreferenceControllers(Context context) { protected List<PreferenceController> getPreferenceControllers(Context context) {
final AmbientDisplayConfiguration ambientConfig = new AmbientDisplayConfiguration(context); final AmbientDisplayConfiguration ambientConfig = new AmbientDisplayConfiguration(context);
final List<PreferenceController> controllers = new ArrayList<>(); final List<PreferenceController> controllers = new ArrayList<>();
controllers.add(new SwipeToNotificationPreferenceController(context)); final Lifecycle lifecycle = getLifecycle();
controllers.add(new DoubleTapPowerPreferenceController(context)); controllers.add(new SwipeToNotificationPreferenceController(context, lifecycle));
controllers.add(new DoubleTwistPreferenceController(context)); controllers.add(new DoubleTapPowerPreferenceController(context, lifecycle));
controllers.add(new DoubleTwistPreferenceController(context, lifecycle));
controllers.add(new PickupGesturePreferenceController( controllers.add(new PickupGesturePreferenceController(
context, ambientConfig, UserHandle.myUserId())); context, lifecycle, ambientConfig, UserHandle.myUserId()));
controllers.add(new DoubleTapScreenPreferenceController( controllers.add(new DoubleTapScreenPreferenceController(
context, ambientConfig, UserHandle.myUserId())); context, lifecycle, ambientConfig, UserHandle.myUserId()));
return controllers; return controllers;
} }
@@ -167,17 +169,17 @@ public class GestureSettings extends DashboardFragment {
ArrayList<String> result = new ArrayList<String>(); ArrayList<String> result = new ArrayList<String>();
AmbientDisplayConfiguration ambientConfig AmbientDisplayConfiguration ambientConfig
= new AmbientDisplayConfiguration(context); = new AmbientDisplayConfiguration(context);
new DoubleTapPowerPreferenceController(context) new DoubleTapPowerPreferenceController(context, null /* lifecycle */)
.updateNonIndexableKeys(result); .updateNonIndexableKeys(result);
new PickupGesturePreferenceController( new PickupGesturePreferenceController(
context, ambientConfig, UserHandle.myUserId()) context, null /* lifecycle */, ambientConfig, UserHandle.myUserId())
.updateNonIndexableKeys(result); .updateNonIndexableKeys(result);
new DoubleTapScreenPreferenceController( new DoubleTapScreenPreferenceController(
context, ambientConfig, UserHandle.myUserId()) context, null /* lifecycle */, ambientConfig, UserHandle.myUserId())
.updateNonIndexableKeys(result); .updateNonIndexableKeys(result);
new SwipeToNotificationPreferenceController(context) new SwipeToNotificationPreferenceController(context, null /* lifecycle */)
.updateNonIndexableKeys(result); .updateNonIndexableKeys(result);
new DoubleTwistPreferenceController(context) new DoubleTwistPreferenceController(context, null /* lifecycle */)
.updateNonIndexableKeys(result); .updateNonIndexableKeys(result);
return result; return result;
} }

View File

@@ -20,23 +20,22 @@ import android.annotation.UserIdInt;
import android.content.Context; import android.content.Context;
import android.provider.Settings; import android.provider.Settings;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.TwoStatePreference;
import com.android.internal.hardware.AmbientDisplayConfiguration; import com.android.internal.hardware.AmbientDisplayConfiguration;
import com.android.settings.core.PreferenceController; import com.android.settings.core.lifecycle.Lifecycle;
public class PickupGesturePreferenceController extends PreferenceController public class PickupGesturePreferenceController extends GesturePreferenceController {
implements Preference.OnPreferenceChangeListener {
private static final String PREF_VIDEO_KEY = "gesture_pick_up_video";
private static final String PREF_KEY_PICK_UP = "gesture_pick_up"; private static final String PREF_KEY_PICK_UP = "gesture_pick_up";
private final AmbientDisplayConfiguration mAmbientConfig; private final AmbientDisplayConfiguration mAmbientConfig;
@UserIdInt @UserIdInt
private final int mUserId; private final int mUserId;
public PickupGesturePreferenceController(Context context, AmbientDisplayConfiguration config, public PickupGesturePreferenceController(Context context, Lifecycle lifecycle,
@UserIdInt int userId) { AmbientDisplayConfiguration config, @UserIdInt int userId) {
super(context); super(context, lifecycle);
mAmbientConfig = config; mAmbientConfig = config;
mUserId = userId; mUserId = userId;
} }
@@ -47,8 +46,13 @@ public class PickupGesturePreferenceController extends PreferenceController
} }
@Override @Override
public boolean handlePreferenceTreeClick(Preference preference) { protected String getVideoPrefKey() {
return false; return PREF_VIDEO_KEY;
}
@Override
protected boolean isSwitchPrefEnabled() {
return mAmbientConfig.pulseOnPickupEnabled(mUserId);
} }
@Override @Override
@@ -56,20 +60,6 @@ public class PickupGesturePreferenceController extends PreferenceController
return PREF_KEY_PICK_UP; return PREF_KEY_PICK_UP;
} }
@Override
public void updateState(Preference preference) {
final boolean isEnabled = mAmbientConfig.pulseOnPickupEnabled(mUserId);
if (preference != null) {
if (preference instanceof TwoStatePreference) {
((TwoStatePreference) preference).setChecked(isEnabled);
} else {
preference.setSummary(isEnabled
? com.android.settings.R.string.gesture_setting_on
: com.android.settings.R.string.gesture_setting_off);
}
}
}
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean enabled = (boolean) newValue; final boolean enabled = (boolean) newValue;

View File

@@ -54,8 +54,8 @@ public class PickupGestureSettings extends DashboardFragment {
@Override @Override
protected List<PreferenceController> getPreferenceControllers(Context context) { protected List<PreferenceController> getPreferenceControllers(Context context) {
final List<PreferenceController> controllers = new ArrayList<>(); final List<PreferenceController> controllers = new ArrayList<>();
controllers.add(new PickupGesturePreferenceController( controllers.add(new PickupGesturePreferenceController(context, getLifecycle(),
context, new AmbientDisplayConfiguration(context), UserHandle.myUserId())); new AmbientDisplayConfiguration(context), UserHandle.myUserId()));
return controllers; return controllers;
} }
} }

View File

@@ -19,18 +19,16 @@ package com.android.settings.gestures;
import android.content.Context; import android.content.Context;
import android.provider.Settings; import android.provider.Settings;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.TwoStatePreference;
import com.android.settings.R; import com.android.settings.core.lifecycle.Lifecycle;
import com.android.settings.core.PreferenceController;
public class SwipeToNotificationPreferenceController extends PreferenceController public class SwipeToNotificationPreferenceController extends GesturePreferenceController {
implements Preference.OnPreferenceChangeListener {
private static final String PREF_KEY_VIDEO = "gesture_swipe_down_fingerprint_video";
private static final String PREF_KEY_SWIPE_DOWN_FINGERPRINT = "gesture_swipe_down_fingerprint"; private static final String PREF_KEY_SWIPE_DOWN_FINGERPRINT = "gesture_swipe_down_fingerprint";
public SwipeToNotificationPreferenceController(Context context) { public SwipeToNotificationPreferenceController(Context context, Lifecycle lifecycle) {
super(context); super(context, lifecycle);
} }
@Override @Override
@@ -44,18 +42,8 @@ public class SwipeToNotificationPreferenceController extends PreferenceControlle
} }
@Override @Override
public void updateState(Preference preference) { protected String getVideoPrefKey() {
super.updateState(preference); return PREF_KEY_VIDEO;
final boolean isEnabled = isSystemUINavigationEnabled();
if (preference != null) {
if (preference instanceof TwoStatePreference) {
((TwoStatePreference) preference).setChecked(isEnabled);
} else {
preference.setSummary(isEnabled
? R.string.gesture_setting_on
: R.string.gesture_setting_off);
}
}
} }
@Override @Override
@@ -64,16 +52,17 @@ public class SwipeToNotificationPreferenceController extends PreferenceControlle
com.android.internal.R.bool.config_supportSystemNavigationKeys); com.android.internal.R.bool.config_supportSystemNavigationKeys);
} }
private boolean isSystemUINavigationEnabled() {
return Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.SYSTEM_NAVIGATION_KEYS_ENABLED, 0)
== 1;
}
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.SYSTEM_NAVIGATION_KEYS_ENABLED, (boolean) newValue ? 1 : 0); Settings.Secure.SYSTEM_NAVIGATION_KEYS_ENABLED, (boolean) newValue ? 1 : 0);
return true; return true;
} }
@Override
protected boolean isSwitchPrefEnabled() {
return Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.SYSTEM_NAVIGATION_KEYS_ENABLED, 0)
== 1;
}
} }

View File

@@ -52,7 +52,7 @@ public class SwipeToNotificationSettings extends DashboardFragment {
@Override @Override
protected List<PreferenceController> getPreferenceControllers(Context context) { protected List<PreferenceController> getPreferenceControllers(Context context) {
final List<PreferenceController> controllers = new ArrayList<>(); final List<PreferenceController> controllers = new ArrayList<>();
controllers.add(new SwipeToNotificationPreferenceController(context)); controllers.add(new SwipeToNotificationPreferenceController(context, getLifecycle()));
return controllers; return controllers;
} }
} }

View File

@@ -23,6 +23,7 @@ import android.support.annotation.VisibleForTesting;
import com.android.internal.hardware.AmbientDisplayConfiguration; import com.android.internal.hardware.AmbientDisplayConfiguration;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.PreferenceController; import com.android.settings.core.PreferenceController;
import com.android.settings.core.lifecycle.Lifecycle;
import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.gestures.DoubleTapPowerPreferenceController; import com.android.settings.gestures.DoubleTapPowerPreferenceController;
import com.android.settings.gestures.DoubleTapScreenPreferenceController; import com.android.settings.gestures.DoubleTapScreenPreferenceController;
@@ -62,6 +63,7 @@ public class InputAndGestureSettings extends DashboardFragment {
@Override @Override
protected List<PreferenceController> getPreferenceControllers(Context context) { protected List<PreferenceController> getPreferenceControllers(Context context) {
final Lifecycle lifecycle = getLifecycle();
final GameControllerPreferenceController gameControllerPreferenceController final GameControllerPreferenceController gameControllerPreferenceController
= new GameControllerPreferenceController(context); = new GameControllerPreferenceController(context);
getLifecycle().addObserver(gameControllerPreferenceController); getLifecycle().addObserver(gameControllerPreferenceController);
@@ -72,13 +74,14 @@ public class InputAndGestureSettings extends DashboardFragment {
final List<PreferenceController> controllers = new ArrayList<>(); final List<PreferenceController> controllers = new ArrayList<>();
controllers.add(gameControllerPreferenceController); controllers.add(gameControllerPreferenceController);
// Gestures // Gestures
controllers.add(new SwipeToNotificationPreferenceController(context));
controllers.add(new DoubleTwistPreferenceController(context)); controllers.add(new SwipeToNotificationPreferenceController(context, lifecycle));
controllers.add(new DoubleTapPowerPreferenceController(context)); controllers.add(new DoubleTwistPreferenceController(context, lifecycle));
controllers.add(new DoubleTapPowerPreferenceController(context, lifecycle));
controllers.add(new PickupGesturePreferenceController( controllers.add(new PickupGesturePreferenceController(
context, mAmbientDisplayConfig, UserHandle.myUserId())); context, lifecycle, mAmbientDisplayConfig, UserHandle.myUserId()));
controllers.add(new DoubleTapScreenPreferenceController( controllers.add(new DoubleTapScreenPreferenceController(
context, mAmbientDisplayConfig, UserHandle.myUserId())); context, lifecycle, mAmbientDisplayConfig, UserHandle.myUserId()));
return controllers; return controllers;
} }

View File

@@ -62,7 +62,7 @@ public class ConfigureNotificationSettings extends DashboardFragment {
mLockScreenNotificationController = new LockScreenNotificationPreferenceController(context); mLockScreenNotificationController = new LockScreenNotificationPreferenceController(context);
getLifecycle().addObserver(pulseController); getLifecycle().addObserver(pulseController);
getLifecycle().addObserver(mLockScreenNotificationController); getLifecycle().addObserver(mLockScreenNotificationController);
controllers.add(new SwipeToNotificationPreferenceController(context)); controllers.add(new SwipeToNotificationPreferenceController(context, getLifecycle()));
controllers.add(pulseController); controllers.add(pulseController);
controllers.add(mLockScreenNotificationController); controllers.add(mLockScreenNotificationController);
return controllers; return controllers;

View File

@@ -0,0 +1,163 @@
/*
* Copyright (C) 2016 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.widget;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceViewHolder;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.ImageView;
import com.android.settings.R;
/**
* A full width preference that hosts a MP4 video.
*/
public class VideoPreference extends Preference {
private static final String TAG = "VideoPreference";
private final Context mContext;
private Uri mVideoPath;
private MediaPlayer mMediaPlayer;
private boolean mAnimationAvailable;
private boolean mVideoReady;
private int mPreviewResource;
public VideoPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
TypedArray attributes = context.getTheme().obtainStyledAttributes(
attrs,
com.android.settings.R.styleable.VideoPreference,
0, 0);
try {
int animation = attributes.getResourceId(R.styleable.VideoPreference_animation, 0);
mVideoPath = new Uri.Builder().scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(context.getPackageName())
.appendPath(String.valueOf(animation))
.build();
mMediaPlayer = MediaPlayer.create(mContext, mVideoPath);
if (mMediaPlayer != null && mMediaPlayer.getDuration() > 0) {
setLayoutResource(R.layout.video_preference);
mPreviewResource = attributes.getResourceId(
R.styleable.VideoPreference_preview, 0);
mMediaPlayer.setOnSeekCompleteListener(mp -> mVideoReady = true);
mMediaPlayer.setOnPreparedListener(mediaPlayer -> mediaPlayer.setLooping(true));
mAnimationAvailable = true;
}
} catch (Exception e) {
Log.w(TAG, "Animation resource not found. Will not show animation.");
} finally {
attributes.recycle();
}
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
if (!mAnimationAvailable) {
return;
}
final TextureView video = (TextureView) holder.findViewById(R.id.video_texture_view);
final ImageView imageView = (ImageView) holder.findViewById(R.id.video_preview_image);
final ImageView playButton = (ImageView) holder.findViewById(R.id.video_play_button);
imageView.setImageResource(mPreviewResource);
video.setOnClickListener(v -> {
if (mMediaPlayer != null) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
playButton.setVisibility(View.VISIBLE);
} else {
mMediaPlayer.start();
playButton.setVisibility(View.GONE);
}
}
});
video.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width,
int height) {
if (mMediaPlayer != null) {
mMediaPlayer.setSurface(new Surface(surfaceTexture));
mVideoReady = false;
mMediaPlayer.seekTo(0);
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width,
int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
imageView.setVisibility(View.VISIBLE);
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
if (mVideoReady && imageView.getVisibility() == View.VISIBLE) {
imageView.setVisibility(View.GONE);
}
if (mMediaPlayer != null && !mMediaPlayer.isPlaying() &&
playButton.getVisibility() != View.VISIBLE) {
playButton.setVisibility(View.VISIBLE);
}
}
});
}
@Override
public void onDetached() {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.reset();
mMediaPlayer.release();
}
super.onDetached();
}
public void onViewVisible() {
if (mVideoReady && mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
mMediaPlayer.seekTo(0);
}
}
public void onViewInvisible() {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
}
}
}

View File

@@ -18,11 +18,8 @@ package com.android.settings.gestures;
import android.content.Context; import android.content.Context;
import android.provider.Settings; import android.provider.Settings;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceScreen;
import android.support.v7.preference.TwoStatePreference;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig; import com.android.settings.TestConfig;
@@ -36,10 +33,7 @@ import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication; import org.robolectric.shadows.ShadowApplication;
import static android.provider.Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED; import static android.provider.Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED;
import static org.mockito.Matchers.any; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@@ -55,81 +49,46 @@ public class DoubleTapPowerPreferenceControllerTest {
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mController = new DoubleTapPowerPreferenceController(mContext); mController = new DoubleTapPowerPreferenceController(mContext, null);
} }
@Test @Test
public void display_configIsTrue_shouldDisplay() { public void isAvailable_configIsTrue_shouldReturnTrue() {
when(mContext.getResources(). when(mContext.getResources().
getBoolean(com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled)) getBoolean(com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled))
.thenReturn(true); .thenReturn(true);
mController.displayPreference(mScreen);
verify(mScreen, never()).removePreference(any(Preference.class)); assertThat(mController.isAvailable()).isTrue();
} }
@Test @Test
public void display_configIsFalse_shouldNotDisplay() { public void isAvailable_configIsTrue_shouldReturnFalse() {
when(mContext.getResources(). when(mContext.getResources().
getBoolean(com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled)) getBoolean(com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled))
.thenReturn(false); .thenReturn(false);
when(mScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mock(Preference.class));
mController.displayPreference(mScreen); assertThat(mController.isAvailable()).isFalse();
verify(mScreen).removePreference(any(Preference.class));
} }
@Test @Test
public void updateState_preferenceSetCheckedWhenSettingIsOn() { public void testSwitchEnabled_configIsNotSet_shouldReturnTrue() {
// Mock a TwoStatePreference
final TwoStatePreference preference = mock(TwoStatePreference.class);
// Set the setting to be enabled. // Set the setting to be enabled.
final Context context = ShadowApplication.getInstance().getApplicationContext(); final Context context = ShadowApplication.getInstance().getApplicationContext();
Settings.System.putInt(context.getContentResolver(), Settings.System.putInt(context.getContentResolver(),
CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 0); CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 0);
mController = new DoubleTapPowerPreferenceController(context, null);
// Run through updateState assertThat(mController.isSwitchPrefEnabled()).isTrue();
mController = new DoubleTapPowerPreferenceController(context);
mController.updateState(preference);
// Verify pref is checked (as setting is enabled).
verify(preference).setChecked(true);
} }
@Test @Test
public void updateState_preferenceSetUncheckedWhenSettingIsOff() { public void testSwitchEnabled_configIsSet_shouldReturnFalse() {
// Mock a TwoStatePreference
final TwoStatePreference preference = mock(TwoStatePreference.class);
// Set the setting to be disabled. // Set the setting to be disabled.
final Context context = ShadowApplication.getInstance().getApplicationContext(); final Context context = ShadowApplication.getInstance().getApplicationContext();
Settings.System.putInt(context.getContentResolver(), Settings.System.putInt(context.getContentResolver(),
CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 1); CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 1);
mController = new DoubleTapPowerPreferenceController(context, null);
// Run through updateState assertThat(mController.isSwitchPrefEnabled()).isFalse();
mController = new DoubleTapPowerPreferenceController(context);
mController.updateState(preference);
// Verify pref is unchecked (as setting is disabled).
verify(preference).setChecked(false);
} }
@Test
public void updateState_notTwoStatePreference_setSummary() {
// Mock a regular preference
final Preference preference = mock(Preference.class);
// Set the setting to be disabled.
final Context context = ShadowApplication.getInstance().getApplicationContext();
Settings.System.putInt(context.getContentResolver(),
CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 1);
// Run through updateState
mController = new DoubleTapPowerPreferenceController(context);
mController.updateState(preference);
// Verify summary is set to off (as setting is disabled).
verify(preference).setSummary(R.string.gesture_setting_off);
}
} }

View File

@@ -17,12 +17,8 @@
package com.android.settings.gestures; package com.android.settings.gestures;
import android.content.Context; import android.content.Context;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.support.v7.preference.TwoStatePreference;
import com.android.internal.hardware.AmbientDisplayConfiguration; import com.android.internal.hardware.AmbientDisplayConfiguration;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig; import com.android.settings.TestConfig;
@@ -34,11 +30,8 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import static org.mockito.Matchers.any; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@@ -47,8 +40,6 @@ public class DoubleTapScreenPreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS) @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext; private Context mContext;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PreferenceScreen mScreen;
@Mock @Mock
private AmbientDisplayConfiguration mAmbientDisplayConfiguration; private AmbientDisplayConfiguration mAmbientDisplayConfiguration;
private DoubleTapScreenPreferenceController mController; private DoubleTapScreenPreferenceController mController;
@@ -57,68 +48,35 @@ public class DoubleTapScreenPreferenceControllerTest {
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mController = new DoubleTapScreenPreferenceController( mController = new DoubleTapScreenPreferenceController(
mContext, mAmbientDisplayConfiguration, 0); mContext, null, mAmbientDisplayConfiguration, 0);
} }
@Test @Test
public void display_configIsTrue_shouldDisplay() { public void isAvailable_configIsTrue_shouldReturnTrue() {
when(mAmbientDisplayConfiguration.pulseOnDoubleTapAvailable()).thenReturn(true); when(mAmbientDisplayConfiguration.pulseOnDoubleTapAvailable()).thenReturn(true);
mController.displayPreference(mScreen); assertThat(mController.isAvailable()).isTrue();
verify(mScreen, never()).removePreference(any(Preference.class));
} }
@Test @Test
public void display_configIsFalse_shouldNotDisplay() { public void isAvailable_configIsFalse_shouldReturnFalse() {
when(mAmbientDisplayConfiguration.pulseOnDoubleTapAvailable()).thenReturn(false); when(mAmbientDisplayConfiguration.pulseOnDoubleTapAvailable()).thenReturn(false);
when(mScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mock(Preference.class));
mController.displayPreference(mScreen); assertThat(mController.isAvailable()).isFalse();
verify(mScreen).removePreference(any(Preference.class));
} }
@Test @Test
public void updateState_preferenceSetCheckedWhenSettingIsOn() { public void testSwitchEnabled_configIsSet_shouldReturnTrue() {
// Mock a TwoStatePreference
final TwoStatePreference preference = mock(TwoStatePreference.class);
// Set the setting to be enabled. // Set the setting to be enabled.
when(mAmbientDisplayConfiguration.pulseOnDoubleTapEnabled(anyInt())).thenReturn(true); when(mAmbientDisplayConfiguration.pulseOnDoubleTapEnabled(anyInt())).thenReturn(true);
// Run through updateState assertThat(mController.isSwitchPrefEnabled()).isTrue();
mController.updateState(preference);
// Verify pref is checked (as setting is enabled).
verify(preference).setChecked(true);
} }
@Test @Test
public void updateState_preferenceSetUncheckedWhenSettingIsOff() { public void testSwitchEnabled_configIsNotSet_shouldReturnFalse() {
// Mock a TwoStatePreference
final TwoStatePreference preference = mock(TwoStatePreference.class);
// Set the setting to be disabled.
when(mAmbientDisplayConfiguration.pulseOnDoubleTapEnabled(anyInt())).thenReturn(false); when(mAmbientDisplayConfiguration.pulseOnDoubleTapEnabled(anyInt())).thenReturn(false);
// Run through updateState assertThat(mController.isSwitchPrefEnabled()).isFalse();
mController.updateState(preference);
// Verify pref is unchecked (as setting is disabled).
verify(preference).setChecked(false);
}
@Test
public void updateState_notTwoStatePreference_setSummary() {
// Mock a regular preference
final Preference preference = mock(Preference.class);
// Set the setting to be disabled.
when(mAmbientDisplayConfiguration.pulseOnDoubleTapEnabled(anyInt())).thenReturn(false);
// Run through updateState
mController.updateState(preference);
// Verify summary is set to off (as setting is disabled).
verify(preference).setSummary(R.string.gesture_setting_off);
} }
} }

View File

@@ -20,9 +20,6 @@ import android.content.Context;
import android.hardware.Sensor; import android.hardware.Sensor;
import android.hardware.SensorManager; import android.hardware.SensorManager;
import android.provider.Settings; import android.provider.Settings;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.support.v7.preference.TwoStatePreference;
import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig; import com.android.settings.TestConfig;
@@ -40,11 +37,9 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import static android.provider.Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED; import static android.provider.Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED;
import static org.mockito.Matchers.any; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@@ -54,19 +49,17 @@ public class DoubleTwistPreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS) @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext; private Context mContext;
@Mock(answer = Answers.RETURNS_DEEP_STUBS) @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PreferenceScreen mScreen;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private SensorManager mSensorManager; private SensorManager mSensorManager;
private DoubleTwistPreferenceController mController; private DoubleTwistPreferenceController mController;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mController = new DoubleTwistPreferenceController(mContext); mController = new DoubleTwistPreferenceController(mContext, null);
} }
@Test @Test
public void display_hasSensor_shouldDisplay() { public void isAvailable_hasSensor_shouldReturnTrue() {
// Mock sensors // Mock sensors
final List<Sensor> sensorList = new ArrayList<>(); final List<Sensor> sensorList = new ArrayList<>();
sensorList.add(mock(Sensor.class)); sensorList.add(mock(Sensor.class));
@@ -75,28 +68,17 @@ public class DoubleTwistPreferenceControllerTest {
when(mSensorManager.getSensorList(anyInt())).thenReturn(sensorList); when(mSensorManager.getSensorList(anyInt())).thenReturn(sensorList);
when(sensorList.get(0).getName()).thenReturn("test"); when(sensorList.get(0).getName()).thenReturn("test");
when(sensorList.get(0).getVendor()).thenReturn("test"); when(sensorList.get(0).getVendor()).thenReturn("test");
when(mScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mock(Preference.class));
// Run through display assertThat(mController.isAvailable()).isTrue();
mController.displayPreference(mScreen);
// Verify preference is not removed
verify(mScreen, never()).removePreference(any(Preference.class));
} }
@Test @Test
public void display_noSensor_shouldNotDisplay() { public void isAvailable_noSensor_shouldReturnFalse() {
when(mScreen.findPreference(mController.getPreferenceKey())) assertThat(mController.isAvailable()).isFalse();
.thenReturn(mock(Preference.class));
mController.displayPreference(mScreen);
verify(mScreen).removePreference(any(Preference.class));
} }
@Test @Test
public void display_differentSensor_shouldNotDisplay() { public void isAvailable_differentSensor_shouldReturnFalse() {
// Mock sensors // Mock sensors
final List<Sensor> sensorList = new ArrayList<>(); final List<Sensor> sensorList = new ArrayList<>();
sensorList.add(mock(Sensor.class)); sensorList.add(mock(Sensor.class));
@@ -104,68 +86,29 @@ public class DoubleTwistPreferenceControllerTest {
when(mContext.getSystemService(Context.SENSOR_SERVICE)).thenReturn(mSensorManager); when(mContext.getSystemService(Context.SENSOR_SERVICE)).thenReturn(mSensorManager);
when(mSensorManager.getSensorList(anyInt())).thenReturn(sensorList); when(mSensorManager.getSensorList(anyInt())).thenReturn(sensorList);
when(sensorList.get(0).getName()).thenReturn("not_test"); when(sensorList.get(0).getName()).thenReturn("not_test");
when(mScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mock(Preference.class));
when(mScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mock(Preference.class));
// Run through display assertThat(mController.isAvailable()).isFalse();
mController.displayPreference(mScreen);
// Verify preference is not removed
verify(mScreen).removePreference(any(Preference.class));
} }
@Test @Test
public void updateState_preferenceSetCheckedWhenSettingIsOn() { public void testSwitchEnabled_configIsSet_shouldReturnTrue() {
// Mock a TwoStatePreference
final TwoStatePreference preference = mock(TwoStatePreference.class);
// Set the setting to be enabled. // Set the setting to be enabled.
final Context context = ShadowApplication.getInstance().getApplicationContext(); final Context context = ShadowApplication.getInstance().getApplicationContext();
Settings.System.putInt(context.getContentResolver(), Settings.System.putInt(context.getContentResolver(),
CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 1); CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 1);
mController = new DoubleTwistPreferenceController(context, null);
// Run through updateState assertThat(mController.isSwitchPrefEnabled()).isTrue();
mController = new DoubleTwistPreferenceController(context);
mController.updateState(preference);
// Verify pref is checked (as setting is enabled).
verify(preference).setChecked(true);
} }
@Test @Test
public void updateState_preferenceSetUncheckedWhenSettingIsOff() { public void testSwitchEnabled_configIsNotSet_shouldReturnFalse() {
// Mock a TwoStatePreference
final TwoStatePreference preference = mock(TwoStatePreference.class);
// Set the setting to be disabled. // Set the setting to be disabled.
final Context context = ShadowApplication.getInstance().getApplicationContext(); final Context context = ShadowApplication.getInstance().getApplicationContext();
Settings.System.putInt(context.getContentResolver(), Settings.System.putInt(context.getContentResolver(),
CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 0); CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 0);
mController = new DoubleTwistPreferenceController(context, null);
// Run through updateState assertThat(mController.isSwitchPrefEnabled()).isFalse();
mController = new DoubleTwistPreferenceController(context);
mController.updateState(preference);
// Verify pref is unchecked (as setting is disabled).
verify(preference).setChecked(false);
} }
@Test
public void updateState_notTwoStatePreference_setSummary() {
// Mock a regular preference
final Preference preference = mock(Preference.class);
// Set the setting to be disabled.
final Context context = ShadowApplication.getInstance().getApplicationContext();
Settings.System.putInt(context.getContentResolver(),
CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 0);
// Run through updateState
mController = new DoubleTwistPreferenceController(context);
mController.updateState(preference);
// Verify summary is set to off (as setting is disabled).
verify(preference).setSummary(com.android.settings.R.string.gesture_setting_off);
}
} }

View File

@@ -0,0 +1,184 @@
/*
* Copyright (C) 2016 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.gestures;
import android.content.Context;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.support.v7.preference.TwoStatePreference;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.core.lifecycle.Lifecycle;
import com.android.settings.widget.VideoPreference;
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.annotation.Config;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class GesturePreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PreferenceScreen mScreen;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Lifecycle mLifecycle;
private TestPrefController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mController = new TestPrefController(mContext, mLifecycle);
}
@Test
public void display_configIsTrue_shouldDisplay() {
mController.mIsPrefAvailable = true;
when(mScreen.findPreference(anyString())).thenReturn(mock(VideoPreference.class));
mController.displayPreference(mScreen);
verify(mScreen, never()).removePreference(any(Preference.class));
}
@Test
public void display_configIsFalse_shouldNotDisplay() {
mController.mIsPrefAvailable = false;
when(mScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mock(Preference.class));
mController.displayPreference(mScreen);
verify(mScreen).removePreference(any(Preference.class));
}
@Test
public void onStart_shouldStartVideoPreference() {
final VideoPreference videoPreference = mock(VideoPreference.class);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(videoPreference);
mController.mIsPrefAvailable = true;
mController.displayPreference(mScreen);
mController.onStart();
verify(videoPreference).onViewVisible();
}
@Test
public void onStop_shouldStopVideoPreference() {
final VideoPreference videoPreference = mock(VideoPreference.class);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(videoPreference);
mController.mIsPrefAvailable = true;
mController.displayPreference(mScreen);
mController.onStop();
verify(videoPreference).onViewInvisible();
}
@Test
public void updateState_preferenceSetCheckedWhenSettingIsOn() {
// Mock a TwoStatePreference
final TwoStatePreference preference = mock(TwoStatePreference.class);
// Set the setting to be enabled.
mController.mIsPrefEnabled = true;
// Run through updateState
mController.updateState(preference);
// Verify pref is checked (as setting is enabled).
verify(preference).setChecked(true);
}
@Test
public void updateState_preferenceSetUncheckedWhenSettingIsOff() {
// Mock a TwoStatePreference
final TwoStatePreference preference = mock(TwoStatePreference.class);
// Set the setting to be disabled.
mController.mIsPrefEnabled = false;
// Run through updateState
mController.updateState(preference);
// Verify pref is unchecked (as setting is disabled).
verify(preference).setChecked(false);
}
@Test
public void updateState_notTwoStatePreference_setSummary() {
// Mock a regular preference
final Preference preference = mock(Preference.class);
// Set the setting to be disabled.
mController.mIsPrefEnabled = false;
// Run through updateState
mController.updateState(preference);
// Verify summary is set to off (as setting is disabled).
verify(preference).setSummary(com.android.settings.R.string.gesture_setting_off);
}
private class TestPrefController extends GesturePreferenceController {
boolean mIsPrefAvailable;
boolean mIsPrefEnabled;
public TestPrefController(Context context,
Lifecycle lifecycle) {
super(context, lifecycle);
}
@Override
public boolean isAvailable() {
return mIsPrefAvailable;
}
@Override
public String getPreferenceKey() {
return null;
}
@Override
protected String getVideoPrefKey() {
return null;
}
@Override
protected boolean isSwitchPrefEnabled() {
return mIsPrefEnabled;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
return false;
}
}
}

View File

@@ -17,12 +17,8 @@
package com.android.settings.gestures; package com.android.settings.gestures;
import android.content.Context; import android.content.Context;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.support.v7.preference.TwoStatePreference;
import com.android.internal.hardware.AmbientDisplayConfiguration; import com.android.internal.hardware.AmbientDisplayConfiguration;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig; import com.android.settings.TestConfig;
@@ -34,11 +30,8 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import static org.mockito.Matchers.any; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@@ -47,8 +40,6 @@ public class PIckupGesturePreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS) @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext; private Context mContext;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PreferenceScreen mScreen;
@Mock @Mock
private AmbientDisplayConfiguration mAmbientDisplayConfiguration; private AmbientDisplayConfiguration mAmbientDisplayConfiguration;
@@ -58,69 +49,37 @@ public class PIckupGesturePreferenceControllerTest {
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mController = new PickupGesturePreferenceController( mController = new PickupGesturePreferenceController(
mContext, mAmbientDisplayConfiguration, 0); mContext, null, mAmbientDisplayConfiguration, 0);
} }
@Test @Test
public void display_configIsTrue_shouldDisplay() { public void isAvailable_configIsTrue_shouldReturnTrue() {
when(mAmbientDisplayConfiguration.pulseOnPickupAvailable()).thenReturn(true); when(mAmbientDisplayConfiguration.pulseOnPickupAvailable()).thenReturn(true);
mController.displayPreference(mScreen); assertThat(mController.isAvailable()).isTrue();
verify(mScreen, never()).removePreference(any(Preference.class));
} }
@Test @Test
public void display_configIsFalse_shouldNotDisplay() { public void isAvailable_configIsFalse_shouldReturnFalse() {
when(mAmbientDisplayConfiguration.pulseOnPickupAvailable()).thenReturn(false); when(mAmbientDisplayConfiguration.pulseOnPickupAvailable()).thenReturn(false);
when(mScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mock(Preference.class));
mController.displayPreference(mScreen); assertThat(mController.isAvailable()).isFalse();
verify(mScreen).removePreference(any(Preference.class));
} }
@Test @Test
public void updateState_preferenceSetCheckedWhenSettingIsOn() { public void testSwitchEnabled_configIsSet_shouldReturnTrue() {
// Mock a TwoStatePreference
final TwoStatePreference preference = mock(TwoStatePreference.class);
// Set the setting to be enabled. // Set the setting to be enabled.
when(mAmbientDisplayConfiguration.pulseOnPickupEnabled(anyInt())).thenReturn(true); when(mAmbientDisplayConfiguration.pulseOnPickupEnabled(anyInt())).thenReturn(true);
// Run through updateState assertThat(mController.isSwitchPrefEnabled()).isTrue();
mController.updateState(preference);
// Verify pref is checked (as setting is enabled).
verify(preference).setChecked(true);
} }
@Test @Test
public void updateState_preferenceSetUncheckedWhenSettingIsOff() { public void testSwitchEnabled_configIsNotSet_shouldReturnFalse() {
// Mock a TwoStatePreference
final TwoStatePreference preference = mock(TwoStatePreference.class);
// Set the setting to be disabled. // Set the setting to be disabled.
when(mAmbientDisplayConfiguration.pulseOnPickupEnabled(anyInt())).thenReturn(false); when(mAmbientDisplayConfiguration.pulseOnPickupEnabled(anyInt())).thenReturn(false);
// Run through updateState assertThat(mController.isSwitchPrefEnabled()).isFalse();
mController.updateState(preference);
// Verify pref is unchecked (as setting is disabled).
verify(preference).setChecked(false);
}
@Test
public void updateState_notTwoStatePreference_setSummary() {
// Mock a regular preference
final Preference preference = mock(Preference.class);
// Set the setting to be disabled.
when(mAmbientDisplayConfiguration.pulseOnPickupEnabled(anyInt())).thenReturn(false);
// Run through updateState
mController.updateState(preference);
// Verify summary is set to off (as setting is disabled).
verify(preference).setSummary(R.string.gesture_setting_off);
} }
} }

View File

@@ -18,11 +18,7 @@ package com.android.settings.gestures;
import android.content.Context; import android.content.Context;
import android.provider.Settings; import android.provider.Settings;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.support.v7.preference.TwoStatePreference;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig; import com.android.settings.TestConfig;
@@ -36,10 +32,7 @@ import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication; import org.robolectric.shadows.ShadowApplication;
import static android.provider.Settings.Secure.SYSTEM_NAVIGATION_KEYS_ENABLED; import static android.provider.Settings.Secure.SYSTEM_NAVIGATION_KEYS_ENABLED;
import static org.mockito.Matchers.any; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@@ -48,85 +41,50 @@ public class SwipeToNotificationPreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS) @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext; private Context mContext;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PreferenceScreen mScreen;
private SwipeToNotificationPreferenceController mController; private SwipeToNotificationPreferenceController mController;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mController = new SwipeToNotificationPreferenceController(mContext); mController = new SwipeToNotificationPreferenceController(mContext, null);
} }
@Test @Test
public void display_configIsTrue_shouldDisplay() { public void isAvailable_configIsTrue_shouldReturnTrue() {
when(mContext.getResources(). when(mContext.getResources().
getBoolean(com.android.internal.R.bool.config_supportSystemNavigationKeys)) getBoolean(com.android.internal.R.bool.config_supportSystemNavigationKeys))
.thenReturn(true); .thenReturn(true);
mController.displayPreference(mScreen);
verify(mScreen, never()).removePreference(any(Preference.class)); assertThat(mController.isAvailable()).isTrue();
} }
@Test @Test
public void display_configIsFalse_shouldNotDisplay() { public void isAvailable_configIsFalse_shouldReturnFalse() {
when(mContext.getResources(). when(mContext.getResources().
getBoolean(com.android.internal.R.bool.config_supportSystemNavigationKeys)) getBoolean(com.android.internal.R.bool.config_supportSystemNavigationKeys))
.thenReturn(false); .thenReturn(false);
when(mScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mock(Preference.class));
mController.displayPreference(mScreen); assertThat(mController.isAvailable()).isFalse();
verify(mScreen).removePreference(any(Preference.class));
} }
@Test @Test
public void updateState_preferenceSetCheckedWhenSettingIsOn() { public void testSwitchEnabled_configIsSet_shouldReturnTrue() {
// Mock a TwoStatePreference
final TwoStatePreference preference = mock(TwoStatePreference.class);
// Set the setting to be enabled. // Set the setting to be enabled.
final Context context = ShadowApplication.getInstance().getApplicationContext(); final Context context = ShadowApplication.getInstance().getApplicationContext();
Settings.System.putInt(context.getContentResolver(), SYSTEM_NAVIGATION_KEYS_ENABLED, 1); Settings.System.putInt(context.getContentResolver(), SYSTEM_NAVIGATION_KEYS_ENABLED, 1);
mController = new SwipeToNotificationPreferenceController(context, null);
// Run through updateState assertThat(mController.isSwitchPrefEnabled()).isTrue();
mController = new SwipeToNotificationPreferenceController(context);
mController.updateState(preference);
// Verify pref is checked (as setting is enabled).
verify(preference).setChecked(true);
} }
@Test @Test
public void updateState_preferenceSetUncheckedWhenSettingIsOff() { public void testSwitchEnabled_configIsNotSet_shouldReturnFalse() {
// Mock a TwoStatePreference
final TwoStatePreference preference = mock(TwoStatePreference.class);
// Set the setting to be disabled. // Set the setting to be disabled.
final Context context = ShadowApplication.getInstance().getApplicationContext(); final Context context = ShadowApplication.getInstance().getApplicationContext();
Settings.System.putInt(context.getContentResolver(), SYSTEM_NAVIGATION_KEYS_ENABLED, 0); Settings.System.putInt(context.getContentResolver(), SYSTEM_NAVIGATION_KEYS_ENABLED, 0);
mController = new SwipeToNotificationPreferenceController(context, null);
// Run through updateState assertThat(mController.isSwitchPrefEnabled()).isFalse();
mController = new SwipeToNotificationPreferenceController(context);
mController.updateState(preference);
// Verify pref is unchecked (as setting is disabled).
verify(preference).setChecked(false);
}
@Test
public void updateState_notTwoStatePreference_setSummary() {
// Mock a regular preference
final Preference preference = mock(Preference.class);
// Set the setting to be disabled.
final Context context = ShadowApplication.getInstance().getApplicationContext();
Settings.System.putInt(context.getContentResolver(), SYSTEM_NAVIGATION_KEYS_ENABLED, 0);
// Run through updateState
mController = new SwipeToNotificationPreferenceController(context);
mController.updateState(preference);
// Verify summary is set to off (as setting is disabled).
verify(preference).setSummary(R.string.gesture_setting_off);
} }
} }