OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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.chromecast.shell; |
| 6 |
| 7 import static org.mockito.Mockito.any; |
| 8 import static org.mockito.Mockito.eq; |
| 9 import static org.mockito.Mockito.verify; |
| 10 |
| 11 import android.app.Activity; |
| 12 import android.content.BroadcastReceiver; |
| 13 import android.content.Context; |
| 14 import android.content.Intent; |
| 15 import android.content.IntentFilter; |
| 16 import android.content.ServiceConnection; |
| 17 import android.os.PatternMatcher; |
| 18 import android.support.v4.content.LocalBroadcastManager; |
| 19 |
| 20 import org.junit.Assert; |
| 21 import org.junit.Assume; |
| 22 import org.junit.Before; |
| 23 import org.junit.Test; |
| 24 import org.junit.runner.RunWith; |
| 25 import org.mockito.ArgumentCaptor; |
| 26 import org.mockito.Mock; |
| 27 import org.mockito.Mockito; |
| 28 import org.mockito.MockitoAnnotations; |
| 29 import org.robolectric.Robolectric; |
| 30 import org.robolectric.Shadows; |
| 31 import org.robolectric.annotation.Config; |
| 32 import org.robolectric.shadows.ShadowActivity; |
| 33 |
| 34 import org.chromium.content_public.browser.WebContents; |
| 35 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 36 |
| 37 /** |
| 38 * Tests for CastWebContentsComponent. |
| 39 */ |
| 40 @RunWith(LocalRobolectricTestRunner.class) |
| 41 @Config(manifest = Config.NONE) |
| 42 public class CastWebContentsComponentTest { |
| 43 private static final String INSTANCE_ID = "1"; |
| 44 |
| 45 @Mock |
| 46 private WebContents mWebContents; |
| 47 |
| 48 private Activity mActivity; |
| 49 private ShadowActivity mShadowActivity; |
| 50 |
| 51 @Before |
| 52 public void setUp() { |
| 53 MockitoAnnotations.initMocks(this); |
| 54 mActivity = Mockito.spy(Robolectric.buildActivity(Activity.class).setup(
).get()); |
| 55 mShadowActivity = Shadows.shadowOf(mActivity); |
| 56 } |
| 57 |
| 58 @Test |
| 59 public void testStartStartsWebContentsActivity() { |
| 60 Assume.assumeFalse(BuildConfig.DISPLAY_WEB_CONTENTS_IN_SERVICE); |
| 61 |
| 62 CastWebContentsComponent component = new CastWebContentsComponent(INSTAN
CE_ID, null, null); |
| 63 component.start(mActivity, mWebContents); |
| 64 Intent intent = mShadowActivity.getNextStartedActivity(); |
| 65 Assert.assertEquals( |
| 66 intent.getComponent().getClassName(), CastWebContentsActivity.cl
ass.getName()); |
| 67 |
| 68 component.stop(mActivity); |
| 69 } |
| 70 |
| 71 @Test |
| 72 public void testStopSendsStopSignalToActivity() { |
| 73 Assume.assumeFalse(BuildConfig.DISPLAY_WEB_CONTENTS_IN_SERVICE); |
| 74 |
| 75 BroadcastReceiver receiver = Mockito.mock(BroadcastReceiver.class); |
| 76 IntentFilter intentFilter = new IntentFilter(CastWebContentsActivity.ACT
ION_STOP_ACTIVITY); |
| 77 intentFilter.addDataScheme(CastWebContentsComponent.ACTION_DATA_SCHEME); |
| 78 intentFilter.addDataAuthority(CastWebContentsComponent.ACTION_DATA_AUTHO
RITY, null); |
| 79 intentFilter.addDataPath("/" + INSTANCE_ID, PatternMatcher.PATTERN_LITER
AL); |
| 80 LocalBroadcastManager.getInstance(mActivity).registerReceiver(receiver,
intentFilter); |
| 81 |
| 82 CastWebContentsComponent component = new CastWebContentsComponent(INSTAN
CE_ID, null, null); |
| 83 component.start(mActivity, mWebContents); |
| 84 component.stop(mActivity); |
| 85 |
| 86 LocalBroadcastManager.getInstance(mActivity).unregisterReceiver(receiver
); |
| 87 |
| 88 verify(receiver).onReceive(any(Context.class), any(Intent.class)); |
| 89 } |
| 90 |
| 91 @Test |
| 92 public void testStartBindsWebContentsService() { |
| 93 Assume.assumeTrue(BuildConfig.DISPLAY_WEB_CONTENTS_IN_SERVICE); |
| 94 |
| 95 CastWebContentsComponent component = new CastWebContentsComponent(INSTAN
CE_ID, null, null); |
| 96 component.start(mActivity, mWebContents); |
| 97 component.stop(mActivity); |
| 98 |
| 99 ArgumentCaptor<Intent> intent = ArgumentCaptor.forClass(Intent.class); |
| 100 verify(mActivity).bindService( |
| 101 intent.capture(), any(ServiceConnection.class), eq(Context.BIND_
AUTO_CREATE)); |
| 102 Assert.assertEquals(intent.getValue().getComponent().getClassName(), |
| 103 CastWebContentsService.class.getName()); |
| 104 } |
| 105 |
| 106 @Test |
| 107 public void testStopUnbindsWebContentsService() { |
| 108 Assume.assumeTrue(BuildConfig.DISPLAY_WEB_CONTENTS_IN_SERVICE); |
| 109 |
| 110 CastWebContentsComponent component = new CastWebContentsComponent(INSTAN
CE_ID, null, null); |
| 111 component.start(mActivity, mWebContents); |
| 112 component.stop(mActivity); |
| 113 |
| 114 verify(mActivity).unbindService(any(ServiceConnection.class)); |
| 115 } |
| 116 |
| 117 @Test |
| 118 public void testOnComponentClosedCallsCallback() { |
| 119 CastWebContentsComponent.OnComponentClosedHandler callback = |
| 120 Mockito.mock(CastWebContentsComponent.OnComponentClosedHandler.c
lass); |
| 121 |
| 122 CastWebContentsComponent component = |
| 123 new CastWebContentsComponent(INSTANCE_ID, callback, null); |
| 124 component.start(mActivity, mWebContents); |
| 125 CastWebContentsComponent.onComponentClosed(mActivity, INSTANCE_ID); |
| 126 verify(callback).onComponentClosed(); |
| 127 |
| 128 component.stop(mActivity); |
| 129 } |
| 130 |
| 131 @Test |
| 132 public void testOnKeyDownCallsCallback() { |
| 133 CastWebContentsComponent.OnKeyDownHandler callback = |
| 134 Mockito.mock(CastWebContentsComponent.OnKeyDownHandler.class); |
| 135 |
| 136 CastWebContentsComponent component = |
| 137 new CastWebContentsComponent(INSTANCE_ID, null, callback); |
| 138 component.start(mActivity, mWebContents); |
| 139 CastWebContentsComponent.onKeyDown(mActivity, INSTANCE_ID, 42); |
| 140 component.stop(mActivity); |
| 141 |
| 142 verify(callback).onKeyDown(42); |
| 143 } |
| 144 } |
OLD | NEW |