Chromium Code Reviews| 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..384ce63e303d68c9049b8e5e7945e5aa61d27047 |
| --- /dev/null |
| +++ b/chrome/browser/ui/views/global_error_bubble_view_unittest.cc |
| @@ -0,0 +1,179 @@ |
| +// Copyright (c) 2017 The Chromium Authors. All rights reserved. |
|
sky
2017/02/04 22:16:31
no (c)
CJ
2017/02/06 23:54:50
Done. Is this a new thing with the no (c)? (I've o
Avi (use Gerrit)
2017/02/07 00:11:46
It's been that way for years officially, but we ne
|
| +// 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) { |
|
sky
2017/02/04 22:16:31
Can you use CreateImage() in image_unittest_util?
CJ
2017/02/06 23:54:50
Yup. Done. I think there was something funky going
|
| + 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_; |
| +}; |
|
sky
2017/02/04 22:16:31
private: DISALLOW...
CJ
2017/02/06 23:54:50
Done.
|
| + |
| +TEST_F(GlobalErrorBubbleViewTest, Basic) { |
| + EXPECT_CALL(*mock_global_error_with_standard_bubble_, GetBubbleViewTitle()); |
| + view_->GetWindowTitle(); |
| + |
| + 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(); |
| + |
| + EXPECT_TRUE(view_->ShouldShowWindowIcon()); |
| + |
| + EXPECT_CALL(*mock_global_error_with_standard_bubble_, |
| + BubbleViewDidClose(nullptr)); |
| + view_->WindowClosing(); |
| + |
| + 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); |
| + |
| + EXPECT_CALL(*mock_global_error_with_standard_bubble_, |
| + ShouldShowCloseButton()); |
| + view_->ShouldShowCloseButton(); |
| + |
| + 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); |
| + |
| + EXPECT_CALL(*mock_global_error_with_standard_bubble_, |
| + GetBubbleViewCancelButtonLabel()); |
| + view_->GetDialogButtons(); |
| + |
| + EXPECT_CALL(*mock_global_error_with_standard_bubble_, |
| + BubbleViewCancelButtonPressed(nullptr)); |
| + view_->Cancel(); |
| + |
| + EXPECT_CALL(*mock_global_error_with_standard_bubble_, |
| + BubbleViewAcceptButtonPressed(nullptr)); |
| + view_->Accept(); |
| +} |
| + |
| +TEST_F(GlobalErrorBubbleViewTest, ErrorIsNull) { |
| + mock_global_error_with_standard_bubble_.reset(); |
| + view_->GetWindowTitle(); |
| + view_->WindowClosing(); |
| + |
| + views::LabelButton* button = |
| + new views::LabelButton(new MockButtonListener(), base::string16()); |
| + view_->UpdateButton(button, ui::DIALOG_BUTTON_OK); |
| + view_->ShouldShowCloseButton(); |
| + |
| + EXPECT_EQ(base::string16(), |
| + view_->GetDialogButtonLabel(ui::DIALOG_BUTTON_OK)); |
| + EXPECT_EQ(base::string16(), |
| + view_->GetDialogButtonLabel(ui::DIALOG_BUTTON_CANCEL)); |
| + EXPECT_EQ(ui::DIALOG_BUTTON_NONE, view_->GetDialogButtons()); |
| + |
| + view_->Cancel(); |
| + view_->Accept(); |
| +} |
| + |
| +} // namespace views |