OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "remoting/host/it2me/it2me_confirmation_dialog_proxy.h" |
| 6 |
| 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" |
| 16 |
| 17 using ::testing::InvokeWithoutArgs; |
| 18 using ::testing::CreateFunctor; |
| 19 |
| 20 namespace remoting { |
| 21 |
| 22 class StubIt2MeConfirmationDialog : public It2MeConfirmationDialog { |
| 23 public: |
| 24 explicit StubIt2MeConfirmationDialog( |
| 25 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 26 : task_runner_(task_runner) { |
| 27 } |
| 28 ~StubIt2MeConfirmationDialog() override { |
| 29 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
| 30 } |
| 31 |
| 32 void ReportResult(Result result) { |
| 33 ASSERT_TRUE(task_runner_->BelongsToCurrentThread()); |
| 34 callback_.Run(result); |
| 35 } |
| 36 |
| 37 MOCK_METHOD0(OnShow, void()); |
| 38 |
| 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(); |
| 45 } |
| 46 |
| 47 private: |
| 48 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 49 ResultCallback callback_; |
| 50 }; |
| 51 |
| 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) { |
| 58 } |
| 59 |
| 60 MOCK_METHOD1(OnDialogResult, void(It2MeConfirmationDialog::Result)); |
| 61 |
| 62 It2MeConfirmationDialog::ResultCallback MakeCallback() { |
| 63 return base::Bind(&ResultCallbackTarget::HandleDialogResult, |
| 64 base::Unretained(this)); |
| 65 } |
| 66 |
| 67 private: |
| 68 void HandleDialogResult(It2MeConfirmationDialog::Result result) { |
| 69 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
| 70 OnDialogResult(result); |
| 71 } |
| 72 |
| 73 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 74 }; |
| 75 |
| 76 class It2MeConfirmationDialogProxyTest : public testing::Test { |
| 77 public: |
| 78 It2MeConfirmationDialogProxyTest(); |
| 79 ~It2MeConfirmationDialogProxyTest() override; |
| 80 |
| 81 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner() { |
| 82 return message_loop_.task_runner(); |
| 83 } |
| 84 |
| 85 scoped_refptr<base::SingleThreadTaskRunner> dialog_task_runner() { |
| 86 return dialog_thread_.task_runner(); |
| 87 } |
| 88 |
| 89 void Run() { |
| 90 run_loop_.Run(); |
| 91 } |
| 92 |
| 93 void Quit() { |
| 94 run_loop_.Quit(); |
| 95 } |
| 96 |
| 97 It2MeConfirmationDialogProxy* dialog_proxy() { |
| 98 return dialog_proxy_.get(); |
| 99 } |
| 100 |
| 101 StubIt2MeConfirmationDialog* dialog() { |
| 102 return dialog_; |
| 103 } |
| 104 |
| 105 private: |
| 106 base::MessageLoop message_loop_; |
| 107 base::RunLoop run_loop_; |
| 108 base::Thread dialog_thread_; |
| 109 |
| 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_; |
| 114 }; |
| 115 |
| 116 It2MeConfirmationDialogProxyTest::It2MeConfirmationDialogProxyTest() |
| 117 : dialog_thread_("test dialog thread") { |
| 118 dialog_thread_.Start(); |
| 119 |
| 120 dialog_ = new StubIt2MeConfirmationDialog(dialog_task_runner()); |
| 121 dialog_proxy_.reset(new It2MeConfirmationDialogProxy( |
| 122 dialog_task_runner(), |
| 123 scoped_ptr<It2MeConfirmationDialog>(dialog_))); |
| 124 } |
| 125 |
| 126 It2MeConfirmationDialogProxyTest::~It2MeConfirmationDialogProxyTest() {} |
| 127 |
| 128 TEST_F(It2MeConfirmationDialogProxyTest, Show) { |
| 129 ResultCallbackTarget callback_target(main_task_runner()); |
| 130 |
| 131 EXPECT_CALL(*dialog(), OnShow()) |
| 132 .WillOnce( |
| 133 InvokeWithoutArgs( |
| 134 CreateFunctor( |
| 135 dialog(), &StubIt2MeConfirmationDialog::ReportResult, |
| 136 It2MeConfirmationDialog::Result::CANCEL))); |
| 137 |
| 138 EXPECT_CALL(callback_target, |
| 139 OnDialogResult(It2MeConfirmationDialog::Result::CANCEL)) |
| 140 .WillOnce( |
| 141 InvokeWithoutArgs(this, &It2MeConfirmationDialogProxyTest::Quit)); |
| 142 |
| 143 dialog_proxy()->Show(callback_target.MakeCallback()); |
| 144 |
| 145 Run(); |
| 146 } |
| 147 |
| 148 } // namespace remoting |
OLD | NEW |