Merge "Only register conditions receiver when needed." into oc-dev am: 258e8a8117

am: c5cc3ad8ab

Change-Id: Iee3b638cf04acd6fa6ea01ec5ec24e001e15141a
This commit is contained in:
Doris Ling
2017-03-29 20:37:01 +00:00
committed by android-build-merger
10 changed files with 234 additions and 73 deletions

View File

@@ -15,7 +15,9 @@
*/
package com.android.settings.dashboard.conditional;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.graphics.drawable.Icon;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.SettingsRobolectricTestRunner;
@@ -31,7 +33,9 @@ import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@@ -41,6 +45,8 @@ public class ConditionTest {
private ConditionManager mConditionManager;
@Mock
private MetricsFeatureProvider mMetricsFeatureProvider;
@Mock
private Context mContext;
private TestCondition mCondition;
@@ -48,6 +54,7 @@ public class ConditionTest {
public void setUp() {
MockitoAnnotations.initMocks(this);
mCondition = new TestCondition(mConditionManager, mMetricsFeatureProvider);
when(mConditionManager.getContext()).thenReturn(mContext);
}
@Test
@@ -66,9 +73,26 @@ public class ConditionTest {
eq(TestCondition.TEST_METRIC_CONSTANT));
}
@Test
public void onSilenceChanged_silenced_shouldRegisterReceiver() {
mCondition.onSilenceChanged(true);
verify(mContext).registerReceiver(
TestCondition.mReceiver, TestCondition.TESTS_INTENT_FILTER);
}
@Test
public void onSilenceChanged_notSilenced_shouldUnregisterReceiver() {
mCondition.onSilenceChanged(false);
verify(mContext).unregisterReceiver(TestCondition.mReceiver);
}
private static final class TestCondition extends Condition {
private static final int TEST_METRIC_CONSTANT = 1234;
private static final IntentFilter TESTS_INTENT_FILTER = new IntentFilter("TestIntent");
private static final BroadcastReceiver mReceiver = mock(BroadcastReceiver.class);
TestCondition(ConditionManager manager,
MetricsFeatureProvider metricsFeatureProvider) {
@@ -114,5 +138,16 @@ public class ConditionTest {
public void onActionClick(int index) {
}
@Override
public BroadcastReceiver getReceiver() {
return mReceiver;
}
@Override
public IntentFilter getIntentFilter() {
return TESTS_INTENT_FILTER;
}
}
}

View File

@@ -0,0 +1,96 @@
/*
* 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.dashboard.conditional;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
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 DndConditionTest {
@Mock
private ConditionManager mConditionManager;
@Mock
private PackageManager mPackageManager;
@Mock
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mConditionManager.getContext()).thenReturn(mContext);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
}
@Test
public void constructor_shouldNotDisableReceiver() {
DndCondition condition = new DndCondition(mConditionManager);
verify(mPackageManager, never()).setComponentEnabledSetting(any(ComponentName.class),
eq(PackageManager.COMPONENT_ENABLED_STATE_DISABLED), eq(PackageManager.DONT_KILL_APP));
}
@Test
public void constructor_shouldRegisterReceiver() {
DndCondition condition = new DndCondition(mConditionManager);
verify(mContext).registerReceiver(any(DndCondition.Receiver.class),
eq(DndCondition.DND_FILTER));
}
@Test
public void silence_shouldNotDisableReceiver() {
DndCondition condition = new DndCondition(mConditionManager);
condition.silence();
verify(mPackageManager, never()).setComponentEnabledSetting(any(ComponentName.class),
eq(PackageManager.COMPONENT_ENABLED_STATE_DISABLED), eq(PackageManager.DONT_KILL_APP));
}
@Test
public void onResume_shouldRegisterReceiver() {
DndCondition condition = new DndCondition(mConditionManager);
condition.onPause();
condition.onResume();
// one from constructor, one from onResume()
verify(mContext, times(2)).registerReceiver(any(DndCondition.Receiver.class),
eq(DndCondition.DND_FILTER));
}
@Test
public void onPause_shouldUnregisterReceiver() {
DndCondition condition = new DndCondition(mConditionManager);
condition.onPause();
verify(mContext).unregisterReceiver(any(DndCondition.Receiver.class));
}
}