Only register conditions receiver when needed.

1. Update the dnd receiver to listen when dashboard summary running.
- remove the dnd receiver from Android manifest, and create it inside
the dnd condition.
- add lifecycle implementation to condition manager, so that the dnd
condition can know when to register and unregister the receiver.
- remove getReceiverClass() from dnd condition so that its receiver will
not be disabled by the default condition handling when condition is
silenced.

2. Remove all other conditions receiver from Android manifest.
- the broadcast receivers for HotspotCondition, AirplaneModeCondition,
CellularDataCondition from the manifest and create them inside the
condition classes.
- update Condition.onSilenceChanged() to register/unregister the
receivers instead of enable/disable the receiver class.

Change-Id: Iea6288382680df2b02884d1934b8db85daae404c
Fix: 35968517
Test: make RunSettingsRoboTests
This commit is contained in:
Doris Ling
2017-03-28 16:02:04 -07:00
parent 0f1d2bb81c
commit 0f01c849cd
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));
}
}