[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" |
gab | f64a25e | 2017-05-12 19:42:56 | [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) { |
| 31 | task_runner->PostTask(FROM_HERE, base::Bind(&SetFlagTask, success)); |
| 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: |
| 58 | AutoThreadTest() : message_loop_quit_correctly_(false) { |
| 59 | } |
| 60 | |
| 61 | void RunMessageLoop() { |
| 62 | // Release |main_task_runner_|, then run |message_loop_| until other |
| 63 | // references created in tests are gone. We also post a delayed quit |
| 64 | // task to |message_loop_| so the test will not hang on failure. |
| 65 | main_task_runner_ = NULL; |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 66 | message_loop_.task_runner()->PostDelayedTask( |
| 67 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), |
| 68 | base::TimeDelta::FromSeconds(5)); |
| 69 | base::RunLoop().Run(); |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 70 | } |
| 71 | |
dcheng | 440d8e1c | 2014-10-28 01:23:15 | [diff] [blame] | 72 | void SetUp() override { |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 73 | main_task_runner_ = new AutoThreadTaskRunner( |
skyostil | 95f00086 | 2015-06-12 18:55:05 | [diff] [blame] | 74 | message_loop_.task_runner(), |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 75 | base::Bind(&AutoThreadTest::QuitMainMessageLoop, |
| 76 | base::Unretained(this))); |
| 77 | } |
| 78 | |
dcheng | 440d8e1c | 2014-10-28 01:23:15 | [diff] [blame] | 79 | void TearDown() override { |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 80 | // Verify that |message_loop_| was quit by the AutoThreadTaskRunner. |
| 81 | EXPECT_TRUE(message_loop_quit_correctly_); |
| 82 | } |
| 83 | |
| 84 | protected: |
| 85 | void QuitMainMessageLoop() { |
| 86 | message_loop_quit_correctly_ = true; |
fdoray | 2ad58be | 2016-06-22 20:36:16 | [diff] [blame] | 87 | message_loop_.task_runner()->PostTask( |
| 88 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | faea9d2d | 2013-04-30 03:18:44 | [diff] [blame] | 91 | base::MessageLoop message_loop_; |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 92 | bool message_loop_quit_correctly_; |
| 93 | scoped_refptr<AutoThreadTaskRunner> main_task_runner_; |
| 94 | }; |
| 95 | |
| 96 | TEST_F(AutoThreadTest, StartAndStop) { |
| 97 | // Create an AutoThread joined by our MessageLoop. |
| 98 | scoped_refptr<base::TaskRunner> task_runner = |
| 99 | AutoThread::Create(kThreadName, main_task_runner_); |
| 100 | EXPECT_TRUE(task_runner.get()); |
| 101 | |
| 102 | task_runner = NULL; |
| 103 | RunMessageLoop(); |
| 104 | } |
| 105 | |
| 106 | TEST_F(AutoThreadTest, ProcessTask) { |
| 107 | // Create an AutoThread joined by our MessageLoop. |
| 108 | scoped_refptr<base::TaskRunner> task_runner = |
| 109 | AutoThread::Create(kThreadName, main_task_runner_); |
| 110 | EXPECT_TRUE(task_runner.get()); |
| 111 | |
| 112 | // Post a task to it. |
| 113 | bool success = false; |
| 114 | task_runner->PostTask(FROM_HERE, base::Bind(&SetFlagTask, &success)); |
| 115 | |
| 116 | task_runner = NULL; |
| 117 | RunMessageLoop(); |
| 118 | |
| 119 | EXPECT_TRUE(success); |
| 120 | } |
| 121 | |
| 122 | TEST_F(AutoThreadTest, ThreadDependency) { |
| 123 | // Create two AutoThreads joined by our MessageLoop. |
| 124 | scoped_refptr<base::TaskRunner> task_runner1 = |
| 125 | AutoThread::Create(kThreadName, main_task_runner_); |
| 126 | EXPECT_TRUE(task_runner1.get()); |
| 127 | scoped_refptr<base::TaskRunner> task_runner2 = |
| 128 | AutoThread::Create(kThreadName, main_task_runner_); |
| 129 | EXPECT_TRUE(task_runner2.get()); |
| 130 | |
| 131 | // Post a task to thread 1 that will post a task to thread 2. |
| 132 | bool success = false; |
| 133 | task_runner1->PostTask(FROM_HERE, |
| 134 | base::Bind(&PostSetFlagTask, task_runner2, &success)); |
| 135 | |
| 136 | task_runner1 = NULL; |
| 137 | task_runner2 = NULL; |
| 138 | RunMessageLoop(); |
| 139 | |
| 140 | EXPECT_TRUE(success); |
| 141 | } |
| 142 | |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 143 | #if defined(OS_WIN) |
| 144 | TEST_F(AutoThreadTest, ThreadWithComMta) { |
| 145 | scoped_refptr<base::TaskRunner> task_runner = |
[email protected] | faea9d2d | 2013-04-30 03:18:44 | [diff] [blame] | 146 | AutoThread::CreateWithLoopAndComInitTypes(kThreadName, |
| 147 | main_task_runner_, |
| 148 | base::MessageLoop::TYPE_DEFAULT, |
| 149 | AutoThread::COM_INIT_MTA); |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 150 | EXPECT_TRUE(task_runner.get()); |
| 151 | |
| 152 | // Post a task to query the COM apartment type. |
| 153 | HRESULT hresult = E_FAIL; |
| 154 | APTTYPE apt_type = APTTYPE_NA; |
| 155 | task_runner->PostTask(FROM_HERE, |
| 156 | base::Bind(&CheckComAptTypeTask, &apt_type, &hresult)); |
| 157 | |
| 158 | task_runner = NULL; |
| 159 | RunMessageLoop(); |
| 160 | |
pmonette | dddfcf1 | 2017-06-02 22:38:43 | [diff] [blame] | 161 | EXPECT_EQ(S_OK, hresult); |
| 162 | EXPECT_EQ(APTTYPE_MTA, apt_type); |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | TEST_F(AutoThreadTest, ThreadWithComSta) { |
| 166 | scoped_refptr<base::TaskRunner> task_runner = |
[email protected] | faea9d2d | 2013-04-30 03:18:44 | [diff] [blame] | 167 | AutoThread::CreateWithLoopAndComInitTypes(kThreadName, |
| 168 | main_task_runner_, |
| 169 | base::MessageLoop::TYPE_UI, |
| 170 | AutoThread::COM_INIT_STA); |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 171 | EXPECT_TRUE(task_runner.get()); |
| 172 | |
| 173 | // Post a task to query the COM apartment type. |
| 174 | HRESULT hresult = E_FAIL; |
| 175 | APTTYPE apt_type = APTTYPE_NA; |
| 176 | task_runner->PostTask(FROM_HERE, |
| 177 | base::Bind(&CheckComAptTypeTask, &apt_type, &hresult)); |
| 178 | |
| 179 | task_runner = NULL; |
| 180 | RunMessageLoop(); |
| 181 | |
pmonette | dddfcf1 | 2017-06-02 22:38:43 | [diff] [blame] | 182 | EXPECT_EQ(S_OK, hresult); |
| 183 | // Whether the thread is the "main" STA apartment depends upon previous |
| 184 | // COM activity in this test process, so allow both types here. |
| 185 | EXPECT_TRUE(apt_type == APTTYPE_MAINSTA || apt_type == APTTYPE_STA); |
[email protected] | 6d64642 | 2012-11-28 09:06:09 | [diff] [blame] | 186 | } |
| 187 | #endif // defined(OS_WIN) |
| 188 | |
[email protected] | b35254d | 2012-10-10 03:56:12 | [diff] [blame] | 189 | } // namespace remoting |