blob: 224b5057aee2f94ce8a0888959511f7bfbffc2c3 [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"
gabf64a25e2017-05-12 19:42:569#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) {
31 task_runner->PostTask(FROM_HERE, base::Bind(&SetFlagTask, success));
32}
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:
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;
fdoray2ad58be2016-06-22 20:36:1666 message_loop_.task_runner()->PostDelayedTask(
67 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
68 base::TimeDelta::FromSeconds(5));
69 base::RunLoop().Run();
[email protected]b35254d2012-10-10 03:56:1270 }
71
dcheng440d8e1c2014-10-28 01:23:1572 void SetUp() override {
[email protected]b35254d2012-10-10 03:56:1273 main_task_runner_ = new AutoThreadTaskRunner(
skyostil95f000862015-06-12 18:55:0574 message_loop_.task_runner(),
[email protected]b35254d2012-10-10 03:56:1275 base::Bind(&AutoThreadTest::QuitMainMessageLoop,
76 base::Unretained(this)));
77 }
78
dcheng440d8e1c2014-10-28 01:23:1579 void TearDown() override {
[email protected]b35254d2012-10-10 03:56:1280 // 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;
fdoray2ad58be2016-06-22 20:36:1687 message_loop_.task_runner()->PostTask(
88 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
[email protected]b35254d2012-10-10 03:56:1289 }
90
[email protected]faea9d2d2013-04-30 03:18:4491 base::MessageLoop message_loop_;
[email protected]b35254d2012-10-10 03:56:1292 bool message_loop_quit_correctly_;
93 scoped_refptr<AutoThreadTaskRunner> main_task_runner_;
94};
95
96TEST_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
106TEST_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
122TEST_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]6d646422012-11-28 09:06:09143#if defined(OS_WIN)
144TEST_F(AutoThreadTest, ThreadWithComMta) {
145 scoped_refptr<base::TaskRunner> task_runner =
[email protected]faea9d2d2013-04-30 03:18:44146 AutoThread::CreateWithLoopAndComInitTypes(kThreadName,
147 main_task_runner_,
148 base::MessageLoop::TYPE_DEFAULT,
149 AutoThread::COM_INIT_MTA);
[email protected]6d646422012-11-28 09:06:09150 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
pmonettedddfcf12017-06-02 22:38:43161 EXPECT_EQ(S_OK, hresult);
162 EXPECT_EQ(APTTYPE_MTA, apt_type);
[email protected]6d646422012-11-28 09:06:09163}
164
165TEST_F(AutoThreadTest, ThreadWithComSta) {
166 scoped_refptr<base::TaskRunner> task_runner =
[email protected]faea9d2d2013-04-30 03:18:44167 AutoThread::CreateWithLoopAndComInitTypes(kThreadName,
168 main_task_runner_,
169 base::MessageLoop::TYPE_UI,
170 AutoThread::COM_INIT_STA);
[email protected]6d646422012-11-28 09:06:09171 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
pmonettedddfcf12017-06-02 22:38:43182 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]6d646422012-11-28 09:06:09186}
187#endif // defined(OS_WIN)
188
[email protected]b35254d2012-10-10 03:56:12189} // namespace remoting