Chromium Code Reviews| Index: chrome/android/junit/src/org/chromium/chrome/browser/document/ChromeLauncherActivityTest.java |
| diff --git a/chrome/android/junit/src/org/chromium/chrome/browser/document/ChromeLauncherActivityTest.java b/chrome/android/junit/src/org/chromium/chrome/browser/document/ChromeLauncherActivityTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..93480dd92b368514fb4debbcaa114f079e9cb4f4 |
| --- /dev/null |
| +++ b/chrome/android/junit/src/org/chromium/chrome/browser/document/ChromeLauncherActivityTest.java |
| @@ -0,0 +1,91 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.document; |
| + |
| +import static org.junit.Assert.assertEquals; |
| +import static org.mockito.Mockito.doReturn; |
| +import static org.mockito.Mockito.mock; |
| + |
| +import android.content.Intent; |
| +import android.net.Uri; |
| +import android.os.BadParcelableException; |
| + |
| +import org.chromium.chrome.browser.TestableChromeApplication; |
| +import org.chromium.chrome.browser.instantapps.InstantAppsHandler; |
| +import org.chromium.chrome.browser.multiwindow.MultiWindowUtils; |
| +import org.chromium.testing.local.LocalRobolectricTestRunner; |
| +import org.junit.Before; |
| +import org.junit.Test; |
| +import org.junit.runner.RunWith; |
| +import org.mockito.MockitoAnnotations; |
| +import org.mockito.invocation.InvocationOnMock; |
| +import org.mockito.stubbing.Answer; |
| +import org.robolectric.annotation.Config; |
| +import org.robolectric.util.ReflectionHelpers; |
| + |
| +import java.util.concurrent.atomic.AtomicReference; |
| + |
| +/** |
| + * Unit tests for {@link org.chromium.chrome.browser.documents.ChromeLauncherActivity}. |
| + */ |
| +@RunWith(LocalRobolectricTestRunner.class) |
| +@Config(manifest = Config.NONE, application = TestableChromeApplication.class) |
| +public class ChromeLauncherActivityTest { |
| + |
| + private static final Uri INTENT_URI = Uri.parse("http://www.google.com"); |
| + private static final String INTENT_ACTION = Intent.ACTION_VIEW; |
| + |
| + @Before |
| + public void setUp() { |
| + MockitoAnnotations.initMocks(this); |
| + } |
| + |
| + /** |
| + * Test method for to make sure ChromeLauncherActivity does not crash when handed an intent |
| + * with an unparcable extra. |
| + * We instead expect it to sanitize the intent and continue execution. |
| + */ |
| + @Test |
| + public void testChromeLauncherActivitySanitizesBadIntents() { |
| + // Prepare |
| + final Intent badIntent = getBadIntent(); |
| + final ChromeLauncherActivity activity = createActivityForOnCreate(); |
| + activity.setIntent(badIntent); |
| + |
| + // Execute |
| + activity.doOnCreate(null); |
| + |
| + // Verify assumptions |
| + final Intent sanitizedIntent = activity.getIntent(); |
| + assertEquals("Intent type was not sanitized to a regular intent", Intent.class, |
| + sanitizedIntent.getClass()); |
| + assertEquals(badIntent.getDataString(), sanitizedIntent.getDataString()); |
| + assertEquals(badIntent.getAction(), sanitizedIntent.getAction()); |
| + } |
| + |
| + private ChromeLauncherActivity createActivityForOnCreate() { |
| + final AtomicReference<MultiWindowUtils> multiWindowUtils = |
|
Ted C
2016/10/04 20:19:23
out of curiosity, why do we need to mock these?
I
kraush
2016/10/04 21:36:36
Without this change, this line will fail:
https://
|
| + ReflectionHelpers.getStaticField(MultiWindowUtils.class, "sInstance"); |
| + multiWindowUtils.set(new MultiWindowUtils()); |
| + ReflectionHelpers.setStaticField(InstantAppsHandler.class, "sInstance", |
| + mock(InstantAppsHandler.class)); |
| + |
| + return new ChromeLauncherActivity(); |
| + } |
| + |
| + private Intent getBadIntent() { |
| + final Intent badIntent = mock(Intent.class, new BadParcelableExceptionAnswer()); |
| + doReturn(INTENT_ACTION).when(badIntent).getAction(); |
| + doReturn(INTENT_URI).when(badIntent).getData(); |
| + doReturn(INTENT_URI.toString()).when(badIntent).getDataString(); |
| + return badIntent; |
| + } |
| + |
| + private static class BadParcelableExceptionAnswer implements Answer<Object> { |
| + public Object answer(InvocationOnMock invocation) throws Throwable { |
| + throw new BadParcelableException("Interaction with extras - crashing."); |
| + } |
| + } |
| +} |