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