Flutter Engine
The Flutter Engine
pipeline_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#define FML_USED_ON_EMBEDDER
6
7#include "flutter/shell/common/pipeline.h"
8
9#include <functional>
10#include <future>
11#include <memory>
12
13#include "gtest/gtest.h"
14
15namespace flutter {
16namespace testing {
17
20
21TEST(PipelineTest, ConsumeOneVal) {
22 std::shared_ptr<IntPipeline> pipeline = std::make_shared<IntPipeline>(2);
23
24 Continuation continuation = pipeline->Produce();
25
26 const int test_val = 1;
28 continuation.Complete(std::make_unique<int>(test_val));
29 ASSERT_EQ(result.success, true);
30 ASSERT_EQ(result.is_first_item, true);
31
32 PipelineConsumeResult consume_result = pipeline->Consume(
33 [&test_val](std::unique_ptr<int> v) { ASSERT_EQ(*v, test_val); });
34
35 ASSERT_EQ(consume_result, PipelineConsumeResult::Done);
36}
37
38TEST(PipelineTest, ContinuationCanOnlyBeUsedOnce) {
39 std::shared_ptr<IntPipeline> pipeline = std::make_shared<IntPipeline>(2);
40
41 Continuation continuation = pipeline->Produce();
42
43 const int test_val = 1;
45 continuation.Complete(std::make_unique<int>(test_val));
46 ASSERT_EQ(result.success, true);
47 ASSERT_EQ(result.is_first_item, true);
48
49 PipelineConsumeResult consume_result_1 = pipeline->Consume(
50 [&test_val](std::unique_ptr<int> v) { ASSERT_EQ(*v, test_val); });
51
52 result = continuation.Complete(std::make_unique<int>(test_val));
53 ASSERT_EQ(result.success, false);
54 ASSERT_EQ(consume_result_1, PipelineConsumeResult::Done);
55
56 PipelineConsumeResult consume_result_2 =
57 pipeline->Consume([](std::unique_ptr<int> v) { FAIL(); });
58
59 result = continuation.Complete(std::make_unique<int>(test_val));
60 ASSERT_EQ(result.success, false);
61 ASSERT_EQ(consume_result_2, PipelineConsumeResult::NoneAvailable);
62}
63
64TEST(PipelineTest, PushingMoreThanDepthCompletesFirstSubmission) {
65 const int depth = 1;
66 std::shared_ptr<IntPipeline> pipeline = std::make_shared<IntPipeline>(depth);
67
68 Continuation continuation_1 = pipeline->Produce();
69 Continuation continuation_2 = pipeline->Produce();
70
71 const int test_val_1 = 1, test_val_2 = 2;
73 continuation_1.Complete(std::make_unique<int>(test_val_1));
74 ASSERT_EQ(result.success, true);
75 ASSERT_EQ(result.is_first_item, true);
76 result = continuation_2.Complete(std::make_unique<int>(test_val_2));
77 ASSERT_EQ(result.success, false);
78
79 PipelineConsumeResult consume_result_1 = pipeline->Consume(
80 [&test_val_1](std::unique_ptr<int> v) { ASSERT_EQ(*v, test_val_1); });
81
82 ASSERT_EQ(consume_result_1, PipelineConsumeResult::Done);
83}
84
85TEST(PipelineTest, PushingMultiProcessesInOrder) {
86 const int depth = 2;
87 std::shared_ptr<IntPipeline> pipeline = std::make_shared<IntPipeline>(depth);
88
89 Continuation continuation_1 = pipeline->Produce();
90 Continuation continuation_2 = pipeline->Produce();
91
92 const int test_val_1 = 1, test_val_2 = 2;
94 continuation_1.Complete(std::make_unique<int>(test_val_1));
95 ASSERT_EQ(result.success, true);
96 ASSERT_EQ(result.is_first_item, true);
97 result = continuation_2.Complete(std::make_unique<int>(test_val_2));
98 ASSERT_EQ(result.success, true);
99 ASSERT_EQ(result.is_first_item, false);
100
101 PipelineConsumeResult consume_result_1 = pipeline->Consume(
102 [&test_val_1](std::unique_ptr<int> v) { ASSERT_EQ(*v, test_val_1); });
103 ASSERT_EQ(consume_result_1, PipelineConsumeResult::MoreAvailable);
104
105 PipelineConsumeResult consume_result_2 = pipeline->Consume(
106 [&test_val_2](std::unique_ptr<int> v) { ASSERT_EQ(*v, test_val_2); });
107 ASSERT_EQ(consume_result_2, PipelineConsumeResult::Done);
108}
109
110TEST(PipelineTest, ProduceIfEmptyDoesNotConsumeWhenQueueIsNotEmpty) {
111 const int depth = 2;
112 std::shared_ptr<IntPipeline> pipeline = std::make_shared<IntPipeline>(depth);
113
114 Continuation continuation_1 = pipeline->Produce();
115 Continuation continuation_2 = pipeline->ProduceIfEmpty();
116
117 const int test_val_1 = 1, test_val_2 = 2;
119 continuation_1.Complete(std::make_unique<int>(test_val_1));
120 ASSERT_EQ(result.success, true);
121 ASSERT_EQ(result.is_first_item, true);
122 result = continuation_2.Complete(std::make_unique<int>(test_val_2));
123 ASSERT_EQ(result.success, false);
124
125 PipelineConsumeResult consume_result_1 = pipeline->Consume(
126 [&test_val_1](std::unique_ptr<int> v) { ASSERT_EQ(*v, test_val_1); });
127 ASSERT_EQ(consume_result_1, PipelineConsumeResult::Done);
128}
129
130TEST(PipelineTest, ProduceIfEmptySuccessfulIfQueueIsEmpty) {
131 const int depth = 1;
132 std::shared_ptr<IntPipeline> pipeline = std::make_shared<IntPipeline>(depth);
133
134 Continuation continuation_1 = pipeline->ProduceIfEmpty();
135
136 const int test_val_1 = 1;
138 continuation_1.Complete(std::make_unique<int>(test_val_1));
139 ASSERT_EQ(result.success, true);
140 ASSERT_EQ(result.is_first_item, true);
141
142 PipelineConsumeResult consume_result_1 = pipeline->Consume(
143 [&test_val_1](std::unique_ptr<int> v) { ASSERT_EQ(*v, test_val_1); });
144 ASSERT_EQ(consume_result_1, PipelineConsumeResult::Done);
145}
146
147} // namespace testing
148} // namespace flutter
PipelineProduceResult Complete(ResourcePtr resource)
Completes the continuation with the specified resource.
Definition: pipeline.h:98
#define FAIL(name, result)
GAsyncResult * result
TEST(DisplayListComplexity, EmptyDisplayList)
PipelineConsumeResult
Definition: pipeline.h:29