Game Driver: Add SwitchBar to control GUP feature

Uncheck the global switch will hide the preference controllers and force
all apps to use system graphics driver. This change also add a content
observer to notify all the preference controllers of settings global
changes.

Bug: 119221883
Test: make RunSettingsRoboTests
Change-Id: Ice9ded17c759791a3728c552f79881e2215ac081
Merged-In: Ice9ded17c759791a3728c552f79881e2215ac081
This commit is contained in:
Yiwei Zhang
2019-01-19 16:45:20 +08:00
parent 75548c4a33
commit b15cc090d1
9 changed files with 536 additions and 39 deletions

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2019 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.development.gup;
import android.content.ContentResolver;
import android.database.ContentObserver;
import android.os.Handler;
import android.provider.Settings;
import androidx.annotation.VisibleForTesting;
/**
* Helper class to observe Game Driver settings global change.
*/
public class GameDriverContentObserver extends ContentObserver {
interface OnGameDriverContentChangedListener {
void onGameDriverContentChanged();
}
@VisibleForTesting
OnGameDriverContentChangedListener mListener;
public GameDriverContentObserver(Handler handler, OnGameDriverContentChangedListener listener) {
super(handler);
mListener = listener;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
mListener.onGameDriverContentChanged();
}
public void register(ContentResolver contentResolver) {
contentResolver.registerContentObserver(
Settings.Global.getUriFor(Settings.Global.GUP_DEV_ALL_APPS), false, this);
}
public void unregister(ContentResolver contentResolver) {
contentResolver.unregisterContentObserver(this);
}
}

View File

@@ -17,14 +17,20 @@
package com.android.settings.development.gup; package com.android.settings.development.gup;
import android.content.Context; import android.content.Context;
import android.os.Bundle;
import com.android.internal.logging.nano.MetricsProto; import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.core.AbstractPreferenceController; import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.SwitchBarController;
import java.util.List; import java.util.List;
/**
* Dashboard for Game Driver preferences.
*/
public class GupDashboard extends DashboardFragment { public class GupDashboard extends DashboardFragment {
private static final String TAG = "GupDashboard"; private static final String TAG = "GupDashboard";
@@ -47,4 +53,16 @@ public class GupDashboard extends DashboardFragment {
public int getHelpResource() { public int getHelpResource() {
return 0; return 0;
} }
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final SettingsActivity activity = (SettingsActivity) getActivity();
final SwitchBar switchBar = activity.getSwitchBar();
final GupGlobalSwitchBarController switchBarController =
new GupGlobalSwitchBarController(activity, new SwitchBarController(switchBar));
getLifecycle().addObserver(switchBarController);
switchBar.show();
}
} }

View File

@@ -18,55 +18,92 @@ package com.android.settings.development.gup;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings; import android.provider.Settings;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreference;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
import com.android.settingslib.development.DevelopmentSettingsEnabler; import com.android.settingslib.development.DevelopmentSettingsEnabler;
public class GupEnableForAllAppsPreferenceController /**
extends BasePreferenceController implements Preference.OnPreferenceChangeListener { * Controller of global switch to enable Game Driver for all Apps.
*/
public class GupEnableForAllAppsPreferenceController extends BasePreferenceController
implements Preference.OnPreferenceChangeListener,
GameDriverContentObserver.OnGameDriverContentChangedListener, LifecycleObserver,
OnStart, OnStop {
public static final int GUP_DEFAULT = 0; public static final int GUP_DEFAULT = 0;
public static final int GUP_ALL_APPS = 1; public static final int GUP_ALL_APPS = 1;
public static final int GUP_OFF = 2;
private final Context mContext;
private final ContentResolver mContentResolver; private final ContentResolver mContentResolver;
@VisibleForTesting
GameDriverContentObserver mGameDriverContentObserver;
private SwitchPreference mPreference;
public GupEnableForAllAppsPreferenceController(Context context, String key) { public GupEnableForAllAppsPreferenceController(Context context, String key) {
super(context, key); super(context, key);
mContext = context;
mContentResolver = context.getContentResolver(); mContentResolver = context.getContentResolver();
mGameDriverContentObserver =
new GameDriverContentObserver(new Handler(Looper.getMainLooper()), this);
} }
@Override @Override
public int getAvailabilityStatus() { public int getAvailabilityStatus() {
return DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext) return DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext)
&& (Settings.Global.getInt(
mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT)
!= GUP_OFF)
? AVAILABLE ? AVAILABLE
: DISABLED_DEPENDENT_SETTING; : CONDITIONALLY_UNAVAILABLE;
} }
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
final SwitchPreference switchPreference = mPreference = (SwitchPreference) screen.findPreference(getPreferenceKey());
(SwitchPreference) screen.findPreference(getPreferenceKey()); }
if (switchPreference == null) {
return;
}
@Override
public void onStart() {
mGameDriverContentObserver.register(mContentResolver);
}
@Override
public void onStop() {
mGameDriverContentObserver.unregister(mContentResolver);
}
@Override
public void updateState(Preference preference) {
final SwitchPreference switchPreference = (SwitchPreference) preference;
switchPreference.setVisible(isAvailable());
switchPreference.setChecked(Settings.Global.getInt(mContentResolver, switchPreference.setChecked(Settings.Global.getInt(mContentResolver,
Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT) Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT)
== GUP_ALL_APPS); == GUP_ALL_APPS);
switchPreference.setOnPreferenceChangeListener(this);
} }
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
// When developer option is present, always overwrite GUP_DEV_ALL_APPS.
Settings.Global.putInt(mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, Settings.Global.putInt(mContentResolver, Settings.Global.GUP_DEV_ALL_APPS,
(boolean) newValue ? GUP_ALL_APPS : GUP_DEFAULT); (boolean) newValue ? GUP_ALL_APPS : GUP_DEFAULT);
return true; return true;
} }
@Override
public void onGameDriverContentChanged() {
updateState(mPreference);
}
} }

View File

@@ -0,0 +1,98 @@
/*
* Copyright 2019 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.development.gup;
import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_ALL_APPS;
import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_DEFAULT;
import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_OFF;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import androidx.annotation.VisibleForTesting;
import com.android.settings.widget.SwitchWidgetController;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
import com.android.settingslib.development.DevelopmentSettingsEnabler;
/**
* Controller of global switch bar used to fully turn off Game Driver.
*/
public class GupGlobalSwitchBarController
implements SwitchWidgetController.OnSwitchChangeListener,
GameDriverContentObserver.OnGameDriverContentChangedListener, LifecycleObserver,
OnStart, OnStop {
private final Context mContext;
private final ContentResolver mContentResolver;
@VisibleForTesting
SwitchWidgetController mSwitchWidgetController;
@VisibleForTesting
GameDriverContentObserver mGameDriverContentObserver;
GupGlobalSwitchBarController(Context context, SwitchWidgetController switchWidgetController) {
mContext = context;
mContentResolver = context.getContentResolver();
mGameDriverContentObserver =
new GameDriverContentObserver(new Handler(Looper.getMainLooper()), this);
mSwitchWidgetController = switchWidgetController;
mSwitchWidgetController.setEnabled(
DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(context));
mSwitchWidgetController.setChecked(Settings.Global.getInt(mContentResolver,
Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT)
!= GUP_OFF);
mSwitchWidgetController.setListener(this);
}
@Override
public void onStart() {
mSwitchWidgetController.startListening();
mGameDriverContentObserver.register(mContentResolver);
}
@Override
public void onStop() {
mSwitchWidgetController.stopListening();
mGameDriverContentObserver.unregister(mContentResolver);
}
@Override
public boolean onSwitchToggled(boolean isChecked) {
if (!isChecked) {
Settings.Global.putInt(mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_OFF);
return true;
}
if (Settings.Global.getInt(mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT)
!= GUP_ALL_APPS) {
Settings.Global.putInt(mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT);
}
return true;
}
@Override
public void onGameDriverContentChanged() {
mSwitchWidgetController.setChecked(Settings.Global.getInt(mContentResolver,
Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT)
!= GUP_OFF);
}
}

