| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync/sync_global_error.h" | 5 #include "chrome/browser/sync/sync_global_error.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/prefs/pref_service_mock_builder.h" |
| 10 #include "chrome/browser/prefs/testing_pref_store.h" |
| 9 #include "chrome/browser/sync/profile_sync_service_mock.h" | 11 #include "chrome/browser/sync/profile_sync_service_mock.h" |
| 12 #include "chrome/browser/sync/signin_manager.h" |
| 13 #include "chrome/common/pref_names.h" |
| 14 #include "chrome/test/base/testing_profile.h" |
| 10 #include "content/test/test_browser_thread.h" | 15 #include "content/test/test_browser_thread.h" |
| 11 #include "testing/gmock/include/gmock/gmock-actions.h" | 16 #include "testing/gmock/include/gmock/gmock-actions.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 19 |
| 15 using ::testing::Return; | 20 using ::testing::Return; |
| 16 using ::testing::NiceMock; | 21 using ::testing::NiceMock; |
| 17 using content::BrowserThread; | 22 using content::BrowserThread; |
| 18 | 23 |
| 19 namespace { | 24 namespace { |
| 20 | 25 |
| 21 // Utility function to test that SyncGlobalError behaves correct for the given | 26 // Utility function to test that SyncGlobalError behaves correct for the given |
| 22 // error condition. | 27 // error condition. |
| 23 void VerifySyncGlobalErrorResult(NiceMock<ProfileSyncServiceMock>* service, | 28 void VerifySyncGlobalErrorResult(NiceMock<ProfileSyncServiceMock>* service, |
| 24 SyncGlobalError* error, | 29 SyncGlobalError* error, |
| 25 GoogleServiceAuthError::State error_state, | 30 GoogleServiceAuthError::State error_state, |
| 26 bool is_signed_in, | 31 bool is_signed_in, |
| 27 bool is_error) { | 32 bool is_error) { |
| 28 GoogleServiceAuthError auth_error(error_state); | 33 GoogleServiceAuthError auth_error(error_state); |
| 29 service->UpdateAuthErrorState(auth_error); | 34 service->UpdateAuthErrorState(auth_error); |
| 30 | 35 |
| 31 EXPECT_CALL(*service, HasSyncSetupCompleted()) | 36 EXPECT_CALL(*service, HasSyncSetupCompleted()) |
| 32 .WillRepeatedly(Return(is_signed_in)); | 37 .WillRepeatedly(Return(is_signed_in)); |
| 33 if (error_state == GoogleServiceAuthError::SERVICE_UNAVAILABLE) { | |
| 34 EXPECT_CALL(*service, GetAuthenticatedUsername()) | |
| 35 .WillRepeatedly(Return(UTF8ToUTF16(""))); | |
| 36 } else { | |
| 37 EXPECT_CALL(*service, GetAuthenticatedUsername()) | |
| 38 .WillRepeatedly(Return(UTF8ToUTF16("foo"))); | |
| 39 } | |
| 40 | 38 |
| 41 error->OnStateChanged(); | 39 error->OnStateChanged(); |
| 42 | 40 |
| 43 // If there is an error then a wrench button badge, menu item, and bubble view | 41 // If there is an error then a wrench button badge, menu item, and bubble view |
| 44 // should be shown. | 42 // should be shown. |
| 45 EXPECT_EQ(error->HasBadge(), is_error); | 43 EXPECT_EQ(error->HasBadge(), is_error); |
| 46 EXPECT_EQ(error->HasMenuItem() || error->HasCustomizedSyncMenuItem(), | 44 EXPECT_EQ(error->HasMenuItem() || error->HasCustomizedSyncMenuItem(), |
| 47 is_error); | 45 is_error); |
| 48 EXPECT_EQ(error->HasBubbleView(), is_error); | 46 EXPECT_EQ(error->HasBubbleView(), is_error); |
| 49 | 47 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 60 // Test message handler. | 58 // Test message handler. |
| 61 if (is_error) { | 59 if (is_error) { |
| 62 EXPECT_CALL(*service, ShowErrorUI()); | 60 EXPECT_CALL(*service, ShowErrorUI()); |
| 63 error->ExecuteMenuItem(NULL); | 61 error->ExecuteMenuItem(NULL); |
| 64 EXPECT_CALL(*service, ShowErrorUI()); | 62 EXPECT_CALL(*service, ShowErrorUI()); |
| 65 error->BubbleViewAcceptButtonPressed(); | 63 error->BubbleViewAcceptButtonPressed(); |
| 66 error->BubbleViewDidClose(); | 64 error->BubbleViewDidClose(); |
| 67 } | 65 } |
| 68 } | 66 } |
| 69 | 67 |
| 68 Profile* MakeProfile() { |
| 69 TestingProfile* profile = new TestingProfile(); |
| 70 TestingPrefStore* user_prefs = new TestingPrefStore(); |
| 71 PrefService* prefs = PrefServiceMockBuilder() |
| 72 .WithUserPrefs(user_prefs) |
| 73 .Create(); |
| 74 profile->SetPrefService(prefs); |
| 75 SigninManager::RegisterUserPrefs(prefs); |
| 76 user_prefs->SetString(prefs::kGoogleServicesUsername, "foo"); |
| 77 return profile; |
| 78 } |
| 79 |
| 70 } // namespace | 80 } // namespace |
| 71 | 81 |
| 72 // Test that SyncGlobalError shows an error if a passphrase is required. | 82 // Test that SyncGlobalError shows an error if a passphrase is required. |
| 73 TEST(SyncGlobalErrorTest, PassphraseGlobalError) { | 83 TEST(SyncGlobalErrorTest, PassphraseGlobalError) { |
| 74 MessageLoopForUI message_loop; | 84 MessageLoopForUI message_loop; |
| 75 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); | 85 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); |
| 76 NiceMock<ProfileSyncServiceMock> service; | 86 NiceMock<ProfileSyncServiceMock> service(MakeProfile()); |
| 77 SyncGlobalError error(&service); | 87 SyncGlobalError error(&service); |
| 78 | 88 |
| 79 EXPECT_CALL(service, IsPassphraseRequired()) | 89 EXPECT_CALL(service, IsPassphraseRequired()) |
| 80 .WillRepeatedly(Return(true)); | 90 .WillRepeatedly(Return(true)); |
| 81 EXPECT_CALL(service, IsPassphraseRequiredForDecryption()) | 91 EXPECT_CALL(service, IsPassphraseRequiredForDecryption()) |
| 82 .WillRepeatedly(Return(true)); | 92 .WillRepeatedly(Return(true)); |
| 83 VerifySyncGlobalErrorResult( | 93 VerifySyncGlobalErrorResult( |
| 84 &service, &error, GoogleServiceAuthError::NONE, true, true); | 94 &service, &error, GoogleServiceAuthError::NONE, true, true); |
| 85 } | 95 } |
| 86 | 96 |
| 87 // Test that SyncGlobalError shows an error for conditions that can be resolved | 97 // Test that SyncGlobalError shows an error for conditions that can be resolved |
| 88 // by the user and suppresses errors for conditions that cannot be resolved by | 98 // by the user and suppresses errors for conditions that cannot be resolved by |
| 89 // the user. | 99 // the user. |
| 90 TEST(SyncGlobalErrorTest, AuthStateGlobalError) { | 100 TEST(SyncGlobalErrorTest, AuthStateGlobalError) { |
| 91 MessageLoopForUI message_loop; | 101 MessageLoopForUI message_loop; |
| 92 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); | 102 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); |
| 93 NiceMock<ProfileSyncServiceMock> service; | 103 NiceMock<ProfileSyncServiceMock> service(MakeProfile()); |
| 94 SyncGlobalError error(&service); | 104 SyncGlobalError error(&service); |
| 95 | 105 |
| 96 browser_sync::SyncBackendHost::Status status; | 106 browser_sync::SyncBackendHost::Status status; |
| 97 EXPECT_CALL(service, QueryDetailedSyncStatus()) | 107 EXPECT_CALL(service, QueryDetailedSyncStatus()) |
| 98 .WillRepeatedly(Return(status)); | 108 .WillRepeatedly(Return(status)); |
| 99 | 109 |
| 100 struct { | 110 struct { |
| 101 GoogleServiceAuthError::State error_state; | 111 GoogleServiceAuthError::State error_state; |
| 102 bool is_error; | 112 bool is_error; |
| 103 } table[] = { | 113 } table[] = { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 114 { GoogleServiceAuthError::HOSTED_NOT_ALLOWED, true }, | 124 { GoogleServiceAuthError::HOSTED_NOT_ALLOWED, true }, |
| 115 }; | 125 }; |
| 116 | 126 |
| 117 for (size_t i = 0; i < sizeof(table)/sizeof(*table); ++i) { | 127 for (size_t i = 0; i < sizeof(table)/sizeof(*table); ++i) { |
| 118 VerifySyncGlobalErrorResult( | 128 VerifySyncGlobalErrorResult( |
| 119 &service, &error, table[i].error_state, true, table[i].is_error); | 129 &service, &error, table[i].error_state, true, table[i].is_error); |
| 120 VerifySyncGlobalErrorResult( | 130 VerifySyncGlobalErrorResult( |
| 121 &service, &error, table[i].error_state, false, false); | 131 &service, &error, table[i].error_state, false, false); |
| 122 } | 132 } |
| 123 } | 133 } |
| OLD | NEW |