Remove summary provider

- use SummaryProvider to provide the summary of UserSettings
- use WifiDisplayPreferenceController to replace the summary loader
in WifiDisplaySettings
- use ConfigureNotificationPreferernceController to replace the
sumary load in ConfigureNotificationSettings

Fixes: 141653158
Test: robolectric
Change-Id: Id5f5ed645707caa0b25ecae5252174cbf017651c
This commit is contained in:
Raff Tsai
2019-10-02 16:43:23 +08:00
parent 9e3a9fd255
commit 6db277ebb7
21 changed files with 399 additions and 767 deletions

View File

@@ -1,87 +0,0 @@
/*
* 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.dashboard;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verifyZeroInteractions;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.drawer.CategoryKey;
import com.android.settingslib.drawer.Tile;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class SummaryLoaderTest {
private static final String SUMMARY_1 = "summary1";
private static final String SUMMARY_2 = "summary2";
private Context mContext;
private SummaryLoader mSummaryLoader;
private boolean mCallbackInvoked;
private Tile mTile;
private FakeFeatureFactory mFeatureFactory;
@Before
public void SetUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mFeatureFactory = FakeFeatureFactory.setupForTest();
final ActivityInfo activityInfo = new ActivityInfo();
activityInfo.packageName = "pkg";
activityInfo.name = "class";
mTile = new Tile(activityInfo, CategoryKey.CATEGORY_HOMEPAGE);
mTile.overrideSummary(SUMMARY_1);
mCallbackInvoked = false;
final Activity activity = Robolectric.buildActivity(Activity.class).get();
mSummaryLoader = new SummaryLoader(activity, CategoryKey.CATEGORY_HOMEPAGE);
mSummaryLoader.setSummaryConsumer(tile -> mCallbackInvoked = true);
}
@Test
public void newInstance_shouldNotLoadCategory() {
verifyZeroInteractions(mFeatureFactory.dashboardFeatureProvider);
}
@Test
public void testUpdateSummaryIfNeeded_SummaryIdentical_NoCallback() {
mSummaryLoader.updateSummaryIfNeeded(mContext, mTile, SUMMARY_1);
assertThat(mCallbackInvoked).isFalse();
}
@Test
public void testUpdateSummaryIfNeeded_SummaryChanged_HasCallback() {
mSummaryLoader.updateSummaryIfNeeded(mContext, mTile, SUMMARY_2);
assertThat(mCallbackInvoked).isTrue();
}
}

View File

@@ -19,9 +19,7 @@ package com.android.settings.datausage;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.endsWith;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
@@ -36,7 +34,6 @@ import android.telephony.TelephonyManager;
import androidx.fragment.app.FragmentActivity;
import com.android.settings.dashboard.SummaryLoader;
import com.android.settings.testutils.shadow.ShadowDashboardFragment;
import com.android.settings.testutils.shadow.ShadowDataUsageUtils;
import com.android.settings.testutils.shadow.ShadowUserManager;
@@ -65,8 +62,6 @@ import org.robolectric.shadows.ShadowTelephonyManager;
@RunWith(RobolectricTestRunner.class)
public class DataUsageSummaryTest {
@Mock
private SummaryLoader mSummaryLoader;
@Mock
private NetworkPolicyManager mNetworkPolicyManager;
@Mock

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) 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.notification;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import com.android.settings.testutils.shadow.ShadowNotificationBackend;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowNotificationBackend.class)
public class ConfigureNotificationPreferenceControllerTest {
private ConfigureNotificationPreferenceController mController;
private Context mContext;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mController = new ConfigureNotificationPreferenceController(mContext, "key");
}
@Test
public void getSummary_noBlockedApps() {
ShadowNotificationBackend.setBlockedAppCount(0);
assertThat(mController.getSummary().toString()).contains("On");
}
@Test
public void getSummary_someBlockedApps() {
ShadowNotificationBackend.setBlockedAppCount(5);
assertThat(mController.getSummary().toString()).contains("Off");
assertThat(mController.getSummary().toString()).contains("5");
}
}

View File

@@ -1,85 +0,0 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification;
import static com.android.settings.notification.ConfigureNotificationSettings.SUMMARY_PROVIDER_FACTORY;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import com.android.settings.dashboard.SummaryLoader;
import com.android.settings.notification.ConfigureNotificationSettings.SummaryProvider;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class ConfigureNotificationSettingsTest {
private Activity mActivity;
@Before
public void setUp() {
mActivity = spy(Robolectric.buildActivity(Activity.class).get());
}
@Test
public void getSummary_noneBlocked() {
SummaryLoader loader = mock(SummaryLoader.class);
NotificationBackend backend = mock(NotificationBackend.class);
when(backend.getBlockedAppCount()).thenReturn(0);
SummaryProvider provider =
(SummaryProvider) SUMMARY_PROVIDER_FACTORY.createSummaryProvider(mActivity, loader);
provider.setBackend(backend);
provider.setListening(true);
ArgumentCaptor<CharSequence> captor = ArgumentCaptor.forClass(CharSequence.class);
verify(loader).setSummary(any(), captor.capture());
assertThat(captor.getValue().toString()).contains("On");
}
@Test
public void getSummary_someBlocked() {
SummaryLoader loader = mock(SummaryLoader.class);
NotificationBackend backend = mock(NotificationBackend.class);
when(backend.getBlockedAppCount()).thenReturn(5);
SummaryProvider provider =
(SummaryProvider) SUMMARY_PROVIDER_FACTORY.createSummaryProvider(mActivity, loader);
provider.setBackend(backend);
provider.setListening(true);
ArgumentCaptor<CharSequence> captor = ArgumentCaptor.forClass(CharSequence.class);
verify(loader).setSummary(any(), captor.capture());
assertThat(captor.getValue().toString()).contains("Off");
assertThat(captor.getValue().toString()).contains("5");
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright (C) 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.testutils.shadow;
import com.android.settings.notification.NotificationBackend;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
@Implements(NotificationBackend.class)
public class ShadowNotificationBackend {
private static int sBlockAppCount;
@Implementation
protected int getBlockedAppCount() {
return sBlockAppCount;
}
public static void setBlockedAppCount(int blockAppCount) {
sBlockAppCount = blockAppCount;
}
}

View File

@@ -32,7 +32,6 @@ import static org.mockito.Mockito.when;
import android.content.ComponentName;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.UserInfo;
import android.graphics.drawable.Drawable;
import android.os.UserHandle;
import android.os.UserManager;
@@ -49,8 +48,6 @@ import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.dashboard.SummaryLoader;
import com.android.settings.testutils.shadow.ShadowDevicePolicyManager;
import com.android.settings.testutils.shadow.ShadowUserManager;
import com.android.settingslib.RestrictedPreference;
@@ -86,14 +83,11 @@ public class UserSettingsTest {
private UserPreference mMePreference;
@Mock
private UserManager mUserManager;
@Mock
private SummaryLoader mSummaryLoader;
private FragmentActivity mActivity;
private Context mContext;
private UserSettings mFragment;
private UserCapabilities mUserCapabilities;
private SummaryLoader.SummaryProvider mSummaryProvider;
@Before
public void setUp() {
@@ -123,9 +117,6 @@ public class UserSettingsTest {
final SharedPreferences prefs = mock(SharedPreferences .class);
when(mMockPreferenceManager.getSharedPreferences()).thenReturn(prefs);
when(mMockPreferenceManager.getContext()).thenReturn(mContext);
mSummaryProvider =
UserSettings.SUMMARY_PROVIDER_FACTORY.createSummaryProvider(mActivity, mSummaryLoader);
}
@After
@@ -134,19 +125,6 @@ public class UserSettingsTest {
Settings.Global.DEVICE_PROVISIONED, mProvisioned);
}
@Test
public void setListening_shouldSetSummaryWithUserName() {
final String name = "John";
final UserInfo userInfo = new UserInfo();
userInfo.name = name;
when(mUserManager.getUserInfo(anyInt())).thenReturn(userInfo);
mSummaryProvider.setListening(true);
verify(mSummaryLoader)
.setSummary(mSummaryProvider, mActivity.getString(R.string.users_summary, name));
}
@Test
public void testAssignDefaultPhoto_ContextNull_ReturnFalseAndNotCrash() {
// Should not crash here

View File

@@ -0,0 +1,77 @@
/*
* Copyright (C) 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.wfd;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.PackageManager;
import android.media.MediaRouter;
import com.android.settings.R;
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 WifiDisplayPreferenceControllerTest {
private Context mContext;
private WifiDisplayPreferenceController mWifiDisplayPreferenceController;
@Mock
private MediaRouter mMediaRouter;
@Mock
private PackageManager mPackageManager;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(Context.MEDIA_ROUTER_SERVICE)).thenReturn(mMediaRouter);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT)).thenReturn(true);
mWifiDisplayPreferenceController = new WifiDisplayPreferenceController(mContext, "key");
}
@Test
public void getSummary_disconnected_shouldProvideDisconnectedSummary() {
assertThat(mWifiDisplayPreferenceController.getSummary().toString()).contains(
mContext.getString(R.string.disconnected));
}
@Test
public void getSummary_connected_shouldProvideConnectedSummary() {
final MediaRouter.RouteInfo route = mock(MediaRouter.RouteInfo.class);
when(mMediaRouter.getRouteCount()).thenReturn(1);
when(mMediaRouter.getRouteAt(0)).thenReturn(route);
when(route.matchesTypes(anyInt())).thenReturn(true);
when(route.isSelected()).thenReturn(true);
when(route.isConnecting()).thenReturn(false);
assertThat(mWifiDisplayPreferenceController.getSummary().toString()).contains(
mContext.getString(R.string.wifi_display_status_connected));
}
}

View File

@@ -19,8 +19,6 @@ package com.android.settings.wfd;
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 android.app.Activity;
@@ -30,9 +28,6 @@ import android.hardware.display.DisplayManager;
import android.media.MediaRouter;
import android.net.wifi.p2p.WifiP2pManager;
import com.android.settings.R;
import com.android.settings.dashboard.SummaryLoader;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -46,45 +41,16 @@ public class WifiDisplaySettingsTest {
@Mock
private Activity mActivity;
@Mock
private SummaryLoader mSummaryLoader;
@Mock
private MediaRouter mMediaRouter;
@Mock
private PackageManager mPackageManager;
private SummaryLoader.SummaryProvider mSummaryProvider;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mActivity.getSystemService(Context.MEDIA_ROUTER_SERVICE)).thenReturn(mMediaRouter);
when(mActivity.getPackageManager()).thenReturn(mPackageManager);
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT)).thenReturn(true);
mSummaryProvider = WifiDisplaySettings.SUMMARY_PROVIDER_FACTORY
.createSummaryProvider(mActivity, mSummaryLoader);
}
@Test
public void listenToSummary_disconnected_shouldProvideDisconnectedSummary() {
mSummaryProvider.setListening(true);
verify(mActivity).getString(R.string.disconnected);
verify(mActivity, never()).getString(R.string.wifi_display_status_connected);
}
@Test
public void listenToSummary_connected_shouldProvideConnectedSummary() {
final MediaRouter.RouteInfo route = mock(MediaRouter.RouteInfo.class);
when(mMediaRouter.getRouteCount()).thenReturn(1);
when(mMediaRouter.getRouteAt(0)).thenReturn(route);
when(route.matchesTypes(MediaRouter.ROUTE_TYPE_REMOTE_DISPLAY)).thenReturn(true);
when(route.isSelected()).thenReturn(true);
when(route.isConnecting()).thenReturn(false);
mSummaryProvider.setListening(true);
verify(mActivity).getString(R.string.wifi_display_status_connected);
}
@Test