Commit d0e6bcc4 authored by Wez's avatar Wez Committed by Commit Bot

Move FakeContext & FakeFrame test helpers out to their own header.

Enable re-use of these helpers across different test suites, by putting
them in their own header & cc.

Bug: 893229
Change-Id: I1d239baf708226d08a25a0aa741e8c5f6d0cf1a8
Reviewed-on: https://chromium-review.googlesource.com/c/1334559
Commit-Queue: Wez <wez@chromium.org>
Reviewed-by: default avatarKevin Marshall <kmarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#611178}
parent 638a124a
......@@ -88,6 +88,20 @@ executable("webrunner_exe") {
]
}
source_set("test_support") {
testonly = true
sources = [
"test/fake_context.cc",
"test/fake_context.h",
]
deps = [
"//base",
]
public_deps = [
":web_fidl",
]
}
fuchsia_package("service_pkg") {
binary = ":service_exe"
package_name_override = "chromium"
......@@ -270,6 +284,7 @@ test("webrunner_unittests") {
]
deps = [
":service_lib",
":test_support",
":web_fidl",
"//base/test:run_all_unittests",
"//base/test:test_support",
......
......@@ -32,6 +32,7 @@
#include "testing/multiprocess_func_list.h"
#include "webrunner/fidl/chromium/web/cpp/fidl_test_base.h"
#include "webrunner/service/common.h"
#include "webrunner/test/fake_context.h"
namespace webrunner {
namespace {
......@@ -41,70 +42,6 @@ constexpr char kTestDataFileOut[] = "DataFileOut";
constexpr char kUrl[] = "chrome://:emorhc";
constexpr char kTitle[] = "Palindrome";
// A fake Frame implementation that manages its own lifetime.
class FakeFrame : public chromium::web::testing::Frame_TestBase {
public:
explicit FakeFrame(fidl::InterfaceRequest<chromium::web::Frame> request)
: binding_(this, std::move(request)) {
binding_.set_error_handler([this](zx_status_t status) { delete this; });
}
~FakeFrame() override = default;
void set_on_set_observer_callback(base::OnceClosure callback) {
on_set_observer_callback_ = std::move(callback);
}
chromium::web::NavigationEventObserver* observer() { return observer_.get(); }
// chromium::web::Frame implementation.
void SetNavigationEventObserver(
fidl::InterfaceHandle<chromium::web::NavigationEventObserver> observer)
override {
observer_.Bind(std::move(observer));
std::move(on_set_observer_callback_).Run();
}
// chromium::web::testing::Frame_TestBase implementation.
void NotImplemented_(const std::string& name) override {
NOTREACHED() << name;
}
private:
fidl::Binding<chromium::web::Frame> binding_;
chromium::web::NavigationEventObserverPtr observer_;
base::OnceClosure on_set_observer_callback_;
DISALLOW_COPY_AND_ASSIGN(FakeFrame);
};
// An implementation of Context that creates and binds FakeFrames.
class FakeContext : public chromium::web::Context {
public:
using CreateFrameCallback = base::RepeatingCallback<void(FakeFrame*)>;
FakeContext() = default;
~FakeContext() override = default;
// Sets a callback that is invoked whenever new Frames are bound.
void set_on_create_frame_callback(CreateFrameCallback callback) {
on_create_frame_callback_ = callback;
}
void CreateFrame(
fidl::InterfaceRequest<chromium::web::Frame> frame_request) override {
FakeFrame* new_frame = new FakeFrame(std::move(frame_request));
on_create_frame_callback_.Run(new_frame);
// |new_frame| owns itself, so we intentionally leak the pointer.
}
private:
CreateFrameCallback on_create_frame_callback_;
DISALLOW_COPY_AND_ASSIGN(FakeContext);
};
MULTIPROCESS_TEST_MAIN(SpawnContextServer) {
base::MessageLoopForIO message_loop;
......
// Copyright 2018 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 "webrunner/test/fake_context.h"
#include "base/logging.h"
namespace webrunner {
FakeFrame::FakeFrame(fidl::InterfaceRequest<chromium::web::Frame> request)
: binding_(this, std::move(request)) {
binding_.set_error_handler([this](zx_status_t status) { delete this; });
}
FakeFrame::~FakeFrame() = default;
void FakeFrame::SetNavigationEventObserver(
fidl::InterfaceHandle<chromium::web::NavigationEventObserver> observer) {
observer_.Bind(std::move(observer));
std::move(on_set_observer_callback_).Run();
}
void FakeFrame::NotImplemented_(const std::string& name) {
NOTREACHED() << name;
}
FakeContext::FakeContext() = default;
FakeContext::~FakeContext() = default;
void FakeContext::CreateFrame(
fidl::InterfaceRequest<chromium::web::Frame> frame_request) {
FakeFrame* new_frame = new FakeFrame(std::move(frame_request));
on_create_frame_callback_.Run(new_frame);
// |new_frame| owns itself, so we intentionally leak the pointer.
}
void FakeContext::NotImplemented_(const std::string& name) {
NOTREACHED() << name;
}
} // namespace webrunner
// Copyright 2018 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.
#ifndef WEBRUNNER_TEST_FAKE_CONTEXT_H_
#define WEBRUNNER_TEST_FAKE_CONTEXT_H_
#include <lib/fidl/cpp/binding.h>
#include <utility>
#include "base/callback.h"
#include "base/macros.h"
#include "webrunner/fidl/chromium/web/cpp/fidl.h"
#include "webrunner/fidl/chromium/web/cpp/fidl_test_base.h"
namespace webrunner {
// A fake Frame implementation that manages its own lifetime.
class FakeFrame : public chromium::web::testing::Frame_TestBase {
public:
explicit FakeFrame(fidl::InterfaceRequest<chromium::web::Frame> request);
~FakeFrame() override;
void set_on_set_observer_callback(base::OnceClosure callback) {
on_set_observer_callback_ = std::move(callback);
}
chromium::web::NavigationEventObserver* observer() { return observer_.get(); }
// chromium::web::Frame implementation.
void SetNavigationEventObserver(
fidl::InterfaceHandle<chromium::web::NavigationEventObserver> observer)
override;
// chromium::web::testing::Frame_TestBase implementation.
void NotImplemented_(const std::string& name) override;
private:
fidl::Binding<chromium::web::Frame> binding_;
chromium::web::NavigationEventObserverPtr observer_;
base::OnceClosure on_set_observer_callback_;
DISALLOW_COPY_AND_ASSIGN(FakeFrame);
};
// An implementation of Context that creates and binds FakeFrames.
class FakeContext : public chromium::web::testing::Context_TestBase {
public:
using CreateFrameCallback = base::RepeatingCallback<void(FakeFrame*)>;
FakeContext();
~FakeContext() override;
// Sets a callback that is invoked whenever new Frames are bound.
void set_on_create_frame_callback(CreateFrameCallback callback) {
on_create_frame_callback_ = callback;
}
// chromium::web::Context implementation.
void CreateFrame(
fidl::InterfaceRequest<chromium::web::Frame> frame_request) override;
// chromium::web::testing::Frame_TestBase implementation.
void NotImplemented_(const std::string& name) override;
private:
CreateFrameCallback on_create_frame_callback_;
DISALLOW_COPY_AND_ASSIGN(FakeContext);
};
} // namespace webrunner
#endif // WEBRUNNER_TEST_FAKE_CONTEXT_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