blob: 350cc785d0c930638e16980a3f59942ffc42009f [file] [log] [blame]
[email protected]b35254d2012-10-10 03:56:121// 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
gabf64a25e2017-05-12 19:42:565#include "remoting/base/auto_thread.h"
[email protected]b35254d2012-10-10 03:56:126#include "base/bind.h"
[email protected]57999812013-02-24 05:40:527#include "base/files/file_path.h"
[email protected]b35254d2012-10-10 03:56:128#include "base/memory/ref_counted.h"
Alexander Timin4f9c35c2018-11-01 20:15:209#include "base/message_loop/message_loop.h"
fdoray2ad58be2016-06-22 20:36:1610#include "base/run_loop.h"
[email protected]6d646422012-11-28 09:06:0911#include "base/scoped_native_library.h"
fdoray2ad58be2016-06-22 20:36:1612#include "base/single_thread_task_runner.h"
avi5a080f012015-12-22 23:15:4313#include "build/build_config.h"
[email protected]b35254d2012-10-10 03:56:1214#include "testing/gtest/include/gtest/gtest.h"
15
[email protected]6d646422012-11-28 09:06:0916#if defined(OS_WIN)
17#include <objbase.h>
[email protected]6d646422012-11-28 09:06:0918#endif
19
[email protected]b35254d2012-10-10 03:56:1220namespace {
21
22const char kThreadName[] = "Test thread";
[email protected]b35254d2012-10-10 03:56:1223
24void SetFlagTask(bool* success) {
25 *success = true;
26}
27
28void PostSetFlagTask(
29 scoped_refptr<base::TaskRunner> task_runner,
30 bool* success) {
kylechar2caf4d62019-02-15 20:03:4231 task_runner->PostTask(FROM_HERE, base::BindOnce(&SetFlagTask, success));
[email protected]b35254d2012-10-10 03:56:1232}
33
[email protected]6d646422012-11-28 09:06:0934#if defined(OS_WIN)
35void CheckComAptTypeTask(APTTYPE* apt_type_out, HRESULT* hresult) {
36 typedef HRESULT (WINAPI * CoGetApartmentTypeFunc)
37 (APTTYPE*, APTTYPEQUALIFIER*);
38
[email protected]6d646422012-11-28 09:06:0939 // Dynamic link to the API so the same test binary can run on older systems.
[email protected]5257bcf2013-02-19 05:47:1040 base::ScopedNativeLibrary com_library(base::FilePath(L"ole32.dll"));
[email protected]6d646422012-11-28 09:06:0941 ASSERT_TRUE(com_library.is_valid());
42 CoGetApartmentTypeFunc co_get_apartment_type =
[email protected]994fa4b2014-07-15 15:19:1943 reinterpret_cast<CoGetApartmentTypeFunc>(
[email protected]6d646422012-11-28 09:06:0944 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]b35254d2012-10-10 03:56:1252} // namespace
53
54namespace remoting {
55
56class AutoThreadTest : public testing::Test {
57 public:
[email protected]b35254d2012-10-10 03:56:1258 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;
Wez8910d0d252018-09-24 00:34:1163 base::RunLoop run_loop;
64 quit_closure_ = run_loop.QuitClosure();
fdoray2ad58be2016-06-22 20:36:1665 message_loop_.task_runner()->PostDelayedTask(
Wez8910d0d252018-09-24 00:34:1166 FROM_HERE, run_loop.QuitClosure(), base::TimeDelta::FromSeconds(5));
67 run_loop.Run();
[email protected]b35254d2012-10-10 03:56:1268 }
69
dcheng440d8e1c2014-10-28 01:23:1570 void SetUp() override {
[email protected]b35254d2012-10-10 03:56:1271 main_task_runner_ = new AutoThreadTaskRunner(
skyostil95f000862015-06-12 18:55:0572 message_loop_.task_runner(),
[email protected]b35254d2012-10-10 03:56:1273 base::Bind(&AutoThreadTest::QuitMainMessageLoop,
74 base::Unretained(this)));
75 }
76
dcheng440d8e1c2014-10-28 01:23:1577 void TearDown() override {
[email protected]b35254d2012-10-10 03:56:1278 // Verify that |message_loop_| was quit by the AutoThreadTaskRunner.
Wez8910d0d252018-09-24 00:34:1179 EXPECT_FALSE(quit_closure_);
[email protected]b35254d2012-10-10 03:56:1280 }
81
82 protected:
Wez8910d0d252018-09-24 00:34:1183 void QuitMainMessageLoop() { std::move(quit_closure_).Run(); }
[email protected]b35254d2012-10-10 03:56:1284
[email protected]faea9d2d2013-04-30 03:18:4485 base::MessageLoop message_loop_;
Wez8910d0d252018-09-24 00:34:1186 base::OnceClosure quit_closure_;
[email protected]b35254d2012-10-10 03:56:1287 scoped_refptr<AutoThreadTaskRunner> main_task_runner_;
88};
89
90TEST_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_);
Wez8910d0d252018-09-24 00:34:1194 EXPECT_TRUE(task_runner);
[email protected]b35254d2012-10-10 03:56:1295
96 task_runner = NULL;
97 RunMessageLoop();
98}
99
100TEST_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_);
Wez8910d0d252018-09-24 00:34:11104 EXPECT_TRUE(task_runner);
[email protected]b35254d2012-10-10 03:56:12105
106 // Post a task to it.
107 bool success = false;
kylechar2caf4d62019-02-15 20:03:42108 task_runner->PostTask(FROM_HERE, base::BindOnce(&SetFlagTask, &success));
[email protected]b35254d2012-10-10 03:56:12109
110 task_runner = NULL;
111 RunMessageLoop();
112
113 EXPECT_TRUE(success);
114}
115
116TEST_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_);
Wez8910d0d252018-09-24 00:34:11120 EXPECT_TRUE(task_runner1);
[email protected]b35254d2012-10-10 03:56:12121 scoped_refptr<base::TaskRunner> task_runner2 =
122 AutoThread::Create(kThreadName, main_task_runner_);
Wez8910d0d252018-09-24 00:34:11123 EXPECT_TRUE(task_runner2);
[email protected]b35254d2012-10-10 03:56:12124
125 // Post a task to thread 1 that will post a task to thread 2.
126 bool success = false;
kylechar2caf4d62019-02-15 20:03:42127 task_runner1->PostTask(
128 FROM_HERE, base::BindOnce(&PostSetFlagTask, task_runner2, &success));
[email protected]b35254d2012-10-10 03:56:12129
130 task_runner1 = NULL;
131 task_runner2 = NULL;
132 RunMessageLoop();
133
134 EXPECT_TRUE(success);
135}
136
[email protected]6d646422012-11-28 09:06:09137#if defined(OS_WIN)
138TEST_F(AutoThreadTest, ThreadWithComMta) {
139 scoped_refptr<base::TaskRunner> task_runner =
[email protected]faea9d2d2013-04-30 03:18:44140 AutoThread::CreateWithLoopAndComInitTypes(kThreadName,
141 main_task_runner_,
142 base::MessageLoop::TYPE_DEFAULT,
143 AutoThread::COM_INIT_MTA);
Wez8910d0d252018-09-24 00:34:11144 EXPECT_TRUE(task_runner);
[email protected]6d646422012-11-28 09:06:09145
146 // Post a task to query the COM apartment type.
147 HRESULT hresult = E_FAIL;
148 APTTYPE apt_type = APTTYPE_NA;
kylechar2caf4d62019-02-15 20:03:42149 task_runner->PostTask(
150 FROM_HERE, base::BindOnce(&CheckComAptTypeTask, &apt_type, &hresult));
[email protected]6d646422012-11-28 09:06:09151
152 task_runner = NULL;
153 RunMessageLoop();
154
pmonettedddfcf12017-06-02 22:38:43155 EXPECT_EQ(S_OK, hresult);
156 EXPECT_EQ(APTTYPE_MTA, apt_type);
[email protected]6d646422012-11-28 09:06:09157}
158
159TEST_F(AutoThreadTest, ThreadWithComSta) {
160 scoped_refptr<base::TaskRunner> task_runner =
[email protected]faea9d2d2013-04-30 03:18:44161 AutoThread::CreateWithLoopAndComInitTypes(kThreadName,
162 main_task_runner_,
163 base::MessageLoop::TYPE_UI,
164 AutoThread::COM_INIT_STA);
Wez8910d0d252018-09-24 00:34:11165 EXPECT_TRUE(task_runner);
[email protected]6d646422012-11-28 09:06:09166
167 // Post a task to query the COM apartment type.
168 HRESULT hresult = E_FAIL;
169 APTTYPE apt_type = APTTYPE_NA;
kylechar2caf4d62019-02-15 20:03:42170 task_runner->PostTask(
171 FROM_HERE, base::BindOnce(&CheckComAptTypeTask, &apt_type, &hresult));
[email protected]6d646422012-11-28 09:06:09172
173 task_runner = NULL;
174 RunMessageLoop();
175
pmonettedddfcf12017-06-02 22:38:43176 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]6d646422012-11-28 09:06:09180}
181#endif // defined(OS_WIN)
182
[email protected]b35254d2012-10-10 03:56:12183} // namespace remoting