Flutter Engine
stub_flutter_api.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 
5 #ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_TESTING_STUB_FLUTTER_API_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_TESTING_STUB_FLUTTER_API_H_
7 
8 #include <memory>
9 
10 #include "flutter/shell/platform/common/cpp/public/flutter_messenger.h"
11 #include "flutter/shell/platform/common/cpp/public/flutter_plugin_registrar.h"
12 
13 namespace flutter {
14 namespace testing {
15 
16 // Base class for a object that provides test implementations of the APIs in
17 // the headers in platform/common/cpp/public/.
18 
19 // Linking this class into a test binary will provide dummy forwarding
20 // implementantions of that C API, so that the wrapper can be tested separately
21 // from the actual library.
23  public:
24  // Used by the callers to simulate a result from the engine when sending a
25  // message.
26  bool message_engine_result = true;
27 
28  // Sets |stub| as the instance to which calls to the Flutter library C APIs
29  // will be forwarded.
30  static void SetTestStub(StubFlutterApi* stub);
31 
32  // Returns the current stub, as last set by SetTestFluttterStub.
33  static StubFlutterApi* GetTestStub();
34 
35  virtual ~StubFlutterApi() {}
36 
37  // Called for FlutterDesktopPluginRegistrarSetDestructionHandler.
40 
41  // Called for FlutterDesktopMessengerSend.
42  virtual bool MessengerSend(const char* channel,
43  const uint8_t* message,
44  const size_t message_size) {
45  return message_engine_result;
46  }
47 
48  // Called for FlutterDesktopMessengerSendWithReply.
49  virtual bool MessengerSendWithReply(const char* channel,
50  const uint8_t* message,
51  const size_t message_size,
52  const FlutterDesktopBinaryReply reply,
53  void* user_data) {
54  return message_engine_result;
55  }
56 
57  // Called for FlutterDesktopMessengerSendResponse.
58  virtual void MessengerSendResponse(
60  const uint8_t* data,
61  size_t data_length) {}
62 
63  // Called for FlutterDesktopMessengerSetCallback.
64  virtual void MessengerSetCallback(const char* channel,
66  void* user_data) {}
67 };
68 
69 // A test helper that owns a stub implementation, making it the test stub for
70 // the lifetime of the object, then restoring the previous value.
72  public:
73  // Calls SetTestFlutterStub with |stub|.
74  ScopedStubFlutterApi(std::unique_ptr<StubFlutterApi> stub);
75 
76  // Restores the previous test stub.
78 
79  StubFlutterApi* stub() { return stub_.get(); }
80 
81  private:
82  std::unique_ptr<StubFlutterApi> stub_;
83  // The previous stub.
84  StubFlutterApi* previous_stub_;
85 };
86 
87 } // namespace testing
88 } // namespace flutter
89 
90 #endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_TESTING_STUB_FLUTTER_API_H_
virtual void PluginRegistrarSetDestructionHandler(FlutterDesktopOnPluginRegistrarDestroyed callback)
static StubFlutterApi * GetTestStub()
virtual bool MessengerSend(const char *channel, const uint8_t *message, const size_t message_size)
virtual bool MessengerSendWithReply(const char *channel, const uint8_t *message, const size_t message_size, const FlutterDesktopBinaryReply reply, void *user_data)
static void SetTestStub(StubFlutterApi *stub)
G_BEGIN_DECLS FlValue gpointer user_data
void(* FlutterDesktopBinaryReply)(const uint8_t *data, size_t data_size, void *user_data)
void(* FlutterDesktopOnPluginRegistrarDestroyed)(FlutterDesktopPluginRegistrarRef)
virtual void MessengerSetCallback(const char *channel, FlutterDesktopMessageCallback callback, void *user_data)
void(* FlutterDesktopMessageCallback)(FlutterDesktopMessengerRef, const FlutterDesktopMessage *, void *)
virtual void MessengerSendResponse(const FlutterDesktopMessageResponseHandle *handle, const uint8_t *data, size_t data_length)