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.browser; | |
6 | |
7 import android.util.Log; | |
8 | |
9 import org.junit.Assert; | |
10 | |
11 import org.chromium.base.annotations.SuppressFBWarnings; | |
12 import org.chromium.base.test.util.UrlUtils; | |
13 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; | |
14 import org.chromium.content_public.browser.LoadUrlParams; | |
15 import org.chromium.content_shell_apk.ContentShellActivity; | |
16 import org.chromium.content_shell_apk.ContentShellActivityTestRule; | |
17 | |
18 import java.lang.annotation.Annotation; | |
19 | |
20 /** | |
21 * ActivityTestRule with common functionality for testing the Java Bridge. | |
22 */ | |
23 public class JavaBridgeActivityTestRule extends ContentShellActivityTestRule { | |
24 protected TestCallbackHelperContainer mTestCallbackHelperContainer; | |
25 | |
26 /** | |
27 * Sets up the ContentView. Intended to be called from setUp(). | |
28 */ | |
29 private void setUpContentView() { | |
30 // This starts the activity, so must be called on the test thread. | |
31 final ContentShellActivity activity = launchContentShellWithUrl( | |
32 UrlUtils.encodeHtmlDataUri("<html><head></head><body>test</body> </html>")); | |
33 | |
34 waitForActiveShellToBeDoneLoading(); | |
35 | |
36 try { | |
37 runOnUiThread(new Runnable() { | |
38 @Override | |
39 public void run() { | |
40 mTestCallbackHelperContainer = | |
41 new TestCallbackHelperContainer(activity.getActiveCo ntentViewCore()); | |
42 } | |
43 }); | |
44 } catch (Throwable e) { | |
45 throw new RuntimeException( | |
46 "Failed to set up ContentView: " + Log.getStackTraceString(e )); | |
47 } | |
48 } | |
49 | |
50 @Override | |
51 protected void beforeActivityLaunched() { | |
52 super.beforeActivityLaunched(); | |
53 setUpContentView(); | |
54 } | |
55 | |
56 @SuppressFBWarnings("CHROMIUM_SYNCHRONIZED_METHOD") | |
57 protected class Controller { | |
58 private boolean mIsResultReady; | |
59 | |
60 protected synchronized void notifyResultIsReady() { | |
61 mIsResultReady = true; | |
62 notify(); | |
63 } | |
64 | |
65 protected synchronized void waitForResult() { | |
66 while (!mIsResultReady) { | |
67 try { | |
68 wait(5000); | |
mikecase (-- gone --)
2017/02/23 19:21:35
Consider replacing this value with CONSTANT. Maybe
the real yoland
2017/02/27 18:32:49
Done
| |
69 } catch (Exception e) { | |
70 continue; | |
71 } | |
72 if (!mIsResultReady) { | |
73 Assert.fail("Wait timed out"); | |
74 } | |
75 } | |
76 mIsResultReady = false; | |
77 } | |
78 } | |
79 | |
80 public void executeJavaScript(final String script) throws Throwable { | |
81 runOnUiThread(new Runnable() { | |
82 @Override | |
83 public void run() { | |
84 // When a JavaScript URL is executed, if the value of the last | |
85 // expression evaluated is not 'undefined', this value is | |
86 // converted to a string and used as the new document for the | |
87 // frame. We don't want this behaviour, so wrap the script in | |
88 // an anonymous function. | |
89 getWebContents().getNavigationController().loadUrl( | |
90 new LoadUrlParams("javascript:(function() { " + script + " })()")); | |
91 } | |
92 }); | |
93 } | |
94 | |
95 public void injectObjectAndReload(final Object object, final String name) th rows Exception { | |
96 injectObjectAndReload(object, name, null); | |
97 } | |
98 | |
99 public void injectObjectAndReload(final Object object, final String name, | |
100 final Class<? extends Annotation> requiredAnnotation) throws Excepti on { | |
101 injectObjectsAndReload(object, name, null, null, requiredAnnotation); | |
102 } | |
103 | |
104 public void injectObjectsAndReload(final Object object1, final String name1, | |
105 final Object object2, final String name2, | |
106 final Class<? extends Annotation> requiredAnnotation) throws Excepti on { | |
107 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = | |
108 mTestCallbackHelperContainer.getOnPageFinishedHelper(); | |
109 int currentCallCount = onPageFinishedHelper.getCallCount(); | |
110 try { | |
111 runOnUiThread(new Runnable() { | |
112 @Override | |
113 public void run() { | |
114 getContentViewCore().addPossiblyUnsafeJavascriptInterface( | |
115 object1, name1, requiredAnnotation); | |
116 if (object2 != null && name2 != null) { | |
117 getContentViewCore().addPossiblyUnsafeJavascriptInterfac e( | |
118 object2, name2, requiredAnnotation); | |
119 } | |
120 getContentViewCore().getWebContents().getNavigationControlle r().reload(true); | |
121 } | |
122 }); | |
123 onPageFinishedHelper.waitForCallback(currentCallCount); | |
124 } catch (Throwable e) { | |
125 throw new RuntimeException( | |
126 "Failed to injectObjectsAndReload: " + Log.getStackTraceStri ng(e)); | |
127 } | |
128 } | |
129 | |
130 public void synchronousPageReload() throws Throwable { | |
131 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = | |
132 mTestCallbackHelperContainer.getOnPageFinishedHelper(); | |
133 int currentCallCount = onPageFinishedHelper.getCallCount(); | |
134 runOnUiThread(new Runnable() { | |
135 @Override | |
136 public void run() { | |
137 getContentViewCore().getWebContents().getNavigationController(). reload(true); | |
138 } | |
139 }); | |
140 onPageFinishedHelper.waitForCallback(currentCallCount); | |
141 } | |
142 } | |
OLD | NEW |