Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
texture_unittests.cc
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#include "flutter/common/graphics/texture.h"
6
7#include <functional>
8
9#include "flutter/flow/testing/mock_texture.h"
10#include "gmock/gmock.h"
11#include "gtest/gtest.h"
12
13namespace flutter {
14namespace testing {
15
16namespace {
17using int_closure = std::function<void(int)>;
18
19struct TestContextListener : public ContextListener {
20 TestContextListener(uintptr_t p_id,
21 int_closure p_create,
22 int_closure p_destroy)
23 : id(p_id), create(std::move(p_create)), destroy(std::move(p_destroy)) {}
24
25 virtual ~TestContextListener() = default;
26
27 const uintptr_t id;
28 int_closure create;
29 int_closure destroy;
30
31 void OnGrContextCreated() override { create(id); }
32
33 void OnGrContextDestroyed() override { destroy(id); }
34};
35} // namespace
36
37TEST(TextureRegistryTest, UnregisterTextureCallbackTriggered) {
38 TextureRegistry registry;
39 auto mock_texture1 = std::make_shared<MockTexture>(0);
40 auto mock_texture2 = std::make_shared<MockTexture>(1);
41
42 registry.RegisterTexture(mock_texture1);
43 registry.RegisterTexture(mock_texture2);
44 ASSERT_EQ(registry.GetTexture(0), mock_texture1);
45 ASSERT_EQ(registry.GetTexture(1), mock_texture2);
46 ASSERT_FALSE(mock_texture1->unregistered());
47 ASSERT_FALSE(mock_texture2->unregistered());
48
49 registry.UnregisterTexture(0);
50 ASSERT_EQ(registry.GetTexture(0), nullptr);
51 ASSERT_TRUE(mock_texture1->unregistered());
52 ASSERT_FALSE(mock_texture2->unregistered());
53
54 registry.UnregisterTexture(1);
55 ASSERT_EQ(registry.GetTexture(1), nullptr);
56 ASSERT_TRUE(mock_texture1->unregistered());
57 ASSERT_TRUE(mock_texture2->unregistered());
58}
59
60TEST(TextureRegistryTest, GrContextCallbackTriggered) {
61 TextureRegistry registry;
62 auto mock_texture1 = std::make_shared<MockTexture>(0);
63 auto mock_texture2 = std::make_shared<MockTexture>(1);
64
65 registry.RegisterTexture(mock_texture1);
66 registry.RegisterTexture(mock_texture2);
67 ASSERT_FALSE(mock_texture1->gr_context_created());
68 ASSERT_FALSE(mock_texture2->gr_context_created());
69 ASSERT_FALSE(mock_texture1->gr_context_destroyed());
70 ASSERT_FALSE(mock_texture2->gr_context_destroyed());
71
72 registry.OnGrContextCreated();
73 ASSERT_TRUE(mock_texture1->gr_context_created());
74 ASSERT_TRUE(mock_texture2->gr_context_created());
75
76 registry.UnregisterTexture(0);
77 registry.OnGrContextDestroyed();
78 ASSERT_FALSE(mock_texture1->gr_context_destroyed());
79 ASSERT_TRUE(mock_texture2->gr_context_created());
80}
81
82TEST(TextureRegistryTest, RegisterTextureTwice) {
83 TextureRegistry registry;
84 auto mock_texture1 = std::make_shared<MockTexture>(0);
85 auto mock_texture2 = std::make_shared<MockTexture>(0);
86
87 registry.RegisterTexture(mock_texture1);
88 ASSERT_EQ(registry.GetTexture(0), mock_texture1);
89 registry.RegisterTexture(mock_texture2);
90 ASSERT_EQ(registry.GetTexture(0), mock_texture2);
91 ASSERT_FALSE(mock_texture1->unregistered());
92 ASSERT_FALSE(mock_texture2->unregistered());
93
94 registry.UnregisterTexture(0);
95 ASSERT_EQ(registry.GetTexture(0), nullptr);
96 ASSERT_FALSE(mock_texture1->unregistered());
97 ASSERT_TRUE(mock_texture2->unregistered());
98}
99
100TEST(TextureRegistryTest, ReuseSameTextureSlot) {
101 TextureRegistry registry;
102 auto mock_texture1 = std::make_shared<MockTexture>(0);
103 auto mock_texture2 = std::make_shared<MockTexture>(0);
104
105 registry.RegisterTexture(mock_texture1);
106 ASSERT_EQ(registry.GetTexture(0), mock_texture1);
107
108 registry.UnregisterTexture(0);
109 ASSERT_EQ(registry.GetTexture(0), nullptr);
110 ASSERT_TRUE(mock_texture1->unregistered());
111 ASSERT_FALSE(mock_texture2->unregistered());
112
113 registry.RegisterTexture(mock_texture2);
114 ASSERT_EQ(registry.GetTexture(0), mock_texture2);
115
116 registry.UnregisterTexture(0);
117 ASSERT_EQ(registry.GetTexture(0), nullptr);
118 ASSERT_TRUE(mock_texture1->unregistered());
119 ASSERT_TRUE(mock_texture2->unregistered());
120}
121
122TEST(TextureRegistryTest, CallsOnGrContextCreatedInInsertionOrder) {
123 TextureRegistry registry;
124 std::vector<int> create_order;
125 std::vector<int> destroy_order;
126 auto create = [&](int id) { create_order.push_back(id); };
127 auto destroy = [&](int id) { destroy_order.push_back(id); };
128 auto a = std::make_shared<TestContextListener>(5, create, destroy);
129 auto b = std::make_shared<TestContextListener>(4, create, destroy);
130 auto c = std::make_shared<TestContextListener>(3, create, destroy);
131 registry.RegisterContextListener(a->id, a);
132 registry.RegisterContextListener(b->id, b);
133 registry.RegisterContextListener(c->id, c);
134 registry.OnGrContextDestroyed();
135 registry.OnGrContextCreated();
136
137 EXPECT_THAT(create_order, ::testing::ElementsAre(5, 4, 3));
138 EXPECT_THAT(destroy_order, ::testing::ElementsAre(5, 4, 3));
139}
140
141} // namespace testing
142} // namespace flutter
#define TEST(S, s, D, expected)
void RegisterTexture(const std::shared_ptr< Texture > &texture)
Definition texture.cc:19
std::shared_ptr< Texture > GetTexture(int64_t id)
Definition texture.cc:93
void UnregisterTexture(int64_t id)
Definition texture.cc:38
void RegisterContextListener(uintptr_t id, std::weak_ptr< ContextListener > image)
Definition texture.cc:26
static bool b
struct MyStruct a[10]
Definition ref_ptr.h:256
const uintptr_t id
int_closure destroy