• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.base;
6 
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.verify;
9 
10 import android.app.Activity;
11 import android.view.KeyEvent;
12 
13 import junit.framework.Assert;
14 
15 import org.chromium.base.BaseChromiumApplication.WindowFocusChangedListener;
16 import org.chromium.base.test.shadows.ShadowMultiDex;
17 import org.chromium.testing.local.LocalRobolectricTestRunner;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.robolectric.Robolectric;
21 import org.robolectric.annotation.Config;
22 import org.robolectric.annotation.Implementation;
23 import org.robolectric.annotation.Implements;
24 import org.robolectric.shadows.ShadowActivity;
25 import org.robolectric.util.ActivityController;
26 
27 /** Unit tests for {@link BaseChromiumApplication}. */
28 @RunWith(LocalRobolectricTestRunner.class)
29 @Config(manifest = Config.NONE,
30         application = BaseChromiumApplication.class,
31         shadows = {BaseChromiumApplicationTest.TrackingShadowActivity.class, ShadowMultiDex.class})
32 public class BaseChromiumApplicationTest {
33 
34     /** Shadow that tracks calls to onWindowFocusChanged and dispatchKeyEvent. */
35     @Implements(Activity.class)
36     public static class TrackingShadowActivity extends ShadowActivity {
37         private int mWindowFocusCalls;
38         private int mDispatchKeyEventCalls;
39         private boolean mReturnValueForKeyDispatch;
40 
41         @Implementation
onWindowFocusChanged(@uppressWarnings"unused") boolean hasFocus)42         public void onWindowFocusChanged(@SuppressWarnings("unused") boolean hasFocus) {
43             mWindowFocusCalls++;
44         }
45 
46         @Implementation
dispatchKeyEvent(@uppressWarnings"unused") KeyEvent event)47         public boolean dispatchKeyEvent(@SuppressWarnings("unused") KeyEvent event) {
48             mDispatchKeyEventCalls++;
49             return mReturnValueForKeyDispatch;
50         }
51     }
52 
53     @Test
testWindowsFocusChanged()54     public void testWindowsFocusChanged() throws Exception {
55         BaseChromiumApplication app = (BaseChromiumApplication) Robolectric.application;
56 
57         WindowFocusChangedListener mock = mock(WindowFocusChangedListener.class);
58         app.registerWindowFocusChangedListener(mock);
59 
60         ActivityController<Activity> controller =
61                 Robolectric.buildActivity(Activity.class).create().start().visible();
62         TrackingShadowActivity shadow =
63                 (TrackingShadowActivity) Robolectric.shadowOf(controller.get());
64 
65         controller.get().getWindow().getCallback().onWindowFocusChanged(true);
66         // Assert that listeners were notified.
67         verify(mock).onWindowFocusChanged(controller.get(), true);
68         // Also ensure that the original activity is forwarded the notification.
69         Assert.assertEquals(1, shadow.mWindowFocusCalls);
70     }
71 }
72