| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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::CreateWithType(kThreadName, main_task_runner_, |
| 134 AutoThread::TYPE_UI_COM_MTA); |
| 135 EXPECT_TRUE(task_runner.get()); |
| 136 |
| 137 // Post a task to query the COM apartment type. |
| 138 HRESULT hresult = E_FAIL; |
| 139 APTTYPE apt_type = APTTYPE_NA; |
| 140 task_runner->PostTask(FROM_HERE, |
| 141 base::Bind(&CheckComAptTypeTask, &apt_type, &hresult)); |
| 142 |
| 143 task_runner = NULL; |
| 144 RunMessageLoop(); |
| 145 |
| 146 EXPECT_EQ(S_OK, hresult); |
| 147 EXPECT_EQ(APTTYPE_MTA, apt_type); |
| 148 } |
| 149 |
| 150 TEST_F(AutoThreadTest, ThreadWithComSta) { |
| 151 // CoGetApartmentType requires Windows 7 or above. |
| 152 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
| 153 return; |
| 154 |
| 155 scoped_refptr<base::TaskRunner> task_runner = |
| 156 AutoThread::CreateWithType(kThreadName, main_task_runner_, |
| 157 AutoThread::TYPE_UI_COM_STA); |
| 158 EXPECT_TRUE(task_runner.get()); |
| 159 |
| 160 // Post a task to query the COM apartment type. |
| 161 HRESULT hresult = E_FAIL; |
| 162 APTTYPE apt_type = APTTYPE_NA; |
| 163 task_runner->PostTask(FROM_HERE, |
| 164 base::Bind(&CheckComAptTypeTask, &apt_type, &hresult)); |
| 165 |
| 166 task_runner = NULL; |
| 167 RunMessageLoop(); |
| 168 |
| 169 EXPECT_EQ(S_OK, hresult); |
| 170 // Whether the thread is the "main" STA apartment depends upon previous |
| 171 // COM activity in this test process, so allow both types here. |
| 172 EXPECT_TRUE(apt_type == APTTYPE_MAINSTA || apt_type == APTTYPE_STA); |
| 173 } |
| 174 #endif |
| 175 |
| 115 } // namespace remoting | 176 } // namespace remoting |
| OLD | NEW |