OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/tabs/tab.h" | 5 #include "chrome/browser/ui/views/tabs/tab.h" |
6 | 6 |
7 #include "base/i18n/rtl.h" | 7 #include "base/i18n/rtl.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/ui/tabs/tab_utils.h" | 9 #include "chrome/browser/ui/tabs/tab_utils.h" |
10 #include "chrome/browser/ui/views/tabs/media_indicator_button.h" | 10 #include "chrome/browser/ui/views/tabs/media_indicator_button.h" |
11 #include "chrome/browser/ui/views/tabs/tab_controller.h" | 11 #include "chrome/browser/ui/views/tabs/tab_controller.h" |
12 #include "grit/theme_resources.h" | 12 #include "grit/theme_resources.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "ui/base/models/list_selection_model.h" | 14 #include "ui/base/models/list_selection_model.h" |
15 #include "ui/views/controls/button/image_button.h" | 15 #include "ui/views/controls/button/image_button.h" |
16 #include "ui/views/controls/label.h" | 16 #include "ui/views/controls/label.h" |
17 #include "ui/views/test/views_test_base.h" | 17 #include "ui/views/test/views_test_base.h" |
18 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
19 | 19 |
20 using views::Widget; | 20 using views::Widget; |
21 | 21 |
22 class FakeTabController : public TabController { | 22 class FakeTabController : public TabController { |
23 public: | 23 public: |
24 FakeTabController() : immersive_style_(false), active_tab_(false) { | 24 FakeTabController() {} |
25 } | |
26 ~FakeTabController() override {} | |
27 | 25 |
28 void set_immersive_style(bool value) { immersive_style_ = value; } | 26 void set_immersive_style(bool value) { immersive_style_ = value; } |
29 void set_active_tab(bool value) { active_tab_ = value; } | 27 void set_active_tab(bool value) { active_tab_ = value; } |
| 28 void set_paint_throbber_to_layer(bool value) { |
| 29 paint_throbber_to_layer_ = value; |
| 30 } |
30 | 31 |
31 const ui::ListSelectionModel& GetSelectionModel() const override { | 32 const ui::ListSelectionModel& GetSelectionModel() const override { |
32 return selection_model_; | 33 return selection_model_; |
33 } | 34 } |
34 bool SupportsMultipleSelection() override { return false; } | 35 bool SupportsMultipleSelection() override { return false; } |
35 bool ShouldHideCloseButtonForInactiveTabs() override { | 36 bool ShouldHideCloseButtonForInactiveTabs() override { |
36 return false; | 37 return false; |
37 } | 38 } |
38 void SelectTab(Tab* tab) override {} | 39 void SelectTab(Tab* tab) override {} |
39 void ExtendSelectionTo(Tab* tab) override {} | 40 void ExtendSelectionTo(Tab* tab) override {} |
(...skipping 13 matching lines...) Expand all Loading... |
53 const ui::ListSelectionModel& original_selection) override {} | 54 const ui::ListSelectionModel& original_selection) override {} |
54 void ContinueDrag(views::View* view, const ui::LocatedEvent& event) override { | 55 void ContinueDrag(views::View* view, const ui::LocatedEvent& event) override { |
55 } | 56 } |
56 bool EndDrag(EndDragReason reason) override { return false; } | 57 bool EndDrag(EndDragReason reason) override { return false; } |
57 Tab* GetTabAt(Tab* tab, const gfx::Point& tab_in_tab_coordinates) override { | 58 Tab* GetTabAt(Tab* tab, const gfx::Point& tab_in_tab_coordinates) override { |
58 return NULL; | 59 return NULL; |
59 } | 60 } |
60 void OnMouseEventInTab(views::View* source, | 61 void OnMouseEventInTab(views::View* source, |
61 const ui::MouseEvent& event) override {} | 62 const ui::MouseEvent& event) override {} |
62 bool ShouldPaintTab(const Tab* tab, gfx::Rect* clip) override { return true; } | 63 bool ShouldPaintTab(const Tab* tab, gfx::Rect* clip) override { return true; } |
| 64 bool CanPaintThrobberToLayer() const override { |
| 65 return paint_throbber_to_layer_; |
| 66 } |
63 bool IsImmersiveStyle() const override { return immersive_style_; } | 67 bool IsImmersiveStyle() const override { return immersive_style_; } |
64 int GetBackgroundResourceId(bool* custom_image) const override { | 68 int GetBackgroundResourceId(bool* custom_image) const override { |
65 *custom_image = false; | 69 *custom_image = false; |
66 return IDR_THEME_TAB_BACKGROUND; | 70 return IDR_THEME_TAB_BACKGROUND; |
67 } | 71 } |
68 void UpdateTabAccessibilityState(const Tab* tab, | 72 void UpdateTabAccessibilityState(const Tab* tab, |
69 ui::AXViewState* state) override{}; | 73 ui::AXViewState* state) override{}; |
70 | 74 |
71 private: | 75 private: |
72 ui::ListSelectionModel selection_model_; | 76 ui::ListSelectionModel selection_model_; |
73 bool immersive_style_; | 77 bool immersive_style_ = false; |
74 bool active_tab_; | 78 bool active_tab_ = false; |
| 79 bool paint_throbber_to_layer_ = true; |
75 | 80 |
76 DISALLOW_COPY_AND_ASSIGN(FakeTabController); | 81 DISALLOW_COPY_AND_ASSIGN(FakeTabController); |
77 }; | 82 }; |
78 | 83 |
79 class TabTest : public views::ViewsTestBase, | 84 class TabTest : public views::ViewsTestBase, |
80 public ::testing::WithParamInterface<bool> { | 85 public ::testing::WithParamInterface<bool> { |
81 public: | 86 public: |
82 TabTest() {} | 87 TabTest() {} |
83 virtual ~TabTest() {} | 88 virtual ~TabTest() {} |
84 | 89 |
85 bool testing_for_rtl_locale() const { return GetParam(); } | 90 bool testing_for_rtl_locale() const { return GetParam(); } |
86 | 91 |
87 void SetUp() override { | 92 void SetUp() override { |
88 if (testing_for_rtl_locale()) { | 93 if (testing_for_rtl_locale()) { |
89 original_locale_ = base::i18n::GetConfiguredLocale(); | 94 original_locale_ = base::i18n::GetConfiguredLocale(); |
90 base::i18n::SetICUDefaultLocale("he"); | 95 base::i18n::SetICUDefaultLocale("he"); |
91 } | 96 } |
92 views::ViewsTestBase::SetUp(); | 97 views::ViewsTestBase::SetUp(); |
93 } | 98 } |
94 | 99 |
95 void TearDown() override { | 100 void TearDown() override { |
96 views::ViewsTestBase::TearDown(); | 101 views::ViewsTestBase::TearDown(); |
97 if (testing_for_rtl_locale()) | 102 if (testing_for_rtl_locale()) |
98 base::i18n::SetICUDefaultLocale(original_locale_); | 103 base::i18n::SetICUDefaultLocale(original_locale_); |
99 } | 104 } |
100 | 105 |
| 106 static views::ImageButton* GetCloseButton(const Tab& tab) { |
| 107 return tab.close_button_; |
| 108 } |
| 109 |
| 110 static views::View* GetThrobberView(const Tab& tab) { |
| 111 // Reinterpret to keep the definition encapsulated (which works so long as |
| 112 // multiple inheritance isn't involved). |
| 113 return reinterpret_cast<views::View*>(tab.throbber_); |
| 114 } |
| 115 |
| 116 static gfx::Rect GetFaviconBounds(const Tab& tab) { |
| 117 return tab.favicon_bounds_; |
| 118 } |
| 119 |
| 120 static void LayoutTab(Tab* tab) { tab->Layout(); } |
| 121 |
101 static void CheckForExpectedLayoutAndVisibilityOfElements(const Tab& tab) { | 122 static void CheckForExpectedLayoutAndVisibilityOfElements(const Tab& tab) { |
102 // Check whether elements are visible when they are supposed to be, given | 123 // Check whether elements are visible when they are supposed to be, given |
103 // Tab size and TabRendererData state. | 124 // Tab size and TabRendererData state. |
104 if (tab.data_.pinned) { | 125 if (tab.data_.pinned) { |
105 EXPECT_EQ(1, tab.IconCapacity()); | 126 EXPECT_EQ(1, tab.IconCapacity()); |
106 if (tab.data_.media_state != TAB_MEDIA_STATE_NONE) { | 127 if (tab.data_.media_state != TAB_MEDIA_STATE_NONE) { |
107 EXPECT_FALSE(tab.ShouldShowIcon()); | 128 EXPECT_FALSE(tab.ShouldShowIcon()); |
108 EXPECT_TRUE(tab.ShouldShowMediaIndicator()); | 129 EXPECT_TRUE(tab.ShouldShowMediaIndicator()); |
109 } else { | 130 } else { |
110 EXPECT_TRUE(tab.ShouldShowIcon()); | 131 EXPECT_TRUE(tab.ShouldShowIcon()); |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 // shouldn't change the insets of the close button. | 412 // shouldn't change the insets of the close button. |
392 TEST_P(TabTest, CloseButtonLayout) { | 413 TEST_P(TabTest, CloseButtonLayout) { |
393 if (testing_for_rtl_locale() && !base::i18n::IsRTL()) { | 414 if (testing_for_rtl_locale() && !base::i18n::IsRTL()) { |
394 LOG(WARNING) << "Testing of RTL locale not supported on current platform."; | 415 LOG(WARNING) << "Testing of RTL locale not supported on current platform."; |
395 return; | 416 return; |
396 } | 417 } |
397 | 418 |
398 FakeTabController tab_controller; | 419 FakeTabController tab_controller; |
399 Tab tab(&tab_controller); | 420 Tab tab(&tab_controller); |
400 tab.SetBounds(0, 0, 100, 50); | 421 tab.SetBounds(0, 0, 100, 50); |
401 tab.Layout(); | 422 LayoutTab(&tab); |
402 gfx::Insets close_button_insets = tab.close_button_->GetInsets(); | 423 gfx::Insets close_button_insets = GetCloseButton(tab)->GetInsets(); |
403 tab.Layout(); | 424 LayoutTab(&tab); |
404 gfx::Insets close_button_insets_2 = tab.close_button_->GetInsets(); | 425 gfx::Insets close_button_insets_2 = GetCloseButton(tab)->GetInsets(); |
405 EXPECT_EQ(close_button_insets.top(), close_button_insets_2.top()); | 426 EXPECT_EQ(close_button_insets.top(), close_button_insets_2.top()); |
406 EXPECT_EQ(close_button_insets.left(), close_button_insets_2.left()); | 427 EXPECT_EQ(close_button_insets.left(), close_button_insets_2.left()); |
407 EXPECT_EQ(close_button_insets.bottom(), close_button_insets_2.bottom()); | 428 EXPECT_EQ(close_button_insets.bottom(), close_button_insets_2.bottom()); |
408 EXPECT_EQ(close_button_insets.right(), close_button_insets_2.right()); | 429 EXPECT_EQ(close_button_insets.right(), close_button_insets_2.right()); |
409 | 430 |
410 // Also make sure the close button is sized as large as the tab. | 431 // Also make sure the close button is sized as large as the tab. |
411 EXPECT_EQ(50, tab.close_button_->bounds().height()); | 432 EXPECT_EQ(50, GetCloseButton(tab)->bounds().height()); |
| 433 } |
| 434 |
| 435 // Tests expected changes to the ThrobberView state when the WebContents loading |
| 436 // state changes or the animation timer (usually in BrowserView) triggers. |
| 437 TEST_P(TabTest, LayeredThrobber) { |
| 438 if (testing_for_rtl_locale() && !base::i18n::IsRTL()) { |
| 439 LOG(WARNING) << "Testing of RTL locale not supported on current platform."; |
| 440 return; |
| 441 } |
| 442 |
| 443 Widget widget; |
| 444 InitWidget(&widget); |
| 445 |
| 446 FakeTabController tab_controller; |
| 447 Tab tab(&tab_controller); |
| 448 widget.GetContentsView()->AddChildView(&tab); |
| 449 tab.SetBoundsRect(gfx::Rect(Tab::GetStandardSize())); |
| 450 |
| 451 views::View* throbber = GetThrobberView(tab); |
| 452 EXPECT_FALSE(throbber->visible()); |
| 453 EXPECT_EQ(TabRendererData::NETWORK_STATE_NONE, tab.data().network_state); |
| 454 EXPECT_EQ(throbber->bounds(), GetFaviconBounds(tab)); |
| 455 |
| 456 tab.UpdateLoadingAnimation(TabRendererData::NETWORK_STATE_NONE); |
| 457 EXPECT_FALSE(throbber->visible()); |
| 458 |
| 459 // Simulate a "normal" tab load: should paint to a layer. |
| 460 tab.UpdateLoadingAnimation(TabRendererData::NETWORK_STATE_WAITING); |
| 461 EXPECT_TRUE(tab_controller.CanPaintThrobberToLayer()); |
| 462 EXPECT_TRUE(throbber->visible()); |
| 463 EXPECT_TRUE(throbber->layer()); |
| 464 tab.UpdateLoadingAnimation(TabRendererData::NETWORK_STATE_LOADING); |
| 465 EXPECT_TRUE(throbber->visible()); |
| 466 EXPECT_TRUE(throbber->layer()); |
| 467 tab.UpdateLoadingAnimation(TabRendererData::NETWORK_STATE_NONE); |
| 468 EXPECT_FALSE(throbber->visible()); |
| 469 |
| 470 // Simulate a drag started and stopped during a load: layer painting stops |
| 471 // temporarily. |
| 472 tab.UpdateLoadingAnimation(TabRendererData::NETWORK_STATE_WAITING); |
| 473 EXPECT_TRUE(throbber->visible()); |
| 474 EXPECT_TRUE(throbber->layer()); |
| 475 tab_controller.set_paint_throbber_to_layer(false); |
| 476 tab.UpdateLoadingAnimation(TabRendererData::NETWORK_STATE_WAITING); |
| 477 EXPECT_TRUE(throbber->visible()); |
| 478 EXPECT_FALSE(throbber->layer()); |
| 479 tab_controller.set_paint_throbber_to_layer(true); |
| 480 tab.UpdateLoadingAnimation(TabRendererData::NETWORK_STATE_WAITING); |
| 481 EXPECT_TRUE(throbber->visible()); |
| 482 EXPECT_TRUE(throbber->layer()); |
| 483 tab.UpdateLoadingAnimation(TabRendererData::NETWORK_STATE_NONE); |
| 484 EXPECT_FALSE(throbber->visible()); |
| 485 |
| 486 // Simulate a tab load starting and stopping during tab dragging (or with |
| 487 // stacked tabs): no layer painting. |
| 488 tab_controller.set_paint_throbber_to_layer(false); |
| 489 tab.UpdateLoadingAnimation(TabRendererData::NETWORK_STATE_WAITING); |
| 490 EXPECT_TRUE(throbber->visible()); |
| 491 EXPECT_FALSE(throbber->layer()); |
| 492 tab.UpdateLoadingAnimation(TabRendererData::NETWORK_STATE_NONE); |
| 493 EXPECT_FALSE(throbber->visible()); |
412 } | 494 } |
413 | 495 |
414 // Test in both a LTR and a RTL locale. Note: The fact that the UI code is | 496 // Test in both a LTR and a RTL locale. Note: The fact that the UI code is |
415 // configured for an RTL locale does *not* change how the coordinates are | 497 // configured for an RTL locale does *not* change how the coordinates are |
416 // examined in the tests above because views::View and friends are supposed to | 498 // examined in the tests above because views::View and friends are supposed to |
417 // auto-mirror the widgets when painting. Thus, what we're testing here is that | 499 // auto-mirror the widgets when painting. Thus, what we're testing here is that |
418 // there's no code in Tab that will erroneously subvert this automatic | 500 // there's no code in Tab that will erroneously subvert this automatic |
419 // coordinate translation. http://crbug.com/384179 | 501 // coordinate translation. http://crbug.com/384179 |
420 INSTANTIATE_TEST_CASE_P(, TabTest, ::testing::Values(false, true)); | 502 INSTANTIATE_TEST_CASE_P(, TabTest, ::testing::Values(false, true)); |
OLD | NEW |