aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/org/cyanogenmod/tests/externalviews/keyguardexternalviews/KeyguardExternalViewTest.java
blob: a12ac27314a5cf31370519589c51902fc2e7f83a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
 * Copyright (c) 2016, The CyanogenMod 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 org.cyanogenmod.tests.externalviews.keyguardexternalviews;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.test.AndroidTestCase;
import android.view.WindowManager;
import cyanogenmod.externalviews.IExternalViewProviderFactory;
import cyanogenmod.externalviews.IKeyguardExternalViewCallbacks;
import cyanogenmod.externalviews.IKeyguardExternalViewProvider;
import cyanogenmod.externalviews.KeyguardExternalView;
import org.cyanogenmod.tests.common.MockIBinderStubForInterface;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

public class KeyguardExternalViewTest extends AndroidTestCase {
    private IKeyguardExternalViewProvider.Stub mIKeyguardExternalViewProvider;
    private IExternalViewProviderFactory.Stub mExternalViewProviderFactory;
    private WindowManager mWindowManagerMock;
    private Context mContextMock;
    private ServiceConnection mServiceConnection;
    private IKeyguardExternalViewCallbacks mKeyguardCallback;
    private KeyguardExternalView mExternalView;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        WindowManager windowManager = (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);

        // Ensure we mock context but invoke non intercepted calls to impl
        mContextMock = Mockito.mock(Context.class, Mockito.CALLS_REAL_METHODS);

        // Needed since ExternalView's base class instantiates things off this.
        // We can't use a spy here since ContextImpl is hidden (PowerMock ?)
        // For now just redirect these to the test context
        Mockito.doReturn(getContext().getApplicationInfo()).when(mContextMock).getApplicationInfo();
        Mockito.doReturn(getContext().getResources()).when(mContextMock).getResources();
        Mockito.doReturn(getContext().getTheme()).when(mContextMock).getTheme();

        // Mock window manager to ensure we don't try to add the windows
        mWindowManagerMock = Mockito.mock(WindowManager.class);
        Mockito.doReturn(mWindowManagerMock).when(mContextMock).getSystemService(Context.WINDOW_SERVICE);
        Mockito.doReturn(windowManager.getDefaultDisplay()).when(mWindowManagerMock).getDefaultDisplay();

        // Mock the viewProvider/KeyguardView to keep track of callback invocations
        mIKeyguardExternalViewProvider = MockIBinderStubForInterface
                .getMockInterface(IKeyguardExternalViewProvider.Stub.class);
        mExternalViewProviderFactory = MockIBinderStubForInterface
                .getMockInterface(IExternalViewProviderFactory.Stub.class);

        // Ensure we return our view provider when the factory is asked to create external view
        Mockito.doReturn(mIKeyguardExternalViewProvider)
                .when(mExternalViewProviderFactory)
                .createExternalView(Mockito.any(Bundle.class));

