This source file includes following definitions.
- active_tab_
- set_immersive_style
- set_active_tab
- GetSelectionModel
- SupportsMultipleSelection
- SelectTab
- ExtendSelectionTo
- ToggleSelected
- AddSelectionFromAnchorTo
- CloseTab
- ShowContextMenuForTab
- IsActiveTab
- IsTabSelected
- IsTabPinned
- MaybeStartDrag
- ContinueDrag
- EndDrag
- GetTabAt
- OnMouseEventInTab
- ShouldPaintTab
- IsImmersiveStyle
- DisableMediaIndicatorAnimation
- CheckForExpectedLayoutAndVisibilityOfElements
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/ui/views/tabs/tab.h"
#include "chrome/browser/ui/views/tabs/tab_controller.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/models/list_selection_model.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/widget.h"
using views::Widget;
class FakeTabController : public TabController {
public:
FakeTabController() : immersive_style_(false), active_tab_(false) {
}
virtual ~FakeTabController() {}
void set_immersive_style(bool value) { immersive_style_ = value; }
void set_active_tab(bool value) { active_tab_ = value; }
virtual const ui::ListSelectionModel& GetSelectionModel() OVERRIDE {
return selection_model_;
}
virtual bool SupportsMultipleSelection() OVERRIDE { return false; }
virtual void SelectTab(Tab* tab) OVERRIDE {}
virtual void ExtendSelectionTo(Tab* tab) OVERRIDE {}
virtual void ToggleSelected(Tab* tab) OVERRIDE {}
virtual void AddSelectionFromAnchorTo(Tab* tab) OVERRIDE {}
virtual void CloseTab(Tab* tab, CloseTabSource source) OVERRIDE {}
virtual void ShowContextMenuForTab(Tab* tab,
const gfx::Point& p,
ui::MenuSourceType source_type) OVERRIDE {}
virtual bool IsActiveTab(const Tab* tab) const OVERRIDE {
return active_tab_;
}
virtual bool IsTabSelected(const Tab* tab) const OVERRIDE {
return false;
}
virtual bool IsTabPinned(const Tab* tab) const OVERRIDE { return false; }
virtual void MaybeStartDrag(
Tab* tab,
const ui::LocatedEvent& event,
const ui::ListSelectionModel& original_selection) OVERRIDE {}
virtual void ContinueDrag(views::View* view,
const ui::LocatedEvent& event) OVERRIDE {}
virtual bool EndDrag(EndDragReason reason) OVERRIDE { return false; }
virtual Tab* GetTabAt(Tab* tab,
const gfx::Point& tab_in_tab_coordinates) OVERRIDE {
return NULL;
}
virtual void OnMouseEventInTab(views::View* source,
const ui::MouseEvent& event) OVERRIDE {}
virtual bool ShouldPaintTab(const Tab* tab, gfx::Rect* clip) OVERRIDE {
return true;
}
virtual bool IsImmersiveStyle() const OVERRIDE { return immersive_style_; }
private:
ui::ListSelectionModel selection_model_;
bool immersive_style_;
bool active_tab_;
DISALLOW_COPY_AND_ASSIGN(FakeTabController);
};
class TabTest : public views::ViewsTestBase {
public:
TabTest() {}
virtual ~TabTest() {}
static void DisableMediaIndicatorAnimation(Tab* tab) {
tab->media_indicator_animation_.reset();
tab->animating_media_state_ = tab->data_.media_state;
}
static void CheckForExpectedLayoutAndVisibilityOfElements(const Tab& tab) {
if (tab.data_.mini) {
EXPECT_EQ(1, tab.IconCapacity());
if (tab.data_.media_state != TAB_MEDIA_STATE_NONE) {
EXPECT_FALSE(tab.ShouldShowIcon());
EXPECT_TRUE(tab.ShouldShowMediaIndicator());
} else {
EXPECT_TRUE(tab.ShouldShowIcon());
EXPECT_FALSE(tab.ShouldShowMediaIndicator());
}
EXPECT_FALSE(tab.ShouldShowCloseBox());
} else if (tab.IsActive()) {
EXPECT_TRUE(tab.ShouldShowCloseBox());
switch (tab.IconCapacity()) {
case 0:
case 1:
EXPECT_FALSE(tab.ShouldShowIcon());
EXPECT_FALSE(tab.ShouldShowMediaIndicator());
break;
case 2:
if (tab.data_.media_state != TAB_MEDIA_STATE_NONE) {
EXPECT_FALSE(tab.ShouldShowIcon());
EXPECT_TRUE(tab.ShouldShowMediaIndicator());
} else {
EXPECT_TRUE(tab.ShouldShowIcon());
EXPECT_FALSE(tab.ShouldShowMediaIndicator());
}
break;
default:
EXPECT_LE(3, tab.IconCapacity());
EXPECT_TRUE(tab.ShouldShowIcon());
if (tab.data_.media_state != TAB_MEDIA_STATE_NONE)
EXPECT_TRUE(tab.ShouldShowMediaIndicator());
else
EXPECT_FALSE(tab.ShouldShowMediaIndicator());
break;
}
} else {
switch (tab.IconCapacity()) {
case 0:
EXPECT_FALSE(tab.ShouldShowCloseBox());
EXPECT_FALSE(tab.ShouldShowIcon());
EXPECT_FALSE(tab.ShouldShowMediaIndicator());
break;
case 1:
EXPECT_FALSE(tab.ShouldShowCloseBox());
if (tab.data_.media_state != TAB_MEDIA_STATE_NONE) {
EXPECT_FALSE(tab.ShouldShowIcon());
EXPECT_TRUE(tab.ShouldShowMediaIndicator());
} else {
EXPECT_TRUE(tab.ShouldShowIcon());
EXPECT_FALSE(tab.ShouldShowMediaIndicator());
}
break;
default:
EXPECT_LE(2, tab.IconCapacity());
EXPECT_TRUE(tab.ShouldShowIcon());
if (tab.data_.media_state != TAB_MEDIA_STATE_NONE)
EXPECT_TRUE(tab.ShouldShowMediaIndicator());
else
EXPECT_FALSE(tab.ShouldShowMediaIndicator());
break;
}
}
const gfx::Rect contents_bounds = tab.GetContentsBounds();
if (tab.ShouldShowIcon()) {
EXPECT_LE(contents_bounds.x(), tab.favicon_bounds_.x());
if (tab.title_bounds_.width() > 0)
EXPECT_LE(tab.favicon_bounds_.right(), tab.title_bounds_.x());
EXPECT_LE(contents_bounds.y(), tab.favicon_bounds_.y());
EXPECT_LE(tab.favicon_bounds_.bottom(), contents_bounds.bottom());
}
if (tab.ShouldShowIcon() && tab.ShouldShowMediaIndicator())
EXPECT_LE(tab.favicon_bounds_.right(), tab.media_indicator_bounds_.x());
if (tab.ShouldShowMediaIndicator()) {
if (tab.title_bounds_.width() > 0)
EXPECT_LE(tab.title_bounds_.right(), tab.media_indicator_bounds_.x());
EXPECT_LE(tab.media_indicator_bounds_.right(), contents_bounds.right());
EXPECT_LE(contents_bounds.y(), tab.media_indicator_bounds_.y());
EXPECT_LE(tab.media_indicator_bounds_.bottom(), contents_bounds.bottom());
}
if (tab.ShouldShowMediaIndicator() && tab.ShouldShowCloseBox()) {
EXPECT_LE(tab.media_indicator_bounds_.right(),
tab.close_button_->bounds().x() +
tab.close_button_->GetInsets().left());
}
if (tab.ShouldShowCloseBox()) {
if (tab.title_bounds_.width() > 0) {
EXPECT_LE(tab.title_bounds_.right(),
tab.close_button_->bounds().x() +
tab.close_button_->GetInsets().left());
}
EXPECT_LE(tab.close_button_->bounds().right(), contents_bounds.right());
EXPECT_LE(contents_bounds.y(), tab.close_button_->bounds().y());
EXPECT_LE(tab.close_button_->bounds().bottom(), contents_bounds.bottom());
}
}
};
TEST_F(TabTest, HitTestTopPixel) {
Widget widget;
Widget::InitParams params(CreateParams(Widget::InitParams::TYPE_WINDOW));
params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds.SetRect(10, 20, 300, 400);
widget.Init(params);
FakeTabController tab_controller;
Tab tab(&tab_controller);
widget.GetContentsView()->AddChildView(&tab);
tab.SetBoundsRect(gfx::Rect(gfx::Point(0, 0), Tab::GetStandardSize()));
int middle_x = tab.width() / 2;
EXPECT_FALSE(tab.HitTestPoint(gfx::Point(middle_x, 0)));
int middle_y = tab.height() / 2;
EXPECT_FALSE(tab.HitTestPoint(gfx::Point(0, middle_y)));
widget.Maximize();
EXPECT_TRUE(tab.HitTestPoint(gfx::Point(middle_x, 0)));
EXPECT_FALSE(tab.HitTestPoint(gfx::Point(0, 0)));
EXPECT_FALSE(tab.HitTestPoint(gfx::Point(tab.width() - 1, 0)));
}
TEST_F(TabTest, LayoutAndVisibilityOfElements) {
static const TabMediaState kMediaStatesToTest[] = {
TAB_MEDIA_STATE_NONE, TAB_MEDIA_STATE_CAPTURING,
TAB_MEDIA_STATE_AUDIO_PLAYING
};
FakeTabController controller;
Tab tab(&controller);
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
bitmap.allocPixels();
TabRendererData data;
data.favicon = gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
for (int is_mini_tab = 0; is_mini_tab < 2; ++is_mini_tab) {
for (int is_active_tab = 0; is_active_tab < 2; ++is_active_tab) {
for (size_t media_state_index = 0;
media_state_index < arraysize(kMediaStatesToTest);
++media_state_index) {
const TabMediaState media_state = kMediaStatesToTest[media_state_index];
SCOPED_TRACE(::testing::Message()
<< (is_active_tab ? "Active" : "Inactive") << ' '
<< (is_mini_tab ? "Mini " : "")
<< "Tab with media indicator state " << media_state);
data.mini = !!is_mini_tab;
controller.set_active_tab(!!is_active_tab);
data.media_state = media_state;
tab.SetData(data);
DisableMediaIndicatorAnimation(&tab);
gfx::Rect bounds(gfx::Point(0, 0), Tab::GetStandardSize());
int min_width;
if (is_mini_tab) {
bounds.set_width(Tab::GetMiniWidth());
min_width = Tab::GetMiniWidth();
} else {
min_width = is_active_tab ? Tab::GetMinimumSelectedSize().width() :
Tab::GetMinimumUnselectedSize().width();
}
while (bounds.width() >= min_width) {
SCOPED_TRACE(::testing::Message() << "bounds=" << bounds.ToString());
tab.SetBoundsRect(bounds);
CheckForExpectedLayoutAndVisibilityOfElements(tab);
bounds.set_width(bounds.width() - 1);
}
}
}
}
}
TEST_F(TabTest, CloseButtonLayout) {
FakeTabController tab_controller;
Tab tab(&tab_controller);
tab.SetBounds(0, 0, 100, 50);
tab.Layout();
gfx::Insets close_button_insets = tab.close_button_->GetInsets();
tab.Layout();
gfx::Insets close_button_insets_2 = tab.close_button_->GetInsets();
EXPECT_EQ(close_button_insets.top(), close_button_insets_2.top());
EXPECT_EQ(close_button_insets.left(), close_button_insets_2.left());
EXPECT_EQ(close_button_insets.bottom(), close_button_insets_2.bottom());
EXPECT_EQ(close_button_insets.right(), close_button_insets_2.right());
EXPECT_EQ(50, tab.close_button_->bounds().height());
}