Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: remoting/base/auto_thread_unittest.cc

Issue 11348087: Add AutoThread types for Windows that initialize COM. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add missing OS checks. Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « remoting/base/auto_thread.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/memory/ref_counted.h" 6 #include "base/memory/ref_counted.h"
7 #include "remoting/base/auto_thread.h" 7 #include "remoting/base/auto_thread.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 9
10 #if defined(OS_WIN)
11 #include <objbase.h>
12 #include "base/win/windows_version.h"
13 #endif
14
10 namespace { 15 namespace {
11 16
12 const char kThreadName[] = "Test thread"; 17 const char kThreadName[] = "Test thread";
13 const char kThreadName2[] = "Test thread 2"; 18 const char kThreadName2[] = "Test thread 2";
14 19
15 void SetFlagTask(bool* success) { 20 void SetFlagTask(bool* success) {
16 *success = true; 21 *success = true;
17 } 22 }
18 23
19 void PostSetFlagTask( 24 void PostSetFlagTask(
20 scoped_refptr<base::TaskRunner> task_runner, 25 scoped_refptr<base::TaskRunner> task_runner,
21 bool* success) { 26 bool* success) {
22 task_runner->PostTask(FROM_HERE, base::Bind(&SetFlagTask, success)); 27 task_runner->PostTask(FROM_HERE, base::Bind(&SetFlagTask, success));
23 } 28 }
24 29
30 #if defined(OS_WIN)
31 void CheckComAptTypeTask(APTTYPE* apt_type_out, HRESULT* hresult) {
32 APTTYPEQUALIFIER apt_type_qualifier;
33 *hresult = CoGetApartmentType(apt_type_out, &apt_type_qualifier);
34 }
35 #endif
36
25 } // namespace 37 } // namespace
26 38
27 namespace remoting { 39 namespace remoting {
28 40
29 class AutoThreadTest : public testing::Test { 41 class AutoThreadTest : public testing::Test {
30 public: 42 public:
31 AutoThreadTest() : message_loop_quit_correctly_(false) { 43 AutoThreadTest() : message_loop_quit_correctly_(false) {
32 } 44 }
33 45
34 void RunMessageLoop() { 46 void RunMessageLoop() {
(...skipping 17 matching lines...) Expand all
52 // Verify that |message_loop_| was quit by the AutoThreadTaskRunner. 64 // Verify that |message_loop_| was quit by the AutoThreadTaskRunner.
53 EXPECT_TRUE(message_loop_quit_correctly_); 65 EXPECT_TRUE(message_loop_quit_correctly_);
54 } 66 }
55 67
56 protected: 68 protected:
57 void QuitMainMessageLoop() { 69 void QuitMainMessageLoop() {
58 message_loop_quit_correctly_ = true; 70 message_loop_quit_correctly_ = true;
59 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure()); 71 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure());
60 } 72 }
61 73
62
63 MessageLoop message_loop_; 74 MessageLoop message_loop_;
64 bool message_loop_quit_correctly_; 75 bool message_loop_quit_correctly_;
65 scoped_refptr<AutoThreadTaskRunner> main_task_runner_; 76 scoped_refptr<AutoThreadTaskRunner> main_task_runner_;
66 }; 77 };
67 78
68 TEST_F(AutoThreadTest, StartAndStop) { 79 TEST_F(AutoThreadTest, StartAndStop) {
69 // Create an AutoThread joined by our MessageLoop. 80 // Create an AutoThread joined by our MessageLoop.
70 scoped_refptr<base::TaskRunner> task_runner = 81 scoped_refptr<base::TaskRunner> task_runner =
71 AutoThread::Create(kThreadName, main_task_runner_); 82 AutoThread::Create(kThreadName, main_task_runner_);
72 EXPECT_TRUE(task_runner.get()); 83 EXPECT_TRUE(task_runner.get());
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 task_runner1->PostTask(FROM_HERE, 116 task_runner1->PostTask(FROM_HERE,
106 base::Bind(&PostSetFlagTask, task_runner2, &success)); 117 base::Bind(&PostSetFlagTask, task_runner2, &success));
107 118
108 task_runner1 = NULL; 119 task_runner1 = NULL;
109 task_runner2 = NULL; 120 task_runner2 = NULL;
110 RunMessageLoop(); 121 RunMessageLoop();
111 122
112 EXPECT_TRUE(success); 123 EXPECT_TRUE(success);
113 } 124 }
114 125
126 #if defined(OS_WIN)
127 TEST_F(AutoThreadTest, ThreadWithComMta) {
128 // CoGetApartmentType requires Windows 7 or above.
129 if (base::win::GetVersion() < base::win::VERSION_WIN7)
130 return;
131
132 scoped_refptr<base::TaskRunner> task_runner =
133 AutoThread::CreateWithLoopAndComInitTypes(
134 kThreadName, main_task_runner_, MessageLoop::TYPE_DEFAULT,
135 AutoThread::COM_INIT_MTA);
136 EXPECT_TRUE(task_runner.get());
137
138 // Post a task to query the COM apartment type.
139 HRESULT hresult = E_FAIL;
140 APTTYPE apt_type = APTTYPE_NA;
141 task_runner->PostTask(FROM_HERE,
142 base::Bind(&CheckComAptTypeTask, &apt_type, &hresult));
143
144 task_runner = NULL;
145 RunMessageLoop();
146
147 EXPECT_EQ(S_OK, hresult);
148 EXPECT_EQ(APTTYPE_MTA, apt_type);
149 }
150
151 TEST_F(AutoThreadTest, ThreadWithComSta) {
152 // CoGetApartmentType requires Windows 7 or above.
153 if (base::win::GetVersion() < base::win::VERSION_WIN7)
154 return;
155
156 scoped_refptr<base::TaskRunner> task_runner =
157 AutoThread::CreateWithLoopAndComInitTypes(
158 kThreadName, main_task_runner_, MessageLoop::TYPE_UI,
159 AutoThread::COM_INIT_STA);
160 EXPECT_TRUE(task_runner.get());
161
162 // Post a task to query the COM apartment type.
163 HRESULT hresult = E_FAIL;
164 APTTYPE apt_type = APTTYPE_NA;
165 task_runner->PostTask(FROM_HERE,
166 base::Bind(&CheckComAptTypeTask, &apt_type, &hresult));
167
168 task_runner = NULL;
169 RunMessageLoop();
170
171 EXPECT_EQ(S_OK, hresult);
172 // Whether the thread is the "main" STA apartment depends upon previous
173 // COM activity in this test process, so allow both types here.
174 EXPECT_TRUE(apt_type == APTTYPE_MAINSTA || apt_type == APTTYPE_STA);
175 }
176 #endif // defined(OS_WIN)
177
115 } // namespace remoting 178 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/base/auto_thread.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698