#include "remoting/base/auto_thread.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_pump_type.h"
#include "base/run_loop.h"
#include "base/scoped_native_library.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_WIN)
#include <objbase.h>
#endif
namespace {
const char kThreadName[] = …;
void SetFlagTask(bool* success) { … }
void PostSetFlagTask(scoped_refptr<base::TaskRunner> task_runner,
bool* success) { … }
#if BUILDFLAG(IS_WIN)
void CheckComAptTypeTask(APTTYPE* apt_type_out, HRESULT* hresult) {
typedef HRESULT(WINAPI * CoGetApartmentTypeFunc)(APTTYPE*, APTTYPEQUALIFIER*);
base::ScopedNativeLibrary com_library(base::FilePath(L"ole32.dll"));
ASSERT_TRUE(com_library.is_valid());
CoGetApartmentTypeFunc co_get_apartment_type =
reinterpret_cast<CoGetApartmentTypeFunc>(
com_library.GetFunctionPointer("CoGetApartmentType"));
ASSERT_TRUE(co_get_apartment_type != NULL);
APTTYPEQUALIFIER apt_type_qualifier;
*hresult = (*co_get_apartment_type)(apt_type_out, &apt_type_qualifier);
}
#endif
}
namespace remoting {
class AutoThreadTest : public testing::Test { … };
TEST_F(AutoThreadTest, StartAndStop) { … }
TEST_F(AutoThreadTest, ProcessTask) { … }
TEST_F(AutoThreadTest, ThreadDependency) { … }
#if BUILDFLAG(IS_WIN)
TEST_F(AutoThreadTest, ThreadWithComMta) {
scoped_refptr<base::TaskRunner> task_runner =
AutoThread::CreateWithLoopAndComInitTypes(kThreadName, main_task_runner_,
base::MessagePumpType::DEFAULT,
AutoThread::COM_INIT_MTA);
EXPECT_TRUE(task_runner);
HRESULT hresult = E_FAIL;
APTTYPE apt_type = APTTYPE_NA;
task_runner->PostTask(
FROM_HERE, base::BindOnce(&CheckComAptTypeTask, &apt_type, &hresult));
task_runner.reset();
RunMessageLoop();
EXPECT_EQ(S_OK, hresult);
EXPECT_EQ(APTTYPE_MTA, apt_type);
}
TEST_F(AutoThreadTest, ThreadWithComSta) {
scoped_refptr<base::TaskRunner> task_runner =
AutoThread::CreateWithLoopAndComInitTypes(kThreadName, main_task_runner_,
base::MessagePumpType::UI,
AutoThread::COM_INIT_STA);
EXPECT_TRUE(task_runner);
HRESULT hresult = E_FAIL;
APTTYPE apt_type = APTTYPE_NA;
task_runner->PostTask(
FROM_HERE, base::BindOnce(&CheckComAptTypeTask, &apt_type, &hresult));
task_runner.reset();
RunMessageLoop();
EXPECT_EQ(S_OK, hresult);
EXPECT_TRUE(apt_type == APTTYPE_MAINSTA || apt_type == APTTYPE_STA);
}
#endif
}