[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
gab | f64a25e | 2017-05-12 19:42:56 | [diff] [blame] | 5 | #include "remoting/base/auto_thread.h" |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 6 | #include "base/bind.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 7 | #include "base/files/file_path.h" |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 8 | #include "base/memory/ref_counted.h" |
Alexander Timin | 4f9c35c | 2018-11-01 20:15:20 | [diff] [blame] | 9 | #include "base/message_loop/message_loop.h" |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 10 | #include "base/run_loop.h" |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 11 | #include "base/scoped_native_library.h" |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 12 | #include "base/single_thread_task_runner.h" |
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 13 | #include "build/build_config.h" |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 16 | #if defined(OS_WIN) |
| 17 | #include <objbase.h> |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 18 | #endif |
| 19 | |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | const char kThreadName[] = "Test thread"; |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 23 | |
| 24 | void SetFlagTask(bool* success) { |
| 25 | *success = true; |
| 26 | } |
| 27 | |
| 28 | void PostSetFlagTask( |
| 29 | scoped_refptr<base::TaskRunner> task_runner, |
| 30 | bool* success) { |
kylechar | 2caf4d6 | 2019-02-15 20:03:42 | [diff] [blame] | 31 | task_runner->PostTask(FROM_HERE, base::BindOnce(&SetFlagTask, success)); |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 32 | } |
| 33 | |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 34 | #if defined(OS_WIN) |
| 35 | void CheckComAptTypeTask(APTTYPE* apt_type_out, HRESULT* hresult) { |
| 36 | typedef HRESULT (WINAPI * CoGetApartmentTypeFunc) |
| 37 | (APTTYPE*, APTTYPEQUALIFIER*); |
| 38 | |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 39 | // Dynamic link to the API so the same test binary can run on older systems. |
[email protected] | 5257bcf | 2013-02-19 05:47:10 | [diff] [blame] | 40 | base::ScopedNativeLibrary com_library(base::FilePath(L"ole32.dll")); |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 41 | ASSERT_TRUE(com_library.is_valid()); |
| 42 | CoGetApartmentTypeFunc co_get_apartment_type = |
[email protected] | 994fa4b | 2014-07-15 15:19:19 | [diff] [blame] | 43 | reinterpret_cast<CoGetApartmentTypeFunc>( |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 44 | com_library.GetFunctionPointer("CoGetApartmentType")); |
| 45 | ASSERT_TRUE(co_get_apartment_type != NULL); |
| 46 | |
| 47 | APTTYPEQUALIFIER apt_type_qualifier; |
| 48 | *hresult = (*co_get_apartment_type)(apt_type_out, &apt_type_qualifier); |
| 49 | } |
| 50 | #endif |
| 51 | |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 52 | } // namespace |
| 53 | |
| 54 | namespace remoting { |
| 55 | |
| 56 | class AutoThreadTest : public testing::Test { |
| 57 | public: |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 58 | void RunMessageLoop() { |
| 59 | // Release |main_task_runner_|, then run |message_loop_| until other |
| 60 | // references created in tests are gone. We also post a delayed quit |
| 61 | // task to |message_loop_| so the test will not hang on failure. |
| 62 | main_task_runner_ = NULL; |
Wez | 8910d0d25 | 2018-09-24 00:34:11 | [diff] [blame] | 63 | base::RunLoop run_loop; |
| 64 | quit_closure_ = run_loop.QuitClosure(); |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 65 | message_loop_.task_runner()->PostDelayedTask( |
Wez | 8910d0d25 | 2018-09-24 00:34:11 | [diff] [blame] | 66 | FROM_HERE, run_loop.QuitClosure(), base::TimeDelta::FromSeconds(5)); |
| 67 | run_loop.Run(); |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 68 | } |
| 69 | |
dcheng | 440d8e1c | 2014-10-28 01:23:15 | [diff] [blame] | 70 | void SetUp() override { |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 71 | main_task_runner_ = new AutoThreadTaskRunner( |
skyostil | 95f00086 | 2015-06-12 18:55:05 | [diff] [blame] | 72 | message_loop_.task_runner(), |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 73 | base::Bind(&AutoThreadTest::QuitMainMessageLoop, |
| 74 | base::Unretained(this))); |
| 75 | } |
| 76 | |
dcheng | 440d8e1c | 2014-10-28 01:23:15 | [diff] [blame] | 77 | void TearDown() override { |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 78 | // Verify that |message_loop_| was quit by the AutoThreadTaskRunner. |
Wez | 8910d0d25 | 2018-09-24 00:34:11 | [diff] [blame] | 79 | EXPECT_FALSE(quit_closure_); |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | protected: |
Wez | 8910d0d25 | 2018-09-24 00:34:11 | [diff] [blame] | 83 | void QuitMainMessageLoop() { std::move(quit_closure_).Run(); } |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 84 | |
[email protected] | faea9d2d | 2013-04-30 03:18:44 | [diff] [blame] | 85 | base::MessageLoop message_loop_; |
Wez | 8910d0d25 | 2018-09-24 00:34:11 | [diff] [blame] | 86 | base::OnceClosure quit_closure_; |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 87 | scoped_refptr<AutoThreadTaskRunner> main_task_runner_; |
| 88 | }; |
| 89 | |
| 90 | TEST_F(AutoThreadTest, StartAndStop) { |
| 91 | // Create an AutoThread joined by our MessageLoop. |
| 92 | scoped_refptr<base::TaskRunner> task_runner = |
| 93 | AutoThread::Create(kThreadName, main_task_runner_); |
Wez | 8910d0d25 | 2018-09-24 00:34:11 | [diff] [blame] | 94 | EXPECT_TRUE(task_runner); |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 95 | |
| 96 | task_runner = NULL; |
| 97 | RunMessageLoop(); |
| 98 | } |
| 99 | |
| 100 | TEST_F(AutoThreadTest, ProcessTask) { |
| 101 | // Create an AutoThread joined by our MessageLoop. |
| 102 | scoped_refptr<base::TaskRunner> task_runner = |
| 103 | AutoThread::Create(kThreadName, main_task_runner_); |
Wez | 8910d0d25 | 2018-09-24 00:34:11 | [diff] [blame] | 104 | EXPECT_TRUE(task_runner); |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 105 | |
| 106 | // Post a task to it. |
| 107 | bool success = false; |
kylechar | 2caf4d6 | 2019-02-15 20:03:42 | [diff] [blame] | 108 | task_runner->PostTask(FROM_HERE, base::BindOnce(&SetFlagTask, &success)); |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 109 | |
| 110 | task_runner = NULL; |
| 111 | RunMessageLoop(); |
| 112 | |
| 113 | EXPECT_TRUE(success); |
| 114 | } |
| 115 | |
| 116 | TEST_F(AutoThreadTest, ThreadDependency) { |
| 117 | // Create two AutoThreads joined by our MessageLoop. |
| 118 | scoped_refptr<base::TaskRunner> task_runner1 = |
| 119 | AutoThread::Create(kThreadName, main_task_runner_); |
Wez | 8910d0d25 | 2018-09-24 00:34:11 | [diff] [blame] | 120 | EXPECT_TRUE(task_runner1); |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 121 | scoped_refptr<base::TaskRunner> task_runner2 = |
| 122 | AutoThread::Create(kThreadName, main_task_runner_); |
Wez | 8910d0d25 | 2018-09-24 00:34:11 | [diff] [blame] | 123 | EXPECT_TRUE(task_runner2); |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 124 | |
| 125 | // Post a task to thread 1 that will post a task to thread 2. |
| 126 | bool success = false; |
kylechar | 2caf4d6 | 2019-02-15 20:03:42 | [diff] [blame] | 127 | task_runner1->PostTask( |
| 128 | FROM_HERE, base::BindOnce(&PostSetFlagTask, task_runner2, &success)); |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 129 | |
| 130 | task_runner1 = NULL; |
| 131 | task_runner2 = NULL; |
| 132 | RunMessageLoop(); |
| 133 | |
| 134 | EXPECT_TRUE(success); |
| 135 | } |
| 136 | |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 137 | #if defined(OS_WIN) |
| 138 | TEST_F(AutoThreadTest, ThreadWithComMta) { |
| 139 | scoped_refptr<base::TaskRunner> task_runner = |
[email protected] | faea9d2d | 2013-04-30 03:18:44 | [diff] [blame] | 140 | AutoThread::CreateWithLoopAndComInitTypes(kThreadName, |
| 141 | main_task_runner_, |
| 142 | base::MessageLoop::TYPE_DEFAULT, |
| 143 | AutoThread::COM_INIT_MTA); |
Wez | 8910d0d25 | 2018-09-24 00:34:11 | [diff] [blame] | 144 | EXPECT_TRUE(task_runner); |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 145 | |
| 146 | // Post a task to query the COM apartment type. |
| 147 | HRESULT hresult = E_FAIL; |
| 148 | APTTYPE apt_type = APTTYPE_NA; |
kylechar | 2caf4d6 | 2019-02-15 20:03:42 | [diff] [blame] | 149 | task_runner->PostTask( |
| 150 | FROM_HERE, base::BindOnce(&CheckComAptTypeTask, &apt_type, &hresult)); |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 151 | |
| 152 | task_runner = NULL; |
| 153 | RunMessageLoop(); |
| 154 | |
pmonette | dddfcf1 | 2017-06-02 22:38:43 | [diff] [blame] | 155 | EXPECT_EQ(S_OK, hresult); |
| 156 | EXPECT_EQ(APTTYPE_MTA, apt_type); |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | TEST_F(AutoThreadTest, ThreadWithComSta) { |
| 160 | scoped_refptr<base::TaskRunner> task_runner = |
[email protected] | faea9d2d | 2013-04-30 03:18:44 | [diff] [blame] | 161 | AutoThread::CreateWithLoopAndComInitTypes(kThreadName, |
| 162 | main_task_runner_, |
| 163 | base::MessageLoop::TYPE_UI, |
| 164 | AutoThread::COM_INIT_STA); |
Wez | 8910d0d25 | 2018-09-24 00:34:11 | [diff] [blame] | 165 | EXPECT_TRUE(task_runner); |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 166 | |
| 167 | // Post a task to query the COM apartment type. |
| 168 | HRESULT hresult = E_FAIL; |
| 169 | APTTYPE apt_type = APTTYPE_NA; |
kylechar | 2caf4d6 | 2019-02-15 20:03:42 | [diff] [blame] | 170 | task_runner->PostTask( |
| 171 | FROM_HERE, base::BindOnce(&CheckComAptTypeTask, &apt_type, &hresult)); |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 172 | |
| 173 | task_runner = NULL; |
| 174 | RunMessageLoop(); |
| 175 | |
pmonette | dddfcf1 | 2017-06-02 22:38:43 | [diff] [blame] | 176 | EXPECT_EQ(S_OK, hresult); |
| 177 | // Whether the thread is the "main" STA apartment depends upon previous |
| 178 | // COM activity in this test process, so allow both types here. |
| 179 | EXPECT_TRUE(apt_type == APTTYPE_MAINSTA || apt_type == APTTYPE_STA); |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 180 | } |
| 181 | #endif // defined(OS_WIN) |
| 182 | |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 183 | } // namespace remoting |