// 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 "fuchsia/base/frame_test_util.h" #include "base/json/json_reader.h" #include "base/run_loop.h" #include "fuchsia/base/fit_adapter.h" #include "fuchsia/base/mem_buffer_util.h" #include "fuchsia/base/result_receiver.h" #include "fuchsia/base/test_navigation_listener.h" namespace cr_fuchsia { bool LoadUrlAndExpectResponse( fuchsia::web::NavigationController* navigation_controller, fuchsia::web::LoadUrlParams load_url_params, base::StringPiece url) { DCHECK(navigation_controller); base::RunLoop run_loop; ResultReceiver result( run_loop.QuitClosure()); navigation_controller->LoadUrl( url.as_string(), std::move(load_url_params), CallbackToFitFunction(result.GetReceiveCallback())); run_loop.Run(); return result->is_response(); } base::Optional ExecuteJavaScript(fuchsia::web::Frame* frame, base::StringPiece script) { base::RunLoop run_loop; ResultReceiver result( run_loop.QuitClosure()); frame->ExecuteJavaScript({"*"}, MemBufferFromString(script, "test"), CallbackToFitFunction(result.GetReceiveCallback())); run_loop.Run(); if (!result.has_value() || !result->is_response()) return {}; std::string result_json; if (!StringFromMemBuffer(result->response().result, &result_json)) { return {}; } return base::JSONReader::Read(result_json); } fuchsia::web::LoadUrlParams CreateLoadUrlParamsWithUserActivation() { fuchsia::web::LoadUrlParams load_url_params; load_url_params.set_was_user_activated(true); return load_url_params; } fuchsia::web::WebMessage CreateWebMessageWithMessagePortRequest( fidl::InterfaceRequest message_port_request, fuchsia::mem::Buffer buffer) { fuchsia::web::OutgoingTransferable outgoing; outgoing.set_message_port(std::move(message_port_request)); std::vector outgoing_vector; outgoing_vector.push_back(std::move(outgoing)); fuchsia::web::WebMessage web_message; web_message.set_outgoing_transfer(std::move(outgoing_vector)); web_message.set_data(std::move(buffer)); return web_message; } } // namespace cr_fuchsia