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

Unified Diff: ui/views/bubble/bubble_frame_view_unittest.cc

Issue 2821413002: views: support dialog width snapping once and for all (Closed)
Patch Set: Created 3 years, 8 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: ui/views/bubble/bubble_frame_view_unittest.cc
diff --git a/ui/views/bubble/bubble_frame_view_unittest.cc b/ui/views/bubble/bubble_frame_view_unittest.cc
index 3474899d99a83b9c15b20173e5287c20bade7266..075ad780b9bf2da3cc451320ebf40b4faff54999 100644
--- a/ui/views/bubble/bubble_frame_view_unittest.cc
+++ b/ui/views/bubble/bubble_frame_view_unittest.cc
@@ -12,6 +12,7 @@
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/views/bubble/bubble_border.h"
+#include "ui/views/bubble/bubble_dialog_delegate.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/test/test_views.h"
#include "ui/views/test/views_test_base.h"
@@ -488,4 +489,82 @@ TEST_F(BubbleFrameViewTest, GetMaximumSize) {
#endif
}
+namespace {
+
+class TestBubbleDialogDelegateView : public BubbleDialogDelegateView {
+ public:
+ TestBubbleDialogDelegateView()
+ : BubbleDialogDelegateView(nullptr, BubbleBorder::NONE) {
+ set_shadow(BubbleBorder::NO_ASSETS);
+ }
+ ~TestBubbleDialogDelegateView() override {}
+
+ void SetAnchorView(View* anchor_view) {
tapted 2017/04/20 01:12:56 `using BubbleDialogDelegateView::SetAnchorView;`
Elly Fong-Jones 2017/04/20 19:54:00 Ooh, I didn't even know you could do this! TIL :)
+ BubbleDialogDelegateView::SetAnchorView(anchor_view);
+ }
+
+ // This delegate is owned by the test case itself, so it should not delete
+ // itself here.
tapted 2017/04/20 01:12:56 nit: here, // BubbleDialogDelegateView: I'd put t
Elly Fong-Jones 2017/04/20 19:54:00 Done.
+ void DeleteDelegate() override {}
+
tapted 2017/04/20 01:12:56 We should override GetDialogButtons() as well to r
Elly Fong-Jones 2017/04/20 19:53:59 Done.
+ private:
+ DISALLOW_COPY_AND_ASSIGN(TestBubbleDialogDelegateView);
+};
+
+class TestLayoutProvider : public LayoutProvider {
+ public:
+ TestLayoutProvider() : LayoutProvider() {}
+ ~TestLayoutProvider() override {}
+
+ int GetSnappedDialogWidth(int min_width) const override {
tapted 2017/04/20 01:12:56 nit: // LayoutProvider:
Elly Fong-Jones 2017/04/20 19:54:00 Done.
+ return snap_to_ ? snap_to_ : min_width;
+ }
+
+ void set_snap_to(int width) { snap_to_ = width; }
+
+ private:
+ int snap_to_ = 0;
+
+ DISALLOW_COPY_AND_ASSIGN(TestLayoutProvider);
+};
+
+} // namespace
+
+TEST_F(BubbleFrameViewTest, WidthSnaps) {
tapted 2017/04/20 01:12:56 nit: comment describing the test purpose
Elly Fong-Jones 2017/04/20 19:54:00 Done.
+ // It would be good to test directly against HarmonyLayoutProvider, but //ui
+ // can't depend on //chrome to get at it. Instead, this test has its own
+ // LayoutProvider.
Peter Kasting 2017/04/19 19:26:13 Nit: I feel like even talking about HarmonyLayoutP
Elly Fong-Jones 2017/04/20 19:53:59 Done.
+ TestLayoutProvider provider;
+
+ std::unique_ptr<TestBubbleDialogDelegateView> delegate(
+ new TestBubbleDialogDelegateView());
Peter Kasting 2017/04/19 19:26:13 Nit: Prefer = base::MakeUnique to bare new
tapted 2017/04/20 01:12:56 could this just go on the stack? TestBubbleDialog
Elly Fong-Jones 2017/04/20 19:54:00 Acknowledged.
Elly Fong-Jones 2017/04/20 19:54:00 Ooh, it can. Done :)
+
+ std::unique_ptr<Widget> anchor(new Widget());
tapted 2017/04/20 01:12:56 this can probably go on the stack too
Elly Fong-Jones 2017/04/20 19:54:00 Done.
+ Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
+ params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
tapted 2017/04/20 01:12:56 nit: `views::` not required
Elly Fong-Jones 2017/04/20 19:54:00 Done.
+ anchor->Init(params);
+ anchor->Show();
+
+ // This is a bit awkward: |anchor| needs to outlive |delegate|, but |delegate|
Peter Kasting 2017/04/19 19:26:13 I'm confused. If |anchor| needs to outlive |deleg
Elly Fong-Jones 2017/04/20 19:54:00 After more thought, this entire comment was nonsen
+ // needs |anchor|'s ContentView, so instead of passing
+ // |anchor->GetContentsView()| directly to the |TestBubbleDialogDelegateView|
+ // constructor, the anchor view has to be set here, after |anchor| is
+ // constructed.
+ delegate->SetAnchorView(anchor->GetContentsView());
tapted 2017/04/20 01:12:56 Why do we need an anchor view? can we just leave i
Elly Fong-Jones 2017/04/20 19:54:00 I'm surprised that it works without an anchor view
+
+ const int kTestWidth = 300;
Peter Kasting 2017/04/19 19:26:13 Nit: Prefer constexpr for compile-time constants
Elly Fong-Jones 2017/04/20 19:54:00 Done.
+
+ Widget* w0 = BubbleDialogDelegateView::CreateBubble(delegate.get());
+ w0->Show();
+ EXPECT_NE(w0->GetWindowBoundsInScreen().width(), kTestWidth);
Peter Kasting 2017/04/19 19:26:13 Nit: (expected, actual) (2 places)
tapted 2017/04/20 01:12:56 Can we add a TestBubbleDialogDelegateView::GetPref
Elly Fong-Jones 2017/04/20 19:53:59 nice :D Done.
Elly Fong-Jones 2017/04/20 19:54:00 Acknowledged.
+
+ provider.set_snap_to(kTestWidth);
+
+ // The Widget's snapped width should exactly match the width returned by the
+ // LayoutProvider.
+ Widget* w1 = BubbleDialogDelegateView::CreateBubble(delegate.get());
+ w1->Show();
+ EXPECT_EQ(w1->GetWindowBoundsInScreen().width(), kTestWidth);
+}
tapted 2017/04/20 01:12:56 w0->CloseNow(); w1->CloseNow(); (otherwise.. I th
Elly Fong-Jones 2017/04/20 19:53:59 Does this also serve to delete w0 and w1? I can't
tapted 2017/04/21 00:20:31 Yup. Widget destruction triggers a CloseNow() when
+
} // namespace views

Powered by Google App Engine
This is Rietveld 408576698