summaryrefslogtreecommitdiffstats
path: root/chrome/browser/protector/protector_service_browsertest.cc
blob: 17912ffe1c99ec6e105d6063dc61e3653a922c2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright (c) 2012 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 "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/protector/mock_setting_change.h"
#include "chrome/browser/protector/protector_service.h"
#include "chrome/browser/protector/protector_service_factory.h"
#include "chrome/browser/protector/settings_change_global_error.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/global_error.h"
#include "chrome/browser/ui/global_error_bubble_view_base.h"
#include "chrome/browser/ui/global_error_service.h"
#include "chrome/browser/ui/global_error_service_factory.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"

using ::testing::InvokeWithoutArgs;
using ::testing::NiceMock;
using ::testing::Return;

namespace protector {

class ProtectorServiceTest : public InProcessBrowserTest {
 public:
  virtual void SetUpOnMainThread() {
    protector_service_ =
        ProtectorServiceFactory::GetForProfile(browser()->profile());
    // ProtectService will own this change instance.
    mock_change_ = new NiceMock<MockSettingChange>();
  }

 protected:
  GlobalError* GetGlobalError(BaseSettingChange* change) {
    std::vector<ProtectorService::Item>::iterator item =
        std::find_if(protector_service_->items_.begin(),
                     protector_service_->items_.end(),
                     ProtectorService::MatchItemByChange(change));
    return item == protector_service_->items_.end() ?
        NULL : item->error.get();
  }

  // Checks that |protector_service_| has an error instance corresponding to
  // |change| and that GlobalErrorService knows about it.
  bool IsGlobalErrorActive(BaseSettingChange* change) {
    GlobalError* error = GetGlobalError(change);
    if (!error)
      return false;
    if (!GlobalErrorServiceFactory::GetForProfile(browser()->profile())->
            GetGlobalErrorByMenuItemCommandID(error->MenuItemCommandID())) {
      return false;
    }
    return protector_service_->IsShowingChange();
  }

  ProtectorService* protector_service_;
  MockSettingChange* mock_change_;
};

IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ChangeInitError) {
  // Init fails and causes the change to be ignored.
  EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
      WillOnce(Return(false));
  protector_service_->ShowChange(mock_change_);
  EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
}

IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndDismiss) {
  // Show the change and immediately dismiss it.
  EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
      WillOnce(Return(true));
  protector_service_->ShowChange(mock_change_);
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
  protector_service_->DismissChange(mock_change_);
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
}

IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndApply) {
  // Show the change and apply it.
  EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
      WillOnce(Return(true));
  protector_service_->ShowChange(mock_change_);
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
  EXPECT_CALL(*mock_change_, Apply(browser()));
  protector_service_->ApplyChange(mock_change_, browser());
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
}

IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndApplyManually) {
  // Show the change and apply it, mimicking a button click.
  EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
      WillOnce(Return(true));
  protector_service_->ShowChange(mock_change_);
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
  EXPECT_CALL(*mock_change_, Apply(browser()));
  // Pressing Cancel applies the change.
  GlobalError* error = GetGlobalError(mock_change_);
  ASSERT_TRUE(error);
  error->BubbleViewCancelButtonPressed(browser());
  error->GetBubbleView()->CloseBubbleView();
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
}

IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndDiscard) {
  // Show the change and discard it.
  EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
      WillOnce(Return(true));
  protector_service_->ShowChange(mock_change_);
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
  EXPECT_CALL(*mock_change_, Discard(browser()));
  protector_service_->DiscardChange(mock_change_, browser());
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
}

IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowAndDiscardManually) {
  // Show the change and discard it, mimicking a button click.
  EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
      WillOnce(Return(true));
  protector_service_->ShowChange(mock_change_);
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
  EXPECT_CALL(*mock_change_, Discard(browser()));
  // Pressing Apply discards the change.
  GlobalError* error = GetGlobalError(mock_change_);
  ASSERT_TRUE(error);
  error->BubbleViewAcceptButtonPressed(browser());
  error->GetBubbleView()->CloseBubbleView();
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
}

IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, BubbleClosedInsideApply) {
  EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
      WillOnce(Return(true));
  protector_service_->ShowChange(mock_change_);
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_TRUE(IsGlobalErrorActive(mock_change_));

  GlobalError* error = GetGlobalError(mock_change_);
  ASSERT_TRUE(error);
  GlobalErrorBubbleViewBase* bubble_view = error->GetBubbleView();
  ASSERT_TRUE(bubble_view);
  EXPECT_CALL(*mock_change_, Apply(browser())).WillOnce(InvokeWithoutArgs(
      bubble_view, &GlobalErrorBubbleViewBase::CloseBubbleView));
  // Pressing Cancel applies the change.
  error->BubbleViewCancelButtonPressed(browser());
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
}

IN_PROC_BROWSER_TEST_F(ProtectorServiceTest, ShowMultipleChangesAndApply) {
  // Show the first change.
  EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
      WillOnce(Return(true));
  protector_service_->ShowChange(mock_change_);
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_TRUE(IsGlobalErrorActive(mock_change_));

  // ProtectService will own this change instance as well.
  MockSettingChange* mock_change2 = new NiceMock<MockSettingChange>();
  // Show the second change.
  EXPECT_CALL(*mock_change2, MockInit(browser()->profile())).
      WillOnce(Return(true));
  protector_service_->ShowChange(mock_change2);
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
  EXPECT_TRUE(IsGlobalErrorActive(mock_change2));

  // Apply the first change, the second should still be active.
  EXPECT_CALL(*mock_change_, Apply(browser()));
  protector_service_->ApplyChange(mock_change_, browser());
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
  EXPECT_TRUE(IsGlobalErrorActive(mock_change2));

  // Finally apply the second change.
  EXPECT_CALL(*mock_change2, Apply(browser()));
  protector_service_->ApplyChange(mock_change2, browser());
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
  EXPECT_FALSE(IsGlobalErrorActive(mock_change2));
}

