OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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.android_webview.test; | 5 package org.chromium.android_webview.test; |
6 | 6 |
7 import android.test.suitebuilder.annotation.SmallTest; | 7 import android.test.suitebuilder.annotation.SmallTest; |
8 | 8 |
9 import org.chromium.android_webview.AwContents; | 9 import org.chromium.android_webview.AwContents; |
10 import org.chromium.base.test.util.Feature; | 10 import org.chromium.base.test.util.Feature; |
(...skipping 25 matching lines...) Expand all Loading... |
36 @JavascriptInterface | 36 @JavascriptInterface |
37 public void destroy() { | 37 public void destroy() { |
38 try { | 38 try { |
39 runTestOnUiThread(new Runnable() { | 39 runTestOnUiThread(new Runnable() { |
40 @Override | 40 @Override |
41 public void run() { | 41 public void run() { |
42 awContents.destroy(); | 42 awContents.destroy(); |
43 } | 43 } |
44 }); | 44 }); |
45 // Destroying one AwContents from within the JS callback sho
uld still | 45 // Destroying one AwContents from within the JS callback sho
uld still |
46 // leave others functioning. | 46 // leave others functioning. Note that we must do this async
hronously, |
47 loadDataSync(view2.getAwContents(), client2.getOnPageFinishe
dHelper(), | 47 // as Blink thread is currently blocked waiting for this met
hod to finish. |
48 html, "text/html", false); | 48 loadDataAsync(view2.getAwContents(), html, "text/html", fals
e); |
49 } catch (Throwable t) { | 49 } catch (Throwable t) { |
50 throw new RuntimeException(t); | 50 throw new RuntimeException(t); |
51 } | 51 } |
52 } | 52 } |
53 } | 53 } |
54 | 54 |
55 enableJavaScriptOnUiThread(awContents); | 55 enableJavaScriptOnUiThread(awContents); |
56 runTestOnUiThread(new Runnable() { | 56 runTestOnUiThread(new Runnable() { |
57 @Override | 57 @Override |
58 public void run() { | 58 public void run() { |
59 awContents.addPossiblyUnsafeJavascriptInterface(new Test(),
"test", null); | 59 awContents.addPossiblyUnsafeJavascriptInterface(new Test(),
"test", null); |
60 } | 60 } |
61 }); | 61 }); |
62 | 62 |
63 loadDataSync(awContents, mContentsClient.getOnPageFinishedHelper(), html
, | 63 loadDataSync(awContents, mContentsClient.getOnPageFinishedHelper(), html
, |
64 "text/html", false); | 64 "text/html", false); |
65 | 65 |
66 // Ensure the JS interface object is there, and invoke the test method. | 66 // Ensure the JS interface object is there, and invoke the test method. |
67 assertEquals("\"function\"", executeJavaScriptAndWaitForResult( | 67 assertEquals("\"function\"", executeJavaScriptAndWaitForResult( |
68 awContents, mContentsClient, "typeof test.destroy")); | 68 awContents, mContentsClient, "typeof test.destroy")); |
| 69 int currentCallCount = client2.getOnPageFinishedHelper().getCallCount(); |
69 awContents.evaluateJavaScript("test.destroy()", null); | 70 awContents.evaluateJavaScript("test.destroy()", null); |
70 | 71 |
71 client2.getOnPageFinishedHelper().waitForCallback( | 72 client2.getOnPageFinishedHelper().waitForCallback(currentCallCount); |
72 client2.getOnPageFinishedHelper().getCallCount()); | 73 } |
| 74 |
| 75 @SmallTest |
| 76 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 77 public void testTwoWebViewsCreatedSimultaneously() throws Throwable { |
| 78 final AwContents awContents1 = mTestContainerView.getAwContents(); |
| 79 final TestAwContentsClient client2 = new TestAwContentsClient(); |
| 80 final AwTestContainerView view2 = createAwTestContainerViewOnMainSync(cl
ient2); |
| 81 final AwContents awContents2 = view2.getAwContents(); |
| 82 |
| 83 enableJavaScriptOnUiThread(awContents1); |
| 84 enableJavaScriptOnUiThread(awContents2); |
| 85 |
| 86 class Test { |
| 87 Test(int value) { |
| 88 mValue = value; |
| 89 } |
| 90 @JavascriptInterface |
| 91 public int getValue() { |
| 92 return mValue; |
| 93 } |
| 94 private int mValue; |
| 95 } |
| 96 |
| 97 runTestOnUiThread(new Runnable() { |
| 98 @Override |
| 99 public void run() { |
| 100 awContents1.addPossiblyUnsafeJavascriptInterface(new Test(1)
, "test", null); |
| 101 awContents2.addPossiblyUnsafeJavascriptInterface(new Test(2)
, "test", null); |
| 102 } |
| 103 }); |
| 104 final String html = "<html>Hello World</html>"; |
| 105 loadDataSync(awContents1, mContentsClient.getOnPageFinishedHelper(), htm
l, |
| 106 "text/html", false); |
| 107 loadDataSync(awContents2, client2.getOnPageFinishedHelper(), html, |
| 108 "text/html", false); |
| 109 |
| 110 assertEquals("1", |
| 111 executeJavaScriptAndWaitForResult(awContents1, mContentsClient,
"test.getValue()")); |
| 112 assertEquals("2", |
| 113 executeJavaScriptAndWaitForResult(awContents2, client2, "test.ge
tValue()")); |
| 114 } |
| 115 |
| 116 @SmallTest |
| 117 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 118 public void testTwoWebViewsSecondCreatedAfterLoadingInFirst() throws Throwab
le { |
| 119 final AwContents awContents1 = mTestContainerView.getAwContents(); |
| 120 enableJavaScriptOnUiThread(awContents1); |
| 121 |
| 122 class Test { |
| 123 Test(int value) { |
| 124 mValue = value; |
| 125 } |
| 126 @JavascriptInterface |
| 127 public int getValue() { |
| 128 return mValue; |
| 129 } |
| 130 private int mValue; |
| 131 } |
| 132 |
| 133 runTestOnUiThread(new Runnable() { |
| 134 @Override |
| 135 public void run() { |
| 136 awContents1.addPossiblyUnsafeJavascriptInterface(new Test(1)
, "test", null); |
| 137 } |
| 138 }); |
| 139 final String html = "<html>Hello World</html>"; |
| 140 loadDataSync(awContents1, mContentsClient.getOnPageFinishedHelper(), htm
l, |
| 141 "text/html", false); |
| 142 assertEquals("1", |
| 143 executeJavaScriptAndWaitForResult(awContents1, mContentsClient,
"test.getValue()")); |
| 144 |
| 145 final TestAwContentsClient client2 = new TestAwContentsClient(); |
| 146 final AwTestContainerView view2 = createAwTestContainerViewOnMainSync(cl
ient2); |
| 147 final AwContents awContents2 = view2.getAwContents(); |
| 148 enableJavaScriptOnUiThread(awContents2); |
| 149 |
| 150 runTestOnUiThread(new Runnable() { |
| 151 @Override |
| 152 public void run() { |
| 153 awContents2.addPossiblyUnsafeJavascriptInterface(new Test(2)
, "test", null); |
| 154 } |
| 155 }); |
| 156 loadDataSync(awContents2, client2.getOnPageFinishedHelper(), html, |
| 157 "text/html", false); |
| 158 |
| 159 assertEquals("1", |
| 160 executeJavaScriptAndWaitForResult(awContents1, mContentsClient,
"test.getValue()")); |
| 161 assertEquals("2", |
| 162 executeJavaScriptAndWaitForResult(awContents2, client2, "test.ge
tValue()")); |
73 } | 163 } |
74 } | 164 } |
OLD | NEW |