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