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

Side by Side Diff: chrome/browser/ui/views/global_error_bubble_view_unittest.cc

Issue 2650923002: Fixes ungraceful handling of destroyed GlobalError GlobalErrorBubbleView. (Closed)
Patch Set: Created 3 years, 11 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
OLDNEW
(Empty)
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/views/global_error_bubble_view.h"
6
7 #include <memory>
8 #include <vector>
9
10 #include "chrome/browser/ui/global_error/global_error.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/gfx/codec/png_codec.h"
14 #include "ui/gfx/image/image.h"
15 #include "ui/gfx/image/image_png_rep.h"
16 #include "ui/views/controls/button/button.h"
17 #include "ui/views/controls/button/label_button.h"
18
19 using ::testing::_;
20 using ::testing::Return;
21 using ::testing::StrictMock;
22
23 namespace views {
24 class MockButtonListener : public views::ButtonListener {
25 public:
26 MockButtonListener() {}
27 ~MockButtonListener() override;
28 MOCK_METHOD2(ButtonPressed, void(Button* sender, const ui::Event& event));
29
30 private:
31 DISALLOW_COPY_AND_ASSIGN(MockButtonListener);
32 };
33
34 MockButtonListener::~MockButtonListener() {}
35
36 namespace {
37
38 class MockGlobalErrorWithStandardBubble : public GlobalErrorWithStandardBubble {
39 public:
40 MockGlobalErrorWithStandardBubble() {}
41 ~MockGlobalErrorWithStandardBubble() override;
42
43 MOCK_METHOD0(GetBubbleViewIcon, gfx::Image());
44 MOCK_METHOD0(GetBubbleViewTitle, base::string16());
45 MOCK_METHOD0(GetBubbleViewMessage, std::vector<base::string16>());
46 MOCK_METHOD0(GetBubbleViewAcceptButtonLabel, base::string16());
47 MOCK_CONST_METHOD0(ShouldShowCloseButton, bool());
48 MOCK_METHOD0(ShouldAddElevationIconToAcceptButton, bool());
49 MOCK_METHOD0(GetBubbleViewCancelButtonLabel, base::string16());
50 MOCK_METHOD1(BubbleViewDidClose, void(Browser* browser));
51 MOCK_METHOD1(OnBubbleViewDidClose, void(Browser* browser));
52 MOCK_METHOD1(BubbleViewAcceptButtonPressed, void(Browser* browser));
53 MOCK_METHOD1(BubbleViewCancelButtonPressed, void(Browser* browser));
54 MOCK_CONST_METHOD0(ShouldCloseOnDeactivate, bool());
55 MOCK_METHOD0(HasBubbleView, bool());
56 MOCK_METHOD0(HasShownBubbleView, bool());
57 MOCK_METHOD1(ShowBubbleView, void(Browser* browser));
58 MOCK_METHOD0(GetBubbleView, GlobalErrorBubbleViewBase*());
59 MOCK_METHOD0(HasMenuItem, bool());
60 MOCK_METHOD0(MenuItemCommandID, int());
61 MOCK_METHOD0(MenuItemLabel, base::string16());
62 MOCK_METHOD1(ExecuteMenuItem, void(Browser* browser));
63 MOCK_METHOD0(GetBubbleViewMessages, std::vector<base::string16>());
64
65 private:
66 DISALLOW_COPY_AND_ASSIGN(MockGlobalErrorWithStandardBubble);
67 };
68
69 MockGlobalErrorWithStandardBubble::~MockGlobalErrorWithStandardBubble() {}
70
71 const SkBitmap CreateBitmap(int width, int height) {
72 SkBitmap bitmap;
73 bitmap.allocN32Pixels(width, height);
74 bitmap.eraseARGB(255, 0, 255, 0);
75 return bitmap;
76 }
77
78 scoped_refptr<base::RefCountedMemory> CreatePNGBytes(int edge_size) {
79 SkBitmap bitmap = CreateBitmap(edge_size, edge_size);
80 scoped_refptr<base::RefCountedBytes> bytes(new base::RefCountedBytes());
81 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &bytes->data());
82 return bytes;
83 }
84
85 } // namespace
86
87 class GlobalErrorBubbleViewTest : public testing::Test {
88 public:
89 GlobalErrorBubbleViewTest()
90 : mock_global_error_with_standard_bubble_(
91 base::MakeUnique<StrictMock<MockGlobalErrorWithStandardBubble>>()),
92 view_(base::MakeUnique<GlobalErrorBubbleView>(
93 new views::View(),
94 anchor_point_,
95 arrow_,
96 nullptr,
97 mock_global_error_with_standard_bubble_->AsWeakPtr())) {}
98
99 protected:
100 std::unique_ptr<StrictMock<MockGlobalErrorWithStandardBubble>>
101 mock_global_error_with_standard_bubble_;
102 const gfx::Point anchor_point_;
103 views::BubbleBorder::Arrow arrow_;
104 std::unique_ptr<GlobalErrorBubbleView> view_;
105 };
106
107 TEST_F(GlobalErrorBubbleViewTest, GetWindowTitleCallsToError) {
108 EXPECT_CALL(*mock_global_error_with_standard_bubble_, GetBubbleViewTitle());
Wez 2017/01/24 01:45:55 For calls that are basically just pass-through, I'
CJ 2017/01/25 01:12:14 Done.
109 view_->GetWindowTitle();
110 }
111
112 TEST_F(GlobalErrorBubbleViewTest, GetWindowTitleHandlesNullError) {
113 mock_global_error_with_standard_bubble_.reset();
114 view_->GetWindowTitle();
115 }
116
117 TEST_F(GlobalErrorBubbleViewTest, GetWindowIconCallsToError) {
118 std::vector<gfx::ImagePNGRep> image_png_reps;
119 image_png_reps.push_back(gfx::ImagePNGRep(CreatePNGBytes(25), 1.0f));
120 gfx::Image image = gfx::Image(image_png_reps);
121 EXPECT_CALL(*mock_global_error_with_standard_bubble_, GetBubbleViewIcon())
122 .WillOnce(Return(image));
123 view_->GetWindowIcon();
124 }
125
126 TEST_F(GlobalErrorBubbleViewTest, ShouldShowWindowIconIsTrue) {
127 EXPECT_TRUE(view_->ShouldShowWindowIcon());
128 }
129
130 TEST_F(GlobalErrorBubbleViewTest, WindowClosingCallsToError) {
131 EXPECT_CALL(*mock_global_error_with_standard_bubble_,
132 BubbleViewDidClose(nullptr));
133 view_->WindowClosing();
134 }
135
136 TEST_F(GlobalErrorBubbleViewTest, WindowClosingHandlesNullError) {
137 mock_global_error_with_standard_bubble_.reset();
138 view_->WindowClosing();
139 }
140
141 TEST_F(GlobalErrorBubbleViewTest, UpdateButtonCallsToError) {
142 views::LabelButton* button =
143 new views::LabelButton(new MockButtonListener(), base::string16());
144 EXPECT_CALL(*mock_global_error_with_standard_bubble_,
145 GetBubbleViewAcceptButtonLabel());
146 EXPECT_CALL(*mock_global_error_with_standard_bubble_,
147 GetBubbleViewCancelButtonLabel());
148 EXPECT_CALL(*mock_global_error_with_standard_bubble_,
149 ShouldAddElevationIconToAcceptButton())
150 .WillOnce(Return(false));
151 view_->UpdateButton(button, ui::DIALOG_BUTTON_OK);
152 }
153
154 TEST_F(GlobalErrorBubbleViewTest, UpdateButtonHandlesNullError) {
155 views::LabelButton* button =
156 new views::LabelButton(new MockButtonListener(), base::string16());
157 mock_global_error_with_standard_bubble_.reset();
158 view_->UpdateButton(button, ui::DIALOG_BUTTON_OK);
159 }
160
161 TEST_F(GlobalErrorBubbleViewTest, ShouldShowCloseButtonCallsToError) {
162 EXPECT_CALL(*mock_global_error_with_standard_bubble_,
163 ShouldShowCloseButton());
164 view_->ShouldShowCloseButton();
165 }
166
167 TEST_F(GlobalErrorBubbleViewTest, ShouldShowCloseButtonHandlesNullError) {
168 mock_global_error_with_standard_bubble_.reset();
169 view_->ShouldShowCloseButton();
170 }
171
172 TEST_F(GlobalErrorBubbleViewTest, GetDialogButtonLabelCallsToError) {
173 EXPECT_CALL(*mock_global_error_with_standard_bubble_,
174 GetBubbleViewAcceptButtonLabel());
175 EXPECT_CALL(*mock_global_error_with_standard_bubble_,
176 GetBubbleViewCancelButtonLabel());
177 view_->GetDialogButtonLabel(ui::DIALOG_BUTTON_OK);
178 view_->GetDialogButtonLabel(ui::DIALOG_BUTTON_CANCEL);
179 }
180
181 TEST_F(GlobalErrorBubbleViewTest, GetDialogButtonButtonsCallsToError) {
182 EXPECT_CALL(*mock_global_error_with_standard_bubble_,
183 GetBubbleViewCancelButtonLabel());
184 view_->GetDialogButtons();
185 }
186
187 TEST_F(GlobalErrorBubbleViewTest, CancelCallsToError) {
188 EXPECT_CALL(*mock_global_error_with_standard_bubble_,
189 BubbleViewCancelButtonPressed(nullptr));
190 view_->Cancel();
191 }
192
193 TEST_F(GlobalErrorBubbleViewTest, CancelHandlesNullError) {
194 mock_global_error_with_standard_bubble_.reset();
195 view_->Cancel();
196 }
197
198 TEST_F(GlobalErrorBubbleViewTest, AcceptCallsToError) {
199 EXPECT_CALL(*mock_global_error_with_standard_bubble_,
200 BubbleViewAcceptButtonPressed(nullptr));
201 view_->Accept();
202 }
203
204 TEST_F(GlobalErrorBubbleViewTest, AcceptHandlesNullError) {
205 mock_global_error_with_standard_bubble_.reset();
206 view_->Accept();
207 }
208
209 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698