summaryrefslogtreecommitdiffstats
path: root/chrome/browser/protector/protector_browsertest.cc
blob: 32b2474688b630f2aa4ae628567fdf1ad5782221 (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
// 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.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_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::Invoke;
using ::testing::NiceMock;
using ::testing::Return;

namespace protector {

class ProtectorTest : public InProcessBrowserTest {
 public:
  virtual void SetUpOnMainThread() {
    // Protect owns the change and deletes himself.
    protector_ = new Protector(browser()->profile());
    mock_change_ = new NiceMock<MockSettingChange>();
  }

 protected:
  GlobalError* GetGlobalError() {
    return protector_->error_.get();
  }

  void ExpectGlobalErrorActive(bool active) {
    GlobalError* error =
        GlobalErrorServiceFactory::GetForProfile(browser()->profile())->
            GetGlobalErrorByMenuItemCommandID(IDC_SHOW_SETTINGS_CHANGES);
    EXPECT_EQ(active, static_cast<bool>(error));
  }

  Protector* protector_;
  MockSettingChange* mock_change_;
};

IN_PROC_BROWSER_TEST_F(ProtectorTest, ChangeInitError) {
  EXPECT_CALL(*mock_change_, MockInit(protector_)).WillOnce(Return(false));
  protector_->ShowChange(mock_change_);
  ExpectGlobalErrorActive(false);
  ui_test_utils::RunAllPendingInMessageLoop();
  ExpectGlobalErrorActive(false);
}

IN_PROC_BROWSER_TEST_F(ProtectorTest, ShowAndDismiss) {
  EXPECT_CALL(*mock_change_, MockInit(protector_)).WillOnce(Return(true));
  protector_->ShowChange(mock_change_);
  ui_test_utils::RunAllPendingInMessageLoop();
  ASSERT_TRUE(GetGlobalError());
  ExpectGlobalErrorActive(true);
  protector_->DismissChange();
  ui_test_utils::RunAllPendingInMessageLoop();
  ExpectGlobalErrorActive(false);
}

IN_PROC_BROWSER_TEST_F(ProtectorTest, ShowAndApply) {
  EXPECT_CALL(*mock_change_, MockInit(protector_)).WillOnce(Return(true));
  protector_->ShowChange(mock_change_);
  ui_test_utils::RunAllPendingInMessageLoop();
  GlobalError* error = GetGlobalError();
  ASSERT_TRUE(error);
  ExpectGlobalErrorActive(true);
  EXPECT_CALL(*mock_change_, Apply());
  // Pressing Cancel applies the change.
  error->BubbleViewCancelButtonPressed();
  error->BubbleViewDidClose();
  ui_test_utils::RunAllPendingInMessageLoop();
  ExpectGlobalErrorActive(false);
}

IN_PROC_BROWSER_TEST_F(ProtectorTest, ShowAndDiscard) {
  EXPECT_CALL(*mock_change_, MockInit(protector_)).WillOnce(Return(true));
  protector_->ShowChange(mock_change_);
  ui_test_utils::RunAllPendingInMessageLoop();
  GlobalError* error = GetGlobalError();
  ASSERT_TRUE(error);
  ExpectGlobalErrorActive(true);
  EXPECT_CALL(*mock_change_, Discard());
  // Pressing Apply discards the change.
  error->BubbleViewAcceptButtonPressed();
  error->BubbleViewDidClose();
  ui_test_utils::RunAllPendingInMessageLoop();
  ExpectGlobalErrorActive(false);
}

IN_PROC_BROWSER_TEST_F(ProtectorTest, BubbleClosedInsideApply) {
  EXPECT_CALL(*mock_change_, MockInit(protector_)).WillOnce(Return(true));
  protector_->ShowChange(mock_change_);
  ui_test_utils::RunAllPendingInMessageLoop();
  GlobalError* error = GetGlobalError();
  ASSERT_TRUE(error);
  ExpectGlobalErrorActive(true);
  EXPECT_CALL(*mock_change_, Apply()).
      WillOnce(Invoke(error, &GlobalError::BubbleViewDidClose));
  // Pressing Cancel applies the change.
  error->BubbleViewCancelButtonPressed();
  ui_test_utils::RunAllPendingInMessageLoop();
  ExpectGlobalErrorActive(false);
}

// TODO(ivankr): Timeout test.

}  // namespace protector