13f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org// Copyright 2012 The Chromium Authors. All rights reserved.
23f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org// Use of this source code is governed by a BSD-style license that can be
33f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org// found in the LICENSE file.
43f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org
53f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.orgpackage org.chromium.content.browser;
63f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org
73f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.orgimport junit.framework.Assert;
83f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org
93f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.orgimport org.chromium.content_public.browser.LoadUrlParams;
103f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org
11ed0b4fb2f28fa291e726fc1cf6852611f2176392turaj@webrtc.org/**
123f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org * Common functionality for testing the Java Bridge.
133f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org */
143f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.orgpublic class JavaBridgeTestBase extends ContentViewTestBase {
15ed0b4fb2f28fa291e726fc1cf6852611f2176392turaj@webrtc.org    protected class Controller {
16ed0b4fb2f28fa291e726fc1cf6852611f2176392turaj@webrtc.org        private boolean mIsResultReady;
173f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org
183f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org        protected synchronized void notifyResultIsReady() {
193f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org            mIsResultReady = true;
203f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org            notify();
213f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org        }
220a1c75a50d10621f70d5921c5b8b9c0eb144bb42turaj@webrtc.org        protected synchronized void waitForResult() {
230a1c75a50d10621f70d5921c5b8b9c0eb144bb42turaj@webrtc.org            while (!mIsResultReady) {
243f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org                try {
253f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org                    wait(5000);
263f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org                } catch (Exception e) {
273f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org                    continue;
283f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org                }
293f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org                if (!mIsResultReady) {
3081b53f5bb323b6b8b15b4396c82d64d8b990e45dminyue@webrtc.org                    Assert.fail("Wait timed out");
3181b53f5bb323b6b8b15b4396c82d64d8b990e45dminyue@webrtc.org                }
323f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org            }
333f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org            mIsResultReady = false;
343f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org        }
353f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org    }
363f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org
373f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org    protected void executeJavaScript(final String script) throws Throwable {
383f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org        runTestOnUiThread(new Runnable() {
393f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org            @Override
403f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org            public void run() {
413f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org                // When a JavaScript URL is executed, if the value of the last
423f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org                // expression evaluated is not 'undefined', this value is
433f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org                // converted to a string and used as the new document for the
443f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org                // frame. We don't want this behaviour, so wrap the script in
453f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org                // an anonymous function.
463f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org                getContentViewCore().getWebContents().getNavigationController().loadUrl(
473f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org                        new LoadUrlParams("javascript:(function() { " + script + " })()"));
483f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org            }
493f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org        });
503f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org    }
513f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org}
523f39c00d9875d7e09062725757b789f4da375ef9turaj@webrtc.org