| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.base; | 5 package org.chromium.base; |
| 6 | 6 |
| 7 import static org.mockito.Mockito.mock; | 7 import static org.mockito.Mockito.mock; |
| 8 import static org.mockito.Mockito.verify; | 8 import static org.mockito.Mockito.verify; |
| 9 | 9 |
| 10 import android.app.Activity; | 10 import android.app.Activity; |
| 11 import android.view.KeyEvent; | 11 import android.view.KeyEvent; |
| 12 | 12 |
| 13 import junit.framework.Assert; | 13 import junit.framework.Assert; |
| 14 | 14 |
| 15 import org.chromium.base.BaseChromiumApplication.WindowFocusChangedListener; | 15 import org.chromium.base.BaseChromiumApplication.WindowFocusChangedListener; |
| 16 import org.chromium.base.test.shadows.ShadowMultiDex; |
| 16 import org.chromium.testing.local.LocalRobolectricTestRunner; | 17 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 17 import org.junit.Test; | 18 import org.junit.Test; |
| 18 import org.junit.runner.RunWith; | 19 import org.junit.runner.RunWith; |
| 19 import org.robolectric.Robolectric; | 20 import org.robolectric.Robolectric; |
| 20 import org.robolectric.annotation.Config; | 21 import org.robolectric.annotation.Config; |
| 21 import org.robolectric.annotation.Implementation; | 22 import org.robolectric.annotation.Implementation; |
| 22 import org.robolectric.annotation.Implements; | 23 import org.robolectric.annotation.Implements; |
| 23 import org.robolectric.shadows.ShadowActivity; | 24 import org.robolectric.shadows.ShadowActivity; |
| 24 import org.robolectric.util.ActivityController; | 25 import org.robolectric.util.ActivityController; |
| 25 | 26 |
| 26 /** Unit tests for {@link BaseChromiumApplication}. */ | 27 /** Unit tests for {@link BaseChromiumApplication}. */ |
| 27 @RunWith(LocalRobolectricTestRunner.class) | 28 @RunWith(LocalRobolectricTestRunner.class) |
| 28 @Config(manifest = Config.NONE, application = BaseChromiumApplication.class, | 29 @Config(manifest = Config.NONE, application = BaseChromiumApplication.class, |
| 29 shadows = {BaseChromiumApplicationTest.TrackingShadowActivity.class}) | 30 shadows = {BaseChromiumApplicationTest.TrackingShadowActivity.class, Sha
dowMultiDex.class}) |
| 30 public class BaseChromiumApplicationTest { | 31 public class BaseChromiumApplicationTest { |
| 31 | 32 /** Shadow that tracks calls to onWindowFocusChanged and dispatchKeyEvent. *
/ |
| 32 @Implements(Activity.class) | 33 @Implements(Activity.class) |
| 33 public static class TrackingShadowActivity extends ShadowActivity { | 34 public static class TrackingShadowActivity extends ShadowActivity { |
| 34 private int mWindowFocusCalls; | 35 private int mWindowFocusCalls; |
| 35 private int mDispatchKeyEventCalls; | 36 private int mDispatchKeyEventCalls; |
| 36 private boolean mReturnValueForKeyDispatch; | 37 private boolean mReturnValueForKeyDispatch; |
| 37 | 38 |
| 38 @Implementation | 39 @Implementation |
| 39 public void onWindowFocusChanged(@SuppressWarnings("unused") boolean has
Focus) { | 40 public void onWindowFocusChanged(@SuppressWarnings("unused") boolean has
Focus) { |
| 40 mWindowFocusCalls++; | 41 mWindowFocusCalls++; |
| 41 } | 42 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 58 Robolectric.buildActivity(Activity.class).create().start().visib
le(); | 59 Robolectric.buildActivity(Activity.class).create().start().visib
le(); |
| 59 TrackingShadowActivity shadow = | 60 TrackingShadowActivity shadow = |
| 60 (TrackingShadowActivity) Robolectric.shadowOf(controller.get()); | 61 (TrackingShadowActivity) Robolectric.shadowOf(controller.get()); |
| 61 | 62 |
| 62 controller.get().getWindow().getCallback().onWindowFocusChanged(true); | 63 controller.get().getWindow().getCallback().onWindowFocusChanged(true); |
| 63 // Assert that listeners were notified. | 64 // Assert that listeners were notified. |
| 64 verify(mock).onWindowFocusChanged(controller.get(), true); | 65 verify(mock).onWindowFocusChanged(controller.get(), true); |
| 65 // Also ensure that the original activity is forwarded the notification. | 66 // Also ensure that the original activity is forwarded the notification. |
| 66 Assert.assertEquals(1, shadow.mWindowFocusCalls); | 67 Assert.assertEquals(1, shadow.mWindowFocusCalls); |
| 67 } | 68 } |
| 68 | |
| 69 @Test | |
| 70 public void testDispatchKeyEvent() throws Exception { | |
| 71 ActivityController<Activity> controller = | |
| 72 Robolectric.buildActivity(Activity.class).create().start().visib
le(); | |
| 73 TrackingShadowActivity shadow = | |
| 74 (TrackingShadowActivity) Robolectric.shadowOf(controller.get()); | |
| 75 | |
| 76 final KeyEvent menuKey = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEY
CODE_MENU); | |
| 77 | |
| 78 // Ensure that key events are forwarded. | |
| 79 Assert.assertFalse(controller.get().getWindow().getCallback().dispatchKe
yEvent(menuKey)); | |
| 80 // This gets called twice - once to see if the activity is swallowing it
, and again to | |
| 81 // dispatch it. | |
| 82 Assert.assertEquals(2, shadow.mDispatchKeyEventCalls); | |
| 83 | |
| 84 // Ensure that our activity can swallow the event. | |
| 85 shadow.mReturnValueForKeyDispatch = true; | |
| 86 Assert.assertTrue(controller.get().getWindow().getCallback().dispatchKey
Event(menuKey)); | |
| 87 Assert.assertEquals(3, shadow.mDispatchKeyEventCalls); | |
| 88 | |
| 89 // A non-enter key only dispatches once. | |
| 90 Assert.assertTrue(controller.get().getWindow().getCallback().dispatchKey
Event( | |
| 91 new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SPACE))); | |
| 92 Assert.assertEquals(4, shadow.mDispatchKeyEventCalls); | |
| 93 } | |
| 94 } | 69 } |
| OLD | NEW |