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 
15 namespace flutter {
16 namespace testing {
17 
20 
21 TEST(PipelineTest, ConsumeOneVal) {
22  fml::RefPtr<IntPipeline> pipeline = fml::MakeRefCounted<IntPipeline>(2);
23 
24  Continuation continuation = pipeline->Produce();
25 
26  const int test_val = 1;
27  bool result = continuation.Complete(std::make_unique<int>(test_val));
28  ASSERT_EQ(result, true);
29 
30  PipelineConsumeResult consume_result = pipeline->Consume(
31  [&test_val](std::unique_ptr<int> v) { ASSERT_EQ(*v, test_val); });
32 
33  ASSERT_EQ(consume_result, PipelineConsumeResult::Done);
34 }
35 
36 TEST(PipelineTest, ContinuationCanOnlyBeUsedOnce) {
37  fml::RefPtr<IntPipeline> pipeline = fml::MakeRefCounted<IntPipeline>(2);
38 
39  Continuation continuation = pipeline->Produce();
40 
41  const int test_val = 1;
42  bool result = continuation.Complete(std::make_unique<int>(test_val));
43  ASSERT_EQ(result, true);
44 
45  PipelineConsumeResult consume_result_1 = pipeline->Consume(
46  [&test_val](std::unique_ptr<int> v) { ASSERT_EQ(*v, test_val); });
47 
48  result = continuation.Complete(std::make_unique<int>(test_val));
49  ASSERT_EQ(result, false);
50  ASSERT_EQ(consume_result_1, PipelineConsumeResult::Done);
51 
52  PipelineConsumeResult consume_result_2 =
53  pipeline->Consume([](std::unique_ptr<int> v) { FAIL(); });
54 
55  result = continuation.Complete(std::make_unique<int>(test_val));
56  ASSERT_EQ(result, false);
57  ASSERT_EQ(consume_result_2, PipelineConsumeResult::NoneAvailable);
58 }
59 
60 TEST(PipelineTest, PushingMoreThanDepthCompletesFirstSubmission) {
61  const int depth = 1;
62  fml::RefPtr<IntPipeline> pipeline = fml::MakeRefCounted<IntPipeline>(depth);
63 
64  Continuation continuation_1 = pipeline->Produce();
65  Continuation continuation_2 = pipeline->Produce();
66 
67  const int test_val_1 = 1, test_val_2 = 2;
68  bool result = continuation_1.Complete(std::make_unique<int>(test_val_1));
69  ASSERT_EQ(result, true);
70  result = continuation_2.Complete(std::make_unique<int>(test_val_2));
71  ASSERT_EQ(result, false);
72 
73  PipelineConsumeResult consume_result_1 = pipeline->Consume(
74  [&test_val_1](std::unique_ptr<int> v) { ASSERT_EQ(*v, test_val_1); });
75 
76  ASSERT_EQ(consume_result_1, PipelineConsumeResult::Done);
77 }
78 
79 TEST(PipelineTest, PushingMultiProcessesInOrder) {
80  const int depth = 2;
81  fml::RefPtr<IntPipeline> pipeline = fml::MakeRefCounted<IntPipeline>(depth);
82 
83  Continuation continuation_1 = pipeline->Produce();
84  Continuation continuation_2 = pipeline->Produce();
85 
86  const int test_val_1 = 1, test_val_2 = 2;
87  bool result = continuation_1.Complete(std::make_unique<int>(test_val_1));
88  ASSERT_EQ(result, true);
89  result = continuation_2.Complete(std::make_unique<int>(test_val_2));
90  ASSERT_EQ(result, true);
91 
92  PipelineConsumeResult consume_result_1 = pipeline->Consume(
93  [&test_val_1](std::unique_ptr<int> v) { ASSERT_EQ(*v, test_val_1); });
94  ASSERT_EQ(consume_result_1, PipelineConsumeResult::MoreAvailable);
95 
96  PipelineConsumeResult consume_result_2 = pipeline->Consume(
97  [&test_val_2](std::unique_ptr<int> v) { ASSERT_EQ(*v, test_val_2); });
98  ASSERT_EQ(consume_result_2, PipelineConsumeResult::Done);
99 }
100 
101 TEST(PipelineTest, ProduceIfEmptyDoesNotConsumeWhenQueueIsNotEmpty) {
102  const int depth = 2;
103  fml::RefPtr<IntPipeline> pipeline = fml::MakeRefCounted<IntPipeline>(depth);
104 
105  Continuation continuation_1 = pipeline->Produce();
106  Continuation continuation_2 = pipeline->ProduceIfEmpty();
107 
108  const int test_val_1 = 1, test_val_2 = 2;
109  bool result = continuation_1.Complete(std::make_unique<int>(test_val_1));
110  ASSERT_EQ(result, true);
111  result = continuation_2.Complete(std::make_unique<int>(test_val_2));
112  ASSERT_EQ(result, false);
113 
114  PipelineConsumeResult consume_result_1 = pipeline->Consume(
115  [&test_val_1](std::unique_ptr<int> v) { ASSERT_EQ(*v, test_val_1); });
116  ASSERT_EQ(consume_result_1, PipelineConsumeResult::Done);
117 }
118 
119 TEST(PipelineTest, ProduceIfEmptySuccessfulIfQueueIsEmpty) {
120  const int depth = 1;
121  fml::RefPtr<IntPipeline> pipeline = fml::MakeRefCounted<IntPipeline>(depth);
122 
123  Continuation continuation_1 = pipeline->ProduceIfEmpty();
124 
125  const int test_val_1 = 1;
126  bool result = continuation_1.Complete(std::make_unique<int>(test_val_1));
127  ASSERT_EQ(result, true);
128 
129  PipelineConsumeResult consume_result_1 = pipeline->Consume(
130  [&test_val_1](std::unique_ptr<int> v) { ASSERT_EQ(*v, test_val_1); });
131  ASSERT_EQ(consume_result_1, PipelineConsumeResult::Done);
132 }
133 
134 } // namespace testing
135 } // namespace flutter
PipelineConsumeResult
Definition: pipeline.h:19
ProducerContinuation ProduceIfEmpty()
Definition: pipeline.h:120
bool Complete(ResourcePtr resource)
Definition: pipeline.h:63
ProducerContinuation Produce()
Definition: pipeline.h:100
PipelineConsumeResult Consume(const Consumer &consumer)
Definition: pipeline.h:139
TEST(EmbeddedViewParams, GetBoundingRectAfterMutationsWithNoMutations)