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