Home | History | Annotate | Download | only in tabbed_pane
      1 // Copyright (c) 2012 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 "base/memory/scoped_ptr.h"
      6 #include "base/message_loop/message_loop.h"
      7 #include "base/strings/utf_string_conversions.h"
      8 #include "testing/gtest/include/gtest/gtest.h"
      9 #include "ui/views/controls/tabbed_pane/tabbed_pane.h"
     10 #include "ui/views/test/views_test_base.h"
     11 
     12 using base::ASCIIToUTF16;
     13 
     14 namespace views {
     15 
     16 namespace {
     17 
     18 // A view for testing that takes a fixed preferred size upon construction.
     19 class FixedSizeView : public View {
     20  public:
     21   explicit FixedSizeView(const gfx::Size& size)
     22     : size_(size) {}
     23 
     24   // Overridden from View:
     25   virtual gfx::Size GetPreferredSize() const OVERRIDE {
     26     return size_;
     27   }
     28 
     29  private:
     30   const gfx::Size size_;
     31 
     32   DISALLOW_COPY_AND_ASSIGN(FixedSizeView);
     33 };
     34 
     35 typedef ViewsTestBase TabbedPaneTest;
     36 
     37 // Tests TabbedPane::GetPreferredSize() and TabbedPane::Layout().
     38 TEST_F(TabbedPaneTest, SizeAndLayout) {
     39   scoped_ptr<TabbedPane> tabbed_pane(new TabbedPane());
     40   View* child1 = new FixedSizeView(gfx::Size(20, 10));
     41   tabbed_pane->AddTab(ASCIIToUTF16("tab1"), child1);
     42   View* child2 = new FixedSizeView(gfx::Size(5, 5));
     43   tabbed_pane->AddTab(ASCIIToUTF16("tab2"), child2);
     44   tabbed_pane->SelectTabAt(0);
     45 
     46   // The |tabbed_pane| implementation of Views has no border by default.
     47   // Therefore it should be as wide as the widest tab. The native Windows
     48   // tabbed pane has a border that used up extra space. Therefore the preferred
     49   // width is larger than the largest child.
     50   gfx::Size pref(tabbed_pane->GetPreferredSize());
     51   EXPECT_GE(pref.width(), 20);
     52   EXPECT_GT(pref.height(), 10);
     53 
     54   // The bounds of our children should be smaller than the tabbed pane's bounds.
     55   tabbed_pane->SetBounds(0, 0, 100, 200);
     56   RunPendingMessages();
     57   gfx::Rect bounds(child1->bounds());
     58   EXPECT_GT(bounds.width(), 0);
     59   // The |tabbed_pane| has no border. Therefore the children should be as wide
     60   // as the |tabbed_pane|.
     61   EXPECT_LE(bounds.width(), 100);
     62   EXPECT_GT(bounds.height(), 0);
     63   EXPECT_LT(bounds.height(), 200);
     64 
     65   // If we switch to the other tab, it should get assigned the same bounds.
     66   tabbed_pane->SelectTabAt(1);
     67   EXPECT_EQ(bounds, child2->bounds());
     68 }
     69 
     70 TEST_F(TabbedPaneTest, AddAndSelect) {
     71   scoped_ptr<TabbedPane> tabbed_pane(new TabbedPane());
     72   // Add several tabs; only the first should be a selected automatically.
     73   for (int i = 0; i < 3; ++i) {
     74     View* tab = new View();
     75     tabbed_pane->AddTab(ASCIIToUTF16("tab"), tab);
     76     EXPECT_EQ(i + 1, tabbed_pane->GetTabCount());
     77     EXPECT_EQ(0, tabbed_pane->selected_tab_index());
     78   }
     79 
     80   // Select each tab.
     81   for (int i = 0; i < tabbed_pane->GetTabCount(); ++i) {
     82     tabbed_pane->SelectTabAt(i);
     83     EXPECT_EQ(i, tabbed_pane->selected_tab_index());
     84   }
     85 
     86   // Add a tab at index 0, it should not be selected automatically.
     87   View* tab0 = new View();
     88   tabbed_pane->AddTabAtIndex(0, ASCIIToUTF16("tab0"), tab0);
     89   EXPECT_NE(tab0, tabbed_pane->GetSelectedTab());
     90   EXPECT_NE(0, tabbed_pane->selected_tab_index());
     91 }
     92 
     93 }  // namespace
     94 
     95 }  // namespace views
     96