Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: chrome/browser/protector/protector_service_browsertest.cc

Issue 9500020: ProtectorService supports multiple change instances. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Test coverage. Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "base/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "base/message_loop.h" 6 #include "base/message_loop.h"
7 #include "chrome/app/chrome_command_ids.h" 7 #include "chrome/app/chrome_command_ids.h"
8 #include "chrome/browser/protector/mock_setting_change.h" 8 #include "chrome/browser/protector/mock_setting_change.h"
9 #include "chrome/browser/protector/protector_service.h" 9 #include "chrome/browser/protector/protector_service.h"
10 #include "chrome/browser/protector/protector_service_factory.h" 10 #include "chrome/browser/protector/protector_service_factory.h"
(...skipping 15 matching lines...) Expand all
26 class ProtectorServiceTest : public InProcessBrowserTest { 26 class ProtectorServiceTest : public InProcessBrowserTest {
27 public: 27 public:
28 virtual void SetUpOnMainThread() { 28 virtual void SetUpOnMainThread() {
29 protector_service_ = 29 protector_service_ =
30 ProtectorServiceFactory::GetForProfile(browser()->profile()); 30 ProtectorServiceFactory::GetForProfile(browser()->profile());
31 // ProtectService will own this change instance. 31 // ProtectService will own this change instance.
32 mock_change_ = new NiceMock<MockSettingChange>(); 32 mock_change_ = new NiceMock<MockSettingChange>();
33 } 33 }
34 34
35 protected: 35 protected:
36 GlobalError* GetGlobalError() { 36 // Default NULL value for |change| argument means using |mock_change_|.
37 return protector_service_->error_.get(); 37
whywhat 2012/02/29 18:55:14 Default arguments are forbidden by the style guide
Ivan Korotkov 2012/02/29 19:23:44 Done :((
38 GlobalError* GetGlobalError(BaseSettingChange* change = NULL) {
39 if (!change)
40 change = mock_change_;
41 std::vector<ProtectorService::Item>::iterator item =
42 std::find_if(protector_service_->items_.begin(),
43 protector_service_->items_.end(),
44 ProtectorService::MatchItemByChange(change));
45 return item == protector_service_->items_.end() ?
46 NULL : item->error.get();
38 } 47 }
39 48
40 void ExpectGlobalErrorActive(bool active) { 49 // Checks that |protector_service_| has an error instance corresponding to
41 GlobalError* error = 50 // |change| and that GlobalErrorService knows about it.
42 GlobalErrorServiceFactory::GetForProfile(browser()->profile())-> 51 bool IsGlobalErrorActive(BaseSettingChange* change = NULL) {
whywhat 2012/02/29 18:55:14 Ditto.
Ivan Korotkov 2012/02/29 19:23:44 Done :(((
43 GetGlobalErrorByMenuItemCommandID(IDC_SHOW_SETTINGS_CHANGES); 52 if (!change)
44 EXPECT_EQ(active, error != NULL); 53 change = mock_change_;
45 EXPECT_EQ(active, GetGlobalError() != NULL); 54 GlobalError* error = GetGlobalError(change);
46 EXPECT_EQ(active, protector_service_->IsShowingChange()); 55 if (!error)
56 return false;
57 if (!GlobalErrorServiceFactory::GetForProfile(browser()->profile())->
58 GetGlobalErrorByMenuItemCommandID(error->MenuItemCommandID())) {
59 return false;
60 }
61 return protector_service_->IsShowingChange();
47 } 62 }
48 63
49 ProtectorService* protector_service_; 64 ProtectorService* protector_service_;
50 MockSettingChange* mock_change_; 65 MockSettingChange* mock_change_;
51 }; 66 };
52 67
53 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ChangeInitError) { 68 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ChangeInitError) {
54 // Init fails and causes the change to be ignored. 69 // Init fails and causes the change to be ignored.
55 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())). 70 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
56 WillOnce(Return(false)); 71 WillOnce(Return(false));
57 protector_service_->ShowChange(mock_change_); 72 protector_service_->ShowChange(mock_change_);
58 ExpectGlobalErrorActive(false); 73 EXPECT_FALSE(IsGlobalErrorActive());
59 ui_test_utils::RunAllPendingInMessageLoop(); 74 ui_test_utils::RunAllPendingInMessageLoop();
60 ExpectGlobalErrorActive(false); 75 EXPECT_FALSE(IsGlobalErrorActive());
61 } 76 }
62 77
63 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndDismiss) { 78 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndDismiss) {
64 // Show the change and immediately dismiss it. 79 // Show the change and immediately dismiss it.
65 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())). 80 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
66 WillOnce(Return(true)); 81 WillOnce(Return(true));
67 protector_service_->ShowChange(mock_change_); 82 protector_service_->ShowChange(mock_change_);
68 ui_test_utils::RunAllPendingInMessageLoop(); 83 ui_test_utils::RunAllPendingInMessageLoop();
69 ExpectGlobalErrorActive(true); 84 EXPECT_TRUE(IsGlobalErrorActive());
70 protector_service_->DismissChange(); 85 protector_service_->DismissChange(mock_change_);
71 ui_test_utils::RunAllPendingInMessageLoop(); 86 ui_test_utils::RunAllPendingInMessageLoop();
72 ExpectGlobalErrorActive(false); 87 EXPECT_FALSE(IsGlobalErrorActive());
73 } 88 }
74 89
75 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndApply) { 90 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndApply) {
76 // Show the change and apply it. 91 // Show the change and apply it.
77 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())). 92 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
78 WillOnce(Return(true)); 93 WillOnce(Return(true));
79 protector_service_->ShowChange(mock_change_); 94 protector_service_->ShowChange(mock_change_);
80 ui_test_utils::RunAllPendingInMessageLoop(); 95 ui_test_utils::RunAllPendingInMessageLoop();
81 ExpectGlobalErrorActive(true); 96 EXPECT_TRUE(IsGlobalErrorActive());
82 EXPECT_CALL(*mock_change_, Apply(browser())); 97 EXPECT_CALL(*mock_change_, Apply(browser()));
83 protector_service_->ApplyChange(browser()); 98 protector_service_->ApplyChange(mock_change_, browser());
84 ui_test_utils::RunAllPendingInMessageLoop(); 99 ui_test_utils::RunAllPendingInMessageLoop();
85 ExpectGlobalErrorActive(false); 100 EXPECT_FALSE(IsGlobalErrorActive());
86 } 101 }
87 102
88 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndApplyManually) { 103 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndApplyManually) {
89 // Show the change and apply it, mimicking a button click. 104 // Show the change and apply it, mimicking a button click.
90 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())). 105 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
91 WillOnce(Return(true)); 106 WillOnce(Return(true));
92 protector_service_->ShowChange(mock_change_); 107 protector_service_->ShowChange(mock_change_);
93 ui_test_utils::RunAllPendingInMessageLoop(); 108 ui_test_utils::RunAllPendingInMessageLoop();
94 ExpectGlobalErrorActive(true); 109 EXPECT_TRUE(IsGlobalErrorActive());
95 EXPECT_CALL(*mock_change_, Apply(browser())); 110 EXPECT_CALL(*mock_change_, Apply(browser()));
96 // Pressing Cancel applies the change. 111 // Pressing Cancel applies the change.
97 GlobalError* error = GetGlobalError(); 112 GlobalError* error = GetGlobalError();
113 ASSERT_TRUE(error);
98 error->BubbleViewCancelButtonPressed(browser()); 114 error->BubbleViewCancelButtonPressed(browser());
99 error->GetBubbleView()->CloseBubbleView(); 115 error->GetBubbleView()->CloseBubbleView();
100 ui_test_utils::RunAllPendingInMessageLoop(); 116 ui_test_utils::RunAllPendingInMessageLoop();
101 ExpectGlobalErrorActive(false); 117 EXPECT_FALSE(IsGlobalErrorActive());
102 } 118 }
103 119
104 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndDiscard) { 120 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndDiscard) {
105 // Show the change and discard it. 121 // Show the change and discard it.
106 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())). 122 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
107 WillOnce(Return(true)); 123 WillOnce(Return(true));
108 protector_service_->ShowChange(mock_change_); 124 protector_service_->ShowChange(mock_change_);
109 ui_test_utils::RunAllPendingInMessageLoop(); 125 ui_test_utils::RunAllPendingInMessageLoop();
110 ExpectGlobalErrorActive(true); 126 EXPECT_TRUE(IsGlobalErrorActive());
111 EXPECT_CALL(*mock_change_, Discard(browser())); 127 EXPECT_CALL(*mock_change_, Discard(browser()));
112 protector_service_->DiscardChange(browser()); 128 protector_service_->DiscardChange(mock_change_, browser());
113 ui_test_utils::RunAllPendingInMessageLoop(); 129 ui_test_utils::RunAllPendingInMessageLoop();
114 ExpectGlobalErrorActive(false); 130 EXPECT_FALSE(IsGlobalErrorActive());
115 } 131 }
116 132
117 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndDiscardManually) { 133 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndDiscardManually) {
118 // Show the change and discard it, mimicking a button click. 134 // Show the change and discard it, mimicking a button click.
119 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())). 135 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
120 WillOnce(Return(true)); 136 WillOnce(Return(true));
121 protector_service_->ShowChange(mock_change_); 137 protector_service_->ShowChange(mock_change_);
122 ui_test_utils::RunAllPendingInMessageLoop(); 138 ui_test_utils::RunAllPendingInMessageLoop();
123 ExpectGlobalErrorActive(true); 139 EXPECT_TRUE(IsGlobalErrorActive());
124 EXPECT_CALL(*mock_change_, Discard(browser())); 140 EXPECT_CALL(*mock_change_, Discard(browser()));
125 // Pressing Apply discards the change. 141 // Pressing Apply discards the change.
126 GlobalError* error = GetGlobalError(); 142 GlobalError* error = GetGlobalError();
143 ASSERT_TRUE(error);
127 error->BubbleViewAcceptButtonPressed(browser()); 144 error->BubbleViewAcceptButtonPressed(browser());
128 error->GetBubbleView()->CloseBubbleView(); 145 error->GetBubbleView()->CloseBubbleView();
129 ui_test_utils::RunAllPendingInMessageLoop(); 146 ui_test_utils::RunAllPendingInMessageLoop();
130 ExpectGlobalErrorActive(false); 147 EXPECT_FALSE(IsGlobalErrorActive());
131 } 148 }
132 149
133 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, BubbleClosedInsideApply) { 150 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, BubbleClosedInsideApply) {
134 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())). 151 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
135 WillOnce(Return(true)); 152 WillOnce(Return(true));
136 protector_service_->ShowChange(mock_change_); 153 protector_service_->ShowChange(mock_change_);
137 ui_test_utils::RunAllPendingInMessageLoop(); 154 ui_test_utils::RunAllPendingInMessageLoop();
138 ExpectGlobalErrorActive(true); 155 EXPECT_TRUE(IsGlobalErrorActive());
139 156
140 GlobalError* error = GetGlobalError(); 157 GlobalError* error = GetGlobalError();
158 ASSERT_TRUE(error);
141 GlobalErrorBubbleViewBase* bubble_view = error->GetBubbleView(); 159 GlobalErrorBubbleViewBase* bubble_view = error->GetBubbleView();
142 EXPECT_TRUE(bubble_view); 160 ASSERT_TRUE(bubble_view);
143 EXPECT_CALL(*mock_change_, Apply(browser())).WillOnce(InvokeWithoutArgs( 161 EXPECT_CALL(*mock_change_, Apply(browser())).WillOnce(InvokeWithoutArgs(
144 bubble_view, &GlobalErrorBubbleViewBase::CloseBubbleView)); 162 bubble_view, &GlobalErrorBubbleViewBase::CloseBubbleView));
145 163
146 // Pressing Cancel applies the change. 164 // Pressing Cancel applies the change.
147 error->BubbleViewCancelButtonPressed(browser()); 165 error->BubbleViewCancelButtonPressed(browser());
148 ui_test_utils::RunAllPendingInMessageLoop(); 166 ui_test_utils::RunAllPendingInMessageLoop();
149 ExpectGlobalErrorActive(false); 167 EXPECT_FALSE(IsGlobalErrorActive());
168 }
169
170 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowMultipleChangesAndApply) {
171 // Show the first change.
172 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
173 WillOnce(Return(true));
174 protector_service_->ShowChange(mock_change_);
175 ui_test_utils::RunAllPendingInMessageLoop();
176 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
177
178 // ProtectService will own this change instance as well.
179 MockSettingChange* mock_change2 = new NiceMock<MockSettingChange>();
180 // Show the second change.
181 EXPECT_CALL(*mock_change2, MockInit(browser()->profile())).
182 WillOnce(Return(true));
183 protector_service_->ShowChange(mock_change2);
184 ui_test_utils::RunAllPendingInMessageLoop();
185 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
186 EXPECT_TRUE(IsGlobalErrorActive(mock_change2));
187
188 // Apply the first change, the second should still be active.
189 EXPECT_CALL(*mock_change_, Apply(browser()));
190 protector_service_->ApplyChange(mock_change_, browser());
191 ui_test_utils::RunAllPendingInMessageLoop();
192 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
193 EXPECT_TRUE(IsGlobalErrorActive(mock_change2));
194
195 // Finally apply the second change.
196 EXPECT_CALL(*mock_change2, Apply(browser()));
197 protector_service_->ApplyChange(mock_change2, browser());
198 ui_test_utils::RunAllPendingInMessageLoop();
199 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
200 EXPECT_FALSE(IsGlobalErrorActive(mock_change2));
201 }
202
203 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest,
204 ShowMultipleChangesDismissAndApply) {
205 // Show the first change.
206 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
207 WillOnce(Return(true));
208 protector_service_->ShowChange(mock_change_);
209 ui_test_utils::RunAllPendingInMessageLoop();
210 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
211
212 // ProtectService will own this change instance as well.
213 MockSettingChange* mock_change2 = new NiceMock<MockSettingChange>();
214 // Show the second change.
215 EXPECT_CALL(*mock_change2, MockInit(browser()->profile())).
216 WillOnce(Return(true));
217 protector_service_->ShowChange(mock_change2);
218 ui_test_utils::RunAllPendingInMessageLoop();
219 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
220 EXPECT_TRUE(IsGlobalErrorActive(mock_change2));
221
222 // Dismiss the first change, the second should still be active.
223 protector_service_->DismissChange(mock_change_);
224 ui_test_utils::RunAllPendingInMessageLoop();
225 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
226 EXPECT_TRUE(IsGlobalErrorActive(mock_change2));
227
228 // Finally apply the second change.
229 EXPECT_CALL(*mock_change2, Apply(browser()));
230 protector_service_->ApplyChange(mock_change2, browser());
231 ui_test_utils::RunAllPendingInMessageLoop();
232 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
233 EXPECT_FALSE(IsGlobalErrorActive(mock_change2));
234 }
235
236 IN_PROC_BROWSER_TEST_F(ProtectorServiceTest,
237 ShowMultipleChangesAndApplyManually) {
238 // Show the first change.
239 EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
240 WillOnce(Return(true));
241 protector_service_->ShowChange(mock_change_);
242 ui_test_utils::RunAllPendingInMessageLoop();
243 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
244
245 // ProtectService will own this change instance as well.
246 MockSettingChange* mock_change2 = new NiceMock<MockSettingChange>();
247 // Show the second change.
248 EXPECT_CALL(*mock_change2, MockInit(browser()->profile())).
249 WillOnce(Return(true));
250 protector_service_->ShowChange(mock_change2);
251 ui_test_utils::RunAllPendingInMessageLoop();
252 EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
253 EXPECT_TRUE(IsGlobalErrorActive(mock_change2));
254
255 // Apply the first change, mimicking a button click; the second should still
256 // be active.
257 EXPECT_CALL(*mock_change_, Apply(browser()));
258 GlobalError* error = GetGlobalError(mock_change_);
259 ASSERT_TRUE(error);
260 error->ShowBubbleView(browser());
261 error->BubbleViewCancelButtonPressed(browser());
262 error->GetBubbleView()->CloseBubbleView();
263 ui_test_utils::RunAllPendingInMessageLoop();
264 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
265 EXPECT_TRUE(IsGlobalErrorActive(mock_change2));
266
267 // Finally apply the second change.
268 EXPECT_CALL(*mock_change2, Apply(browser()));
269 GlobalError* error2 = GetGlobalError(mock_change2);
270 ASSERT_TRUE(error);
271 error2->ShowBubbleView(browser());
272 error2->BubbleViewCancelButtonPressed(browser());
273 error2->GetBubbleView()->CloseBubbleView();
274 ui_test_utils::RunAllPendingInMessageLoop();
275 EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
276 EXPECT_FALSE(IsGlobalErrorActive(mock_change2));
150 } 277 }
151 278
152 // TODO(ivankr): Timeout test. 279 // TODO(ivankr): Timeout test.
153 280
154 } // namespace protector 281 } // namespace protector
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698