| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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; |
| 6 |
| 7 import android.util.Log; |
| 8 |
| 9 import junit.framework.Assert; |
| 10 |
| 11 import org.chromium.content.browser.ContentView; |
| 12 import org.chromium.content.browser.LoadUrlParams; |
| 13 import org.chromium.content.browser.test.TestContentViewClient; |
| 14 |
| 15 /** |
| 16 * Common functionality for testing the Java Bridge. |
| 17 */ |
| 18 public class JavaBridgeTestBase extends ContentShellTestBase { |
| 19 protected class Controller { |
| 20 private boolean mIsResultReady; |
| 21 |
| 22 protected synchronized void notifyResultIsReady() { |
| 23 mIsResultReady = true; |
| 24 notify(); |
| 25 } |
| 26 protected synchronized void waitForResult() { |
| 27 while (!mIsResultReady) { |
| 28 try { |
| 29 wait(5000); |
| 30 } catch (Exception e) { |
| 31 continue; |
| 32 } |
| 33 if (!mIsResultReady) { |
| 34 Assert.fail("Wait timed out"); |
| 35 } |
| 36 } |
| 37 mIsResultReady = false; |
| 38 } |
| 39 } |
| 40 |
| 41 protected TestContentViewClient mContentViewClient; |
| 42 |
| 43 // Sets up the ContentView and injects the supplied object. Intended to be c
alled from setUp(). |
| 44 protected void setUpContentView(final Object object, final String name) thro
ws Exception { |
| 45 mContentViewClient = new TestContentViewClient(); |
| 46 // This starts the activity, so must be called on the test thread. |
| 47 final ContentShellActivity activity = launchContentShellWithUrl( |
| 48 "data:text/html;utf-8,<html><head></head><body>test</body></html
>"); |
| 49 // On the UI thread, load an empty page and wait for it to finish |
| 50 // loading so that the Java object is injected. |
| 51 try { |
| 52 runTestOnUiThread(new Runnable() { |
| 53 @Override |
| 54 public void run() { |
| 55 ContentView contentView = activity.getActiveContentView(); |
| 56 contentView.getContentViewCore().addJavascriptInterface(obje
ct, name, false); |
| 57 setTestContentViewClient(contentView, mContentViewClient); |
| 58 } |
| 59 }); |
| 60 TestContentViewClient.OnPageFinishedHelper onPageFinishedHelper = |
| 61 mContentViewClient.getOnPageFinishedHelper(); |
| 62 onPageFinishedHelper.waitForCallback(0); |
| 63 } catch (Throwable e) { |
| 64 throw new RuntimeException( |
| 65 "Failed to set up ContentView: " + Log.getStackTraceString(e
)); |
| 66 } |
| 67 } |
| 68 |
| 69 protected void executeJavaScript(final String script) throws Throwable { |
| 70 runTestOnUiThread(new Runnable() { |
| 71 @Override |
| 72 public void run() { |
| 73 // When a JavaScript URL is executed, if the value of the last |
| 74 // expression evaluated is not 'undefined', this value is |
| 75 // converted to a string and used as the new document for the |
| 76 // frame. We don't want this behaviour, so wrap the script in |
| 77 // an anonymous function. |
| 78 getContentView().loadUrl(new LoadUrlParams( |
| 79 "javascript:(function() { " + script + " })()")); |
| 80 } |
| 81 }); |
| 82 } |
| 83 } |
| OLD | NEW |