summaryrefslogtreecommitdiff
path: root/chromium/fuchsia/base/frame_test_util.cc
blob: 2150cd86360625a4abfcc666991ed749ec8337f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// 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<fuchsia::web::NavigationController_LoadUrl_Result> 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<base::Value> ExecuteJavaScript(fuchsia::web::Frame* frame,
                                              base::StringPiece script) {
  base::RunLoop run_loop;
  ResultReceiver<fuchsia::web::Frame_ExecuteJavaScript_Result> 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<fuchsia::web::MessagePort> message_port_request,
    fuchsia::mem::Buffer buffer) {
  fuchsia::web::OutgoingTransferable outgoing;
  outgoing.set_message_port(std::move(message_port_request));

  std::vector<fuchsia::web::OutgoingTransferable> 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