Merge "Use DreamManager to get initial state." into tm-qpr-dev
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.android.systemui.dreams.conditions;
|
||||
|
||||
import android.app.DreamManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
@@ -30,6 +31,7 @@ import javax.inject.Inject;
|
||||
*/
|
||||
public class DreamCondition extends Condition {
|
||||
private final Context mContext;
|
||||
private final DreamManager mDreamManager;
|
||||
|
||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
@@ -39,8 +41,10 @@ public class DreamCondition extends Condition {
|
||||
};
|
||||
|
||||
@Inject
|
||||
public DreamCondition(Context context) {
|
||||
public DreamCondition(Context context,
|
||||
DreamManager dreamManager) {
|
||||
mContext = context;
|
||||
mDreamManager = dreamManager;
|
||||
}
|
||||
|
||||
private void processIntent(Intent intent) {
|
||||
@@ -62,8 +66,8 @@ public class DreamCondition extends Condition {
|
||||
final IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(Intent.ACTION_DREAMING_STARTED);
|
||||
filter.addAction(Intent.ACTION_DREAMING_STOPPED);
|
||||
final Intent stickyIntent = mContext.registerReceiver(mReceiver, filter);
|
||||
processIntent(stickyIntent);
|
||||
mContext.registerReceiver(mReceiver, filter);
|
||||
updateCondition(mDreamManager.isDreaming());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -21,9 +21,11 @@ import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.clearInvocations;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.DreamManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
@@ -50,6 +52,9 @@ public class DreamConditionTest extends SysuiTestCase {
|
||||
@Mock
|
||||
Condition.Callback mCallback;
|
||||
|
||||
@Mock
|
||||
DreamManager mDreamManager;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
@@ -59,29 +64,39 @@ public class DreamConditionTest extends SysuiTestCase {
|
||||
* Ensure a dreaming state immediately triggers the condition.
|
||||
*/
|
||||
@Test
|
||||
public void testInitialState() {
|
||||
final Intent intent = new Intent(Intent.ACTION_DREAMING_STARTED);
|
||||
when(mContext.registerReceiver(any(), any())).thenReturn(intent);
|
||||
final DreamCondition condition = new DreamCondition(mContext);
|
||||
public void testInitialDreamingState() {
|
||||
when(mDreamManager.isDreaming()).thenReturn(true);
|
||||
final DreamCondition condition = new DreamCondition(mContext, mDreamManager);
|
||||
condition.addCallback(mCallback);
|
||||
condition.start();
|
||||
|
||||
verify(mCallback).onConditionChanged(eq(condition));
|
||||
assertThat(condition.isConditionMet()).isTrue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure a non-dreaming state does not trigger the condition.
|
||||
*/
|
||||
@Test
|
||||
public void testInitialNonDreamingState() {
|
||||
when(mDreamManager.isDreaming()).thenReturn(false);
|
||||
final DreamCondition condition = new DreamCondition(mContext, mDreamManager);
|
||||
condition.addCallback(mCallback);
|
||||
|
||||
verify(mCallback, never()).onConditionChanged(eq(condition));
|
||||
assertThat(condition.isConditionMet()).isFalse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that changing dream state triggers condition.
|
||||
*/
|
||||
@Test
|
||||
public void testChange() {
|
||||
final Intent intent = new Intent(Intent.ACTION_DREAMING_STARTED);
|
||||
final ArgumentCaptor<BroadcastReceiver> receiverCaptor =
|
||||
ArgumentCaptor.forClass(BroadcastReceiver.class);
|
||||
when(mContext.registerReceiver(receiverCaptor.capture(), any())).thenReturn(intent);
|
||||
final DreamCondition condition = new DreamCondition(mContext);
|
||||
when(mDreamManager.isDreaming()).thenReturn(true);
|
||||
final DreamCondition condition = new DreamCondition(mContext, mDreamManager);
|
||||
condition.addCallback(mCallback);
|
||||
condition.start();
|
||||
verify(mContext).registerReceiver(receiverCaptor.capture(), any());
|
||||
clearInvocations(mCallback);
|
||||
receiverCaptor.getValue().onReceive(mContext, new Intent(Intent.ACTION_DREAMING_STOPPED));
|
||||
verify(mCallback).onConditionChanged(eq(condition));
|
||||
|
||||
Reference in New Issue
Block a user