1 // Copyright 2014 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.
5 #include "remoting/host/it2me/it2me_confirmation_dialog_proxy.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/run_loop.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/threading/thread.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gmock_mutant.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using ::testing::InvokeWithoutArgs
;
18 using ::testing::CreateFunctor
;
22 class StubIt2MeConfirmationDialog
: public It2MeConfirmationDialog
{
24 explicit StubIt2MeConfirmationDialog(
25 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
)
26 : task_runner_(task_runner
) {
28 ~StubIt2MeConfirmationDialog() override
{
29 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
32 void ReportResult(Result result
) {
33 ASSERT_TRUE(task_runner_
->BelongsToCurrentThread());
34 callback_
.Run(result
);
37 MOCK_METHOD0(OnShow
, void());
39 // It2MeConfirmationDialog implementation.
40 void Show(const ResultCallback
& callback
) override
{
41 EXPECT_TRUE(callback_
.is_null());
42 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
48 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
49 ResultCallback callback_
;
52 // Encapsulates a target for It2MeConfirmationDialog::ResultCallback.
53 class ResultCallbackTarget
{
55 explicit ResultCallbackTarget(
56 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
)
57 : task_runner_(task_runner
) {
60 MOCK_METHOD1(OnDialogResult
, void(It2MeConfirmationDialog::Result
));
62 It2MeConfirmationDialog::ResultCallback
MakeCallback() {
63 return base::Bind(&ResultCallbackTarget::HandleDialogResult
,
64 base::Unretained(this));
68 void HandleDialogResult(It2MeConfirmationDialog::Result result
) {
69 EXPECT_TRUE(task_runner_
->BelongsToCurrentThread());
70 OnDialogResult(result
);
73 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
76 class It2MeConfirmationDialogProxyTest
: public testing::Test
{
78 It2MeConfirmationDialogProxyTest();
79 ~It2MeConfirmationDialogProxyTest() override
;
81 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner() {
82 return message_loop_
.task_runner();
85 scoped_refptr
<base::SingleThreadTaskRunner
> dialog_task_runner() {
86 return dialog_thread_
.task_runner();
97 It2MeConfirmationDialogProxy
* dialog_proxy() {
98 return dialog_proxy_
.get();
101 StubIt2MeConfirmationDialog
* dialog() {
106 base::MessageLoop message_loop_
;
107 base::RunLoop run_loop_
;
108 base::Thread dialog_thread_
;
110 // |dialog_| is owned by |dialog_proxy_| but we keep an alias for test
112 StubIt2MeConfirmationDialog
* dialog_
;
113 scoped_ptr
<It2MeConfirmationDialogProxy
> dialog_proxy_
;
116 It2MeConfirmationDialogProxyTest::It2MeConfirmationDialogProxyTest()
117 : dialog_thread_("test dialog thread") {
118 dialog_thread_
.Start();
120 dialog_
= new StubIt2MeConfirmationDialog(dialog_task_runner());
121 dialog_proxy_
.reset(new It2MeConfirmationDialogProxy(
122 dialog_task_runner(),
123 scoped_ptr
<It2MeConfirmationDialog
>(dialog_
)));
126 It2MeConfirmationDialogProxyTest::~It2MeConfirmationDialogProxyTest() {}
128 TEST_F(It2MeConfirmationDialogProxyTest
, Show
) {
129 ResultCallbackTarget
callback_target(main_task_runner());
131 EXPECT_CALL(*dialog(), OnShow())
135 dialog(), &StubIt2MeConfirmationDialog::ReportResult
,
136 It2MeConfirmationDialog::Result::CANCEL
)));
138 EXPECT_CALL(callback_target
,
139 OnDialogResult(It2MeConfirmationDialog::Result::CANCEL
))
141 InvokeWithoutArgs(this, &It2MeConfirmationDialogProxyTest::Quit
));
143 dialog_proxy()->Show(callback_target
.MakeCallback());
148 } // namespace remoting