Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ui / views / controls / scroll_view_unittest.cc
index f0bdbd5..a2cba6f 100644 (file)
@@ -5,11 +5,17 @@
 #include "ui/views/controls/scroll_view.h"
 
 #include "testing/gtest/include/gtest/gtest.h"
+#include "ui/views/controls/scrollbar/overlay_scroll_bar.h"
+#include "ui/views/test/test_views.h"
 
 namespace views {
 
 namespace {
 
+const int kWidth = 100;
+const int kMinHeight = 50;
+const int kMaxHeight = 100;
+
 // View implementation that allows setting the preferred size.
 class CustomView : public View {
  public:
@@ -20,7 +26,7 @@ class CustomView : public View {
     PreferredSizeChanged();
   }
 
-  virtual gfx::Size GetPreferredSize() OVERRIDE {
+  virtual gfx::Size GetPreferredSize() const OVERRIDE {
     return preferred_size_;
   }
 
@@ -148,7 +154,6 @@ TEST(ScrollViewTest, ScrollBarsWithHeader) {
   ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL);
   EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible());
 
-
   // Size the contents such that horizontal scrollbar is needed.
   contents->SetBounds(0, 0, 400, 50);
   scroll_view.Layout();
@@ -214,7 +219,6 @@ TEST(ScrollViewTest, HeaderScrollsWithContent) {
   EXPECT_EQ("-1,0", header->bounds().origin().ToString());
 }
 
-
 // Verifies ScrollRectToVisible() on the child works.
 TEST(ScrollViewTest, ScrollRectToVisible) {
   ScrollView scroll_view;
@@ -237,4 +241,138 @@ TEST(ScrollViewTest, ScrollRectToVisible) {
   EXPECT_EQ(-(415 - viewport_height), contents->y());
 }
 
+// Verifies ClipHeightTo() uses the height of the content when it is between the
+// minimum and maximum height values.
+TEST(ScrollViewTest, ClipHeightToNormalContentHeight) {
+  ScrollView scroll_view;
+
+  scroll_view.ClipHeightTo(kMinHeight, kMaxHeight);
+
+  const int kNormalContentHeight = 75;
+  scroll_view.SetContents(
+      new views::StaticSizedView(gfx::Size(kWidth, kNormalContentHeight)));
+
+  EXPECT_EQ(gfx::Size(kWidth, kNormalContentHeight),
+            scroll_view.GetPreferredSize());
+
+  scroll_view.SizeToPreferredSize();
+  scroll_view.Layout();
+
+  EXPECT_EQ(gfx::Size(kWidth, kNormalContentHeight),
+            scroll_view.contents()->size());
+  EXPECT_EQ(gfx::Size(kWidth, kNormalContentHeight), scroll_view.size());
+}
+
+// Verifies ClipHeightTo() uses the minimum height when the content is shorter
+// thamn the minimum height value.
+TEST(ScrollViewTest, ClipHeightToShortContentHeight) {
+  ScrollView scroll_view;
+
+  scroll_view.ClipHeightTo(kMinHeight, kMaxHeight);
+
+  const int kShortContentHeight = 10;
+  scroll_view.SetContents(
+      new views::StaticSizedView(gfx::Size(kWidth, kShortContentHeight)));
+
+  EXPECT_EQ(gfx::Size(kWidth, kMinHeight), scroll_view.GetPreferredSize());
+
+  scroll_view.SizeToPreferredSize();
+  scroll_view.Layout();
+
+  EXPECT_EQ(gfx::Size(kWidth, kShortContentHeight),
+            scroll_view.contents()->size());
+  EXPECT_EQ(gfx::Size(kWidth, kMinHeight), scroll_view.size());
+}
+
+// Verifies ClipHeightTo() uses the maximum height when the content is longer
+// thamn the maximum height value.
+TEST(ScrollViewTest, ClipHeightToTallContentHeight) {
+  ScrollView scroll_view;
+
+  // Use a scrollbar that is disabled by default, so the width of the content is
+  // not affected.
+  scroll_view.SetVerticalScrollBar(new views::OverlayScrollBar(false));
+
+  scroll_view.ClipHeightTo(kMinHeight, kMaxHeight);
+
+  const int kTallContentHeight = 1000;
+  scroll_view.SetContents(
+      new views::StaticSizedView(gfx::Size(kWidth, kTallContentHeight)));
+
+  EXPECT_EQ(gfx::Size(kWidth, kMaxHeight), scroll_view.GetPreferredSize());
+
+  scroll_view.SizeToPreferredSize();
+  scroll_view.Layout();
+
+  EXPECT_EQ(gfx::Size(kWidth, kTallContentHeight),
+            scroll_view.contents()->size());
+  EXPECT_EQ(gfx::Size(kWidth, kMaxHeight), scroll_view.size());
+}
+
+// Verifies that when ClipHeightTo() produces a scrollbar, it reduces the width
+// of the inner content of the ScrollView.
+TEST(ScrollViewTest, ClipHeightToScrollbarUsesWidth) {
+  ScrollView scroll_view;
+
+  scroll_view.ClipHeightTo(kMinHeight, kMaxHeight);
+
+  // Create a view that will be much taller than it is wide.
+  scroll_view.SetContents(new views::ProportionallySizedView(1000));
+
+  // Without any width, it will default to 0,0 but be overridden by min height.
+  scroll_view.SizeToPreferredSize();
+  EXPECT_EQ(gfx::Size(0, kMinHeight), scroll_view.GetPreferredSize());
+
+  gfx::Size new_size(kWidth, scroll_view.GetHeightForWidth(kWidth));
+  scroll_view.SetSize(new_size);
+  scroll_view.Layout();
+
+  int scroll_bar_width = scroll_view.GetScrollBarWidth();
+  int expected_width = kWidth - scroll_bar_width;
+  EXPECT_EQ(scroll_view.contents()->size().width(), expected_width);
+  EXPECT_EQ(scroll_view.contents()->size().height(), 1000 * expected_width);
+  EXPECT_EQ(gfx::Size(kWidth, kMaxHeight), scroll_view.size());
+}
+
+TEST(ScrollViewTest, CornerViewVisibility) {
+  ScrollView scroll_view;
+  View* contents = new View;
+  scroll_view.SetContents(contents);
+  scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
+  View* corner_view = scroll_view.corner_view_;
+
+  // Corner view should be visible when both scrollbars are visible.
+  contents->SetBounds(0, 0, 200, 200);
+  scroll_view.Layout();
+  EXPECT_EQ(&scroll_view, corner_view->parent());
+  EXPECT_TRUE(corner_view->visible());
+
+  // Corner view should be aligned to the scrollbars.
+  EXPECT_EQ(scroll_view.vertical_scroll_bar()->x(), corner_view->x());
+  EXPECT_EQ(scroll_view.horizontal_scroll_bar()->y(), corner_view->y());
+  EXPECT_EQ(scroll_view.GetScrollBarWidth(), corner_view->width());
+  EXPECT_EQ(scroll_view.GetScrollBarHeight(), corner_view->height());
+
+  // Corner view should be removed when only the vertical scrollbar is visible.
+  contents->SetBounds(0, 0, 50, 200);
+  scroll_view.Layout();
+  EXPECT_FALSE(corner_view->parent());
+
+  // ... or when only the horizontal scrollbar is visible.
+  contents->SetBounds(0, 0, 200, 50);
+  scroll_view.Layout();
+  EXPECT_FALSE(corner_view->parent());
+
+  // ... or when no scrollbar is visible.
+  contents->SetBounds(0, 0, 50, 50);
+  scroll_view.Layout();
+  EXPECT_FALSE(corner_view->parent());
+
+  // Corner view should reappear when both scrollbars reappear.
+  contents->SetBounds(0, 0, 200, 200);
+  scroll_view.Layout();
+  EXPECT_EQ(&scroll_view, corner_view->parent());
+  EXPECT_TRUE(corner_view->visible());
+}
+
 }  // namespace views