fake_api_bindings.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2019 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_RUNNERS_CAST_FAKE_API_BINDINGS_H_
  5. #define FUCHSIA_WEB_RUNNERS_CAST_FAKE_API_BINDINGS_H_
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "base/containers/flat_map.h"
  11. #include "base/strings/string_piece.h"
  12. #include "fuchsia_web/runners/cast/fidl/fidl/chromium/cast/cpp/fidl.h"
  13. // Simple implementation of the ApiBindings service, for use by tests.
  14. class FakeApiBindingsImpl : public chromium::cast::ApiBindings {
  15. public:
  16. FakeApiBindingsImpl();
  17. ~FakeApiBindingsImpl() override;
  18. FakeApiBindingsImpl(const FakeApiBindingsImpl&) = delete;
  19. FakeApiBindingsImpl& operator=(const FakeApiBindingsImpl&) = delete;
  20. // Returns the message port with the specified |name|. Runs the message loop
  21. // if a port of the specified name has not yet been received, and returns an
  22. // invalid handle if that times out before a |Connect()| call is received.
  23. fidl::InterfaceHandle<fuchsia::web::MessagePort> RunAndReturnConnectedPort(
  24. base::StringPiece name);
  25. // Sets the list of bindings which will be returned by GetAll().
  26. void set_bindings(std::vector<chromium::cast::ApiBinding> bindings) {
  27. bindings_ = std::move(bindings);
  28. }
  29. private:
  30. // chromium::cast::ApiBindings implementation.
  31. void GetAll(GetAllCallback callback) override;
  32. void Connect(
  33. std::string name,
  34. fidl::InterfaceHandle<fuchsia::web::MessagePort> message_port) override;
  35. // Bindings to return from GetAll().
  36. std::vector<chromium::cast::ApiBinding> bindings_;
  37. // Holds ports received via Connect(), for tests to take by calling
  38. // RunAndReturnConnectedPort(). Uses std::less<> as the comparator so that
  39. // StringPieces can be used for lookup without requiring a conversion.
  40. base::flat_map<std::string, fidl::InterfaceHandle<fuchsia::web::MessagePort>>
  41. ports_;
  42. // Used to wait for a specific port to be Connect()ed.
  43. base::StringPiece expected_port_name_;
  44. base::OnceClosure on_expected_port_received_;
  45. };
  46. #endif // FUCHSIA_WEB_RUNNERS_CAST_FAKE_API_BINDINGS_H_