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 3 // found in the LICENSE file. 4 5 package org.chromium.content.browser; 6 7 import junit.framework.Assert; 8 9 /** 10 * Common functionality for testing the Java Bridge. 11 */ 12 public class JavaBridgeTestBase extends ContentViewTestBase { 13 protected class Controller { 14 private boolean mIsResultReady; 15 notifyResultIsReady()16 protected synchronized void notifyResultIsReady() { 17 mIsResultReady = true; 18 notify(); 19 } waitForResult()20 protected synchronized void waitForResult() { 21 while (!mIsResultReady) { 22 try { 23 wait(5000); 24 } catch (Exception e) { 25 continue; 26 } 27 if (!mIsResultReady) { 28 Assert.fail("Wait timed out"); 29 } 30 } 31 mIsResultReady = false; 32 } 33 } 34 executeJavaScript(final String script)35 protected void executeJavaScript(final String script) throws Throwable { 36 runTestOnUiThread(new Runnable() { 37 @Override 38 public void run() { 39 // When a JavaScript URL is executed, if the value of the last 40 // expression evaluated is not 'undefined', this value is 41 // converted to a string and used as the new document for the 42 // frame. We don't want this behaviour, so wrap the script in 43 // an anonymous function. 44 getContentViewCore().loadUrl(new LoadUrlParams( 45 "javascript:(function() { " + script + " })()")); 46 } 47 }); 48 } 49 } 50