| OLD | NEW |
| (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, Basic) { |
| 108 EXPECT_CALL(*mock_global_error_with_standard_bubble_, GetBubbleViewTitle()); |
| 109 view_->GetWindowTitle(); |
| 110 |
| 111 std::vector<gfx::ImagePNGRep> image_png_reps; |
| 112 image_png_reps.push_back(gfx::ImagePNGRep(CreatePNGBytes(25), 1.0f)); |
| 113 gfx::Image image = gfx::Image(image_png_reps); |
| 114 EXPECT_CALL(*mock_global_error_with_standard_bubble_, GetBubbleViewIcon()) |
| 115 .WillOnce(Return(image)); |
| 116 view_->GetWindowIcon(); |
| 117 |
| 118 EXPECT_TRUE(view_->ShouldShowWindowIcon()); |
| 119 |
| 120 EXPECT_CALL(*mock_global_error_with_standard_bubble_, |
| 121 BubbleViewDidClose(nullptr)); |
| 122 view_->WindowClosing(); |
| 123 |
| 124 views::LabelButton* button = |
| 125 new views::LabelButton(new MockButtonListener(), base::string16()); |
| 126 EXPECT_CALL(*mock_global_error_with_standard_bubble_, |
| 127 GetBubbleViewAcceptButtonLabel()); |
| 128 EXPECT_CALL(*mock_global_error_with_standard_bubble_, |
| 129 GetBubbleViewCancelButtonLabel()); |
| 130 EXPECT_CALL(*mock_global_error_with_standard_bubble_, |
| 131 ShouldAddElevationIconToAcceptButton()) |
| 132 .WillOnce(Return(false)); |
| 133 view_->UpdateButton(button, ui::DIALOG_BUTTON_OK); |
| 134 |
| 135 EXPECT_CALL(*mock_global_error_with_standard_bubble_, |
| 136 ShouldShowCloseButton()); |
| 137 view_->ShouldShowCloseButton(); |
| 138 |
| 139 EXPECT_CALL(*mock_global_error_with_standard_bubble_, |
| 140 GetBubbleViewAcceptButtonLabel()); |
| 141 EXPECT_CALL(*mock_global_error_with_standard_bubble_, |
| 142 GetBubbleViewCancelButtonLabel()); |
| 143 view_->GetDialogButtonLabel(ui::DIALOG_BUTTON_OK); |
| 144 view_->GetDialogButtonLabel(ui::DIALOG_BUTTON_CANCEL); |
| 145 |
| 146 EXPECT_CALL(*mock_global_error_with_standard_bubble_, |
| 147 GetBubbleViewCancelButtonLabel()); |
| 148 view_->GetDialogButtons(); |
| 149 |
| 150 EXPECT_CALL(*mock_global_error_with_standard_bubble_, |
| 151 BubbleViewCancelButtonPressed(nullptr)); |
| 152 view_->Cancel(); |
| 153 |
| 154 EXPECT_CALL(*mock_global_error_with_standard_bubble_, |
| 155 BubbleViewAcceptButtonPressed(nullptr)); |
| 156 view_->Accept(); |
| 157 } |
| 158 |
| 159 TEST_F(GlobalErrorBubbleViewTest, ErrorIsNull) { |
| 160 mock_global_error_with_standard_bubble_.reset(); |
| 161 view_->GetWindowTitle(); |
| 162 view_->WindowClosing(); |
| 163 |
| 164 views::LabelButton* button = |
| 165 new views::LabelButton(new MockButtonListener(), base::string16()); |
| 166 view_->UpdateButton(button, ui::DIALOG_BUTTON_OK); |
| 167 view_->ShouldShowCloseButton(); |
| 168 |
| 169 EXPECT_EQ(base::string16(), |
| 170 view_->GetDialogButtonLabel(ui::DIALOG_BUTTON_OK)); |
| 171 EXPECT_EQ(base::string16(), |
| 172 view_->GetDialogButtonLabel(ui::DIALOG_BUTTON_CANCEL)); |
| 173 EXPECT_EQ(ui::DIALOG_BUTTON_NONE, view_->GetDialogButtons()); |
| 174 |
| 175 view_->Cancel(); |
| 176 view_->Accept(); |
| 177 } |
| 178 |
| 179 } // namespace views |
| OLD | NEW |