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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/bubble/info_bubble.h"
#include <memory>
#include <utility>
#include "base/strings/utf_string_conversions.h"
#include "ui/views/controls/label.h"
#include "ui/views/test/test_widget_observer.h"
#include "ui/views/test/view_metadata_test_utils.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/widget.h"
namespace views::test {
class InfoBubbleTest : public ViewsTestBase {
public:
InfoBubbleTest() = default;
InfoBubbleTest(const InfoBubbleTest&) = delete;
InfoBubbleTest& operator=(const InfoBubbleTest&) = delete;
~InfoBubbleTest() override = default;
// ViewsTestBase:
void SetUp() override {
ViewsTestBase::SetUp();
Widget::InitParams params =
CreateParamsForTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET,
Widget::InitParams::TYPE_WINDOW);
anchor_widget_ = std::make_unique<Widget>();
anchor_widget_->Init(std::move(params));
anchor_widget_->Show();
}
void TearDown() override {
anchor_widget_.reset();
ViewsTestBase::TearDown();
}
Widget* anchor_widget() { return anchor_widget_.get(); }
private:
std::unique_ptr<Widget> anchor_widget_;
};
TEST_F(InfoBubbleTest, CreateInfoBubble) {
std::u16string text = u"test message";
InfoBubble* info_bubble = new InfoBubble(anchor_widget()->GetContentsView(),
BubbleBorder::Arrow::TOP_LEFT, text);
info_bubble->Show();
TestWidgetObserver bubble_observer(info_bubble->GetWidget());
EXPECT_EQ(info_bubble->GetAnchorView(), anchor_widget()->GetContentsView());
EXPECT_EQ(info_bubble->GetAnchorView()->GetWidget(), anchor_widget());
EXPECT_EQ(text, info_bubble->label_for_testing()->GetText());
EXPECT_TRUE(info_bubble->GetVisible());
EXPECT_FALSE(bubble_observer.widget_closed());
info_bubble->Hide();
RunPendingMessages();
EXPECT_TRUE(bubble_observer.widget_closed());
}
// Ensure the InfoBubble is still sized if not supplied with a preferred width.
TEST_F(InfoBubbleTest, TestPreferredWidthNull) {
InfoBubble* info_bubble =
new InfoBubble(anchor_widget()->GetContentsView(),
BubbleBorder::Arrow::TOP_LEFT, std::u16string());
auto child = std::make_unique<View>();
child->SetPreferredSize(gfx::Size(50, 50));
info_bubble->AddChildView(std::move(child));
info_bubble->Show();
EXPECT_LT(0, info_bubble->GetLocalBounds().width());
info_bubble->Hide();
RunPendingMessages();
}
TEST_F(InfoBubbleTest, TestPreferredWidth) {
constexpr int kPreferredWidthLarge = 800;
constexpr int kPreferredWidthSmall = 50;
InfoBubble* info_bubble =
new InfoBubble(anchor_widget()->GetContentsView(),
BubbleBorder::Arrow::TOP_LEFT, std::u16string());
info_bubble->Show();
info_bubble->set_preferred_width(kPreferredWidthLarge);
info_bubble->SizeToPreferredSize();
// Test to make sure the resulting |info_bubble| honors the preferred size.
// |info_bubble| may be slightly smaller due to having to account for margins
// and bubble border size.
EXPECT_GE(kPreferredWidthLarge, info_bubble->GetLocalBounds().width());
EXPECT_LT(kPreferredWidthSmall, info_bubble->GetLocalBounds().width());
info_bubble->set_preferred_width(kPreferredWidthSmall);
info_bubble->SizeToPreferredSize();
// |info_bubble| should now be at or smaller than the smaller preferred width.
EXPECT_GE(kPreferredWidthSmall, info_bubble->GetLocalBounds().width());
info_bubble->Hide();
RunPendingMessages();
}
TEST_F(InfoBubbleTest, TestInfoBubbleVisibilityHiddenAnchor) {
anchor_widget()->Hide();
InfoBubble* info_bubble =
new InfoBubble(anchor_widget()->GetContentsView(),
BubbleBorder::Arrow::TOP_LEFT, std::u16string());
info_bubble->Show();
EXPECT_FALSE(info_bubble->GetWidget()->IsVisible());
info_bubble->Hide();
RunPendingMessages();
}
TEST_F(InfoBubbleTest, TestInfoBubbleAnchorBoundsChanged) {
InfoBubble* info_bubble = new InfoBubble(anchor_widget()->GetContentsView(),
BubbleBorder::Arrow::TOP_LEFT, u"");
info_bubble->Show();
gfx::Rect original_bounds =
info_bubble->GetWidget()->GetWindowBoundsInScreen();
anchor_widget()->SetBounds(original_bounds - gfx::Vector2d(5, 5));
EXPECT_NE(original_bounds,
info_bubble->GetWidget()->GetWindowBoundsInScreen());
info_bubble->Hide();
RunPendingMessages();
}
// Iterate through the metadata for InfoBubble to ensure it all works.
TEST_F(InfoBubbleTest, MetadataTest) {
InfoBubble* info_bubble = new InfoBubble(anchor_widget()->GetContentsView(),
BubbleBorder::Arrow::TOP_LEFT, u"");
info_bubble->Show();
test::TestViewMetadata(info_bubble);
info_bubble->Hide();
RunPendingMessages();
}
} // namespace views::test
|