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.android_webview.crash; |
| 6 |
| 7 import static org.junit.Assert.assertEquals; |
| 8 import static org.mockito.Mockito.eq; |
| 9 import static org.mockito.Mockito.spy; |
| 10 import static org.mockito.Mockito.when; |
| 11 |
| 12 import android.content.Context; |
| 13 import android.webkit.ValueCallback; |
| 14 |
| 15 import org.junit.Before; |
| 16 import org.junit.Test; |
| 17 import org.junit.runner.RunWith; |
| 18 import org.mockito.Answers; |
| 19 import org.mockito.Mock; |
| 20 import org.mockito.MockitoAnnotations; |
| 21 import org.robolectric.Robolectric; |
| 22 import org.robolectric.annotation.Config; |
| 23 import org.robolectric.util.ServiceController; |
| 24 |
| 25 import org.chromium.android_webview.PlatformServiceBridge; |
| 26 import org.chromium.android_webview.command_line.CommandLineUtil; |
| 27 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 28 |
| 29 /** Unit tests for {@link CrashReceiverServiceUnit}. */ |
| 30 @RunWith(LocalRobolectricTestRunner.class) |
| 31 @Config(manifest = Config.NONE) |
| 32 public class CrashReceiverServiceUnitTest { |
| 33 @Mock(answer = Answers.RETURNS_DEEP_STUBS) |
| 34 private Context mContext; |
| 35 |
| 36 private CrashReceiverService mCrashReceiverService; |
| 37 |
| 38 @Mock |
| 39 SynchronizedWebViewCommandLine mWebViewCommandLine; |
| 40 |
| 41 @Mock |
| 42 PlatformServiceBridge mPlatformServiceBridge; |
| 43 |
| 44 @Before |
| 45 public void setUp() { |
| 46 MockitoAnnotations.initMocks(this); |
| 47 ServiceController<CrashReceiverService> controller = |
| 48 Robolectric.buildService(CrashReceiverService.class); |
| 49 mCrashReceiverService = spy(controller.get()); |
| 50 } |
| 51 |
| 52 private static enum UserConsentSetup { |
| 53 USER_CONSENT_INTERFACE_NOT_AVAILABLE, |
| 54 NO_USER_CONSENT, |
| 55 USER_CONSENT_OK |
| 56 } |
| 57 |
| 58 @Test |
| 59 public void testCrashCopyingEnabledForTesting() { |
| 60 ensureCrashCopyingEnabledCorrectly( |
| 61 true, UserConsentSetup.USER_CONSENT_INTERFACE_NOT_AVAILABLE, tru
e); |
| 62 } |
| 63 |
| 64 @Test |
| 65 public void testCrashCopyingEnabledIfUserConsent() { |
| 66 ensureCrashCopyingEnabledCorrectly(false, UserConsentSetup.USER_CONSENT_
OK, true); |
| 67 } |
| 68 |
| 69 @Test |
| 70 public void testCrashCopyingDisabledCorrectly() { |
| 71 ensureCrashCopyingEnabledCorrectly(false, UserConsentSetup.NO_USER_CONSE
NT, false); |
| 72 } |
| 73 |
| 74 private static class TestPlatformServiceBridge extends PlatformServiceBridge
{ |
| 75 private boolean mCanUseGms; |
| 76 private boolean mUserConsent; |
| 77 |
| 78 TestPlatformServiceBridge(boolean canUseGms, boolean userConsent) { |
| 79 mCanUseGms = canUseGms; |
| 80 mUserConsent = userConsent; |
| 81 } |
| 82 |
| 83 @Override |
| 84 public boolean canUseGms() { |
| 85 return mCanUseGms; |
| 86 } |
| 87 |
| 88 @Override |
| 89 public void queryMetricsSetting(ValueCallback<Boolean> callback) { |
| 90 callback.onReceiveValue(mUserConsent); |
| 91 } |
| 92 } |
| 93 |
| 94 private void ensureCrashCopyingEnabledCorrectly( |
| 95 boolean enabledForTesting, UserConsentSetup userConsentSetup, boolea
n expectEnabled) { |
| 96 when(mWebViewCommandLine.hasSwitch( |
| 97 eq(CommandLineUtil.CRASH_UPLOADS_ENABLED_FOR_TESTING_SWITCH
))) |
| 98 .thenReturn(enabledForTesting); |
| 99 when(mCrashReceiverService.getSynchronizedWebViewCommandLine()) |
| 100 .thenReturn(mWebViewCommandLine); |
| 101 when(mCrashReceiverService.getPlaformServiceBridge()) |
| 102 .thenReturn(new TestPlatformServiceBridge( |
| 103 userConsentSetup != UserConsentSetup.USER_CONSENT_INTERF
ACE_NOT_AVAILABLE, |
| 104 userConsentSetup == UserConsentSetup.USER_CONSENT_OK)); |
| 105 mCrashReceiverService.onCreate(); |
| 106 assertEquals(expectEnabled, mCrashReceiverService.isCrashCopyingEnabled(
)); |
| 107 } |
| 108 } |
OLD | NEW |