| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/sync/sync_global_error.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/memory/ptr_util.h" | |
| 10 #include "chrome/browser/chrome_notification_types.h" | |
| 11 #include "chrome/browser/sync/profile_sync_test_util.h" | |
| 12 #include "chrome/browser/sync/sync_global_error_factory.h" | |
| 13 #include "chrome/browser/ui/browser.h" | |
| 14 #include "chrome/browser/ui/global_error/global_error_service_factory.h" | |
| 15 #include "chrome/browser/ui/webui/signin/login_ui_service.h" | |
| 16 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" | |
| 17 #include "chrome/test/base/browser_with_test_window_test.h" | |
| 18 #include "chrome/test/base/testing_profile.h" | |
| 19 #include "components/signin/core/common/profile_management_switches.h" | |
| 20 #include "content/public/browser/notification_observer.h" | |
| 21 #include "content/public/browser/notification_registrar.h" | |
| 22 #include "content/public/browser/notification_source.h" | |
| 23 #include "testing/gmock/include/gmock/gmock.h" | |
| 24 #include "testing/gtest/include/gtest/gtest.h" | |
| 25 | |
| 26 using ::testing::NiceMock; | |
| 27 using ::testing::Return; | |
| 28 using ::testing::ReturnRef; | |
| 29 using ::testing::_; | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 class FakeLoginUIService: public LoginUIService { | |
| 34 public: | |
| 35 FakeLoginUIService() : LoginUIService(nullptr) {} | |
| 36 }; | |
| 37 | |
| 38 class FakeLoginUI : public LoginUIService::LoginUI { | |
| 39 public: | |
| 40 FakeLoginUI() : focus_ui_call_count_(0) {} | |
| 41 | |
| 42 ~FakeLoginUI() override {} | |
| 43 | |
| 44 int focus_ui_call_count() const { return focus_ui_call_count_; } | |
| 45 | |
| 46 private: | |
| 47 // Overridden from LoginUIService::LoginUI: | |
| 48 void FocusUI() override { ++focus_ui_call_count_; } | |
| 49 | |
| 50 int focus_ui_call_count_; | |
| 51 }; | |
| 52 | |
| 53 std::unique_ptr<KeyedService> BuildMockLoginUIService( | |
| 54 content::BrowserContext* profile) { | |
| 55 return base::MakeUnique<FakeLoginUIService>(); | |
| 56 } | |
| 57 | |
| 58 // Same as BrowserWithTestWindowTest, but uses MockBrowser to test calls to | |
| 59 // ExecuteCommand method. | |
| 60 class SyncGlobalErrorTest : public BrowserWithTestWindowTest { | |
| 61 public: | |
| 62 SyncGlobalErrorTest() {} | |
| 63 ~SyncGlobalErrorTest() override {} | |
| 64 | |
| 65 void SetUp() override { | |
| 66 profile_ = MakeSignedInTestingProfile(); | |
| 67 | |
| 68 BrowserWithTestWindowTest::SetUp(); | |
| 69 } | |
| 70 | |
| 71 Profile* profile() { return profile_.get(); } | |
| 72 | |
| 73 private: | |
| 74 std::unique_ptr<TestingProfile> profile_; | |
| 75 | |
| 76 DISALLOW_COPY_AND_ASSIGN(SyncGlobalErrorTest); | |
| 77 }; | |
| 78 | |
| 79 // Utility function to test that SyncGlobalError behaves correctly for the given | |
| 80 // error condition. | |
| 81 void VerifySyncGlobalErrorResult(browser_sync::ProfileSyncServiceMock* service, | |
| 82 FakeLoginUIService* login_ui_service, | |
| 83 Browser* browser, | |
| 84 syncer::SyncErrorController* error, | |
| 85 SyncGlobalError* global_error, | |
| 86 GoogleServiceAuthError::State error_state, | |
| 87 bool is_signed_in, | |
| 88 bool is_error) { | |
| 89 EXPECT_CALL(*service, IsFirstSetupComplete()) | |
| 90 .WillRepeatedly(Return(is_signed_in)); | |
| 91 | |
| 92 GoogleServiceAuthError auth_error(error_state); | |
| 93 EXPECT_CALL(*service, GetAuthError()).WillRepeatedly(ReturnRef(auth_error)); | |
| 94 | |
| 95 error->OnStateChanged(service); | |
| 96 EXPECT_EQ(is_error, error->HasError()); | |
| 97 | |
| 98 // If there is an error then a menu item and bubble view should be shown. | |
| 99 EXPECT_EQ(is_error, global_error->HasMenuItem()); | |
| 100 EXPECT_EQ(is_error, global_error->HasBubbleView()); | |
| 101 | |
| 102 // If there is an error then labels should not be empty. | |
| 103 EXPECT_NE(0, global_error->MenuItemCommandID()); | |
| 104 EXPECT_NE(is_error, global_error->MenuItemLabel().empty()); | |
| 105 EXPECT_NE(is_error, global_error->GetBubbleViewAcceptButtonLabel().empty()); | |
| 106 | |
| 107 // We never have a cancel button. | |
| 108 EXPECT_TRUE(global_error->GetBubbleViewCancelButtonLabel().empty()); | |
| 109 // We always return a hardcoded title. | |
| 110 EXPECT_FALSE(global_error->GetBubbleViewTitle().empty()); | |
| 111 | |
| 112 // Test message handler. | |
| 113 if (is_error) { | |
| 114 FakeLoginUI* login_ui = static_cast<FakeLoginUI*>( | |
| 115 login_ui_service->current_login_ui()); | |
| 116 global_error->ExecuteMenuItem(browser); | |
| 117 ASSERT_GT(login_ui->focus_ui_call_count(), 0); | |
| 118 global_error->BubbleViewAcceptButtonPressed(browser); | |
| 119 global_error->BubbleViewDidClose(browser); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 } // namespace | |
| 124 | |
| 125 // Test that SyncGlobalError shows an error if a passphrase is required. | |
| 126 TEST_F(SyncGlobalErrorTest, PassphraseGlobalError) { | |
| 127 // The MD User Menu displays Sync errors in a different way and should be the | |
| 128 // only one to do so. Under that paradigm, this test is obsolete. | |
| 129 if (switches::IsMaterialDesignUserMenu()) | |
| 130 return; | |
| 131 | |
| 132 browser_sync::ProfileSyncServiceMock service( | |
| 133 CreateProfileSyncServiceParamsForTest(profile())); | |
| 134 | |
| 135 FakeLoginUIService* login_ui_service = static_cast<FakeLoginUIService*>( | |
| 136 LoginUIServiceFactory::GetInstance()->SetTestingFactoryAndUse( | |
| 137 profile(), BuildMockLoginUIService)); | |
| 138 FakeLoginUI login_ui; | |
| 139 login_ui_service->SetLoginUI(&login_ui); | |
| 140 | |
| 141 syncer::SyncErrorController error(&service); | |
| 142 SyncGlobalError global_error( | |
| 143 GlobalErrorServiceFactory::GetForProfile(profile()), login_ui_service, | |
| 144 &error, &service); | |
| 145 | |
| 146 syncer::SyncEngine::Status status; | |
| 147 EXPECT_CALL(service, QueryDetailedSyncStatus(_)) | |
| 148 .WillRepeatedly(Return(false)); | |
| 149 | |
| 150 EXPECT_CALL(service, IsPassphraseRequired()) | |
| 151 .WillRepeatedly(Return(true)); | |
| 152 EXPECT_CALL(service, IsPassphraseRequiredForDecryption()) | |
| 153 .WillRepeatedly(Return(true)); | |
| 154 VerifySyncGlobalErrorResult( | |
| 155 &service, login_ui_service, browser(), &error, &global_error, | |
| 156 GoogleServiceAuthError::NONE, true /* signed in*/, true /* error */); | |
| 157 | |
| 158 // Check that no menu item is shown if there is no error. | |
| 159 EXPECT_CALL(service, IsPassphraseRequired()) | |
| 160 .WillRepeatedly(Return(false)); | |
| 161 EXPECT_CALL(service, IsPassphraseRequiredForDecryption()) | |
| 162 .WillRepeatedly(Return(false)); | |
| 163 VerifySyncGlobalErrorResult( | |
| 164 &service, login_ui_service, browser(), &error, &global_error, | |
| 165 GoogleServiceAuthError::NONE, true /* signed in */, false /* no error */); | |
| 166 | |
| 167 // Check that no menu item is shown if sync setup is not completed. | |
| 168 EXPECT_CALL(service, IsPassphraseRequired()) | |
| 169 .WillRepeatedly(Return(true)); | |
| 170 EXPECT_CALL(service, IsPassphraseRequiredForDecryption()) | |
| 171 .WillRepeatedly(Return(true)); | |
| 172 VerifySyncGlobalErrorResult( | |
| 173 &service, login_ui_service, browser(), &error, &global_error, | |
| 174 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, | |
| 175 false /* not signed in */, false /* no error */); | |
| 176 | |
| 177 global_error.Shutdown(); | |
| 178 } | |
| OLD | NEW |