1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.systemui.doze;
18
19import static org.mockito.ArgumentMatchers.anyBoolean;
20import static org.mockito.ArgumentMatchers.eq;
21import static org.mockito.Mockito.mock;
22import static org.mockito.Mockito.reset;
23import static org.mockito.Mockito.verify;
24import static org.mockito.Mockito.when;
25
26import android.app.IWallpaperManager;
27import android.os.RemoteException;
28import android.support.test.filters.SmallTest;
29
30import com.android.keyguard.KeyguardUpdateMonitor;
31import com.android.systemui.SysuiTestCase;
32import com.android.systemui.statusbar.phone.DozeParameters;
33
34import org.junit.Before;
35import org.junit.Test;
36import org.junit.runner.RunWith;
37import org.junit.runners.JUnit4;
38import org.mockito.Mock;
39import org.mockito.MockitoAnnotations;
40
41@RunWith(JUnit4.class)
42@SmallTest
43public class DozeWallpaperStateTest extends SysuiTestCase {
44
45    private DozeWallpaperState mDozeWallpaperState;
46    @Mock IWallpaperManager mIWallpaperManager;
47    @Mock DozeParameters mDozeParameters;
48
49    @Before
50    public void setUp() {
51        MockitoAnnotations.initMocks(this);
52        mDozeWallpaperState = new DozeWallpaperState(mIWallpaperManager, mDozeParameters);
53    }
54
55    @Test
56    public void testDreamNotification() throws RemoteException {
57        // Pre-condition
58        when(mDozeParameters.getAlwaysOn()).thenReturn(true);
59
60        mDozeWallpaperState.transitionTo(DozeMachine.State.UNINITIALIZED,
61                DozeMachine.State.DOZE_AOD);
62        verify(mIWallpaperManager).setInAmbientMode(eq(true), anyBoolean());
63        mDozeWallpaperState.transitionTo(DozeMachine.State.DOZE_AOD, DozeMachine.State.FINISH);
64        verify(mIWallpaperManager).setInAmbientMode(eq(false), anyBoolean());
65
66        // Make sure we're sending false when AoD is off
67        reset(mDozeParameters);
68        mDozeWallpaperState.transitionTo(DozeMachine.State.FINISH, DozeMachine.State.DOZE_AOD);
69        verify(mIWallpaperManager).setInAmbientMode(eq(false), anyBoolean());
70    }
71
72    @Test
73    public void testAnimates_whenSupported() throws RemoteException {
74        // Pre-conditions
75        when(mDozeParameters.getDisplayNeedsBlanking()).thenReturn(false);
76        when(mDozeParameters.shouldControlScreenOff()).thenReturn(true);
77        when(mDozeParameters.getAlwaysOn()).thenReturn(true);
78
79        mDozeWallpaperState.transitionTo(DozeMachine.State.UNINITIALIZED,
80                DozeMachine.State.DOZE_AOD);
81        verify(mIWallpaperManager).setInAmbientMode(eq(true), eq(true));
82
83        mDozeWallpaperState.transitionTo(DozeMachine.State.DOZE_AOD, DozeMachine.State.FINISH);
84        verify(mIWallpaperManager).setInAmbientMode(eq(false), eq(true));
85    }
86
87    @Test
88    public void testDoesNotAnimate_whenNotSupported() throws RemoteException {
89        // Pre-conditions
90        when(mDozeParameters.getDisplayNeedsBlanking()).thenReturn(true);
91        when(mDozeParameters.getAlwaysOn()).thenReturn(true);
92        when(mDozeParameters.shouldControlScreenOff()).thenReturn(false);
93
94        mDozeWallpaperState.transitionTo(DozeMachine.State.UNINITIALIZED,
95                DozeMachine.State.DOZE_AOD);
96        verify(mIWallpaperManager).setInAmbientMode(eq(true), eq(false));
97
98        mDozeWallpaperState.transitionTo(DozeMachine.State.DOZE_AOD, DozeMachine.State.FINISH);
99        verify(mIWallpaperManager).setInAmbientMode(eq(false), eq(false));
100    }
101
102    @Test
103    public void testTransitionTo_requestPulseIsAmbientMode() throws RemoteException {
104        mDozeWallpaperState.transitionTo(DozeMachine.State.DOZE,
105                DozeMachine.State.DOZE_REQUEST_PULSE);
106        verify(mIWallpaperManager).setInAmbientMode(eq(true), eq(false));
107    }
108
109    @Test
110    public void testTransitionTo_pulseIsAmbientMode() throws RemoteException {
111        mDozeWallpaperState.transitionTo(DozeMachine.State.DOZE_REQUEST_PULSE,
112                DozeMachine.State.DOZE_PULSING);
113        verify(mIWallpaperManager).setInAmbientMode(eq(true), eq(false));
114    }
115
116    @Test
117    public void testTransitionTo_animatesWhenWakingUpFromPulse() throws RemoteException {
118        mDozeWallpaperState.transitionTo(DozeMachine.State.DOZE_REQUEST_PULSE,
119                DozeMachine.State.DOZE_PULSING);
120        reset(mIWallpaperManager);
121        mDozeWallpaperState.transitionTo(DozeMachine.State.DOZE_PULSING,
122                DozeMachine.State.FINISH);
123        verify(mIWallpaperManager).setInAmbientMode(eq(false), eq(true));
124    }
125}
126