Commit 90c5851b authored by Clark DuVall's avatar Clark DuVall Committed by Commit Bot

[WebLayer] Add tests for chrome://weblayer page

This page recently broke (fix in http://crrev.com/c/1880171), so tests
seem like a good idea.

This also adds a test helper for executing javascript, and makes the
NavigateAndWaitForCompletion function work more similarly to the one in
the instrumentation tests by waiting until the page is finished loading.

Bug: 1017924
Change-Id: I8ca048f9a9a92808abf6cfa5d0e091550cc3d9a6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1880173
Commit-Queue: Clark DuVall <cduvall@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#709552}
parent 7092dcc9
......@@ -12,6 +12,7 @@
#include "content/public/browser/web_contents.h"
#include "content/public/common/browser_controls_state.h"
#include "weblayer/browser/file_select_helper.h"
#include "weblayer/browser/isolated_world_ids.h"
#include "weblayer/browser/navigation_controller_impl.h"
#include "weblayer/browser/profile_impl.h"
#include "weblayer/public/browser_observer.h"
......@@ -27,7 +28,6 @@
#include "base/android/jni_string.h"
#include "base/json/json_writer.h"
#include "components/embedder_support/android/delegate/color_chooser_android.h"
#include "weblayer/browser/isolated_world_ids.h"
#include "weblayer/browser/java/jni/BrowserControllerImpl_jni.h"
#include "weblayer/browser/top_controls_container_view.h"
#endif
......@@ -47,7 +47,7 @@ struct UserData : public base::SupportsUserData::Data {
#if defined(OS_ANDROID)
BrowserController* g_last_browser_controller;
void JavaScriptResultCallback(
void HandleJavaScriptResult(
const base::android::ScopedJavaGlobalRef<jobject>& callback,
base::Value result) {
std::string json;
......@@ -128,6 +128,12 @@ NavigationController* BrowserControllerImpl::GetNavigationController() {
return navigation_controller_.get();
}
void BrowserControllerImpl::ExecuteScript(const base::string16& script,
JavaScriptResultCallback callback) {
web_contents_->GetMainFrame()->ExecuteJavaScriptInIsolatedWorld(
script, std::move(callback), ISOLATED_WORLD_ID_WEBLAYER);
}
#if !defined(OS_ANDROID)
void BrowserControllerImpl::AttachToView(views::WebView* web_view) {
web_view->SetWebContents(web_contents_.get());
......@@ -168,10 +174,8 @@ void BrowserControllerImpl::ExecuteScript(
const base::android::JavaParamRef<jstring>& script,
const base::android::JavaParamRef<jobject>& callback) {
base::android::ScopedJavaGlobalRef<jobject> jcallback(env, callback);
web_contents_->GetMainFrame()->ExecuteJavaScriptInIsolatedWorld(
base::android::ConvertJavaStringToUTF16(script),
base::BindOnce(&JavaScriptResultCallback, jcallback),
ISOLATED_WORLD_ID_WEBLAYER);
ExecuteScript(base::android::ConvertJavaStringToUTF16(script),
base::BindOnce(&HandleJavaScriptResult, jcallback));
}
#endif
......
......@@ -69,6 +69,8 @@ class BrowserControllerImpl : public BrowserController,
void AddObserver(BrowserObserver* observer) override;
void RemoveObserver(BrowserObserver* observer) override;
NavigationController* GetNavigationController() override;
void ExecuteScript(const base::string16& script,
JavaScriptResultCallback callback) override;
#if !defined(OS_ANDROID)
void AttachToView(views::WebView* web_view) override;
#endif
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "weblayer/test/weblayer_browser_test.h"
#include "build/build_config.h"
#include "weblayer/test/weblayer_browser_test_utils.h"
namespace weblayer {
using WebLayerWebUIBrowserTest = WebLayerBrowserTest;
IN_PROC_BROWSER_TEST_F(WebLayerWebUIBrowserTest, WebUI) {
NavigateAndWaitForCompletion(GURL("chrome://weblayer"), shell());
base::RunLoop run_loop;
bool result =
ExecuteScript(shell(),
"document.getElementById('remote-debug-label').hidden")
.GetBool();
// The remote debug checkbox should only be visible on Android.
#if defined(OS_ANDROID)
EXPECT_FALSE(result);
#else
EXPECT_TRUE(result);
#endif
}
} // namespace weblayer
......@@ -7,8 +7,14 @@
#include <algorithm>
#include "base/callback_forward.h"
#include "base/strings/string16.h"
#include "build/build_config.h"
namespace base {
class Value;
}
#if !defined(OS_ANDROID)
namespace views {
class WebView;
......@@ -46,6 +52,10 @@ class BrowserController {
virtual NavigationController* GetNavigationController() = 0;
using JavaScriptResultCallback = base::OnceCallback<void(base::Value)>;
virtual void ExecuteScript(const base::string16& script,
JavaScriptResultCallback callback) = 0;
#if !defined(OS_ANDROID)
// TODO: this isn't a stable API, so use it now for expediency in the C++ API,
// but if we ever want to have backward or forward compatibility in C++ this
......
......@@ -84,6 +84,7 @@ test("weblayer_browsertests") {
]
sources = [
"../browser/webui/webui_browsertest.cc",
"browsertests_main.cc",
"ssl_browsertest.cc",
"test_launcher_delegate_impl.cc",
......
......@@ -5,6 +5,8 @@
#include "weblayer/test/weblayer_browser_test_utils.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind_test_util.h"
#include "url/gurl.h"
#include "weblayer/public/browser_controller.h"
#include "weblayer/public/navigation.h"
......@@ -40,8 +42,10 @@ class TestNavigationObserver : public NavigationObserver {
// NavigationObserver implementation:
void NavigationCompleted(Navigation* navigation) override {
if (navigation->GetURL() == url_ &&
event_ == NavigationEventToObserve::Completion)
std::move(closure_).Run();
event_ == NavigationEventToObserve::Completion) {
navigation_complete_ = true;
CheckComplete();
}
}
void NavigationFailed(Navigation* navigation) override {
......@@ -51,10 +55,22 @@ class TestNavigationObserver : public NavigationObserver {
}
}
void LoadStateChanged(bool is_loading, bool to_different_document) override {
done_loading_ = !is_loading;
CheckComplete();
}
void CheckComplete() {
if (done_loading_ && navigation_complete_)
std::move(closure_).Run();
}
base::OnceClosure closure_;
const GURL url_;
NavigationEventToObserve event_;
BrowserController* browser_;
bool done_loading_ = false;
bool navigation_complete_ = false;
};
// Navigates to |url| in |shell| and waits for |event| to occur.
......@@ -82,4 +98,18 @@ void NavigateAndWaitForFailure(const GURL& url, Shell* shell) {
url, shell, TestNavigationObserver::NavigationEventToObserve::Failure);
}
base::Value ExecuteScript(Shell* shell, const std::string& script) {
base::Value final_result;
base::RunLoop run_loop;
shell->browser_controller()->ExecuteScript(
base::ASCIIToUTF16(script),
base::BindLambdaForTesting(
[&run_loop, &final_result](base::Value result) {
final_result = std::move(result);
run_loop.Quit();
}));
run_loop.Run();
return final_result;
}
} // namespace weblayer
......@@ -5,6 +5,8 @@
#ifndef WEBLAYER_TEST_WEBLAYER_BROWSER_TEST_UTILS_H_
#define WEBLAYER_TEST_WEBLAYER_BROWSER_TEST_UTILS_H_
#include "base/values.h"
class GURL;
namespace weblayer {
......@@ -16,6 +18,9 @@ void NavigateAndWaitForCompletion(const GURL& url, Shell* shell);
// Navigates |shell| to |url| and wait for failed navigation.
void NavigateAndWaitForFailure(const GURL& url, Shell* shell);
// Executes |script| in |shell| and returns the result.
base::Value ExecuteScript(Shell* shell, const std::string& script);
} // namespace weblayer
#endif // WEBLAYER_TEST_WEBLAYER_BROWSER_TEST_UTILS_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment