fake_context.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef FUCHSIA_WEB_WEBENGINE_FAKE_CONTEXT_H_
  5. #define FUCHSIA_WEB_WEBENGINE_FAKE_CONTEXT_H_
  6. #include <fuchsia/web/cpp/fidl.h>
  7. #include <fuchsia/web/cpp/fidl_test_base.h>
  8. #include <lib/fidl/cpp/binding.h>
  9. #include <lib/fidl/cpp/binding_set.h>
  10. #include <utility>
  11. #include "base/callback.h"
  12. // A fake Frame implementation that manages its own lifetime.
  13. class FakeFrame : public fuchsia::web::testing::Frame_TestBase {
  14. public:
  15. explicit FakeFrame(fidl::InterfaceRequest<fuchsia::web::Frame> request);
  16. FakeFrame(const FakeFrame&) = delete;
  17. FakeFrame& operator=(const FakeFrame&) = delete;
  18. ~FakeFrame() override;
  19. void set_on_set_listener_callback(base::OnceClosure callback) {
  20. on_set_listener_callback_ = std::move(callback);
  21. }
  22. // Tests can provide e.g a mock NavigationController, which the FakeFrame will
  23. // pass bind GetNavigationController() requests to.
  24. void set_navigation_controller(
  25. fuchsia::web::NavigationController* controller) {
  26. navigation_controller_ = controller;
  27. }
  28. fuchsia::web::NavigationEventListener* listener() { return listener_.get(); }
  29. // fuchsia::web::Frame implementation.
  30. void GetNavigationController(
  31. fidl::InterfaceRequest<fuchsia::web::NavigationController> controller)
  32. override;
  33. void SetNavigationEventListener(
  34. fidl::InterfaceHandle<fuchsia::web::NavigationEventListener> listener)
  35. override;
  36. void SetNavigationEventListener2(
  37. fidl::InterfaceHandle<fuchsia::web::NavigationEventListener> listener,
  38. fuchsia::web::NavigationEventListenerFlags flags) override;
  39. // fuchsia::web::testing::Frame_TestBase implementation.
  40. void NotImplemented_(const std::string& name) override;
  41. private:
  42. fidl::Binding<fuchsia::web::Frame> binding_;
  43. fuchsia::web::NavigationEventListenerPtr listener_;
  44. base::OnceClosure on_set_listener_callback_;
  45. fuchsia::web::NavigationController* navigation_controller_ = nullptr;
  46. fidl::BindingSet<fuchsia::web::NavigationController>
  47. navigation_controller_bindings_;
  48. };
  49. // An implementation of Context that creates and binds FakeFrames.
  50. class FakeContext : public fuchsia::web::testing::Context_TestBase {
  51. public:
  52. using CreateFrameCallback = base::RepeatingCallback<void(FakeFrame*)>;
  53. FakeContext();
  54. FakeContext(const FakeContext&) = delete;
  55. FakeContext& operator=(const FakeContext&) = delete;
  56. ~FakeContext() override;
  57. // Sets a callback that is invoked whenever new Frames are bound.
  58. void set_on_create_frame_callback(CreateFrameCallback callback) {
  59. on_create_frame_callback_ = callback;
  60. }
  61. // fuchsia::web::Context implementation.
  62. void CreateFrame(
  63. fidl::InterfaceRequest<fuchsia::web::Frame> frame_request) override;
  64. // fuchsia::web::testing::Context_TestBase implementation.
  65. void NotImplemented_(const std::string& name) override;
  66. private:
  67. CreateFrameCallback on_create_frame_callback_;
  68. };
  69. #endif // FUCHSIA_WEB_WEBENGINE_FAKE_CONTEXT_H_