This source file includes following definitions.
- VerifySplitViewLayout
- SplitHandleMoved
- count
- GetMinimumSize
- TEST
- TEST
#include "ui/views/controls/single_split_view.h"
#include "base/logging.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/views/controls/single_split_view_listener.h"
namespace {
static void VerifySplitViewLayout(const views::SingleSplitView& split) {
ASSERT_EQ(2, split.child_count());
const views::View* leading = split.child_at(0);
const views::View* trailing = split.child_at(1);
if (split.bounds().IsEmpty()) {
EXPECT_TRUE(leading->bounds().IsEmpty());
EXPECT_TRUE(trailing->bounds().IsEmpty());
return;
}
EXPECT_FALSE(leading->bounds().IsEmpty());
EXPECT_FALSE(trailing->bounds().IsEmpty());
EXPECT_FALSE(leading->bounds().Intersects(trailing->bounds()));
if (split.orientation() == views::SingleSplitView::HORIZONTAL_SPLIT) {
EXPECT_EQ(leading->bounds().height(), split.bounds().height());
EXPECT_EQ(trailing->bounds().height(), split.bounds().height());
EXPECT_LT(leading->bounds().width() + trailing->bounds().width(),
split.bounds().width());
} else if (split.orientation() == views::SingleSplitView::VERTICAL_SPLIT) {
EXPECT_EQ(leading->bounds().width(), split.bounds().width());
EXPECT_EQ(trailing->bounds().width(), split.bounds().width());
EXPECT_LT(leading->bounds().height() + trailing->bounds().height(),
split.bounds().height());
} else {
NOTREACHED();
}
}
class SingleSplitViewListenerImpl : public views::SingleSplitViewListener {
public:
SingleSplitViewListenerImpl() : count_(0) {}
virtual bool SplitHandleMoved(views::SingleSplitView* sender) OVERRIDE {
++count_;
return false;
}
int count() const { return count_; }
private:
int count_;
DISALLOW_COPY_AND_ASSIGN(SingleSplitViewListenerImpl);
};
class MinimumSizedView: public views::View {
public:
MinimumSizedView(gfx::Size min_size) : min_size_(min_size) {}
private:
gfx::Size min_size_;
virtual gfx::Size GetMinimumSize() OVERRIDE;
};
gfx::Size MinimumSizedView::GetMinimumSize() {
return min_size_;
}
}
namespace views {
TEST(SingleSplitViewTest, Resize) {
struct TestCase {
bool resize_leading_on_bounds_change;
int primary_axis_size;
int secondary_axis_size;
int divider_offset;
} test_cases[] = {
{ true, 100, 100, 33 },
{ true, 1000, 100, 933 },
{ true, 200, 100, 133 },
{ true, 0, 0, 133 },
{ false, 500, 100, 133 },
{ false, 500, 600, 133 }
};
SingleSplitView::Orientation orientations[] = {
SingleSplitView::HORIZONTAL_SPLIT,
SingleSplitView::VERTICAL_SPLIT
};
for (size_t orientation = 0; orientation < arraysize(orientations);
++orientation) {
SingleSplitView split(
new View(), new View(), orientations[orientation], NULL);
EXPECT_EQ(test_cases[0].primary_axis_size,
test_cases[0].secondary_axis_size);
split.SetBounds(0, 0, test_cases[0].primary_axis_size,
test_cases[0].secondary_axis_size);
split.set_divider_offset(test_cases[0].divider_offset);
split.Layout();
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
split.set_resize_leading_on_bounds_change(
test_cases[i].resize_leading_on_bounds_change);
if (split.orientation() == SingleSplitView::HORIZONTAL_SPLIT) {
split.SetBounds(0, 0, test_cases[i].primary_axis_size,
test_cases[i].secondary_axis_size);
} else {
split.SetBounds(0, 0, test_cases[i].secondary_axis_size,
test_cases[i].primary_axis_size);
}
EXPECT_EQ(test_cases[i].divider_offset, split.divider_offset());
VerifySplitViewLayout(split);
}
split.child_at(0)->SetVisible(false);
split.Layout();
EXPECT_EQ(split.size(), split.child_at(1)->size());
split.child_at(0)->SetVisible(true);
split.child_at(1)->SetVisible(false);
split.Layout();
EXPECT_EQ(split.size(), split.child_at(0)->size());
}
}
TEST(SingleSplitViewTest, MouseDrag) {
const int kMinimumChildSize = 25;
MinimumSizedView *child0 =
new MinimumSizedView(gfx::Size(5, kMinimumChildSize));
MinimumSizedView *child1 =
new MinimumSizedView(gfx::Size(5, kMinimumChildSize));
SingleSplitViewListenerImpl listener;
SingleSplitView split(
child0, child1, SingleSplitView::VERTICAL_SPLIT, &listener);
const int kTotalSplitSize = 100;
split.SetBounds(0, 0, 10, kTotalSplitSize);
const int kInitialDividerOffset = 33;
const int kMouseOffset = 2;
const int kMouseMoveDelta = 7;
split.set_divider_offset(kInitialDividerOffset);
split.Layout();
gfx::Point press_point(7, kInitialDividerOffset + kMouseOffset);
ui::MouseEvent mouse_pressed(
ui::ET_MOUSE_PRESSED, press_point, press_point, 0, 0);
ASSERT_TRUE(split.OnMousePressed(mouse_pressed));
EXPECT_EQ(kInitialDividerOffset, split.divider_offset());
EXPECT_EQ(0, listener.count());
gfx::Point drag_1_point(
5, kInitialDividerOffset + kMouseOffset + kMouseMoveDelta);
ui::MouseEvent mouse_dragged_1(
ui::ET_MOUSE_DRAGGED, drag_1_point, drag_1_point, 0, 0);
ASSERT_TRUE(split.OnMouseDragged(mouse_dragged_1));
EXPECT_EQ(kInitialDividerOffset + kMouseMoveDelta, split.divider_offset());
EXPECT_EQ(1, listener.count());
gfx::Point drag_2_point(
7, kMinimumChildSize - 5);
ui::MouseEvent mouse_dragged_2(
ui::ET_MOUSE_DRAGGED, drag_2_point, drag_2_point, 0,0 );
ASSERT_TRUE(split.OnMouseDragged(mouse_dragged_2));
EXPECT_EQ(kMinimumChildSize, split.divider_offset());
EXPECT_EQ(2, listener.count());
gfx::Point drag_3_point(
7, kTotalSplitSize - kMinimumChildSize + 5);
ui::MouseEvent mouse_dragged_3(
ui::ET_MOUSE_DRAGGED, drag_3_point, drag_3_point, 0, 0);
ASSERT_TRUE(split.OnMouseDragged(mouse_dragged_3));
EXPECT_EQ(kTotalSplitSize - kMinimumChildSize - split.GetDividerSize(),
split.divider_offset());
EXPECT_EQ(3, listener.count());
gfx::Point drag_4_point(
6, kInitialDividerOffset + kMouseOffset + kMouseMoveDelta * 2);
ui::MouseEvent mouse_dragged_4(
ui::ET_MOUSE_DRAGGED, drag_4_point, drag_4_point, 0, 0);
ASSERT_TRUE(split.OnMouseDragged(mouse_dragged_4));
EXPECT_EQ(kInitialDividerOffset + kMouseMoveDelta * 2,
split.divider_offset());
EXPECT_EQ(4, listener.count());
gfx::Point release_point(
7, kInitialDividerOffset + kMouseOffset + kMouseMoveDelta * 2);
ui::MouseEvent mouse_released(
ui::ET_MOUSE_RELEASED, release_point, release_point, 0, 0);
split.OnMouseReleased(mouse_released);
EXPECT_EQ(kInitialDividerOffset + kMouseMoveDelta * 2,
split.divider_offset());
split.OnMouseCaptureLost();
EXPECT_EQ(kInitialDividerOffset, split.divider_offset());
EXPECT_EQ(5, listener.count());
}
}