OLD | NEW |
| (Empty) |
1 // Copyright 2016 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.webapk.shell_apk; | |
6 | |
7 import android.content.Intent; | |
8 import android.content.pm.ActivityInfo; | |
9 import android.content.pm.ResolveInfo; | |
10 import android.os.Bundle; | |
11 | |
12 import org.junit.Assert; | |
13 import org.junit.Before; | |
14 import org.junit.Test; | |
15 import org.junit.runner.RunWith; | |
16 import org.robolectric.Robolectric; | |
17 import org.robolectric.RuntimeEnvironment; | |
18 import org.robolectric.Shadows; | |
19 import org.robolectric.annotation.Config; | |
20 import org.robolectric.res.builder.RobolectricPackageManager; | |
21 import org.robolectric.shadows.ShadowApplication; | |
22 | |
23 import org.chromium.testing.local.LocalRobolectricTestRunner; | |
24 import org.chromium.webapk.lib.common.WebApkMetaDataKeys; | |
25 import org.chromium.webapk.test.WebApkTestHelper; | |
26 | |
27 /** | |
28 * Tests MainActivity. | |
29 */ | |
30 @RunWith(LocalRobolectricTestRunner.class) | |
31 @Config(manifest = Config.NONE, packageName = MainActivityTest.WEBAPK_PACKAGE_NA
ME) | |
32 public class MainActivityTest { | |
33 private static final String HOST_BROWSER_PACKAGE_NAME = "truly.random"; | |
34 | |
35 protected static final String WEBAPK_PACKAGE_NAME = "org.chromium.webapk.tes
t_package"; | |
36 private ShadowApplication mShadowApplication; | |
37 private RobolectricPackageManager mPackageManager; | |
38 | |
39 @Before | |
40 public void setUp() { | |
41 mShadowApplication = Shadows.shadowOf(RuntimeEnvironment.application); | |
42 mPackageManager = | |
43 (RobolectricPackageManager) RuntimeEnvironment.application.getPa
ckageManager(); | |
44 } | |
45 | |
46 /** | |
47 * Tests that when the user launches the WebAPK and the user does not have a
ny browser installed | |
48 * that the WebAPK launches Google Play to install the host browser. | |
49 */ | |
50 @Test | |
51 public void testBrowserNotInstalled() { | |
52 // Throw ActivityNotFoundException if Intent cannot be resolved. | |
53 mShadowApplication.checkActivities(true); | |
54 | |
55 // Set WebAPK's meta-data. | |
56 Bundle metaData = new Bundle(); | |
57 metaData.putString(WebApkMetaDataKeys.RUNTIME_HOST, HOST_BROWSER_PACKAGE
_NAME); | |
58 metaData.putString(WebApkMetaDataKeys.START_URL, "http://random.org"); | |
59 WebApkTestHelper.registerWebApkWithMetaData(WEBAPK_PACKAGE_NAME, metaDat
a); | |
60 | |
61 // Make intents to Google Play not throw ActivityNotFoundException. | |
62 mPackageManager.addResolveInfoForIntent( | |
63 MainActivity.createInstallIntent(HOST_BROWSER_PACKAGE_NAME), | |
64 newResolveInfo("google.play")); | |
65 | |
66 Robolectric.buildActivity(MainActivity.class).create(); | |
67 | |
68 Intent startActivityIntent = mShadowApplication.getNextStartedActivity()
; | |
69 Assert.assertNotNull(startActivityIntent); | |
70 Assert.assertTrue(startActivityIntent.getDataString().startsWith("market
://")); | |
71 } | |
72 | |
73 /** | |
74 * Test that when the user launches the WebAPK and the user has neither a br
owser nor Google | |
75 * Play installed that launching the WebAPK silently fails and does not cras
h. | |
76 */ | |
77 @Test | |
78 public void testBrowserNotInstalledAndGooglePlayNotInstalled() { | |
79 // Throw ActivityNotFoundException if Intent cannot be resolved. | |
80 mShadowApplication.checkActivities(true); | |
81 | |
82 // Set WebAPK's meta-data. | |
83 Bundle metaData = new Bundle(); | |
84 metaData.putString(WebApkMetaDataKeys.RUNTIME_HOST, HOST_BROWSER_PACKAGE
_NAME); | |
85 metaData.putString(WebApkMetaDataKeys.START_URL, "http://random.org"); | |
86 WebApkTestHelper.registerWebApkWithMetaData(WEBAPK_PACKAGE_NAME, metaDat
a); | |
87 | |
88 Robolectric.buildActivity(MainActivity.class).create(); | |
89 | |
90 Intent startActivityIntent = mShadowApplication.getNextStartedActivity()
; | |
91 Assert.assertNull(startActivityIntent); | |
92 } | |
93 | |
94 private static ResolveInfo newResolveInfo(String packageName) { | |
95 ActivityInfo activityInfo = new ActivityInfo(); | |
96 activityInfo.packageName = packageName; | |
97 ResolveInfo resolveInfo = new ResolveInfo(); | |
98 resolveInfo.activityInfo = activityInfo; | |
99 return resolveInfo; | |
100 } | |
101 } | |
OLD | NEW |