        // Store the callback object registered by the view
        Mockito.doAnswer(new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                mKeyguardCallback = (IKeyguardExternalViewCallbacks) invocation.getArguments()[0];
                return null;
            }
        }).when(mIKeyguardExternalViewProvider)
                .registerCallback(Mockito.notNull(IKeyguardExternalViewCallbacks.class));

        // Simulate bound service connection when bindService is invoked
        Mockito.doAnswer(new Answer() {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                ServiceConnection connection = (ServiceConnection) invocation.getArguments()[1];
                connection.onServiceConnected(null, mExternalViewProviderFactory);
                mServiceConnection = connection;
                return true;
            }
        }).when(mContextMock).bindService(Mockito.any(Intent.class),
                Mockito.any(ServiceConnection.class), Mockito.anyInt());
    }

    public void testValidServiceBind() {
        mExternalView = new KeyguardExternalView(mContextMock, null, new ComponentName("", ""));

        // Ensure we attempted to bind to the service
        Mockito.verify(mContextMock, Mockito.times(1)).bindService(Mockito.any(Intent.class),
                Mockito.any(ServiceConnection.class), Mockito.anyInt());
    }

    public void testInvalidServiceBind() {
        mExternalView = new KeyguardExternalView(mContextMock, null, null);
        // Ensure we did not attempt to bind to the service
        Mockito.verify(mContextMock, Mockito.never()).bindService(Mockito.any(Intent.class),
                Mockito.any(ServiceConnection.class), Mockito.anyInt());
    }

    public void testServiceAndCallbacksRegistered() throws RemoteException {
        testValidServiceBind();

        // Ensure a view was asked to be created
        Mockito.verify(mExternalViewProviderFactory, Mockito.times(1))
                .createExternalView(Mockito.any(Bundle.class));

        // Ensure callbacks were registered
        Mockito.verify(mIKeyguardExternalViewProvider, Mockito.times(1))
                .registerCallback(Mockito.notNull(IKeyguardExternalViewCallbacks.class));

        assertNotNull(mKeyguardCallback);
    }

    public void testServiceUnbindAndCallbacksUnRegistered() throws RemoteException {
        testServiceAndCallbacksRegistered();

        assertNotNull(mServiceConnection);
        mServiceConnection.onServiceDisconnected(null);

        // Ensure callbacks were registered
        Mockito.verify(mIKeyguardExternalViewProvider, Mockito.times(1))
                .unregisterCallback(Mockito.notNull(IKeyguardExternalViewCallbacks.class));
    }

    // Ensure provider is alerted view callbacks
    public void testViewProviderCallbacks() throws RemoteException {
        testServiceAndCallbacksRegistered();

        Mockito.reset(mIKeyguardExternalViewProvider);
        mExternalView.onKeyguardShowing(true);
        Mockito.verify(mIKeyguardExternalViewProvider,
                Mockito.times(1)).onKeyguardShowing(Mockito.anyBoolean());
        Mockito.verifyNoMoreInteractions(mIKeyguardExternalViewProvider);

        Mockito.reset(mIKeyguardExternalViewProvider);
        mExternalView.onAttachedToWindow();
        Mockito.verify(mIKeyguardExternalViewProvider,
                Mockito.times(1)).onAttach(Mockito.any(IBinder.class));
        Mockito.verifyNoMoreInteractions(mIKeyguardExternalViewProvider);

        Mockito.reset(mIKeyguardExternalViewProvider);
        mExternalView.onDetachedFromWindow();
        Mockito.verify(mIKeyguardExternalViewProvider, Mockito.times(1)).onDetach();
        Mockito.verifyNoMoreInteractions(mIKeyguardExternalViewProvider);

        Mockito.reset(mIKeyguardExternalViewProvider);
        mExternalView.onBouncerShowing(true);
        Mockito.verify(mIKeyguardExternalViewProvider,
                Mockito.times(1)).onBouncerShowing(Mockito.anyBoolean());
        Mockito.verifyNoMoreInteractions(mIKeyguardExternalViewProvider);

        Mockito.reset(mIKeyguardExternalViewProvider);
        mExternalView.onKeyguardDismissed();
        Mockito.verify(mIKeyguardExternalViewProvider,
                Mockito.times(1)).onKeyguardDismissed();
        Mockito.verifyNoMoreInteractions(mIKeyguardExternalViewProvider);

        Mockito.reset(mIKeyguardExternalViewProvider);
        mExternalView.onLockscreenSlideOffsetChanged(1f);
        Mockito.verify(mIKeyguardExternalViewProvider,
                Mockito.times(1)).onLockscreenSlideOffsetChanged(Mockito.eq(1f));
        Mockito.verifyNoMoreInteractions(mIKeyguardExternalViewProvider);

        Mockito.reset(mIKeyguardExternalViewProvider);
        mExternalView.onScreenTurnedOff();
        Mockito.verify(mIKeyguardExternalViewProvider, Mockito.times(1)).onScreenTurnedOff();
        Mockito.verifyNoMoreInteractions(mIKeyguardExternalViewProvider);

        Mockito.reset(mIKeyguardExternalViewProvider);
        mExternalView.onScreenTurnedOn();
        Mockito.verify(mIKeyguardExternalViewProvider, Mockito.times(1)).onScreenTurnedOn();
        Mockito.verifyNoMoreInteractions(mIKeyguardExternalViewProvider);
    }

    public void testWindowMovement() throws RemoteException {
        testServiceAndCallbacksRegistered();
        Mockito.reset(mIKeyguardExternalViewProvider);

        mExternalView.setLeft(0);
        mExternalView.setTop(0);
        mExternalView.setRight(100);
        mExternalView.setBottom(100);

        mExternalView.onPreDraw();
        Mockito.verify(mIKeyguardExternalViewProvider, Mockito.times(1))
                .alterWindow(Mockito.eq(0), Mockito.eq(0), Mockito.anyInt(),
                        Mockito.anyInt(), Mockito.eq(true), Mockito.any(Rect.class));
        Mockito.verifyNoMoreInteractions(mIKeyguardExternalViewProvider);
    }

    public void testWindowAttachmentCallbacks() throws RemoteException {
        testServiceAndCallbacksRegistered();

        KeyguardExternalView.OnWindowAttachmentChangedListener callback =
                Mockito.mock(KeyguardExternalView.OnWindowAttachmentChangedListener.class);
        mExternalView.registerOnWindowAttachmentChangedListener(callback);

        mKeyguardCallback.onAttachedToWindow();
        Mockito.verify(callback, Mockito.times(1)).onAttachedToWindow();
        Mockito.verifyNoMoreInteractions(callback);

        mKeyguardCallback.onDetachedFromWindow();
        Mockito.verify(callback, Mockito.times(1)).onDetachedFromWindow();
        Mockito.verifyNoMoreInteractions(callback);
    }

    public void testKeyguardViewCallbacks() throws RemoteException {
        testServiceAndCallbacksRegistered();

        KeyguardExternalView.KeyguardExternalViewCallbacks callback = Mockito.mock(
                KeyguardExternalView.KeyguardExternalViewCallbacks.class);
        mExternalView.registerKeyguardExternalViewCallback(callback);

        mKeyguardCallback.requestDismiss();
        Mockito.verify(callback, Mockito.times(1)).requestDismiss();
        Mockito.verifyNoMoreInteractions(callback);

        Intent i = new Intent();
        mKeyguardCallback.requestDismissAndStartActivity(i);
        Mockito.verify(callback, Mockito.times(1))
                .requestDismissAndStartActivity(Mockito.eq(i));
        Mockito.verifyNoMoreInteractions(callback);

        mKeyguardCallback.setInteractivity(true);
        assert(mExternalView.isInteractive());
        Mockito.verifyNoMoreInteractions(callback);

        mKeyguardCallback.slideLockscreenIn();
        Mockito.verify(callback, Mockito.times(1)).slideLockscreenIn();
        Mockito.verifyNoMoreInteractions(callback);

        mExternalView.binderDied();
        Mockito.verify(callback, Mockito.times(1)).providerDied();
        Mockito.verifyNoMoreInteractions(callback);
    }
}