| 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 using testing::StrictMock; |
| 20 |
| 21 namespace { |
| 22 |
| 23 class MockAuthDelegateNonStrict : public ArcSupportHost::AuthDelegate { |
| 24 public: |
| 25 MOCK_METHOD1(OnAuthSucceeded, void(const std::string& auth_code)); |
| 26 MOCK_METHOD0(OnAuthFailed, void()); |
| 27 MOCK_METHOD0(OnAuthRetryClicked, void()); |
| 28 }; |
| 29 |
| 30 using MockAuthDelegate = StrictMock<MockAuthDelegateNonStrict>; |
| 31 |
| 32 class MockTermsOfServiceDelegateNonStrict |
| 33 : public ArcSupportHost::TermsOfServiceDelegate { |
| 34 public: |
| 35 MOCK_METHOD3(OnTermsAgreed, |
| 36 void(bool is_metrics_enabled, |
| 37 bool is_backup_and_restore_enabled, |
| 38 bool is_location_service_enabled)); |
| 39 MOCK_METHOD0(OnTermsRejected, void()); |
| 40 MOCK_METHOD0(OnTermsRetryClicked, void()); |
| 41 }; |
| 42 |
| 43 using MockTermsOfServiceDelegate = |
| 44 StrictMock<MockTermsOfServiceDelegateNonStrict>; |
| 45 |
| 46 class MockErrorDelegateNonStrict : public ArcSupportHost::ErrorDelegate { |
| 47 public: |
| 48 MOCK_METHOD0(OnWindowClosed, void()); |
| 49 MOCK_METHOD0(OnRetryClicked, void()); |
| 50 MOCK_METHOD0(OnSendFeedbackClicked, void()); |
| 51 }; |
| 52 |
| 53 using MockErrorDelegate = StrictMock<MockErrorDelegateNonStrict>; |
| 54 |
| 55 class ArcSupportHostTest : public testing::Test { |
| 56 public: |
| 57 ArcSupportHostTest() = default; |
| 58 ~ArcSupportHostTest() override = default; |
| 59 |
| 60 void SetUp() override { |
| 61 user_manager_enabler_ = |
| 62 base::MakeUnique<chromeos::ScopedUserManagerEnabler>( |
| 63 new chromeos::FakeChromeUserManager()); |
| 64 |
| 65 profile_ = base::MakeUnique<TestingProfile>(); |
| 66 support_host_ = base::MakeUnique<ArcSupportHost>(profile_.get()); |
| 67 fake_arc_support_ = base::MakeUnique<FakeArcSupport>(support_host_.get()); |
| 68 } |
| 69 |
| 70 void TearDown() override { |
| 71 support_host_->SetAuthDelegate(nullptr); |
| 72 support_host_->SetTermsOfServiceDelegate(nullptr); |
| 73 support_host_->SetErrorDelegate(nullptr); |
| 74 |
| 75 fake_arc_support_.reset(); |
| 76 support_host_.reset(); |
| 77 profile_.reset(); |
| 78 user_manager_enabler_.reset(); |
| 79 } |
| 80 |
| 81 ArcSupportHost* support_host() { return support_host_.get(); } |
| 82 FakeArcSupport* fake_arc_support() { return fake_arc_support_.get(); } |
| 83 |
| 84 MockAuthDelegate* CreateMockAuthDelegate() { |
| 85 auth_delegate_ = base::MakeUnique<MockAuthDelegate>(); |
| 86 support_host_->SetAuthDelegate(auth_delegate_.get()); |
| 87 return auth_delegate_.get(); |
| 88 } |
| 89 |
| 90 MockTermsOfServiceDelegate* CreateMockTermsOfServiceDelegate() { |
| 91 tos_delegate_ = base::MakeUnique<MockTermsOfServiceDelegate>(); |
| 92 support_host_->SetTermsOfServiceDelegate(tos_delegate_.get()); |
| 93 return tos_delegate_.get(); |
| 94 } |
| 95 |
| 96 MockErrorDelegate* CreateMockErrorDelegate() { |
| 97 error_delegate_ = base::MakeUnique<MockErrorDelegate>(); |
| 98 support_host_->SetErrorDelegate(error_delegate_.get()); |
| 99 return error_delegate_.get(); |
| 100 } |
| 101 |
| 102 private: |
| 103 // Fake as if the current testing thread is UI thread. |
| 104 content::TestBrowserThreadBundle bundle_; |
| 105 |
| 106 std::unique_ptr<TestingProfile> profile_; |
| 107 std::unique_ptr<ArcSupportHost> support_host_; |
| 108 std::unique_ptr<FakeArcSupport> fake_arc_support_; |
| 109 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; |
| 110 |
| 111 std::unique_ptr<MockAuthDelegate> auth_delegate_; |
| 112 std::unique_ptr<MockTermsOfServiceDelegate> tos_delegate_; |
| 113 std::unique_ptr<MockErrorDelegate> error_delegate_; |
| 114 |
| 115 DISALLOW_COPY_AND_ASSIGN(ArcSupportHostTest); |
| 116 }; |
| 117 |
| 118 TEST_F(ArcSupportHostTest, AuthSucceeded) { |
| 119 constexpr char kFakeCode[] = "fake_code"; |
| 120 MockAuthDelegate* auth_delegate = CreateMockAuthDelegate(); |
| 121 support_host()->SetAuthDelegate(auth_delegate); |
| 122 |
| 123 support_host()->ShowLso(); |
| 124 |
| 125 EXPECT_CALL(*auth_delegate, OnAuthSucceeded(Eq(kFakeCode))); |
| 126 fake_arc_support()->EmulateAuthCodeResponse(kFakeCode); |
| 127 } |
| 128 |
| 129 TEST_F(ArcSupportHostTest, AuthFailed) { |
| 130 MockAuthDelegate* auth_delegate = CreateMockAuthDelegate(); |
| 131 support_host()->SetAuthDelegate(auth_delegate); |
| 132 |
| 133 support_host()->ShowLso(); |
| 134 |
| 135 EXPECT_CALL(*auth_delegate, OnAuthFailed()); |
| 136 fake_arc_support()->EmulateAuthFailure(); |
| 137 } |
| 138 |
| 139 TEST_F(ArcSupportHostTest, AuthRetryOnError) { |
| 140 // Auth error can only happen after terms accepted. |
| 141 MockAuthDelegate* auth_delegate = CreateMockAuthDelegate(); |
| 142 support_host()->SetAuthDelegate(auth_delegate); |
| 143 MockErrorDelegate* error_delegate = CreateMockErrorDelegate(); |
| 144 support_host()->SetErrorDelegate(error_delegate); |
| 145 |
| 146 support_host()->ShowError(ArcSupportHost::Error::NETWORK_UNAVAILABLE_ERROR, |
| 147 false /* should_show_send_feedback */); |
| 148 |
| 149 EXPECT_CALL(*auth_delegate, OnAuthRetryClicked()); |
| 150 EXPECT_CALL(*error_delegate, OnWindowClosed()).Times(0); |
| 151 fake_arc_support()->ClickRetryButton(); |
| 152 } |
| 153 |
| 154 TEST_F(ArcSupportHostTest, TermsOfServiceAccept) { |
| 155 MockTermsOfServiceDelegate tos_delegate; |
| 156 support_host()->SetTermsOfServiceDelegate(&tos_delegate); |
| 157 |
| 158 support_host()->ShowTermsOfService(); |
| 159 fake_arc_support()->set_metrics_mode(false); // just to add some diversity |
| 160 fake_arc_support()->set_backup_and_restore_mode(true); |
| 161 fake_arc_support()->set_location_service_mode(true); |
| 162 |
| 163 EXPECT_CALL(tos_delegate, OnTermsAgreed(false, true, true)); |
| 164 fake_arc_support()->ClickAgreeButton(); |
| 165 } |
| 166 |
| 167 TEST_F(ArcSupportHostTest, TermsOfServiceRejectOrCloseWindow) { |
| 168 MockTermsOfServiceDelegate* tos_delegate = CreateMockTermsOfServiceDelegate(); |
| 169 support_host()->SetTermsOfServiceDelegate(tos_delegate); |
| 170 MockErrorDelegate* error_delegate = CreateMockErrorDelegate(); |
| 171 support_host()->SetErrorDelegate(error_delegate); |
| 172 |
| 173 support_host()->ShowTermsOfService(); |
| 174 |
| 175 EXPECT_CALL(*tos_delegate, OnTermsRejected()); |
| 176 EXPECT_CALL(*error_delegate, OnWindowClosed()).Times(0); |
| 177 // Rejecting ToS and closing the window when ToS is showing is the same thing. |
| 178 fake_arc_support()->Close(); |
| 179 } |
| 180 |
| 181 TEST_F(ArcSupportHostTest, TermsOfServiceRetryOnError) { |
| 182 MockTermsOfServiceDelegate* tos_delegate = CreateMockTermsOfServiceDelegate(); |
| 183 support_host()->SetTermsOfServiceDelegate(tos_delegate); |
| 184 MockErrorDelegate* error_delegate = CreateMockErrorDelegate(); |
| 185 support_host()->SetErrorDelegate(error_delegate); |
| 186 |
| 187 support_host()->ShowError(ArcSupportHost::Error::NETWORK_UNAVAILABLE_ERROR, |
| 188 false /* should_show_send_feedback */); |
| 189 |
| 190 EXPECT_CALL(*tos_delegate, OnTermsRetryClicked()); |
| 191 EXPECT_CALL(*error_delegate, OnWindowClosed()).Times(0); |
| 192 fake_arc_support()->ClickRetryButton(); |
| 193 } |
| 194 |
| 195 TEST_F(ArcSupportHostTest, CloseWindowDuringManualAuth) { |
| 196 // No TermsOfServiceDelegate since it is not ongoing. |
| 197 MockErrorDelegate* error_delegate = CreateMockErrorDelegate(); |
| 198 support_host()->SetErrorDelegate(error_delegate); |
| 199 MockAuthDelegate* auth_delegate = CreateMockAuthDelegate(); |
| 200 support_host()->SetAuthDelegate(auth_delegate); |
| 201 |
| 202 support_host()->ShowArcLoading(); |
| 203 |
| 204 EXPECT_CALL(*error_delegate, OnWindowClosed()); |
| 205 fake_arc_support()->Close(); |
| 206 } |
| 207 |
| 208 TEST_F(ArcSupportHostTest, CloseWindowWithoutTermsOfServiceOrAuthOnging) { |
| 209 // No TermsOfServiceDelegate since it is not ongoing. |
| 210 MockErrorDelegate* error_delegate = CreateMockErrorDelegate(); |
| 211 support_host()->SetErrorDelegate(error_delegate); |
| 212 |
| 213 support_host()->ShowArcLoading(); |
| 214 |
| 215 EXPECT_CALL(*error_delegate, OnWindowClosed()); |
| 216 fake_arc_support()->Close(); |
| 217 } |
| 218 |
| 219 TEST_F(ArcSupportHostTest, RetryOnGeneralError) { |
| 220 // No TermsOfServiceDelegate and AuthDelegate since it is not ongoing. |
| 221 MockErrorDelegate* error_delegate = CreateMockErrorDelegate(); |
| 222 support_host()->SetErrorDelegate(error_delegate); |
| 223 |
| 224 support_host()->ShowError(ArcSupportHost::Error::NETWORK_UNAVAILABLE_ERROR, |
| 225 false /* should_show_send_feedback */); |
| 226 |
| 227 EXPECT_CALL(*error_delegate, OnRetryClicked()); |
| 228 fake_arc_support()->ClickRetryButton(); |
| 229 } |
| 230 |
| 231 TEST_F(ArcSupportHostTest, SendFeedbackOnError) { |
| 232 MockErrorDelegate* error_delegate = CreateMockErrorDelegate(); |
| 233 support_host()->SetErrorDelegate(error_delegate); |
| 234 |
| 235 support_host()->ShowError(ArcSupportHost::Error::NETWORK_UNAVAILABLE_ERROR, |
| 236 true /* should_show_send_feedback */); |
| 237 |
| 238 EXPECT_CALL(*error_delegate, OnSendFeedbackClicked()); |
| 239 fake_arc_support()->ClickSendFeedbackButton(); |
| 240 } |
| 241 |
| 242 } // namespace |
| OLD | NEW |