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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/views/bubble/bubble_frame_view.h" 5 #include "ui/views/bubble/bubble_frame_view.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "build/build_config.h" 10 #include "build/build_config.h"
11 #include "ui/gfx/geometry/insets.h" 11 #include "ui/gfx/geometry/insets.h"
12 #include "ui/gfx/geometry/rect.h" 12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/gfx/geometry/size.h" 13 #include "ui/gfx/geometry/size.h"
14 #include "ui/views/bubble/bubble_border.h" 14 #include "ui/views/bubble/bubble_border.h"
15 #include "ui/views/bubble/bubble_dialog_delegate.h"
15 #include "ui/views/controls/button/label_button.h" 16 #include "ui/views/controls/button/label_button.h"
16 #include "ui/views/test/test_views.h" 17 #include "ui/views/test/test_views.h"
17 #include "ui/views/test/views_test_base.h" 18 #include "ui/views/test/views_test_base.h"
18 #include "ui/views/widget/widget.h" 19 #include "ui/views/widget/widget.h"
19 #include "ui/views/widget/widget_delegate.h" 20 #include "ui/views/widget/widget_delegate.h"
20 21
21 namespace views { 22 namespace views {
22 23
23 typedef ViewsTestBase BubbleFrameViewTest; 24 typedef ViewsTestBase BubbleFrameViewTest;
24 25
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after
481 #else 482 #else
482 maximum_rect.Inset(frame.bubble_border()->GetInsets()); 483 maximum_rect.Inset(frame.bubble_border()->GetInsets());
483 484
484 // Should ignore the contents view's maximum size and use the preferred size. 485 // Should ignore the contents view's maximum size and use the preferred size.
485 gfx::Size expected_size(kPreferredClientWidth + kExpectedAdditionalWidth, 486 gfx::Size expected_size(kPreferredClientWidth + kExpectedAdditionalWidth,
486 kPreferredClientHeight + kExpectedAdditionalHeight); 487 kPreferredClientHeight + kExpectedAdditionalHeight);
487 EXPECT_EQ(expected_size, maximum_rect.size()); 488 EXPECT_EQ(expected_size, maximum_rect.size());
488 #endif 489 #endif
489 } 490 }
490 491
492 namespace {
493
494 class TestBubbleDialogDelegateView : public BubbleDialogDelegateView {
495 public:
496 TestBubbleDialogDelegateView()
497 : BubbleDialogDelegateView(nullptr, BubbleBorder::NONE) {
498 set_shadow(BubbleBorder::NO_ASSETS);
499 }
500 ~TestBubbleDialogDelegateView() override {}
501
502 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 :)
503 BubbleDialogDelegateView::SetAnchorView(anchor_view);
504 }
505
506 // This delegate is owned by the test case itself, so it should not delete
507 // 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.
508 void DeleteDelegate() override {}
509
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.
510 private:
511 DISALLOW_COPY_AND_ASSIGN(TestBubbleDialogDelegateView);
512 };
513
514 class TestLayoutProvider : public LayoutProvider {
515 public:
516 TestLayoutProvider() : LayoutProvider() {}
517 ~TestLayoutProvider() override {}
518
519 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.
520 return snap_to_ ? snap_to_ : min_width;
521 }
522
523 void set_snap_to(int width) { snap_to_ = width; }
524
525 private:
526 int snap_to_ = 0;
527
528 DISALLOW_COPY_AND_ASSIGN(TestLayoutProvider);
529 };
530
531 } // namespace
532
533 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.
534 // It would be good to test directly against HarmonyLayoutProvider, but //ui
535 // can't depend on //chrome to get at it. Instead, this test has its own
536 // 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.
537 TestLayoutProvider provider;
538
539 std::unique_ptr<TestBubbleDialogDelegateView> delegate(
540 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 :)
541
542 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.
543 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
544 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.
545 anchor->Init(params);
546 anchor->Show();
547
548 // 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
549 // needs |anchor|'s ContentView, so instead of passing
550 // |anchor->GetContentsView()| directly to the |TestBubbleDialogDelegateView|
551 // constructor, the anchor view has to be set here, after |anchor| is
552 // constructed.
553 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
554
555 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.
556
557 Widget* w0 = BubbleDialogDelegateView::CreateBubble(delegate.get());
558 w0->Show();
559 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.
560
561 provider.set_snap_to(kTestWidth);
562
563 // The Widget's snapped width should exactly match the width returned by the
564 // LayoutProvider.
565 Widget* w1 = BubbleDialogDelegateView::CreateBubble(delegate.get());
566 w1->Show();
567 EXPECT_EQ(w1->GetWindowBoundsInScreen().width(), kTestWidth);
568 }
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
569
491 } // namespace views 570 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698