OLD | NEW |
| (Empty) |
1 // Copyright 2017 The Chromium 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 "ui/ozone/platform/drm/gpu/proxy_helpers.h" | |
6 #include "base/message_loop/message_loop.h" | |
7 #include "base/run_loop.h" | |
8 #include "base/threading/thread.h" | |
9 #include "base/threading/thread_checker_impl.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace ui { | |
13 | |
14 class ProxyHelpersTest : public testing::Test { | |
15 public: | |
16 void SetUp() override { | |
17 drm_thread_.reset(new base::Thread("drm_thread")); | |
18 drm_thread_->Start(); | |
19 } | |
20 | |
21 void TearDown() override { | |
22 drm_thread_->Stop(); | |
23 drm_thread_ = nullptr; | |
24 } | |
25 | |
26 // QuitFunction runs on the DRM thread. | |
27 void QuitFunction(int a) { | |
28 EXPECT_TRUE(drm_checker_.CalledOnValidThread()); | |
29 EXPECT_FALSE(main_checker_.CalledOnValidThread()); | |
30 | |
31 message_loop_.task_runner()->PostTask( | |
32 FROM_HERE, base::Bind(&ProxyHelpersTest::QuitFunctionCallback, | |
33 base::Unretained(this), 8)); | |
34 } | |
35 | |
36 // QuitFunctionCallback runs on the main thread. | |
37 void QuitFunctionCallback(int a) { | |
38 EXPECT_FALSE(drm_checker_.CalledOnValidThread()); | |
39 EXPECT_TRUE(main_checker_.CalledOnValidThread()); | |
40 | |
41 auto quitter = run_loop_.QuitWhenIdleClosure(); | |
42 message_loop_.task_runner()->PostTask(FROM_HERE, quitter); | |
43 } | |
44 | |
45 void SetDrmChecker() { drm_checker_.DetachFromThread(); } | |
46 | |
47 void MoveType(int a, | |
48 base::OnceCallback<void(std::unique_ptr<int>)> callback) { | |
49 EXPECT_TRUE(drm_checker_.CalledOnValidThread()); | |
50 EXPECT_FALSE(main_checker_.CalledOnValidThread()); | |
51 | |
52 std::unique_ptr<int> p(new int); | |
53 *p = a + 1; | |
54 | |
55 std::move(callback).Run(std::move(p)); | |
56 } | |
57 | |
58 void MoveTypeCallback(std::unique_ptr<int> p) { | |
59 EXPECT_FALSE(drm_checker_.CalledOnValidThread()); | |
60 EXPECT_TRUE(main_checker_.CalledOnValidThread()); | |
61 | |
62 *p = *p + 1; | |
63 move_type_.swap(p); | |
64 EXPECT_EQ(*p, 50); | |
65 } | |
66 | |
67 void ValueType(int a, base::OnceCallback<void(int)> callback) { | |
68 EXPECT_TRUE(drm_checker_.CalledOnValidThread()); | |
69 EXPECT_FALSE(main_checker_.CalledOnValidThread()); | |
70 | |
71 std::move(callback).Run(a + 1); | |
72 } | |
73 | |
74 void ValueTypeCallback(int a) { | |
75 EXPECT_FALSE(drm_checker_.CalledOnValidThread()); | |
76 EXPECT_TRUE(main_checker_.CalledOnValidThread()); | |
77 | |
78 value_type_ = a + 1; | |
79 } | |
80 | |
81 void StringType(std::string a, | |
82 base::OnceCallback<void(std::string)> callback) { | |
83 EXPECT_TRUE(drm_checker_.CalledOnValidThread()); | |
84 EXPECT_FALSE(main_checker_.CalledOnValidThread()); | |
85 std::move(callback).Run(a.append("e")); | |
86 } | |
87 | |
88 void StringTypeCallback(std::string a) { | |
89 EXPECT_FALSE(drm_checker_.CalledOnValidThread()); | |
90 EXPECT_TRUE(main_checker_.CalledOnValidThread()); | |
91 derived_string_ = a.append("r"); | |
92 } | |
93 | |
94 protected: | |
95 // Main thread message loop. | |
96 base::MessageLoop message_loop_; | |
97 base::RunLoop run_loop_; | |
98 | |
99 // Thread to simulate the drm thread in ozone viz process. | |
100 std::unique_ptr<base::Thread> drm_thread_; | |
101 | |
102 base::ThreadChecker main_checker_; | |
103 base::ThreadChecker drm_checker_; | |
104 | |
105 // Variables to record operation. | |
106 int value_type_ = 0; | |
107 std::unique_ptr<int> move_type_; | |
108 std::string original_string_; | |
109 std::string derived_string_; | |
110 }; | |
111 | |
112 TEST_F(ProxyHelpersTest, PostTask) { | |
113 // Binds the thread checker on the drm thread. | |
114 drm_thread_->task_runner()->PostTask( | |
115 FROM_HERE, | |
116 base::Bind(&ProxyHelpersTest::SetDrmChecker, base::Unretained(this))); | |
117 | |
118 // Test passing a type by value. | |
119 auto value_callback = base::BindOnce(&ProxyHelpersTest::ValueTypeCallback, | |
120 base::Unretained(this)); | |
121 auto safe_value_callback = CreateSafeOnceCallback(std::move(value_callback)); | |
122 | |
123 drm_thread_->task_runner()->PostTask( | |
124 FROM_HERE, | |
125 base::BindOnce(&ProxyHelpersTest::ValueType, base::Unretained(this), 100, | |
126 std::move(safe_value_callback))); | |
127 | |
128 // Test passing a move-only type. | |
129 move_type_.reset(new int); | |
130 *move_type_ = 50; | |
131 | |
132 auto move_callback = base::BindOnce(&ProxyHelpersTest::MoveTypeCallback, | |
133 base::Unretained(this)); | |
134 auto safe_move_callback = CreateSafeOnceCallback(std::move(move_callback)); | |
135 | |
136 drm_thread_->task_runner()->PostTask( | |
137 FROM_HERE, | |
138 base::BindOnce(&ProxyHelpersTest::MoveType, base::Unretained(this), 100, | |
139 std::move(safe_move_callback))); | |
140 | |
141 // Test that passing a type that supports both move and value semantics | |
142 // defaults to value. | |
143 original_string_ = "This is a string"; | |
144 | |
145 auto string_callback = base::BindOnce(&ProxyHelpersTest::StringTypeCallback, | |
146 base::Unretained(this)); | |
147 auto safe_string_callback = | |
148 CreateSafeOnceCallback(std::move(string_callback)); | |
149 | |
150 drm_thread_->task_runner()->PostTask( | |
151 FROM_HERE, | |
152 base::BindOnce(&ProxyHelpersTest::StringType, base::Unretained(this), | |
153 original_string_, std::move(safe_string_callback))); | |
154 | |
155 // Shutdown the RunLoop. | |
156 drm_thread_->task_runner()->PostTask( | |
157 FROM_HERE, | |
158 base::Bind(&ProxyHelpersTest::QuitFunction, base::Unretained(this), 42)); | |
159 | |
160 run_loop_.Run(); | |
161 | |
162 EXPECT_EQ(value_type_, 102); | |
163 EXPECT_EQ(*move_type_, 102); | |
164 | |
165 EXPECT_TRUE(original_string_ == "This is a string"); | |
166 EXPECT_TRUE(derived_string_ == "This is a stringer"); | |
167 } | |
168 } // namespace ui | |
OLD | NEW |