Migrate all remaining condition cards to v2.

Bug: 112485407
Test: robotests
Change-Id: I3c304c308fa6f499b4b5e21d4c5735cde7b90220
This commit is contained in:
Fan Zhang
2018-08-14 13:25:47 -07:00
parent 5d4bedb7d2
commit 7373ec042b
39 changed files with 1582 additions and 70 deletions

View File

@@ -14,15 +14,13 @@
* limitations under the License.
*/
package com.android.settings.homepage.conditional;
package com.android.settings.homepage.conditional.v2;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
@@ -35,7 +33,7 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsRobolectricTestRunner.class)
public class AbnormalRingerConditionBaseTest {
public class AbnormalRingerConditionControllerBaseTest {
@Mock
private ConditionManager mConditionManager;
@@ -47,53 +45,39 @@ public class AbnormalRingerConditionBaseTest {
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
when(mConditionManager.getContext()).thenReturn(mContext);
mCondition = new TestCondition(mConditionManager);
mCondition = new TestCondition(mContext, mConditionManager);
}
@Test
public void newInstance_shouldMonitorRingerStateChangeBroadcast() {
public void startMonitor_shouldMonitorRingerStateChangeBroadcast() {
final Intent broadcast1 = new Intent("foo.bar.action");
final Intent broadcast2 = new Intent(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION);
mCondition.startMonitoringStateChange();
mContext.sendBroadcast(broadcast1);
assertThat(mCondition.mRefreshCalled).isFalse();
verify(mConditionManager, never()).onConditionChanged();
mContext.sendBroadcast(broadcast2);
assertThat(mCondition.mRefreshCalled).isTrue();
verify(mConditionManager).onConditionChanged();
}
private static class TestCondition extends AbnormalRingerConditionBase {
private boolean mRefreshCalled;
private static class TestCondition extends AbnormalRingerConditionController {
TestCondition(ConditionManager manager) {
super(manager);
public TestCondition(Context appContext, ConditionManager conditionManager) {
super(appContext, conditionManager);
}
@Override
public void refreshState() {
mRefreshCalled = true;
}
@Override
public int getMetricsConstant() {
public long getId() {
return 0;
}
@Override
public Drawable getIcon() {
return null;
public boolean isDisplayable() {
return false;
}
@Override
public CharSequence getTitle() {
return null;
}
@Override
public CharSequence getSummary() {
return null;
}
}
}

View File

@@ -13,13 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.homepage.conditional;
package com.android.settings.homepage.conditional.v2;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
@@ -31,30 +30,25 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsRobolectricTestRunner.class)
public class BackgroundDataConditionTest {
@Mock
private ConditionManager mConditionManager;
public class BackgroundDataConditionControllerTest {
private Context mContext;
private BackgroundDataConditionController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mConditionManager.getContext()).thenReturn(mContext);
mController = new BackgroundDataConditionController(mContext);
}
@Test
public void onPrimaryClick_shouldReturn2SummaryActivity() {
final ArgumentCaptor<Intent> argumentCaptor = ArgumentCaptor.forClass(Intent.class);
BackgroundDataCondition backgroundDataCondition
= new BackgroundDataCondition(mConditionManager);
backgroundDataCondition.onPrimaryClick();
mController.onPrimaryClick(mContext);
verify(mContext).startActivity(argumentCaptor.capture());
Intent intent = argumentCaptor.getValue();

View File

@@ -0,0 +1,87 @@
/*
* Copyright (C) 2018 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.homepage.conditional.v2;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.content.IntentFilter;
import android.os.PowerManager;
import com.android.settings.fuelgauge.BatterySaverReceiver;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowPowerManager;
@RunWith(SettingsRobolectricTestRunner.class)
public class BatterySaverConditionControllerTest {
@Mock
private ConditionManager mConditionManager;
private ShadowPowerManager mPowerManager;
private Context mContext;
private BatterySaverConditionController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mPowerManager = Shadows.shadowOf(mContext.getSystemService(PowerManager.class));
mController = new BatterySaverConditionController(mContext, mConditionManager);
}
@Test
public void startMonitor_shouldRegisterReceiver() {
mController.startMonitoringStateChange();
verify(mContext).registerReceiver(any(BatterySaverReceiver.class), any(IntentFilter.class));
}
@Test
public void stopMonitor_shouldUnregisterReceiver() {
mController.startMonitoringStateChange();
mController.stopMonitoringStateChange();
verify(mContext).unregisterReceiver(any(BatterySaverReceiver.class));
}
@Test
public void isDisplayable_PowerSaverOn_true() {
mPowerManager.setIsPowerSaveMode(true);
assertThat(mController.isDisplayable()).isTrue();
}
@Test
public void isDisplayable_PowerSaverOff_false() {
mPowerManager.setIsPowerSaveMode(false);
assertThat(mController.isDisplayable()).isFalse();
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright (C) 2018 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.homepage.conditional.v2;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.content.ComponentName;
import android.content.Context;
import com.android.settings.Settings;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsRobolectricTestRunner.class)
public class WorkModeConditionControllerTest {
private Context mContext;
private WorkModeConditionController mController;
@Before
public void setUp() {
mContext = spy(RuntimeEnvironment.application);
mController = new WorkModeConditionController(mContext);
}
@Test
public void onPrimaryClick_shouldLaunchAccountsSetting() {
final ComponentName componentName =
new ComponentName(mContext, Settings.AccountDashboardActivity.class);
mController.onPrimaryClick(mContext);
verify(mContext).startActivity(
argThat(intent -> intent.getComponent().equals(componentName)));
}
}

View File

@@ -17,14 +17,13 @@
package com.android.settings.wallpaper;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import com.android.settings.SubSettings;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.After;
@@ -35,14 +34,11 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Resetter;
import org.robolectric.shadows.ShadowActivity;
import org.robolectric.shadows.ShadowPackageManager;
@RunWith(SettingsRobolectricTestRunner.class)
public class WallpaperSuggestionActivityTest {
@@ -65,20 +61,6 @@ public class WallpaperSuggestionActivityTest {
ShadowWallpaperManager.reset();
}
@Test
public void launch_primarySuggestionActivityDoesNotExist_shouldFallback() {
ShadowPackageManager packageManager =
Shadows.shadowOf(RuntimeEnvironment.application.getPackageManager());
packageManager.removePackage("com.android.settings");
ShadowActivity activity = Shadows.shadowOf(mController.setup().get());
final Intent intent = activity.getNextStartedActivity();
assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName());
assertThat(intent.getFlags()).isEqualTo(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
assertThat(activity.isFinishing()).isTrue();
}
@Test
public void wallpaperServiceEnabled_no_shouldReturnTrue() {
when(mContext.getResources()).thenReturn(mResources);