Chromium Code Reviews| Index: chrome/browser/chromeos/arc/arc_support_host_unittest.cc |
| diff --git a/chrome/browser/chromeos/arc/arc_support_host_unittest.cc b/chrome/browser/chromeos/arc/arc_support_host_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3c68c576a46fdf3b108abfa261ed458a6ec1eeea |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/arc/arc_support_host_unittest.cc |
| @@ -0,0 +1,206 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/arc/arc_support_host.h" |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "chrome/browser/chromeos/arc/extensions/fake_arc_support.h" |
| +#include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" |
| +#include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
| +#include "chrome/test/base/testing_profile.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using testing::_; |
| +using testing::Eq; |
| + |
| +namespace arc { |
| + |
| +class ArcSupportHostTest : public testing::Test { |
|
Luis Héctor Chávez
2017/05/12 15:41:31
Can this class (and the rest of the tests) be in t
victorhsieh0
2017/05/12 18:31:55
Done.
|
| + public: |
| + ArcSupportHostTest() = default; |
| + ~ArcSupportHostTest() override = default; |
| + |
| + void SetUp() override { |
| + user_manager_enabler_ = |
| + base::MakeUnique<chromeos::ScopedUserManagerEnabler>( |
| + new chromeos::FakeChromeUserManager()); |
| + |
| + profile_ = base::MakeUnique<TestingProfile>(); |
| + support_host_ = base::MakeUnique<ArcSupportHost>(profile_.get()); |
| + fake_arc_support_ = base::MakeUnique<FakeArcSupport>(support_host_.get()); |
| + } |
| + |
| + void TearDown() override { |
| + fake_arc_support_.reset(); |
| + support_host_.reset(); |
| + profile_.reset(); |
| + user_manager_enabler_.reset(); |
| + } |
| + |
| + ArcSupportHost* support_host() { return support_host_.get(); } |
| + FakeArcSupport* fake_arc_support() { return fake_arc_support_.get(); } |
| + |
| + protected: |
| + class MockAuthDelegate : public ArcSupportHost::AuthDelegate { |
|
Luis Héctor Chávez
2017/05/12 15:41:31
Can these three classes be in the anonymous namesp
victorhsieh0
2017/05/12 18:31:55
Done.
|
| + public: |
| + MOCK_METHOD1(OnAuthSucceeded, void(const std::string& auth_code)); |
| + MOCK_METHOD0(OnAuthFailed, void()); |
| + MOCK_METHOD0(OnAuthRetry, void()); |
| + }; |
| + |
| + class MockTermsOfServiceDelegate |
| + : public ArcSupportHost::TermsOfServiceDelegate { |
| + public: |
| + MOCK_METHOD3(OnTermsAgreed, |
| + void(bool is_metrics_enabled, |
| + bool is_backup_and_restore_enabled, |
| + bool is_location_service_enabled)); |
| + MOCK_METHOD0(OnTermsRejected, void()); |
| + MOCK_METHOD0(OnTermsRetry, void()); |
| + }; |
| + |
| + class MockErrorDelegate : public ArcSupportHost::ErrorDelegate { |
| + public: |
| + MOCK_METHOD0(OnOptInAborted, void()); |
| + MOCK_METHOD0(OnRetryClicked, void()); |
| + MOCK_METHOD0(OnSendFeedbackClicked, void()); |
| + }; |
| + |
| + private: |
| + // Fake as if the current testing thread is UI thread. |
| + content::TestBrowserThreadBundle bundle_; |
| + |
| + std::unique_ptr<TestingProfile> profile_; |
| + std::unique_ptr<ArcSupportHost> support_host_; |
| + std::unique_ptr<FakeArcSupport> fake_arc_support_; |
| + std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ArcSupportHostTest); |
| +}; |
| + |
| +TEST_F(ArcSupportHostTest, AuthSucceeded) { |
| + MockAuthDelegate auth_delegate; |
| + support_host()->SetAuthDelegate(&auth_delegate); |
| + support_host()->ShowLso(); |
| + |
| + EXPECT_CALL(auth_delegate, OnAuthSucceeded(Eq("fake_code"))); |
| + fake_arc_support()->EmulateAuthCodeResponse("fake_code"); |
| + |
| + support_host()->SetAuthDelegate(nullptr); |
|
Luis Héctor Chávez
2017/05/12 15:41:31
(optional) Maybe add an RAII ScopedAuthDelegate? N
victorhsieh0
2017/05/12 18:31:55
Done.
|
| +} |
| + |
| +TEST_F(ArcSupportHostTest, AuthFailed) { |
| + MockAuthDelegate auth_delegate; |
| + support_host()->SetAuthDelegate(&auth_delegate); |
| + support_host()->ShowLso(); |
| + |
| + EXPECT_CALL(auth_delegate, OnAuthFailed()); |
| + fake_arc_support()->EmulateAuthFailure(); |
| + |
| + support_host()->SetAuthDelegate(nullptr); |
| +} |
| + |
| +TEST_F(ArcSupportHostTest, AuthRetryOnError) { |
| + // Auth error can only happen after terms accepted. |
| + MockAuthDelegate auth_delegate; |
| + support_host()->SetAuthDelegate(&auth_delegate); |
| + support_host()->ShowError(ArcSupportHost::Error::NETWORK_UNAVAILABLE_ERROR, |
| + false /* should_show_send_feedback */); |
| + |
| + EXPECT_CALL(auth_delegate, OnAuthRetry()); |
| + fake_arc_support()->ClickRetryButton(); |
| + |
| + support_host()->SetAuthDelegate(nullptr); |
| +} |
| + |
| +TEST_F(ArcSupportHostTest, TermsOfServiceAccept) { |
| + MockTermsOfServiceDelegate tos_delegate; |
| + support_host()->SetTermsOfServiceDelegate(&tos_delegate); |
| + support_host()->ShowTermsOfService(); |
| + |
| + EXPECT_CALL(tos_delegate, OnTermsAgreed(_, _, _)); |
| + fake_arc_support()->ClickAgreeButton(); |
| + |
| + support_host()->SetTermsOfServiceDelegate(nullptr); |
| +} |
| + |
| +TEST_F(ArcSupportHostTest, TermsOfServiceRejectOrCloseWindow) { |
| + MockTermsOfServiceDelegate tos_delegate; |
| + support_host()->SetTermsOfServiceDelegate(&tos_delegate); |
| + support_host()->ShowTermsOfService(); |
| + |
| + EXPECT_CALL(tos_delegate, OnTermsRejected()); |
| + // Rejecting ToS and closing the window when ToS is showing is the same thing. |
| + fake_arc_support()->Close(); |
| + |
| + support_host()->SetTermsOfServiceDelegate(nullptr); |
| +} |
| + |
| +TEST_F(ArcSupportHostTest, TermsOfServiceRetryOnError) { |
| + MockTermsOfServiceDelegate tos_delegate; |
| + support_host()->SetTermsOfServiceDelegate(&tos_delegate); |
| + support_host()->ShowError(ArcSupportHost::Error::NETWORK_UNAVAILABLE_ERROR, |
| + false /* should_show_send_feedback */); |
| + |
| + EXPECT_CALL(tos_delegate, OnTermsRetry()); |
| + fake_arc_support()->ClickRetryButton(); |
| + |
| + support_host()->SetTermsOfServiceDelegate(nullptr); |
| +} |
| + |
| +TEST_F(ArcSupportHostTest, CloseWindowDuringManualAuth) { |
| + // No TermsOfServiceDelegate since it is not ongoing. |
| + MockErrorDelegate error_delegate; |
| + support_host()->SetErrorDelegate(&error_delegate); |
| + MockAuthDelegate auth_delegate; |
| + support_host()->SetAuthDelegate(&auth_delegate); |
| + support_host()->ShowArcLoading(); |
| + |
| + EXPECT_CALL(error_delegate, OnOptInAborted()); |
|
Luis Héctor Chávez
2017/05/12 15:41:31
Is there an easy way to express that you expect so
victorhsieh0
2017/05/12 18:31:55
.Times(0). But AuthDelegate does not have the rel
|
| + fake_arc_support()->Close(); |
| + |
| + support_host()->SetAuthDelegate(nullptr); |
| + support_host()->SetErrorDelegate(nullptr); |
| +} |
| + |
| +TEST_F(ArcSupportHostTest, CloseWindowWithoutTermsOfServiceOrAuthOnging) { |
| + // No TermsOfServiceDelegate since it is not ongoing. |
| + MockErrorDelegate error_delegate; |
| + support_host()->SetErrorDelegate(&error_delegate); |
| + support_host()->ShowArcLoading(); |
| + |
| + EXPECT_CALL(error_delegate, OnOptInAborted()); |
| + fake_arc_support()->Close(); |
| + |
| + support_host()->SetErrorDelegate(nullptr); |
| +} |
| + |
| +TEST_F(ArcSupportHostTest, RetryOnGeneralError) { |
| + // No TermsOfServiceDelegate and AuthDelegate since it is not ongoing. |
| + MockErrorDelegate error_delegate; |
| + support_host()->SetErrorDelegate(&error_delegate); |
| + support_host()->ShowError(ArcSupportHost::Error::NETWORK_UNAVAILABLE_ERROR, |
| + false /* should_show_send_feedback */); |
| + |
| + EXPECT_CALL(error_delegate, OnRetryClicked()); |
| + fake_arc_support()->ClickRetryButton(); |
| + |
| + support_host()->SetErrorDelegate(nullptr); |
| +} |
| + |
| +TEST_F(ArcSupportHostTest, SendFeedbackOnError) { |
| + MockErrorDelegate error_delegate; |
| + support_host()->SetErrorDelegate(&error_delegate); |
| + support_host()->ShowError(ArcSupportHost::Error::NETWORK_UNAVAILABLE_ERROR, |
| + true /* should_show_send_feedback */); |
| + |
| + EXPECT_CALL(error_delegate, OnSendFeedbackClicked()); |
| + fake_arc_support()->ClickSendFeedbackButton(); |
| + |
| + support_host()->SetErrorDelegate(nullptr); |
| +} |
| + |
| +} // namespace arc |