Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "chrome/browser/chromeos/arc/arc_support_host.h" | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "chrome/browser/chromeos/arc/extensions/fake_arc_support.h" | |
| 9 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" | |
| 10 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | |
| 11 #include "chrome/test/base/testing_profile.h" | |
| 12 #include "content/public/test/test_browser_thread_bundle.h" | |
| 13 #include "testing/gmock/include/gmock/gmock.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 using arc::FakeArcSupport; | |
| 17 using testing::_; | |
| 18 using testing::Eq; | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 class MockAuthDelegate : public ArcSupportHost::AuthDelegate { | |
| 23 public: | |
| 24 MOCK_METHOD1(OnAuthSucceeded, void(const std::string& auth_code)); | |
| 25 MOCK_METHOD0(OnAuthFailed, void()); | |
| 26 MOCK_METHOD0(OnAuthRetryClicked, void()); | |
| 27 }; | |
| 28 | |
| 29 class MockTermsOfServiceDelegate | |
| 30 : public ArcSupportHost::TermsOfServiceDelegate { | |
| 31 public: | |
| 32 MOCK_METHOD3(OnTermsAgreed, | |
| 33 void(bool is_metrics_enabled, | |
| 34 bool is_backup_and_restore_enabled, | |
| 35 bool is_location_service_enabled)); | |
| 36 MOCK_METHOD0(OnTermsRejected, void()); | |
| 37 MOCK_METHOD0(OnTermsRetryClicked, void()); | |
| 38 }; | |
| 39 | |
| 40 class MockErrorDelegate : public ArcSupportHost::ErrorDelegate { | |
| 41 public: | |
| 42 MOCK_METHOD0(OnOptInAborted, void()); | |
| 43 MOCK_METHOD0(OnRetryClicked, void()); | |
| 44 MOCK_METHOD0(OnSendFeedbackClicked, void()); | |
| 45 }; | |
| 46 | |
| 47 class DelegateRAII { | |
|
hidehiko
2017/05/15 09:05:44
Can this be merged into ArcSupportHostTest?
Actual
victorhsieh0
2017/05/15 22:31:53
Done.
| |
| 48 public: | |
| 49 explicit DelegateRAII(ArcSupportHost* support_host) | |
| 50 : support_host_(support_host) {} | |
| 51 | |
| 52 MockAuthDelegate* CreateMockAuthDelegate() { | |
| 53 auth_delegate_ = base::MakeUnique<MockAuthDelegate>(); | |
| 54 support_host_->SetAuthDelegate(auth_delegate_.get()); | |
| 55 return auth_delegate_.get(); | |
| 56 } | |
| 57 | |
| 58 MockTermsOfServiceDelegate* CreateMockTermsOfServiceDelegate() { | |
| 59 tos_delegate_ = base::MakeUnique<MockTermsOfServiceDelegate>(); | |
| 60 support_host_->SetTermsOfServiceDelegate(tos_delegate_.get()); | |
| 61 return tos_delegate_.get(); | |
| 62 } | |
| 63 | |
| 64 MockErrorDelegate* CreateMockErrorDelegate() { | |
| 65 error_delegate_ = base::MakeUnique<MockErrorDelegate>(); | |
| 66 support_host_->SetErrorDelegate(error_delegate_.get()); | |
| 67 return error_delegate_.get(); | |
| 68 } | |
| 69 | |
| 70 virtual ~DelegateRAII() { | |
| 71 support_host_->SetAuthDelegate(nullptr); | |
| 72 support_host_->SetTermsOfServiceDelegate(nullptr); | |
| 73 support_host_->SetErrorDelegate(nullptr); | |
| 74 } | |
| 75 | |
| 76 private: | |
| 77 ArcSupportHost* support_host_; // not owned | |
| 78 std::unique_ptr<MockAuthDelegate> auth_delegate_; | |
| 79 std::unique_ptr<MockTermsOfServiceDelegate> tos_delegate_; | |
| 80 std::unique_ptr<MockErrorDelegate> error_delegate_; | |
| 81 }; | |
| 82 | |
| 83 class ArcSupportHostTest : public testing::Test { | |
| 84 public: | |
| 85 ArcSupportHostTest() = default; | |
| 86 ~ArcSupportHostTest() override = default; | |
| 87 | |
| 88 void SetUp() override { | |
| 89 user_manager_enabler_ = | |
| 90 base::MakeUnique<chromeos::ScopedUserManagerEnabler>( | |
| 91 new chromeos::FakeChromeUserManager()); | |
| 92 | |
| 93 profile_ = base::MakeUnique<TestingProfile>(); | |
| 94 support_host_ = base::MakeUnique<ArcSupportHost>(profile_.get()); | |
| 95 fake_arc_support_ = base::MakeUnique<FakeArcSupport>(support_host_.get()); | |
| 96 } | |
| 97 | |
| 98 void TearDown() override { | |
| 99 fake_arc_support_.reset(); | |
| 100 support_host_.reset(); | |
| 101 profile_.reset(); | |
| 102 user_manager_enabler_.reset(); | |
| 103 } | |
| 104 | |
| 105 ArcSupportHost* support_host() { return support_host_.get(); } | |
| 106 FakeArcSupport* fake_arc_support() { return fake_arc_support_.get(); } | |
| 107 | |
| 108 private: | |
| 109 // Fake as if the current testing thread is UI thread. | |
| 110 content::TestBrowserThreadBundle bundle_; | |
| 111 | |
| 112 std::unique_ptr<TestingProfile> profile_; | |
| 113 std::unique_ptr<ArcSupportHost> support_host_; | |
| 114 std::unique_ptr<FakeArcSupport> fake_arc_support_; | |
| 115 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; | |
| 116 | |
| 117 DISALLOW_COPY_AND_ASSIGN(ArcSupportHostTest); | |
| 118 }; | |
| 119 | |
| 120 TEST_F(ArcSupportHostTest, AuthSucceeded) { | |
| 121 DelegateRAII raii(support_host()); | |
| 122 MockAuthDelegate* auth_delegate = raii.CreateMockAuthDelegate(); | |
| 123 support_host()->SetAuthDelegate(auth_delegate); | |
| 124 | |
| 125 support_host()->ShowLso(); | |
| 126 | |
| 127 EXPECT_CALL(*auth_delegate, OnAuthSucceeded(Eq("fake_code"))); | |
|
hidehiko
2017/05/15 09:05:44
IIUC, even if OnAuthSucceeded is not called, the t
victorhsieh0
2017/05/15 22:31:54
Did you mean uninterested call? IIUC this call mu
hidehiko
2017/05/25 12:05:52
If gmock is continued to be used, StrictMock sound
victorhsieh0
2017/05/26 19:38:41
Done
| |
| 128 fake_arc_support()->EmulateAuthCodeResponse("fake_code"); | |
| 129 } | |
| 130 | |
| 131 TEST_F(ArcSupportHostTest, AuthFailed) { | |
| 132 DelegateRAII raii(support_host()); | |
| 133 MockAuthDelegate* auth_delegate = raii.CreateMockAuthDelegate(); | |
| 134 support_host()->SetAuthDelegate(auth_delegate); | |
| 135 | |
| 136 support_host()->ShowLso(); | |
| 137 | |
| 138 EXPECT_CALL(*auth_delegate, OnAuthFailed()); | |
| 139 fake_arc_support()->EmulateAuthFailure(); | |
| 140 } | |
| 141 | |
| 142 TEST_F(ArcSupportHostTest, AuthRetryOnError) { | |
| 143 // Auth error can only happen after terms accepted. | |
| 144 DelegateRAII raii(support_host()); | |
| 145 MockAuthDelegate* auth_delegate = raii.CreateMockAuthDelegate(); | |
| 146 support_host()->SetAuthDelegate(auth_delegate); | |
| 147 MockErrorDelegate* error_delegate = raii.CreateMockErrorDelegate(); | |
| 148 support_host()->SetErrorDelegate(error_delegate); | |
| 149 | |
| 150 support_host()->ShowError(ArcSupportHost::Error::NETWORK_UNAVAILABLE_ERROR, | |
| 151 false /* should_show_send_feedback */); | |
| 152 | |
| 153 EXPECT_CALL(*auth_delegate, OnAuthRetryClicked()); | |
| 154 EXPECT_CALL(*error_delegate, OnOptInAborted()).Times(0); | |
| 155 fake_arc_support()->ClickRetryButton(); | |
| 156 } | |
| 157 | |
| 158 TEST_F(ArcSupportHostTest, TermsOfServiceAccept) { | |
| 159 MockTermsOfServiceDelegate tos_delegate; | |
| 160 support_host()->SetTermsOfServiceDelegate(&tos_delegate); | |
| 161 | |
| 162 support_host()->ShowTermsOfService(); | |
| 163 | |
| 164 EXPECT_CALL(tos_delegate, OnTermsAgreed(_, _, _)); | |
|
hidehiko
2017/05/15 09:05:44
Can args be checked, too?
victorhsieh0
2017/05/15 22:31:54
Those parameters (whether the user agree to show m
hidehiko
2017/05/25 12:05:52
WDY mean? My understanding is that the preference
victorhsieh0
2017/05/26 19:38:41
Oh, I missed the point. Done.
| |
| 165 fake_arc_support()->ClickAgreeButton(); | |
| 166 | |
| 167 support_host()->SetTermsOfServiceDelegate(nullptr); | |
|
hidehiko
2017/05/15 09:05:44
Unnecessary, too?
victorhsieh0
2017/05/15 22:31:54
Done.
| |
| 168 } | |
| 169 | |
| 170 TEST_F(ArcSupportHostTest, TermsOfServiceRejectOrCloseWindow) { | |
| 171 DelegateRAII raii(support_host()); | |
| 172 MockTermsOfServiceDelegate* tos_delegate = | |
| 173 raii.CreateMockTermsOfServiceDelegate(); | |
| 174 support_host()->SetTermsOfServiceDelegate(tos_delegate); | |
| 175 MockErrorDelegate* error_delegate = raii.CreateMockErrorDelegate(); | |
| 176 support_host()->SetErrorDelegate(error_delegate); | |
| 177 | |
| 178 support_host()->ShowTermsOfService(); | |
| 179 | |
| 180 EXPECT_CALL(*tos_delegate, OnTermsRejected()); | |
| 181 EXPECT_CALL(*error_delegate, OnOptInAborted()).Times(0); | |
| 182 // Rejecting ToS and closing the window when ToS is showing is the same thing. | |
| 183 fake_arc_support()->Close(); | |
| 184 } | |
| 185 | |
| 186 TEST_F(ArcSupportHostTest, TermsOfServiceRetryOnError) { | |
| 187 DelegateRAII raii(support_host()); | |
| 188 MockTermsOfServiceDelegate* tos_delegate = | |
| 189 raii.CreateMockTermsOfServiceDelegate(); | |
| 190 support_host()->SetTermsOfServiceDelegate(tos_delegate); | |
| 191 MockErrorDelegate* error_delegate = raii.CreateMockErrorDelegate(); | |
| 192 support_host()->SetErrorDelegate(error_delegate); | |
| 193 | |
| 194 support_host()->ShowError(ArcSupportHost::Error::NETWORK_UNAVAILABLE_ERROR, | |
| 195 false /* should_show_send_feedback */); | |
| 196 | |
| 197 EXPECT_CALL(*tos_delegate, OnTermsRetryClicked()); | |
| 198 EXPECT_CALL(*error_delegate, OnOptInAborted()).Times(0); | |
| 199 fake_arc_support()->ClickRetryButton(); | |
| 200 | |
| 201 support_host()->SetTermsOfServiceDelegate(nullptr); | |
|
hidehiko
2017/05/15 09:05:44
Ditto.
victorhsieh0
2017/05/15 22:31:54
Done.
| |
| 202 } | |
| 203 | |
| 204 TEST_F(ArcSupportHostTest, CloseWindowDuringManualAuth) { | |
| 205 // No TermsOfServiceDelegate since it is not ongoing. | |
| 206 DelegateRAII raii(support_host()); | |
| 207 MockErrorDelegate* error_delegate = raii.CreateMockErrorDelegate(); | |
| 208 support_host()->SetErrorDelegate(error_delegate); | |
| 209 MockAuthDelegate* auth_delegate = raii.CreateMockAuthDelegate(); | |
| 210 support_host()->SetAuthDelegate(auth_delegate); | |
| 211 | |
| 212 support_host()->ShowArcLoading(); | |
| 213 | |
| 214 EXPECT_CALL(*error_delegate, OnOptInAborted()); | |
| 215 fake_arc_support()->Close(); | |
| 216 } | |
| 217 | |
| 218 TEST_F(ArcSupportHostTest, CloseWindowWithoutTermsOfServiceOrAuthOnging) { | |
| 219 // No TermsOfServiceDelegate since it is not ongoing. | |
| 220 DelegateRAII raii(support_host()); | |
| 221 MockErrorDelegate* error_delegate = raii.CreateMockErrorDelegate(); | |
| 222 support_host()->SetErrorDelegate(error_delegate); | |
| 223 | |
| 224 support_host()->ShowArcLoading(); | |
| 225 | |
| 226 EXPECT_CALL(*error_delegate, OnOptInAborted()); | |
| 227 fake_arc_support()->Close(); | |
| 228 } | |
| 229 | |
| 230 TEST_F(ArcSupportHostTest, RetryOnGeneralError) { | |
| 231 // No TermsOfServiceDelegate and AuthDelegate since it is not ongoing. | |
| 232 DelegateRAII raii(support_host()); | |
| 233 MockErrorDelegate* error_delegate = raii.CreateMockErrorDelegate(); | |
| 234 support_host()->SetErrorDelegate(error_delegate); | |
| 235 | |
| 236 support_host()->ShowError(ArcSupportHost::Error::NETWORK_UNAVAILABLE_ERROR, | |
| 237 false /* should_show_send_feedback */); | |
| 238 | |
| 239 EXPECT_CALL(*error_delegate, OnRetryClicked()); | |
| 240 fake_arc_support()->ClickRetryButton(); | |
| 241 } | |
| 242 | |
| 243 TEST_F(ArcSupportHostTest, SendFeedbackOnError) { | |
| 244 DelegateRAII raii(support_host()); | |
| 245 MockErrorDelegate* error_delegate = raii.CreateMockErrorDelegate(); | |
| 246 support_host()->SetErrorDelegate(error_delegate); | |
| 247 | |
| 248 support_host()->ShowError(ArcSupportHost::Error::NETWORK_UNAVAILABLE_ERROR, | |
| 249 true /* should_show_send_feedback */); | |
| 250 | |
| 251 EXPECT_CALL(*error_delegate, OnSendFeedbackClicked()); | |
| 252 fake_arc_support()->ClickSendFeedbackButton(); | |
| 253 } | |
| 254 | |
| 255 } // namespace | |
| OLD | NEW |