View File

@@ -16,11 +16,16 @@
package com.android.settings.development.gup; package com.android.settings.development.gup;
import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_DEFAULT;
import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_OFF;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings; import android.provider.Settings;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
@@ -31,6 +36,9 @@ import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
import com.android.settingslib.development.DevelopmentSettingsEnabler; import com.android.settingslib.development.DevelopmentSettingsEnabler;
import java.text.Collator; import java.text.Collator;
@@ -42,21 +50,37 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
public class GupPreferenceController /**
extends BasePreferenceController implements Preference.OnPreferenceChangeListener { * Controller of all the per App based list preferences.
*/
public class GupPreferenceController extends BasePreferenceController
implements Preference.OnPreferenceChangeListener,
GameDriverContentObserver.OnGameDriverContentChangedListener, LifecycleObserver,
OnStart, OnStop {
private final Context mContext;
private final ContentResolver mContentResolver;
private final CharSequence[] mEntryList; private final CharSequence[] mEntryList;
private final String mPreferenceTitle; private final String mPreferenceTitle;
private final String mPreferenceDefault; private final String mPreferenceDefault;
private final String mPreferenceGup; private final String mPreferenceGup;
private final String mPreferenceSystem; private final String mPreferenceSystem;
@VisibleForTesting
GameDriverContentObserver mGameDriverContentObserver;
private final List<AppInfo> mAppInfos; private final List<AppInfo> mAppInfos;
private final Set<String> mDevOptInApps; private final Set<String> mDevOptInApps;
private final Set<String> mDevOptOutApps; private final Set<String> mDevOptOutApps;
private PreferenceGroup mPreferenceGroup;
public GupPreferenceController(Context context, String key) { public GupPreferenceController(Context context, String key) {
super(context, key); super(context, key);
mContext = context;
mContentResolver = context.getContentResolver();
mGameDriverContentObserver =
new GameDriverContentObserver(new Handler(Looper.getMainLooper()), this);
final Resources resources = context.getResources(); final Resources resources = context.getResources();
mEntryList = resources.getStringArray(R.array.gup_app_preference_values); mEntryList = resources.getStringArray(R.array.gup_app_preference_values);
mPreferenceTitle = resources.getString(R.string.gup_app_preference_title); mPreferenceTitle = resources.getString(R.string.gup_app_preference_title);
@@ -68,35 +92,57 @@ public class GupPreferenceController
// Update the UI when all the app infos are ready. // Update the UI when all the app infos are ready.
mAppInfos = getAppInfos(context); mAppInfos = getAppInfos(context);
final ContentResolver contentResolver = context.getContentResolver();
mDevOptInApps = mDevOptInApps =
getGlobalSettingsString(contentResolver, Settings.Global.GUP_DEV_OPT_IN_APPS); getGlobalSettingsString(mContentResolver, Settings.Global.GUP_DEV_OPT_IN_APPS);
mDevOptOutApps = mDevOptOutApps =
getGlobalSettingsString(contentResolver, Settings.Global.GUP_DEV_OPT_OUT_APPS); getGlobalSettingsString(mContentResolver, Settings.Global.GUP_DEV_OPT_OUT_APPS);
} }
@Override @Override
public int getAvailabilityStatus() { public int getAvailabilityStatus() {
return DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext) return DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext)
&& (Settings.Global.getInt(
mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT)
!= GUP_OFF)
? AVAILABLE ? AVAILABLE
: DISABLED_DEPENDENT_SETTING; : CONDITIONALLY_UNAVAILABLE;
} }
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
final PreferenceGroup preferenceGroup = mPreferenceGroup = (PreferenceGroup) screen.findPreference(getPreferenceKey());
(PreferenceGroup) screen.findPreference(getPreferenceKey());
if (preferenceGroup == null) {
return;
}
final Context context = mPreferenceGroup.getContext();
for (AppInfo appInfo : mAppInfos) { for (AppInfo appInfo : mAppInfos) {
preferenceGroup.addPreference( mPreferenceGroup.addPreference(
createListPreference(appInfo.info.packageName, appInfo.label)); createListPreference(context, appInfo.info.packageName, appInfo.label));
} }
} }
@Override
public void onStart() {
mGameDriverContentObserver.register(mContentResolver);
}
@Override
public void onStop() {
mGameDriverContentObserver.unregister(mContentResolver);
}
@Override
public void updateState(Preference preference) {
// This is a workaround, because PreferenceGroup.setVisible is not applied to the
// preferences inside the group.
final boolean isGroupAvailable = isAvailable();
final PreferenceGroup group = (PreferenceGroup) preference;
for (int idx = 0; idx < group.getPreferenceCount(); idx++) {
final Preference pref = group.getPreference(idx);
pref.setVisible(isGroupAvailable);
}
preference.setVisible(isGroupAvailable);
}
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
final ListPreference listPref = (ListPreference) preference; final ListPreference listPref = (ListPreference) preference;
@@ -120,14 +166,19 @@ public class GupPreferenceController
// Push the updated Sets for opt-in and opt-out apps to // Push the updated Sets for opt-in and opt-out apps to
// corresponding Settings.Global.GUP_DEV_OPT_(IN|OUT)_APPS // corresponding Settings.Global.GUP_DEV_OPT_(IN|OUT)_APPS
Settings.Global.putString(mContext.getContentResolver(), Settings.Global.putString(mContentResolver, Settings.Global.GUP_DEV_OPT_IN_APPS,
Settings.Global.GUP_DEV_OPT_IN_APPS, String.join(",", mDevOptInApps)); String.join(",", mDevOptInApps));
Settings.Global.putString(mContext.getContentResolver(), Settings.Global.putString(mContentResolver, Settings.Global.GUP_DEV_OPT_OUT_APPS,
Settings.Global.GUP_DEV_OPT_OUT_APPS, String.join(",", mDevOptOutApps)); String.join(",", mDevOptOutApps));
return true; return true;
} }
@Override
public void onGameDriverContentChanged() {
updateState(mPreferenceGroup);
}
// AppInfo class to achieve loading the application label only once // AppInfo class to achieve loading the application label only once
class AppInfo { class AppInfo {
AppInfo(PackageManager packageManager, ApplicationInfo applicationInfo) { AppInfo(PackageManager packageManager, ApplicationInfo applicationInfo) {
@@ -176,8 +227,9 @@ public class GupPreferenceController
}; };
@VisibleForTesting @VisibleForTesting
protected ListPreference createListPreference(String packageName, String appName) { protected ListPreference createListPreference(
final ListPreference listPreference = new ListPreference(mContext); Context context, String packageName, String appName) {
final ListPreference listPreference = new ListPreference(context);
listPreference.setKey(packageName); listPreference.setKey(packageName);
listPreference.setTitle(appName); listPreference.setTitle(appName);

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2019 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.development.gup;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.content.ContentResolver;
import android.provider.Settings;
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;
@RunWith(RobolectricTestRunner.class)
public class GameDriverContentObserverTest {
@Mock
private ContentResolver mResolver;
@Mock
private GameDriverContentObserver.OnGameDriverContentChangedListener mListener;
private GameDriverContentObserver mGameDriverContentObserver;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mGameDriverContentObserver = spy(new GameDriverContentObserver(null, null));
}
@Test
public void onChange_shouldCallListener() {
mGameDriverContentObserver.mListener = mListener;
mGameDriverContentObserver.onChange(true);
verify(mListener).onGameDriverContentChanged();
}
@Test
public void register_shouldRegisterContentObserver() {
mGameDriverContentObserver.register(mResolver);
verify(mResolver).registerContentObserver(
Settings.Global.getUriFor(Settings.Global.GUP_DEV_ALL_APPS), false,
mGameDriverContentObserver);
}
@Test
public void unregister_shouldUnregisterContentObserver() {
mGameDriverContentObserver.unregister(mResolver);
verify(mResolver).unregisterContentObserver(mGameDriverContentObserver);
}
}

View File

@@ -20,6 +20,7 @@ import static com.android.settings.development.gup.GupEnableForAllAppsPreference
import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_DEFAULT; import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_DEFAULT;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -44,6 +45,8 @@ public class GupEnableForAllAppsPreferenceControllerTest {
private PreferenceScreen mScreen; private PreferenceScreen mScreen;
@Mock @Mock
private SwitchPreference mPreference; private SwitchPreference mPreference;
@Mock
private GameDriverContentObserver mGameDriverContentObserver;
private Context mContext; private Context mContext;
private ContentResolver mResolver; private ContentResolver mResolver;
@@ -56,20 +59,38 @@ public class GupEnableForAllAppsPreferenceControllerTest {
mResolver = mContext.getContentResolver(); mResolver = mContext.getContentResolver();
mController = new GupEnableForAllAppsPreferenceController(mContext, "testKey"); mController = new GupEnableForAllAppsPreferenceController(mContext, "testKey");
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
mController.displayPreference(mScreen);
Settings.Global.putInt(mResolver, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
} }
@Test @Test
public void displayPreference_shouldAddSwitchPreference() { public void displayPreference_shouldAddSwitchPreference() {
Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT); Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT);
mController.displayPreference(mScreen); mController.updateState(mPreference);
verify(mPreference).setChecked(false); verify(mPreference).setChecked(false);
} }
@Test
public void onStart_shouldRegister() {
mController.mGameDriverContentObserver = mGameDriverContentObserver;
mController.onStart();
verify(mGameDriverContentObserver).register(mResolver);
}
@Test
public void onStop_shouldUnregister() {
mController.mGameDriverContentObserver = mGameDriverContentObserver;
mController.onStop();
verify(mGameDriverContentObserver).unregister(mResolver);
}
@Test @Test
public void onPreferenceChange_check_shouldUpdateSettingsGlobal() { public void onPreferenceChange_check_shouldUpdateSettingsGlobal() {
Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT); Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT);
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, true); mController.onPreferenceChange(mPreference, true);
assertThat(Settings.Global.getInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT)) assertThat(Settings.Global.getInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT))
@@ -79,7 +100,6 @@ public class GupEnableForAllAppsPreferenceControllerTest {
@Test @Test
public void onPreferenceChange_uncheck_shouldUpdateSettingsGlobal() { public void onPreferenceChange_uncheck_shouldUpdateSettingsGlobal() {
Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_ALL_APPS); Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_ALL_APPS);
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, false); mController.onPreferenceChange(mPreference, false);
assertThat(Settings.Global.getInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT)) assertThat(Settings.Global.getInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT))

