OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
sky
2014/09/15 18:19:13
Is there a reason you put this in chrome and not v
Devlin
2014/09/15 18:31:06
Only because that's where the MenuItemView interac
| |
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 "ui/views/controls/menu/menu_item_view.h" | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/strings/string16.h" | |
9 #include "base/strings/utf_string_conversions.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 #include "ui/views/controls/menu/submenu_view.h" | |
12 #include "ui/views/view.h" | |
13 | |
14 namespace { | |
15 | |
16 // A simple View class that will match its height to the available width. | |
17 class SquareView : public views::View { | |
18 public: | |
19 SquareView() {} | |
20 virtual ~SquareView() {} | |
21 | |
22 private: | |
23 virtual gfx::Size GetPreferredSize() const OVERRIDE { | |
24 return gfx::Size(1, 1); | |
25 } | |
26 virtual int GetHeightForWidth(int width) const OVERRIDE { | |
27 return width; | |
28 } | |
29 }; | |
30 | |
31 // A MenuItemView implementation with a public destructor (so we can clean up | |
32 // in tests). | |
33 class TestMenuItemView : public views::MenuItemView { | |
34 public: | |
35 TestMenuItemView() : views::MenuItemView(NULL) {} | |
36 virtual ~TestMenuItemView() {} | |
37 }; | |
38 | |
39 } // namespace | |
40 | |
41 TEST(MenuItemViewUnitTest, TestMenuItemViewWithFlexibleWidthChild) { | |
42 scoped_ptr<TestMenuItemView> root_menu(new TestMenuItemView()); | |
sky
2014/09/15 18:19:12
nit: AFAICT there is no need to wrap in a scoped_p
Devlin
2014/09/15 18:31:06
Good point. Done.
| |
43 root_menu->set_owned_by_client(); | |
44 | |
45 // Append a normal MenuItemView. | |
46 views::MenuItemView* label_view = | |
47 root_menu->AppendMenuItemWithLabel(1, base::ASCIIToUTF16("item 1")); | |
48 | |
49 // Append a second MenuItemView that has a child SquareView. | |
50 views::MenuItemView* flexible_view = | |
51 root_menu->AppendMenuItemWithLabel(2, base::string16()); | |
52 flexible_view->AddChildView(new SquareView()); | |
53 // Set margins to 0 so that we know width should match height. | |
54 flexible_view->SetMargins(0, 0); | |
55 | |
56 views::SubmenuView* submenu = root_menu->GetSubmenu(); | |
57 | |
58 // The first item should be the label view. | |
59 ASSERT_EQ(label_view, submenu->GetMenuItemAt(0)); | |
60 gfx::Size label_size = label_view->GetPreferredSize(); | |
61 | |
62 // The second item should be the flexible view. | |
63 ASSERT_EQ(flexible_view, submenu->GetMenuItemAt(1)); | |
64 gfx::Size flexible_size = flexible_view->GetPreferredSize(); | |
65 | |
66 // The flexible view's "preferred size" should be 1x1... | |
67 EXPECT_EQ(flexible_size, gfx::Size(1, 1)); | |
68 | |
69 // ...but it should use whatever space is available to make a square. | |
70 int flex_height = flexible_view->GetHeightForWidth(label_size.width()); | |
71 EXPECT_EQ(label_size.width(), flex_height); | |
72 | |
73 // The submenu should be tall enough to allow for both menu items at the given | |
74 // width. | |
75 EXPECT_EQ(label_size.height() + flex_height, | |
76 submenu->GetPreferredSize().height()); | |
77 } | |
OLD | NEW |