IN_PROC_BROWSER_TEST_F(ProtectorServiceTest,
                       ShowMultipleChangesDismissAndApply) {
  // Show the first change.
  EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
      WillOnce(Return(true));
  protector_service_->ShowChange(mock_change_);
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_TRUE(IsGlobalErrorActive(mock_change_));

  // ProtectService will own this change instance as well.
  MockSettingChange* mock_change2 = new NiceMock<MockSettingChange>();
  // Show the second change.
  EXPECT_CALL(*mock_change2, MockInit(browser()->profile())).
      WillOnce(Return(true));
  protector_service_->ShowChange(mock_change2);
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
  EXPECT_TRUE(IsGlobalErrorActive(mock_change2));

  // Dismiss the first change, the second should still be active.
  protector_service_->DismissChange(mock_change_);
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
  EXPECT_TRUE(IsGlobalErrorActive(mock_change2));

  // Finally apply the second change.
  EXPECT_CALL(*mock_change2, Apply(browser()));
  protector_service_->ApplyChange(mock_change2, browser());
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
  EXPECT_FALSE(IsGlobalErrorActive(mock_change2));
}

IN_PROC_BROWSER_TEST_F(ProtectorServiceTest,
                       ShowMultipleChangesAndApplyManually) {
  // Show the first change.
  EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
      WillOnce(Return(true));
  protector_service_->ShowChange(mock_change_);
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_TRUE(IsGlobalErrorActive(mock_change_));

  // The first bubble view has been displayed.
  GlobalError* error = GetGlobalError(mock_change_);
  ASSERT_TRUE(error);
  ASSERT_TRUE(error->HasShownBubbleView());

  // ProtectService will own this change instance as well.
  MockSettingChange* mock_change2 = new NiceMock<MockSettingChange>();
  // Show the second change.
  EXPECT_CALL(*mock_change2, MockInit(browser()->profile())).
      WillOnce(Return(true));
  protector_service_->ShowChange(mock_change2);
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_TRUE(IsGlobalErrorActive(mock_change_));
  EXPECT_TRUE(IsGlobalErrorActive(mock_change2));

  // The second bubble view hasn't been displayed because the first is still
  // shown.
  GlobalError* error2 = GetGlobalError(mock_change2);
  ASSERT_TRUE(error2);
  EXPECT_FALSE(error2->HasShownBubbleView());

  // Apply the first change, mimicking a button click; the second should still
  // be active.
  EXPECT_CALL(*mock_change_, Apply(browser()));
  error->BubbleViewCancelButtonPressed(browser());
  error->GetBubbleView()->CloseBubbleView();
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
  EXPECT_TRUE(IsGlobalErrorActive(mock_change2));

  // Now the second bubble view should be shown.
  ASSERT_TRUE(error2->HasShownBubbleView());

  // Finally apply the second change.
  EXPECT_CALL(*mock_change2, Apply(browser()));
  error2->BubbleViewCancelButtonPressed(browser());
  error2->GetBubbleView()->CloseBubbleView();
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_FALSE(IsGlobalErrorActive(mock_change_));
  EXPECT_FALSE(IsGlobalErrorActive(mock_change2));
}

IN_PROC_BROWSER_TEST_F(ProtectorServiceTest,
                       ShowMultipleChangesAndApplyManuallyBeforeOther) {
  // Show the first change.
  EXPECT_CALL(*mock_change_, MockInit(browser()->profile())).
      WillOnce(Return(true));
  protector_service_->ShowChange(mock_change_);
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_TRUE(IsGlobalErrorActive(mock_change_));

  // The first bubble view has been displayed.
  GlobalError* error = GetGlobalError(mock_change_);
  ASSERT_TRUE(error);
  ASSERT_TRUE(error->HasShownBubbleView());

  // Apply the first change, mimicking a button click.
  EXPECT_CALL(*mock_change_, Apply(browser()));
  error->BubbleViewCancelButtonPressed(browser());
  error->GetBubbleView()->CloseBubbleView();
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_FALSE(IsGlobalErrorActive(mock_change_));

  // ProtectService will own this change instance as well.
  MockSettingChange* mock_change2 = new NiceMock<MockSettingChange>();
  // Show the second change.
  EXPECT_CALL(*mock_change2, MockInit(browser()->profile())).
      WillOnce(Return(true));
  protector_service_->ShowChange(mock_change2);
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_TRUE(IsGlobalErrorActive(mock_change2));

  // The second bubble view has been displayed.
  GlobalError* error2 = GetGlobalError(mock_change2);
  ASSERT_TRUE(error2);
  ASSERT_TRUE(error2->HasShownBubbleView());

  // Finally apply the second change.
  EXPECT_CALL(*mock_change2, Apply(browser()));
  error2->BubbleViewCancelButtonPressed(browser());
  error2->GetBubbleView()->CloseBubbleView();
  ui_test_utils::RunAllPendingInMessageLoop();
  EXPECT_FALSE(IsGlobalErrorActive(mock_change2));
}

// TODO(ivankr): Timeout test.

}  // namespace protector