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 scoped_ptr<Profile> profile(MakeProfile()); |
| 87 NiceMock<ProfileSyncServiceMock> service(profile.get()); |
77 SyncGlobalError error(&service); | 88 SyncGlobalError error(&service); |
78 | 89 |
79 EXPECT_CALL(service, IsPassphraseRequired()) | 90 EXPECT_CALL(service, IsPassphraseRequired()) |
80 .WillRepeatedly(Return(true)); | 91 .WillRepeatedly(Return(true)); |
81 EXPECT_CALL(service, IsPassphraseRequiredForDecryption()) | 92 EXPECT_CALL(service, IsPassphraseRequiredForDecryption()) |
82 .WillRepeatedly(Return(true)); | 93 .WillRepeatedly(Return(true)); |
83 VerifySyncGlobalErrorResult( | 94 VerifySyncGlobalErrorResult( |
84 &service, &error, GoogleServiceAuthError::NONE, true, true); | 95 &service, &error, GoogleServiceAuthError::NONE, true, true); |
85 } | 96 } |
86 | 97 |
87 // Test that SyncGlobalError shows an error for conditions that can be resolved | 98 // 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 | 99 // by the user and suppresses errors for conditions that cannot be resolved by |
89 // the user. | 100 // the user. |
90 TEST(SyncGlobalErrorTest, AuthStateGlobalError) { | 101 TEST(SyncGlobalErrorTest, AuthStateGlobalError) { |
91 MessageLoopForUI message_loop; | 102 MessageLoopForUI message_loop; |
92 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); | 103 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); |
93 NiceMock<ProfileSyncServiceMock> service; | 104 scoped_ptr<Profile> profile(MakeProfile()); |
| 105 NiceMock<ProfileSyncServiceMock> service(profile.get()); |
94 SyncGlobalError error(&service); | 106 SyncGlobalError error(&service); |
95 | 107 |
96 browser_sync::SyncBackendHost::Status status; | 108 browser_sync::SyncBackendHost::Status status; |
97 EXPECT_CALL(service, QueryDetailedSyncStatus()) | 109 EXPECT_CALL(service, QueryDetailedSyncStatus()) |
98 .WillRepeatedly(Return(status)); | 110 .WillRepeatedly(Return(status)); |
99 | 111 |
100 struct { | 112 struct { |
101 GoogleServiceAuthError::State error_state; | 113 GoogleServiceAuthError::State error_state; |
102 bool is_error; | 114 bool is_error; |
103 } table[] = { | 115 } table[] = { |
(...skipping 10 matching lines...) Expand all Loading... |
114 { GoogleServiceAuthError::HOSTED_NOT_ALLOWED, true }, | 126 { GoogleServiceAuthError::HOSTED_NOT_ALLOWED, true }, |
115 }; | 127 }; |
116 | 128 |
117 for (size_t i = 0; i < sizeof(table)/sizeof(*table); ++i) { | 129 for (size_t i = 0; i < sizeof(table)/sizeof(*table); ++i) { |
118 VerifySyncGlobalErrorResult( | 130 VerifySyncGlobalErrorResult( |
119 &service, &error, table[i].error_state, true, table[i].is_error); | 131 &service, &error, table[i].error_state, true, table[i].is_error); |
120 VerifySyncGlobalErrorResult( | 132 VerifySyncGlobalErrorResult( |
121 &service, &error, table[i].error_state, false, false); | 133 &service, &error, table[i].error_state, false, false); |
122 } | 134 } |
123 } | 135 } |
OLD | NEW |