31669354a08ae6a603088939da616efd83a03684
[platform/framework/web/crosswalk.git] / src / ui / views / controls / menu / menu_item_view_unittest.cc
1 // Copyright 2014 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 "ui/views/controls/menu/menu_item_view.h"
6
7 #include "base/strings/string16.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/views/controls/menu/submenu_view.h"
11 #include "ui/views/view.h"
12
13 namespace {
14
15 // A simple View class that will match its height to the available width.
16 class SquareView : public views::View {
17  public:
18   SquareView() {}
19   ~SquareView() override {}
20
21  private:
22   gfx::Size GetPreferredSize() const override { return gfx::Size(1, 1); }
23   int GetHeightForWidth(int width) const override { return width; }
24 };
25
26 // A MenuItemView implementation with a public destructor (so we can clean up
27 // in tests).
28 class TestMenuItemView : public views::MenuItemView {
29  public:
30   TestMenuItemView() : views::MenuItemView(NULL) {}
31   ~TestMenuItemView() override {}
32 };
33
34 }  // namespace
35
36 TEST(MenuItemViewUnitTest, TestMenuItemViewWithFlexibleWidthChild) {
37   TestMenuItemView root_menu;
38   root_menu.set_owned_by_client();
39
40   // Append a normal MenuItemView.
41   views::MenuItemView* label_view =
42       root_menu.AppendMenuItemWithLabel(1, base::ASCIIToUTF16("item 1"));
43
44   // Append a second MenuItemView that has a child SquareView.
45   views::MenuItemView* flexible_view =
46       root_menu.AppendMenuItemWithLabel(2, base::string16());
47   flexible_view->AddChildView(new SquareView());
48   // Set margins to 0 so that we know width should match height.
49   flexible_view->SetMargins(0, 0);
50
51   views::SubmenuView* submenu = root_menu.GetSubmenu();
52
53   // The first item should be the label view.
54   ASSERT_EQ(label_view, submenu->GetMenuItemAt(0));
55   gfx::Size label_size = label_view->GetPreferredSize();
56
57   // The second item should be the flexible view.
58   ASSERT_EQ(flexible_view, submenu->GetMenuItemAt(1));
59   gfx::Size flexible_size = flexible_view->GetPreferredSize();
60
61   // The flexible view's "preferred size" should be 1x1...
62   EXPECT_EQ(flexible_size, gfx::Size(1, 1));
63
64   // ...but it should use whatever space is available to make a square.
65   int flex_height = flexible_view->GetHeightForWidth(label_size.width());
66   EXPECT_EQ(label_size.width(), flex_height);
67
68   // The submenu should be tall enough to allow for both menu items at the given
69   // width.
70   EXPECT_EQ(label_size.height() + flex_height,
71             submenu->GetPreferredSize().height());
72 }