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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/views/global_error_bubble_view_unittest.cc
diff --git a/chrome/browser/ui/views/global_error_bubble_view_unittest.cc b/chrome/browser/ui/views/global_error_bubble_view_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e96e673abb5fa149966531fb5973fe7f24a582f1
--- /dev/null
+++ b/chrome/browser/ui/views/global_error_bubble_view_unittest.cc
@@ -0,0 +1,209 @@
+// Copyright (c) 2017 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 "chrome/browser/ui/views/global_error_bubble_view.h"
+
+#include <memory>
+#include <vector>
+
+#include "chrome/browser/ui/global_error/global_error.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "ui/gfx/codec/png_codec.h"
+#include "ui/gfx/image/image.h"
+#include "ui/gfx/image/image_png_rep.h"
+#include "ui/views/controls/button/button.h"
+#include "ui/views/controls/button/label_button.h"
+
+using ::testing::_;
+using ::testing::Return;
+using ::testing::StrictMock;
+
+namespace views {
+class MockButtonListener : public views::ButtonListener {
+ public:
+ MockButtonListener() {}
+ ~MockButtonListener() override;
+ MOCK_METHOD2(ButtonPressed, void(Button* sender, const ui::Event& event));
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MockButtonListener);
+};
+
+MockButtonListener::~MockButtonListener() {}
+
+namespace {
+
+class MockGlobalErrorWithStandardBubble : public GlobalErrorWithStandardBubble {
+ public:
+ MockGlobalErrorWithStandardBubble() {}
+ ~MockGlobalErrorWithStandardBubble() override;
+
+ MOCK_METHOD0(GetBubbleViewIcon, gfx::Image());
+ MOCK_METHOD0(GetBubbleViewTitle, base::string16());
+ MOCK_METHOD0(GetBubbleViewMessage, std::vector<base::string16>());
+ MOCK_METHOD0(GetBubbleViewAcceptButtonLabel, base::string16());
+ MOCK_CONST_METHOD0(ShouldShowCloseButton, bool());
+ MOCK_METHOD0(ShouldAddElevationIconToAcceptButton, bool());
+ MOCK_METHOD0(GetBubbleViewCancelButtonLabel, base::string16());
+ MOCK_METHOD1(BubbleViewDidClose, void(Browser* browser));
+ MOCK_METHOD1(OnBubbleViewDidClose, void(Browser* browser));
+ MOCK_METHOD1(BubbleViewAcceptButtonPressed, void(Browser* browser));
+ MOCK_METHOD1(BubbleViewCancelButtonPressed, void(Browser* browser));
+ MOCK_CONST_METHOD0(ShouldCloseOnDeactivate, bool());
+ MOCK_METHOD0(HasBubbleView, bool());
+ MOCK_METHOD0(HasShownBubbleView, bool());
+ MOCK_METHOD1(ShowBubbleView, void(Browser* browser));
+ MOCK_METHOD0(GetBubbleView, GlobalErrorBubbleViewBase*());
+ MOCK_METHOD0(HasMenuItem, bool());
+ MOCK_METHOD0(MenuItemCommandID, int());
+ MOCK_METHOD0(MenuItemLabel, base::string16());
+ MOCK_METHOD1(ExecuteMenuItem, void(Browser* browser));
+ MOCK_METHOD0(GetBubbleViewMessages, std::vector<base::string16>());
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MockGlobalErrorWithStandardBubble);
+};
+
+MockGlobalErrorWithStandardBubble::~MockGlobalErrorWithStandardBubble() {}
+
+const SkBitmap CreateBitmap(int width, int height) {
+ SkBitmap bitmap;
+ bitmap.allocN32Pixels(width, height);
+ bitmap.eraseARGB(255, 0, 255, 0);
+ return bitmap;
+}
+
+scoped_refptr<base::RefCountedMemory> CreatePNGBytes(int edge_size) {
+ SkBitmap bitmap = CreateBitmap(edge_size, edge_size);
+ scoped_refptr<base::RefCountedBytes> bytes(new base::RefCountedBytes());
+ gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &bytes->data());
+ return bytes;
+}
+
+} // namespace
+
+class GlobalErrorBubbleViewTest : public testing::Test {
+ public:
+ GlobalErrorBubbleViewTest()
+ : mock_global_error_with_standard_bubble_(
+ base::MakeUnique<StrictMock<MockGlobalErrorWithStandardBubble>>()),
+ view_(base::MakeUnique<GlobalErrorBubbleView>(
+ new views::View(),
+ anchor_point_,
+ arrow_,
+ nullptr,
+ mock_global_error_with_standard_bubble_->AsWeakPtr())) {}
+
+ protected:
+ std::unique_ptr<StrictMock<MockGlobalErrorWithStandardBubble>>
+ mock_global_error_with_standard_bubble_;
+ const gfx::Point anchor_point_;
+ views::BubbleBorder::Arrow arrow_;
+ std::unique_ptr<GlobalErrorBubbleView> view_;
+};
+
+TEST_F(GlobalErrorBubbleViewTest, GetWindowTitleCallsToError) {
+ 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.
+ view_->GetWindowTitle();
+}
+
+TEST_F(GlobalErrorBubbleViewTest, GetWindowTitleHandlesNullError) {
+ mock_global_error_with_standard_bubble_.reset();
+ view_->GetWindowTitle();
+}
+
+TEST_F(GlobalErrorBubbleViewTest, GetWindowIconCallsToError) {
+ std::vector<gfx::ImagePNGRep> image_png_reps;
+ image_png_reps.push_back(gfx::ImagePNGRep(CreatePNGBytes(25), 1.0f));
+ gfx::Image image = gfx::Image(image_png_reps);
+ EXPECT_CALL(*mock_global_error_with_standard_bubble_, GetBubbleViewIcon())
+ .WillOnce(Return(image));
+ view_->GetWindowIcon();
+}
+
+TEST_F(GlobalErrorBubbleViewTest, ShouldShowWindowIconIsTrue) {
+ EXPECT_TRUE(view_->ShouldShowWindowIcon());
+}
+
+TEST_F(GlobalErrorBubbleViewTest, WindowClosingCallsToError) {
+ EXPECT_CALL(*mock_global_error_with_standard_bubble_,
+ BubbleViewDidClose(nullptr));
+ view_->WindowClosing();
+}
+
+TEST_F(GlobalErrorBubbleViewTest, WindowClosingHandlesNullError) {
+ mock_global_error_with_standard_bubble_.reset();
+ view_->WindowClosing();
+}
+
+TEST_F(GlobalErrorBubbleViewTest, UpdateButtonCallsToError) {
+ views::LabelButton* button =
+ new views::LabelButton(new MockButtonListener(), base::string16());
+ EXPECT_CALL(*mock_global_error_with_standard_bubble_,
+ GetBubbleViewAcceptButtonLabel());
+ EXPECT_CALL(*mock_global_error_with_standard_bubble_,
+ GetBubbleViewCancelButtonLabel());
+ EXPECT_CALL(*mock_global_error_with_standard_bubble_,
+ ShouldAddElevationIconToAcceptButton())
+ .WillOnce(Return(false));
+ view_->UpdateButton(button, ui::DIALOG_BUTTON_OK);
+}
+
+TEST_F(GlobalErrorBubbleViewTest, UpdateButtonHandlesNullError) {
+ views::LabelButton* button =
+ new views::LabelButton(new MockButtonListener(), base::string16());
+ mock_global_error_with_standard_bubble_.reset();
+ view_->UpdateButton(button, ui::DIALOG_BUTTON_OK);
+}
+
+TEST_F(GlobalErrorBubbleViewTest, ShouldShowCloseButtonCallsToError) {
+ EXPECT_CALL(*mock_global_error_with_standard_bubble_,
+ ShouldShowCloseButton());
+ view_->ShouldShowCloseButton();
+}
+
+TEST_F(GlobalErrorBubbleViewTest, ShouldShowCloseButtonHandlesNullError) {
+ mock_global_error_with_standard_bubble_.reset();
+ view_->ShouldShowCloseButton();
+}
+
+TEST_F(GlobalErrorBubbleViewTest, GetDialogButtonLabelCallsToError) {
+ EXPECT_CALL(*mock_global_error_with_standard_bubble_,
+ GetBubbleViewAcceptButtonLabel());
+ EXPECT_CALL(*mock_global_error_with_standard_bubble_,
+ GetBubbleViewCancelButtonLabel());
+ view_->GetDialogButtonLabel(ui::DIALOG_BUTTON_OK);
+ view_->GetDialogButtonLabel(ui::DIALOG_BUTTON_CANCEL);
+}
+
+TEST_F(GlobalErrorBubbleViewTest, GetDialogButtonButtonsCallsToError) {
+ EXPECT_CALL(*mock_global_error_with_standard_bubble_,
+ GetBubbleViewCancelButtonLabel());
+ view_->GetDialogButtons();
+}
+
+TEST_F(GlobalErrorBubbleViewTest, CancelCallsToError) {
+ EXPECT_CALL(*mock_global_error_with_standard_bubble_,
+ BubbleViewCancelButtonPressed(nullptr));
+ view_->Cancel();
+}
+
+TEST_F(GlobalErrorBubbleViewTest, CancelHandlesNullError) {
+ mock_global_error_with_standard_bubble_.reset();
+ view_->Cancel();
+}
+
+TEST_F(GlobalErrorBubbleViewTest, AcceptCallsToError) {
+ EXPECT_CALL(*mock_global_error_with_standard_bubble_,
+ BubbleViewAcceptButtonPressed(nullptr));
+ view_->Accept();
+}
+
+TEST_F(GlobalErrorBubbleViewTest, AcceptHandlesNullError) {
+ mock_global_error_with_standard_bubble_.reset();
+ view_->Accept();
+}
+
+} // namespace views

Powered by Google App Engine
This is Rietveld 408576698