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.content_shell_apk; | |
6 | |
7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; | |
8 | |
9 import android.annotation.TargetApi; | |
10 import android.app.Instrumentation; | |
11 import android.content.ComponentName; | |
12 import android.content.Context; | |
13 import android.content.Intent; | |
14 import android.net.Uri; | |
15 import android.os.Build; | |
16 import android.os.PowerManager; | |
17 import android.text.TextUtils; | |
18 import android.view.ViewGroup; | |
19 | |
20 import org.junit.Assert; | |
21 | |
22 import org.chromium.base.ThreadUtils; | |
23 import org.chromium.base.test.util.CallbackHelper; | |
24 import org.chromium.content.browser.ContentView; | |
25 import org.chromium.content.browser.ContentViewCore; | |
26 import org.chromium.content.browser.test.util.Criteria; | |
27 import org.chromium.content.browser.test.util.CriteriaHelper; | |
28 import org.chromium.content_public.browser.WebContents; | |
29 import org.chromium.content_shell.Shell; | |
30 | |
31 import java.util.concurrent.Callable; | |
32 import java.util.concurrent.ExecutionException; | |
33 import java.util.concurrent.TimeUnit; | |
34 | |
35 /** | |
36 * Base test delegate class created for Instrumentation JUnit3 to JUnit4 migrati on | |
37 */ | |
38 public final class ContentShellTestBaseDelegate { | |
39 /** The maximum time the waitForActiveShellToBeDoneLoading method will wait. */ | |
40 private static final long WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = scaleTimeo ut(10000); | |
41 static final long WAIT_PAGE_LOADING_TIMEOUT_SECONDS = scaleTimeout(15); | |
42 | |
43 @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) | |
44 @SuppressWarnings("deprecation") | |
45 void assertScreenIsOn(Instrumentation instrumentation) { | |
46 PowerManager pm = | |
47 (PowerManager) instrumentation.getContext().getSystemService(Con text.POWER_SERVICE); | |
48 | |
49 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { | |
50 Assert.assertTrue("Many tests will fail if the screen is not on.", p m.isInteractive()); | |
51 } else { | |
52 Assert.assertTrue("Many tests will fail if the screen is not on.", p m.isScreenOn()); | |
53 } | |
54 } | |
55 | |
56 Intent getActivityLaunchIntent(Context targetContext, String url) { | |
57 Intent intent = new Intent(Intent.ACTION_MAIN); | |
58 intent.addCategory(Intent.CATEGORY_LAUNCHER); | |
59 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
60 if (url != null) intent.setData(Uri.parse(url)); | |
61 intent.setComponent(new ComponentName(targetContext, ContentShellActivit y.class)); | |
62 return intent; | |
63 } | |
64 | |
65 void waitForActiveShellToBeDoneLoading(final ContentShellActivity activity) { | |
66 // Wait for the Content Shell to be initialized. | |
67 CriteriaHelper.pollUiThread(new Criteria() { | |
68 @Override | |
69 public boolean isSatisfied() { | |
70 Shell shell = activity.getActiveShell(); | |
71 // There are two cases here that need to be accounted for. | |
72 // The first is that we've just created a Shell and it isn't | |
73 // loading because it has no URL set yet. The second is that | |
74 // we've set a URL and it actually is loading. | |
75 if (shell == null) { | |
76 updateFailureReason("Shell is null."); | |
77 return false; | |
78 } | |
79 if (shell.isLoading()) { | |
80 updateFailureReason("Shell is still loading."); | |
81 return false; | |
82 } | |
83 if (TextUtils.isEmpty(shell.getContentViewCore().getWebContents( ).getUrl())) { | |
84 updateFailureReason("Shell's URL is empty or null."); | |
85 return false; | |
86 } | |
87 return true; | |
88 } | |
89 }, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT, CriteriaHelper.DEFAULT_POLLING _INTERVAL); | |
90 } | |
91 | |
92 ContentViewCore getContentViewCore(ContentShellActivity activity) { | |
93 return activity.getActiveShell().getContentViewCore(); | |
94 } | |
95 | |
96 WebContents getWebContents(ContentShellActivity activity) { | |
97 return activity.getActiveShell().getWebContents(); | |
98 } | |
99 | |
100 Shell loadNewShell(final ContentShellActivity activity, final String url) | |
101 throws ExecutionException { | |
102 Shell shell = ThreadUtils.runOnUiThreadBlocking(new Callable<Shell>() { | |
103 @Override | |
104 public Shell call() { | |
105 activity.getShellManager().launchShell(url); | |
106 return activity.getActiveShell(); | |
107 } | |
108 }); | |
109 Assert.assertNotNull("Unable to create shell.", shell); | |
110 Assert.assertEquals("Active shell unexpected.", shell, activity.getActiv eShell()); | |
111 waitForActiveShellToBeDoneLoading(activity); | |
112 | |
113 return shell; | |
114 } | |
115 | |
116 void handleBlockingCallbackAction(CallbackHelper callbackHelper, Runnable ui ThreadAction) | |
117 throws Throwable { | |
118 int currentCallCount = callbackHelper.getCallCount(); | |
119 uiThreadAction.run(); | |
boliu
2017/03/01 23:01:23
something is inconsistent here, the contract here
the real yoland
2017/03/02 20:16:48
Done
| |
120 callbackHelper.waitForCallback( | |
121 currentCallCount, 1, WAIT_PAGE_LOADING_TIMEOUT_SECONDS, TimeUnit .SECONDS); | |
122 } | |
123 | |
124 void assertWaitForPageScaleFactorMatch(final ContentViewCore viewCore, float expectedScale) { | |
125 CriteriaHelper.pollInstrumentationThread( | |
126 Criteria.equals(expectedScale, new Callable<Float>() { | |
127 @Override | |
128 public Float call() { | |
129 return viewCore.getScale(); | |
130 } | |
131 })); | |
132 } | |
133 | |
134 void replaceContainerView(final ContentShellActivity activity) throws Throwa ble { | |
135 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
136 @Override | |
137 public void run() { | |
138 ContentView cv = | |
139 ContentView.createContentView(activity, getContentViewCo re(activity)); | |
140 ((ViewGroup) getContentViewCore(activity).getContainerView().get Parent()) | |
141 .addView(cv); | |
142 getContentViewCore(activity).setContainerView(cv); | |
143 getContentViewCore(activity).setContainerViewInternals(cv); | |
144 cv.requestFocus(); | |
145 } | |
146 }); | |
147 } | |
148 } | |
OLD | NEW |