Fix mouse warp with 2x displays
[chromium-blink-merge.git] / remoting / host / it2me / it2me_confirmation_dialog_proxy_unittest.cc
blobbea275e41a86516b8205e5e6f789bef1bc8cc3f5
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"
7 #include "base/bind.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;
20 namespace remoting {
22 class StubIt2MeConfirmationDialog : public It2MeConfirmationDialog {
23 public:
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());
43 callback_ = callback;
44 OnShow();
47 private:
48 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
49 ResultCallback callback_;
52 // Encapsulates a target for It2MeConfirmationDialog::ResultCallback.
53 class ResultCallbackTarget {
54 public:
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));
67 private:
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 {
77 public:
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();
89 void Run() {
90 run_loop_.Run();
93 void Quit() {
94 run_loop_.Quit();
97 It2MeConfirmationDialogProxy* dialog_proxy() {
98 return dialog_proxy_.get();
101 StubIt2MeConfirmationDialog* dialog() {
102 return dialog_;
105 private:
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
111 // purposes.
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())
132 .WillOnce(
133 InvokeWithoutArgs(
134 CreateFunctor(
135 dialog(), &StubIt2MeConfirmationDialog::ReportResult,
136 It2MeConfirmationDialog::Result::CANCEL)));
138 EXPECT_CALL(callback_target,
139 OnDialogResult(It2MeConfirmationDialog::Result::CANCEL))
140 .WillOnce(
141 InvokeWithoutArgs(this, &It2MeConfirmationDialogProxyTest::Quit));
143 dialog_proxy()->Show(callback_target.MakeCallback());
145 Run();
148 } // namespace remoting