2
2
// Use of this source code is governed by a BSD-style license that can be
3
3
// found in the LICENSE file.
4
4
5
+ #include < map>
5
6
#include < memory>
6
7
#include < vector>
7
8
8
9
#include " flutter/shell/platform/common/cpp/client_wrapper/include/flutter/plugin_registrar.h"
10
+ #include " flutter/shell/platform/common/cpp/client_wrapper/include/flutter/texture_registrar.h"
9
11
#include " flutter/shell/platform/common/cpp/client_wrapper/testing/stub_flutter_api.h"
10
12
#include " gtest/gtest.h"
11
13
@@ -15,6 +17,14 @@ namespace {
15
17
16
18
// Stub implementation to validate calls to the API.
17
19
class TestApi : public testing ::StubFlutterApi {
20
+ public:
21
+ struct FakeTexture {
22
+ int64_t texture_id;
23
+ int32_t mark_count;
24
+ FlutterTextureCallback texture_callback;
25
+ void * user_data;
26
+ };
27
+
18
28
public:
19
29
// |flutter::testing::StubFlutterApi|
20
30
bool MessengerSend (const char * channel,
@@ -52,10 +62,55 @@ class TestApi : public testing::StubFlutterApi {
52
62
return last_destruction_callback_set_;
53
63
}
54
64
65
+ int64_t RegisterExternalTexture (FlutterTextureCallback texture_callback,
66
+ void * user_data) override {
67
+ last_texture_id_++;
68
+
69
+ auto texture = std::make_unique<FakeTexture>();
70
+ texture->texture_callback = texture_callback;
71
+ texture->user_data = user_data;
72
+ texture->mark_count = 0 ;
73
+ texture->texture_id = last_texture_id_;
74
+
75
+ textures_[last_texture_id_] = std::move (texture);
76
+ return last_texture_id_;
77
+ }
78
+
79
+ bool UnregisterExternalTexture (int64_t texture_id) override {
80
+ auto it = textures_.find (texture_id);
81
+ if (it != textures_.end ()) {
82
+ textures_.erase (it);
83
+ return true ;
84
+ }
85
+ return false ;
86
+ }
87
+
88
+ bool TextureFrameAvailable (int64_t texture_id) override {
89
+ auto it = textures_.find (texture_id);
90
+ if (it != textures_.end ()) {
91
+ it->second ->mark_count ++;
92
+ return true ;
93
+ }
94
+ return false ;
95
+ }
96
+
97
+ FakeTexture* GetFakeTexture (int64_t texture_id) {
98
+ auto it = textures_.find (texture_id);
99
+ if (it != textures_.end ())
100
+ return it->second .get ();
101
+ return nullptr ;
102
+ }
103
+
104
+ int64_t last_texture_id () { return last_texture_id_; }
105
+
106
+ size_t textures_size () { return textures_.size (); }
107
+
55
108
private:
56
109
const uint8_t * last_data_sent_ = nullptr ;
57
110
FlutterDesktopMessageCallback last_message_callback_set_ = nullptr ;
58
111
FlutterDesktopOnRegistrarDestroyed last_destruction_callback_set_ = nullptr ;
112
+ int64_t last_texture_id_ = -1 ;
113
+ std::map<int64_t , std::unique_ptr<FakeTexture>> textures_;
59
114
};
60
115
61
116
// A PluginRegistrar whose destruction can be watched for by tests.
@@ -179,4 +234,37 @@ TEST(PluginRegistrarTest, ManagerRemovesOnDestruction) {
179
234
nullptr );
180
235
}
181
236
237
+ // Tests texture register that calls through to the C API.
238
+ TEST (MethodCallTest, RegisterTexture) {
239
+ testing::ScopedStubFlutterApi scoped_api_stub (std::make_unique<TestApi>());
240
+ auto test_api = static_cast <TestApi*>(scoped_api_stub.stub ());
241
+
242
+ auto dummy_registrar_handle =
243
+ reinterpret_cast <FlutterDesktopPluginRegistrarRef>(1 );
244
+ PluginRegistrar registrar (dummy_registrar_handle);
245
+ TextureRegistrar* textures = registrar.textures ();
246
+
247
+ EXPECT_EQ (test_api->last_texture_id (), -1 );
248
+ auto texture = test_api->GetFakeTexture (0 );
249
+ EXPECT_EQ (texture, nullptr );
250
+
251
+ int64_t texture_id = textures->RegisterTexture (reinterpret_cast <Texture*>(2 ));
252
+ EXPECT_EQ (test_api->last_texture_id (), texture_id);
253
+ EXPECT_EQ (test_api->textures_size (), static_cast <size_t >(1 ));
254
+
255
+ texture = test_api->GetFakeTexture (texture_id);
256
+ EXPECT_EQ (texture->texture_id , texture_id);
257
+ EXPECT_EQ (texture->user_data , reinterpret_cast <Texture*>(2 ));
258
+
259
+ textures->MarkTextureFrameAvailable (texture_id);
260
+ textures->MarkTextureFrameAvailable (texture_id);
261
+ textures->MarkTextureFrameAvailable (texture_id);
262
+ EXPECT_EQ (texture->mark_count , 3 );
263
+
264
+ textures->UnregisterTexture (texture_id);
265
+ texture = test_api->GetFakeTexture (texture_id);
266
+ EXPECT_EQ (texture, nullptr );
267
+ EXPECT_EQ (test_api->textures_size (), static_cast <size_t >(0 ));
268
+ }
269
+
182
270
} // namespace flutter
0 commit comments