OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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.content.browser; | 5 package org.chromium.content.browser; |
6 | 6 |
| 7 import android.os.Handler; |
| 8 import android.os.Looper; |
7 import android.test.suitebuilder.annotation.SmallTest; | 9 import android.test.suitebuilder.annotation.SmallTest; |
8 | 10 |
9 import org.chromium.base.test.util.DisabledTest; | 11 import org.chromium.base.test.util.DisabledTest; |
10 import org.chromium.base.test.util.Feature; | 12 import org.chromium.base.test.util.Feature; |
11 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; | 13 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
12 import org.chromium.content_shell_apk.ContentShellActivity; | 14 import org.chromium.content_shell_apk.ContentShellActivity; |
13 | 15 |
14 import java.lang.annotation.Annotation; | 16 import java.lang.annotation.Annotation; |
15 import java.lang.annotation.ElementType; | 17 import java.lang.annotation.ElementType; |
16 import java.lang.annotation.Retention; | 18 import java.lang.annotation.Retention; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 return mLongValue; | 65 return mLongValue; |
64 } | 66 } |
65 public synchronized String waitForStringValue() { | 67 public synchronized String waitForStringValue() { |
66 waitForResult(); | 68 waitForResult(); |
67 return mStringValue; | 69 return mStringValue; |
68 } | 70 } |
69 public synchronized boolean waitForBooleanValue() { | 71 public synchronized boolean waitForBooleanValue() { |
70 waitForResult(); | 72 waitForResult(); |
71 return mBooleanValue; | 73 return mBooleanValue; |
72 } | 74 } |
| 75 |
| 76 public synchronized String getStringValue() { |
| 77 return mStringValue; |
| 78 } |
73 } | 79 } |
74 | 80 |
75 private static class ObjectWithStaticMethod { | 81 private static class ObjectWithStaticMethod { |
76 public static String staticMethod() { | 82 public static String staticMethod() { |
77 return "foo"; | 83 return "foo"; |
78 } | 84 } |
79 } | 85 } |
80 | 86 |
81 TestController mTestController; | 87 TestController mTestController; |
82 | 88 |
(...skipping 745 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
828 } | 834 } |
829 }); | 835 }); |
830 | 836 |
831 injectObjectAndReload(new Test(), nonInspectableObjectName, JavascriptIn
terface.class); | 837 injectObjectAndReload(new Test(), nonInspectableObjectName, JavascriptIn
terface.class); |
832 | 838 |
833 assertEquals("", executeJavaScriptAndGetStringResult( | 839 assertEquals("", executeJavaScriptAndGetStringResult( |
834 String.format(jsObjectKeysTestTemplate, nonInspectableOb
jectName))); | 840 String.format(jsObjectKeysTestTemplate, nonInspectableOb
jectName))); |
835 assertEquals("", executeJavaScriptAndGetStringResult( | 841 assertEquals("", executeJavaScriptAndGetStringResult( |
836 String.format(jsForInTestTemplate, nonInspectableObjectN
ame))); | 842 String.format(jsForInTestTemplate, nonInspectableObjectN
ame))); |
837 } | 843 } |
| 844 |
| 845 @SmallTest |
| 846 @Feature({"AndroidWebView", "Android-JavaBridge"}) |
| 847 public void testAccessToObjectGetClassIsBlocked() throws Throwable { |
| 848 injectObjectAndReload(new Object(), "testObject"); |
| 849 assertEquals("function", executeJavaScriptAndGetStringResult("typeof tes
tObject.getClass")); |
| 850 boolean securityExceptionThrown = false; |
| 851 try { |
| 852 final String result = executeJavaScriptAndWaitForExceptionSynchronou
sly( |
| 853 "typeof testObject.getClass()"); |
| 854 fail("A call to java.lang.Object.getClass has been allowed, result:
'" + result + "'"); |
| 855 } catch (SecurityException exception) { |
| 856 securityExceptionThrown = true; |
| 857 } |
| 858 assertTrue(securityExceptionThrown); |
| 859 } |
| 860 |
| 861 // Unlike executeJavaScriptAndGetStringResult, this method is sitting on the
UI thread |
| 862 // until a non-null result is obtained or a Java exception has been thrown.
This method is |
| 863 // capable of catching Java RuntimeExceptions happening on the UI thread asy
nchronously. |
| 864 private String executeJavaScriptAndWaitForExceptionSynchronously(final Strin
g script) |
| 865 throws Throwable { |
| 866 class ExitLoopException extends RuntimeException { |
| 867 } |
| 868 mTestController.setStringValue(null); |
| 869 runTestOnUiThread(new Runnable() { |
| 870 @Override |
| 871 public void run() { |
| 872 getContentView().loadUrl(new LoadUrlParams("javascript:(function
() { " + |
| 873 "testController.setStringValue(" + script + ") }
)()")); |
| 874 do { |
| 875 final Boolean[] deactivateExitLoopTask = new Boolean[1]; |
| 876 deactivateExitLoopTask[0] = false; |
| 877 // We can't use Loop.quit(), as this is the main looper, so
we throw |
| 878 // an exception to bail out from the loop. |
| 879 new Handler(Looper.myLooper()).post(new Runnable() { |
| 880 @Override |
| 881 public void run() { |
| 882 if (!deactivateExitLoopTask[0]) { |
| 883 throw new ExitLoopException(); |
| 884 } |
| 885 } |
| 886 }); |
| 887 try { |
| 888 Looper.loop(); |
| 889 } catch (ExitLoopException e) { |
| 890 // Intentionally empty. |
| 891 } catch (RuntimeException e) { |
| 892 // Prevent the task that throws the ExitLoopException fr
om exploding |
| 893 // on the main loop outside of this function. |
| 894 deactivateExitLoopTask[0] = true; |
| 895 throw e; |
| 896 } |
| 897 } while (mTestController.getStringValue() == null || |
| 898 // When an exception in an injected method happens, the
function returns |
| 899 // null. We ignore this and wait until the exception on
the browser side |
| 900 // will be thrown. |
| 901 mTestController.getStringValue().equals("null")); |
| 902 } |
| 903 }); |
| 904 return mTestController.getStringValue(); |
| 905 } |
838 } | 906 } |
OLD | NEW |