OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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.chrome.browser.webapps; | 5 package org.chromium.chrome.browser.webapps; |
6 | 6 |
7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; | 7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; |
8 | 8 |
9 import android.content.Intent; | 9 import android.content.Intent; |
10 import android.net.Uri; | 10 import android.net.Uri; |
11 import android.view.ViewGroup; | 11 import android.view.ViewGroup; |
12 | 12 |
| 13 import org.junit.Assert; |
| 14 import org.junit.runner.Description; |
| 15 import org.junit.runners.model.Statement; |
| 16 |
| 17 import org.chromium.base.test.util.UrlUtils; |
13 import org.chromium.chrome.browser.ShortcutHelper; | 18 import org.chromium.chrome.browser.ShortcutHelper; |
14 import org.chromium.chrome.test.ChromeActivityTestCaseBase; | 19 import org.chromium.chrome.test.ChromeActivityTestRule; |
15 import org.chromium.content.browser.test.util.Criteria; | 20 import org.chromium.content.browser.test.util.Criteria; |
16 import org.chromium.content.browser.test.util.CriteriaHelper; | 21 import org.chromium.content.browser.test.util.CriteriaHelper; |
17 | 22 |
18 /** | 23 /** |
19 * The base class of the WebappActivity tests. It provides the common methods to
access the activity | 24 * Custom {@link ChromeActivityTestRule} for tests using {@link WebappActivity}. |
20 * UI. This particular test base only instantiates WebappActivity0. | |
21 */ | 25 */ |
22 public abstract class WebappActivityTestBase extends ChromeActivityTestCaseBase<
WebappActivity0> { | 26 public class WebappActivityTestRule extends ChromeActivityTestRule<WebappActivit
y0> { |
23 static final String WEBAPP_ID = "webapp_id"; | 27 public static final String WEBAPP_ID = "webapp_id"; |
24 static final String WEBAPP_NAME = "webapp name"; | 28 public static final String WEBAPP_NAME = "webapp name"; |
25 static final String WEBAPP_SHORT_NAME = "webapp short name"; | 29 public static final String WEBAPP_SHORT_NAME = "webapp short name"; |
26 | 30 |
27 private static final long STARTUP_TIMEOUT = scaleTimeout(10000); | 31 private static final long STARTUP_TIMEOUT = scaleTimeout(10000); |
28 | 32 |
29 // Empty 192x192 image generated with: | 33 // Empty 192x192 image generated with: |
30 // ShortcutHelper.encodeBitmapAsString(Bitmap.createBitmap(192, 192, Bitmap.
Config.ARGB_4444)); | 34 // ShortcutHelper.encodeBitmapAsString(Bitmap.createBitmap(192, 192, Bitmap.
Config.ARGB_4444)); |
31 protected static final String TEST_ICON = | 35 public static final String TEST_ICON = |
32 "iVBORw0KGgoAAAANSUhEUgAAAMAAAADACAYAAABS3GwHAAAABHNCSVQICAgIfAhkiAA
AAKZJREFU" | 36 "iVBORw0KGgoAAAANSUhEUgAAAMAAAADACAYAAABS3GwHAAAABHNCSVQICAgIfAhkiAA
AAKZJREFU" |
33 + "eJztwTEBAAAAwqD1T20JT6AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA" | 37 + "eJztwTEBAAAAwqD1T20JT6AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA" |
34 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA" | 38 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA" |
35 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD4GQ
N4AAe3mX6IA" | 39 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD4GQ
N4AAe3mX6IA" |
36 + "AAAASUVORK5CYII="; | 40 + "AAAASUVORK5CYII="; |
37 | 41 |
38 // Empty 512x512 image generated with: | 42 // Empty 512x512 image generated with: |
39 // ShortcutHelper.encodeBitmapAsString(Bitmap.createBitmap(512, 512, Bitmap.
Config.ARGB_4444)); | 43 // ShortcutHelper.encodeBitmapAsString(Bitmap.createBitmap(512, 512, Bitmap.
Config.ARGB_4444)); |
40 protected static final String TEST_SPLASH_ICON = | 44 public static final String TEST_SPLASH_ICON = |
41 "iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABHNCSVQICAgIfAhkiAA
ABA9JREFU" | 45 "iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABHNCSVQICAgIfAhkiAA
ABA9JREFU" |
42 + "eJztwTEBAAAAwqD1T20Hb6AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA" | 46 + "eJztwTEBAAAAwqD1T20Hb6AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA" |
43 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA" | 47 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA" |
44 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA" | 48 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA" |
45 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA" | 49 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA" |
46 + "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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA" | 62 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA" |
59 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA" | 63 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAA" |
60 + "AAAAAAAAAOA3AvAAAdln8YgAAAAASUVORK5CYII="; | 64 + "AAAAAAAAAOA3AvAAAdln8YgAAAAASUVORK5CYII="; |
61 | 65 |
62 public WebappActivityTestBase() { | 66 public WebappActivityTestRule() { |
63 super(WebappActivity0.class); | 67 super(WebappActivity0.class); |
64 } | 68 } |
65 | 69 |
66 /** | 70 /** |
67 * Creates the Intent that starts the WebAppActivity. This is meant to be ov
erriden by other | 71 * Creates the Intent that starts the WebAppActivity. This is meant to be ov
erriden by other |
68 * tests in order for them to pass some specific values, but it defaults to
a web app that just | 72 * tests in order for them to pass some specific values, but it defaults to
a web app that just |
69 * loads about:blank to avoid a network load. This results in the URL bar s
howing because | 73 * loads about:blank to avoid a network load. This results in the URL bar s
howing because |
70 * {@link UrlUtils} cannot parse this type of URL. | 74 * {@link UrlUtils} cannot parse this type of URL. |
71 */ | 75 */ |
72 protected Intent createIntent() { | 76 public Intent createIntent() { |
73 Intent intent = new Intent(getInstrumentation().getTargetContext(), Weba
ppActivity0.class); | 77 Intent intent = new Intent(getInstrumentation().getTargetContext(), Weba
ppActivity0.class); |
74 intent.setData(Uri.parse(WebappActivity.WEBAPP_SCHEME + "://" + WEBAPP_I
D)); | 78 intent.setData(Uri.parse(WebappActivity.WEBAPP_SCHEME + "://" + WEBAPP_I
D)); |
75 intent.putExtra(ShortcutHelper.EXTRA_ID, WEBAPP_ID); | 79 intent.putExtra(ShortcutHelper.EXTRA_ID, WEBAPP_ID); |
76 intent.putExtra(ShortcutHelper.EXTRA_URL, "about:blank"); | 80 intent.putExtra(ShortcutHelper.EXTRA_URL, "about:blank"); |
77 intent.putExtra(ShortcutHelper.EXTRA_NAME, WEBAPP_NAME); | 81 intent.putExtra(ShortcutHelper.EXTRA_NAME, WEBAPP_NAME); |
78 intent.putExtra(ShortcutHelper.EXTRA_SHORT_NAME, WEBAPP_SHORT_NAME); | 82 intent.putExtra(ShortcutHelper.EXTRA_SHORT_NAME, WEBAPP_SHORT_NAME); |
79 return intent; | 83 return intent; |
80 } | 84 } |
81 | 85 |
82 @Override | 86 @Override |
83 protected void setUp() throws Exception { | 87 public Statement apply(final Statement base, Description description) { |
84 super.setUp(); | 88 return new Statement() { |
| 89 @Override |
| 90 public void evaluate() throws Throwable { |
| 91 // Register the webapp so when the data storage is opened, the t
est doesn't crash. |
| 92 WebappRegistry.refreshSharedPrefsForTesting(); |
| 93 TestFetchStorageCallback callback = new TestFetchStorageCallback
(); |
| 94 WebappRegistry.getInstance().register(WEBAPP_ID, callback); |
| 95 callback.waitForCallback(0); |
| 96 callback.getStorage().updateFromShortcutIntent(createIntent()); |
85 | 97 |
86 // Register the webapp so when the data storage is opened, the test does
n't crash. | 98 base.evaluate(); |
87 WebappRegistry.refreshSharedPrefsForTesting(); | 99 } |
88 TestFetchStorageCallback callback = new TestFetchStorageCallback(); | 100 }; |
89 WebappRegistry.getInstance().register(WEBAPP_ID, callback); | |
90 callback.waitForCallback(0); | |
91 callback.getStorage().updateFromShortcutIntent(createIntent()); | |
92 } | 101 } |
93 | 102 |
94 /** | 103 /** |
95 * Starts up the WebappActivity and sets up the test observer. | 104 * Starts up the WebappActivity and sets up the test observer. |
96 */ | 105 */ |
97 protected final void startWebappActivity() throws Exception { | 106 public final void startWebappActivity() throws Exception { |
98 startWebappActivity(createIntent()); | 107 startWebappActivity(createIntent()); |
99 } | 108 } |
100 | 109 |
101 /** | 110 /** |
102 * Starts up the WebappActivity with a specific Intent and sets up the test
observer. | 111 * Starts up the WebappActivity with a specific Intent and sets up the test
observer. |
103 */ | 112 */ |
104 protected final void startWebappActivity(Intent intent) throws Exception { | 113 public final void startWebappActivity(Intent intent) throws Exception { |
105 setActivityIntent(intent); | 114 launchActivity(intent); |
106 waitUntilIdle(); | 115 waitUntilIdle(); |
107 } | 116 } |
108 | 117 |
109 /** | 118 /** |
110 * Waits until any loads in progress have completed. | 119 * Waits until any loads in progress have completed. |
111 */ | 120 */ |
112 protected void waitUntilIdle() { | 121 public void waitUntilIdle() { |
113 getInstrumentation().waitForIdleSync(); | 122 getInstrumentation().waitForIdleSync(); |
114 CriteriaHelper.pollInstrumentationThread(new Criteria() { | 123 CriteriaHelper.pollInstrumentationThread(new Criteria() { |
115 @Override | 124 @Override |
116 public boolean isSatisfied() { | 125 public boolean isSatisfied() { |
117 return getActivity().getActivityTab() != null | 126 return getActivity().getActivityTab() != null |
118 && !getActivity().getActivityTab().isLoading(); | 127 && !getActivity().getActivityTab().isLoading(); |
119 } | 128 } |
120 }, STARTUP_TIMEOUT, CriteriaHelper.DEFAULT_POLLING_INTERVAL); | 129 }, STARTUP_TIMEOUT, CriteriaHelper.DEFAULT_POLLING_INTERVAL); |
121 | 130 |
122 getInstrumentation().waitForIdleSync(); | 131 getInstrumentation().waitForIdleSync(); |
123 } | 132 } |
124 | 133 |
125 @Override | |
126 public final void startMainActivity() throws InterruptedException { | |
127 // Do nothing; the WebappActivity may not have been completely set up, y
et. | |
128 } | |
129 | |
130 /** | 134 /** |
131 * Waits for the splash screen to be hidden. | 135 * Waits for the splash screen to be hidden. |
132 */ | 136 */ |
133 protected void waitUntilSplashscreenHides() { | 137 public void waitUntilSplashscreenHides() { |
134 CriteriaHelper.pollInstrumentationThread(new Criteria() { | 138 CriteriaHelper.pollInstrumentationThread(new Criteria() { |
135 @Override | 139 @Override |
136 public boolean isSatisfied() { | 140 public boolean isSatisfied() { |
137 return !getActivity().isSplashScreenVisibleForTests(); | 141 return !getActivity().isSplashScreenVisibleForTests(); |
138 } | 142 } |
139 }); | 143 }); |
140 } | 144 } |
141 | 145 |
142 protected ViewGroup waitUntilSplashScreenAppears() { | 146 public ViewGroup waitUntilSplashScreenAppears() { |
143 CriteriaHelper.pollInstrumentationThread(new Criteria() { | 147 CriteriaHelper.pollInstrumentationThread(new Criteria() { |
144 @Override | 148 @Override |
145 public boolean isSatisfied() { | 149 public boolean isSatisfied() { |
146 return getActivity().getSplashScreenForTests() != null; | 150 return getActivity().getSplashScreenForTests() != null; |
147 } | 151 } |
148 }); | 152 }); |
149 | 153 |
150 ViewGroup splashScreen = getActivity().getSplashScreenForTests(); | 154 ViewGroup splashScreen = getActivity().getSplashScreenForTests(); |
151 if (splashScreen == null) { | 155 if (splashScreen == null) { |
152 fail("No splash screen available."); | 156 Assert.fail("No splash screen available."); |
153 } | 157 } |
154 return splashScreen; | 158 return splashScreen; |
155 } | 159 } |
156 } | 160 } |
OLD | NEW |