| 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(OnOptInAborted, 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 MockAuthDelegate* auth_delegate = CreateMockAuthDelegate(); |
| 120 support_host()->SetAuthDelegate(auth_delegate); |
| 121 |
| 122 support_host()->ShowLso(); |
| 123 |
| 124 EXPECT_CALL(*auth_delegate, OnAuthSucceeded(Eq("fake_code"))); |
| 125 fake_arc_support()->EmulateAuthCodeResponse("fake_code"); |
| 126 } |
| 127 |
| 128 TEST_F(ArcSupportHostTest, AuthFailed) { |
| 129 MockAuthDelegate* auth_delegate = CreateMockAuthDelegate(); |
| 130 support_host()->SetAuthDelegate(auth_delegate); |
| 131 |
| 132 support_host()->ShowLso(); |
| 133 |
| 134 EXPECT_CALL(*auth_delegate, OnAuthFailed()); |
| 135 fake_arc_support()->EmulateAuthFailure(); |
| 136 } |
| 137 |
| 138 TEST_F(ArcSupportHostTest, AuthRetryOnError) { |
| 139 // Auth error can only happen after terms accepted. |
| 140 MockAuthDelegate* auth_delegate = CreateMockAuthDelegate(); |
| 141 support_host()->SetAuthDelegate(auth_delegate); |
| 142 MockErrorDelegate* error_delegate = CreateMockErrorDelegate(); |
| 143 support_host()->SetErrorDelegate(error_delegate); |
| 144 |
| 145 support_host()->ShowError(ArcSupportHost::Error::NETWORK_UNAVAILABLE_ERROR, |
| 146 false /* should_show_send_feedback */); |
| 147 |
| 148 EXPECT_CALL(*auth_delegate, OnAuthRetryClicked()); |
| 149 EXPECT_CALL(*error_delegate, OnOptInAborted()).Times(0); |
| 150 fake_arc_support()->ClickRetryButton(); |
| 151 } |
| 152 |
| 153 TEST_F(ArcSupportHostTest, TermsOfServiceAccept) { |
| 154 MockTermsOfServiceDelegate tos_delegate; |
| 155 support_host()->SetTermsOfServiceDelegate(&tos_delegate); |
| 156 |
| 157 support_host()->ShowTermsOfService(); |
| 158 fake_arc_support()->set_metrics_mode(false); // just to add some diversity |
| 159 fake_arc_support()->set_backup_and_restore_mode(true); |
| 160 fake_arc_support()->set_location_service_mode(true); |
| 161 |
| 162 EXPECT_CALL(tos_delegate, OnTermsAgreed(false, true, true)); |
| 163 fake_arc_support()->ClickAgreeButton(); |
| 164 } |
| 165 |
| 166 TEST_F(ArcSupportHostTest, TermsOfServiceRejectOrCloseWindow) { |
| 167 MockTermsOfServiceDelegate* tos_delegate = CreateMockTermsOfServiceDelegate(); |
| 168 support_host()->SetTermsOfServiceDelegate(tos_delegate); |
| 169 MockErrorDelegate* error_delegate = CreateMockErrorDelegate(); |
| 170 support_host()->SetErrorDelegate(error_delegate); |
| 171 |
| 172 support_host()->ShowTermsOfService(); |
| 173 |
| 174 EXPECT_CALL(*tos_delegate, OnTermsRejected()); |
| 175 EXPECT_CALL(*error_delegate, OnOptInAborted()).Times(0); |
| 176 // Rejecting ToS and closing the window when ToS is showing is the same thing. |
| 177 fake_arc_support()->Close(); |
| 178 } |
| 179 |
| 180 TEST_F(ArcSupportHostTest, TermsOfServiceRetryOnError) { |
| 181 MockTermsOfServiceDelegate* tos_delegate = CreateMockTermsOfServiceDelegate(); |
| 182 support_host()->SetTermsOfServiceDelegate(tos_delegate); |
| 183 MockErrorDelegate* error_delegate = CreateMockErrorDelegate(); |
| 184 support_host()->SetErrorDelegate(error_delegate); |
| 185 |
| 186 support_host()->ShowError(ArcSupportHost::Error::NETWORK_UNAVAILABLE_ERROR, |
| 187 false /* should_show_send_feedback */); |
| 188 |
| 189 EXPECT_CALL(*tos_delegate, OnTermsRetryClicked()); |
| 190 EXPECT_CALL(*error_delegate, OnOptInAborted()).Times(0); |
| 191 fake_arc_support()->ClickRetryButton(); |
| 192 } |
| 193 |
| 194 TEST_F(ArcSupportHostTest, CloseWindowDuringManualAuth) { |
| 195 // No TermsOfServiceDelegate since it is not ongoing. |
| 196 MockErrorDelegate* error_delegate = CreateMockErrorDelegate(); |
| 197 support_host()->SetErrorDelegate(error_delegate); |
| 198 MockAuthDelegate* auth_delegate = CreateMockAuthDelegate(); |
| 199 support_host()->SetAuthDelegate(auth_delegate); |
| 200 |
| 201 support_host()->ShowArcLoading(); |
| 202 |
| 203 EXPECT_CALL(*error_delegate, OnOptInAborted()); |
| 204 fake_arc_support()->Close(); |
| 205 } |
| 206 |
| 207 TEST_F(ArcSupportHostTest, CloseWindowWithoutTermsOfServiceOrAuthOnging) { |
| 208 // No TermsOfServiceDelegate since it is not ongoing. |
| 209 MockErrorDelegate* error_delegate = CreateMockErrorDelegate(); |
| 210 support_host()->SetErrorDelegate(error_delegate); |
| 211 |
| 212 support_host()->ShowArcLoading(); |
| 213 |
| 214 EXPECT_CALL(*error_delegate, OnOptInAborted()); |
| 215 fake_arc_support()->Close(); |
| 216 } |
| 217 |
| 218 TEST_F(ArcSupportHostTest, RetryOnGeneralError) { |
| 219 // No TermsOfServiceDelegate and AuthDelegate since it is not ongoing. |
| 220 MockErrorDelegate* error_delegate = CreateMockErrorDelegate(); |
| 221 support_host()->SetErrorDelegate(error_delegate); |
| 222 |
| 223 support_host()->ShowError(ArcSupportHost::Error::NETWORK_UNAVAILABLE_ERROR, |
| 224 false /* should_show_send_feedback */); |
| 225 |
| 226 EXPECT_CALL(*error_delegate, OnRetryClicked()); |
| 227 fake_arc_support()->ClickRetryButton(); |
| 228 } |
| 229 |
| 230 TEST_F(ArcSupportHostTest, SendFeedbackOnError) { |
| 231 MockErrorDelegate* error_delegate = CreateMockErrorDelegate(); |
| 232 support_host()->SetErrorDelegate(error_delegate); |
| 233 |
| 234 support_host()->ShowError(ArcSupportHost::Error::NETWORK_UNAVAILABLE_ERROR, |
| 235 true /* should_show_send_feedback */); |
| 236 |
| 237 EXPECT_CALL(*error_delegate, OnSendFeedbackClicked()); |
| 238 fake_arc_support()->ClickSendFeedbackButton(); |
| 239 } |
| 240 |
| 241 } // namespace |
| OLD | NEW |