View File

@@ -0,0 +1,124 @@
/*
* Copyright 2019 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.development.gup;
import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_DEFAULT;
import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_OFF;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.SwitchBarController;
import com.android.settings.widget.SwitchWidgetController;
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;
@RunWith(RobolectricTestRunner.class)
public class GupGlobalSwitchBarControllerTest {
@Mock
private SwitchBar mSwitchBar;
@Mock
private SwitchWidgetController mSwitchWidgetController;
@Mock
private GameDriverContentObserver mGameDriverContentObserver;
private Context mContext;
private ContentResolver mResolver;
private GupGlobalSwitchBarController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mResolver = mContext.getContentResolver();
}
@Test
public void constructor_gupOn_shouldCheckSwitchBar() {
Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT);
mController =
new GupGlobalSwitchBarController(mContext, new SwitchBarController(mSwitchBar));
verify(mSwitchBar).setChecked(true);
}
@Test
public void constructor_gupOff_shouldUncheckSwitchBar() {
Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_OFF);
mController =
new GupGlobalSwitchBarController(mContext, new SwitchBarController(mSwitchBar));
verify(mSwitchBar).setChecked(false);
}
@Test
public void onStart_shouldStartListeningAndRegister() {
mController =
new GupGlobalSwitchBarController(mContext, new SwitchBarController(mSwitchBar));
mController.mSwitchWidgetController = mSwitchWidgetController;
mController.mGameDriverContentObserver = mGameDriverContentObserver;
mController.onStart();
verify(mSwitchWidgetController).startListening();
verify(mGameDriverContentObserver).register(mResolver);
}
@Test
public void onStop_shouldStopListeningAndUnregister() {
mController =
new GupGlobalSwitchBarController(mContext, new SwitchBarController(mSwitchBar));
mController.mSwitchWidgetController = mSwitchWidgetController;
mController.mGameDriverContentObserver = mGameDriverContentObserver;
mController.onStop();
verify(mSwitchWidgetController).stopListening();
verify(mGameDriverContentObserver).unregister(mResolver);
}
@Test
public void onSwitchToggled_checked_shouldTurnOnGup() {
Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_OFF);
mController =
new GupGlobalSwitchBarController(mContext, new SwitchBarController(mSwitchBar));
mController.onSwitchToggled(true);
assertThat(Settings.Global.getInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT))
.isEqualTo(GUP_DEFAULT);
}
@Test
public void onSwitchToggled_unchecked_shouldTurnOffGup() {
Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT);
mController =
new GupGlobalSwitchBarController(mContext, new SwitchBarController(mSwitchBar));
mController.onSwitchToggled(false);
assertThat(Settings.Global.getInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT))
.isEqualTo(GUP_OFF);
}
}

View File

@@ -20,6 +20,7 @@ import static com.android.settings.testutils.ApplicationTestUtils.buildInfo;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.content.ContentResolver; import android.content.ContentResolver;
@@ -63,6 +64,8 @@ public class GupPreferenceControllerTest {
private PackageManager mPackageManager; private PackageManager mPackageManager;
@Mock @Mock
private PreferenceScreen mScreen; private PreferenceScreen mScreen;
@Mock
private GameDriverContentObserver mGameDriverContentObserver;
private Context mContext; private Context mContext;
private PreferenceGroup mGroup; private PreferenceGroup mGroup;
@@ -92,11 +95,29 @@ public class GupPreferenceControllerTest {
assertThat(mGroup.getPreference(1).getKey()).isEqualTo(APP_3); assertThat(mGroup.getPreference(1).getKey()).isEqualTo(APP_3);
} }
@Test
public void onStart_shouldRegister() {
loadDefaultConfig();
mController.mGameDriverContentObserver = mGameDriverContentObserver;
mController.onStart();
verify(mGameDriverContentObserver).register(mResolver);
}
@Test
public void onStop_shouldUnregister() {
loadDefaultConfig();
mController.mGameDriverContentObserver = mGameDriverContentObserver;
mController.onStop();
verify(mGameDriverContentObserver).unregister(mResolver);
}
@Test @Test
public void createPreference_configDefault_shouldSetDefaultAttributes() { public void createPreference_configDefault_shouldSetDefaultAttributes() {
loadDefaultConfig(); loadDefaultConfig();
final ListPreference preference = final ListPreference preference =
mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); mController.createListPreference(mContext, TEST_PKG_NAME, TEST_APP_NAME);
assertThat(preference.getKey()).isEqualTo(TEST_PKG_NAME); assertThat(preference.getKey()).isEqualTo(TEST_PKG_NAME);
assertThat(preference.getTitle()).isEqualTo(TEST_APP_NAME); assertThat(preference.getTitle()).isEqualTo(TEST_APP_NAME);
@@ -112,7 +133,7 @@ public class GupPreferenceControllerTest {
public void createPreference_configGup_shouldSetGupAttributes() { public void createPreference_configGup_shouldSetGupAttributes() {
loadConfig(TEST_PKG_NAME, ""); loadConfig(TEST_PKG_NAME, "");
final ListPreference preference = final ListPreference preference =
mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); mController.createListPreference(mContext, TEST_PKG_NAME, TEST_APP_NAME);
assertThat(preference.getKey()).isEqualTo(TEST_PKG_NAME); assertThat(preference.getKey()).isEqualTo(TEST_PKG_NAME);
assertThat(preference.getTitle()).isEqualTo(TEST_APP_NAME); assertThat(preference.getTitle()).isEqualTo(TEST_APP_NAME);
@@ -128,7 +149,7 @@ public class GupPreferenceControllerTest {
public void createPreference_configSystem_shouldSetSystemAttributes() { public void createPreference_configSystem_shouldSetSystemAttributes() {
loadConfig("", TEST_PKG_NAME); loadConfig("", TEST_PKG_NAME);
final ListPreference preference = final ListPreference preference =
mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); mController.createListPreference(mContext, TEST_PKG_NAME, TEST_APP_NAME);
assertThat(preference.getKey()).isEqualTo(TEST_PKG_NAME); assertThat(preference.getKey()).isEqualTo(TEST_PKG_NAME);
assertThat(preference.getTitle()).isEqualTo(TEST_APP_NAME); assertThat(preference.getTitle()).isEqualTo(TEST_APP_NAME);
@@ -144,7 +165,7 @@ public class GupPreferenceControllerTest {
public void onPreferenceChange_selectDefault_shouldUpdateAttributesAndSettingsGlobal() { public void onPreferenceChange_selectDefault_shouldUpdateAttributesAndSettingsGlobal() {
loadDefaultConfig(); loadDefaultConfig();
final ListPreference preference = final ListPreference preference =
mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); mController.createListPreference(mContext, TEST_PKG_NAME, TEST_APP_NAME);
mController.onPreferenceChange(preference, mValueList[DEFAULT]); mController.onPreferenceChange(preference, mValueList[DEFAULT]);
assertThat(preference.getEntry()).isEqualTo(mValueList[DEFAULT]); assertThat(preference.getEntry()).isEqualTo(mValueList[DEFAULT]);
@@ -160,7 +181,7 @@ public class GupPreferenceControllerTest {
public void onPreferenceChange_selectGup_shouldUpdateAttributesAndSettingsGlobal() { public void onPreferenceChange_selectGup_shouldUpdateAttributesAndSettingsGlobal() {
loadDefaultConfig(); loadDefaultConfig();
final ListPreference preference = final ListPreference preference =
mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); mController.createListPreference(mContext, TEST_PKG_NAME, TEST_APP_NAME);
mController.onPreferenceChange(preference, mValueList[GUP]); mController.onPreferenceChange(preference, mValueList[GUP]);
assertThat(preference.getEntry()).isEqualTo(mValueList[GUP]); assertThat(preference.getEntry()).isEqualTo(mValueList[GUP]);
@@ -176,7 +197,7 @@ public class GupPreferenceControllerTest {
public void onPreferenceChange_selectSystem_shouldUpdateAttributesAndSettingsGlobal() { public void onPreferenceChange_selectSystem_shouldUpdateAttributesAndSettingsGlobal() {
loadDefaultConfig(); loadDefaultConfig();
final ListPreference preference = final ListPreference preference =
mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); mController.createListPreference(mContext, TEST_PKG_NAME, TEST_APP_NAME);
mController.onPreferenceChange(preference, mValueList[SYSTEM]); mController.onPreferenceChange(preference, mValueList[SYSTEM]);
assertThat(preference.getEntry()).isEqualTo(mValueList[SYSTEM]); assertThat(preference.getEntry()).isEqualTo(mValueList[SYSTEM]);
@@ -212,6 +233,7 @@ public class GupPreferenceControllerTest {
mController = new GupPreferenceController(mContext, "testKey"); mController = new GupPreferenceController(mContext, "testKey");
mGroup = spy(new PreferenceCategory(mContext)); mGroup = spy(new PreferenceCategory(mContext));
final PreferenceManager preferenceManager = new PreferenceManager(mContext); final PreferenceManager preferenceManager = new PreferenceManager(mContext);
when(mGroup.getContext()).thenReturn(mContext);
when(mGroup.getPreferenceManager()).thenReturn(preferenceManager); when(mGroup.getPreferenceManager()).thenReturn(preferenceManager);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mGroup); when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mGroup);
mController.displayPreference(mScreen); mController.displayPreference(mScreen);