summaryrefslogtreecommitdiffstats
path: root/views/controls/tabbed_pane/tabbed_pane_unittest.cc
blob: 1ceef86fd10509cba1d30f679ac06d5c21cb07c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/message_loop.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "views/controls/tabbed_pane/tabbed_pane.h"
#include "views/window/window.h"
#include "views/window/window_delegate.h"

namespace views {

// A view for testing that takes a fixed preferred size upon construction.
class FixedSizeView : public View {
 public:
  FixedSizeView(const gfx::Size& size)
    : size_(size) {}

  virtual gfx::Size GetPreferredSize() {
    return size_;
  }

 private:
  const gfx::Size size_;

  DISALLOW_COPY_AND_ASSIGN(FixedSizeView);
};

class TabbedPaneTest : public testing::Test, WindowDelegate {
 public:
  TabbedPaneTest() {}

  TabbedPane* tabbed_pane_;

  void RunAllPending() {
    message_loop_.RunAllPending();
  }

 private:
  virtual void SetUp() {
    tabbed_pane_ = new TabbedPane();
    window_ = Window::CreateChromeWindow(NULL, gfx::Rect(0, 0, 100, 100), this);
    window_->Show();
  }

  virtual void TearDown() {
    window_->Close();
    message_loop_.RunAllPending();
  }

  virtual views::View* GetContentsView() {
    return tabbed_pane_;
  }

  MessageLoopForUI message_loop_;
  Window* window_;

  DISALLOW_COPY_AND_ASSIGN(TabbedPaneTest);
};

// Tests that TabbedPane::GetPreferredSize() and TabbedPane::Layout().
TEST_F(TabbedPaneTest, SizeAndLayout) {
  View* child1 = new FixedSizeView(gfx::Size(20, 10));
  tabbed_pane_->AddTab(L"tab1", child1);
  View* child2 = new FixedSizeView(gfx::Size(5, 5));
  tabbed_pane_->AddTab(L"tab2", child2);
  tabbed_pane_->SelectTabAt(0);

  // Check that the preferred size is larger than the largest child.
  gfx::Size pref(tabbed_pane_->GetPreferredSize());
  EXPECT_GT(pref.width(), 20);
  EXPECT_GT(pref.height(), 10);

  // The bounds of our children should be smaller than the tabbed pane's bounds.
  tabbed_pane_->SetBounds(0, 0, 100, 200);
  RunAllPending();
  gfx::Rect bounds(child1->bounds());
  EXPECT_GT(bounds.width(), 0);
  EXPECT_LT(bounds.width(), 100);
  EXPECT_GT(bounds.height(), 0);
  EXPECT_LT(bounds.height(), 200);

  // If we switch to the other tab, it should get assigned the same bounds.
  tabbed_pane_->SelectTabAt(1);
  EXPECT_EQ(bounds, child2->bounds());
}

} // namespace views