page_indicator_view_unittest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2019 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. #include "ash/system/unified/page_indicator_view.h"
  5. #include "ash/public/cpp/pagination/pagination_model.h"
  6. #include "ash/session/test_session_controller_client.h"
  7. #include "ash/system/unified/unified_system_tray_controller.h"
  8. #include "ash/system/unified/unified_system_tray_model.h"
  9. #include "ash/system/unified/unified_system_tray_view.h"
  10. #include "ash/test/ash_test_base.h"
  11. #include "base/memory/scoped_refptr.h"
  12. namespace ash {
  13. namespace {
  14. int kPageCount = 10;
  15. }
  16. class PageIndicatorViewTest : public NoSessionAshTestBase {
  17. public:
  18. PageIndicatorViewTest() = default;
  19. PageIndicatorViewTest(const PageIndicatorViewTest&) = delete;
  20. PageIndicatorViewTest& operator=(const PageIndicatorViewTest&) = delete;
  21. ~PageIndicatorViewTest() override = default;
  22. void SetUp() override {
  23. NoSessionAshTestBase::SetUp();
  24. model_ = base::MakeRefCounted<UnifiedSystemTrayModel>(nullptr);
  25. controller_ = std::make_unique<UnifiedSystemTrayController>(model_.get());
  26. unified_view_ = std::make_unique<UnifiedSystemTrayView>(
  27. controller_.get(), true /* initially_expanded */);
  28. }
  29. void TearDown() override {
  30. controller_.reset();
  31. unified_view_.reset();
  32. model_.reset();
  33. NoSessionAshTestBase::TearDown();
  34. }
  35. protected:
  36. int GetButtonCount() {
  37. return page_indicator_view()->buttons_container()->children().size();
  38. }
  39. bool IsPageSelected(int index) {
  40. return page_indicator_view()->IsPageSelectedForTesting(index);
  41. }
  42. void Layout() { unified_view_->Layout(); }
  43. PaginationModel* pagination_model() { return model_->pagination_model(); }
  44. PageIndicatorView* page_indicator_view() {
  45. return unified_view_->page_indicator_view_for_test();
  46. }
  47. UnifiedSystemTrayView* unified_view() { return unified_view_.get(); }
  48. private:
  49. scoped_refptr<UnifiedSystemTrayModel> model_;
  50. std::unique_ptr<UnifiedSystemTrayController> controller_;
  51. std::unique_ptr<UnifiedSystemTrayView> unified_view_;
  52. };
  53. // Number of buttons is equal to total pages in PaginationModel.
  54. TEST_F(PageIndicatorViewTest, ButtonForEachPage) {
  55. for (int i = 0; i < kPageCount; i++) {
  56. pagination_model()->SetTotalPages(i);
  57. EXPECT_EQ(i, GetButtonCount());
  58. }
  59. }
  60. // Single button corresponding to page in PaginationModel is set to selected.
  61. TEST_F(PageIndicatorViewTest, SelectPage) {
  62. pagination_model()->SetTotalPages(kPageCount);
  63. for (int i = 0; i < kPageCount; i++) {
  64. pagination_model()->SelectPage(i, false /* animate */);
  65. EXPECT_TRUE(IsPageSelected(i));
  66. for (int j = 0; j < kPageCount; j++) {
  67. if (i == j)
  68. continue;
  69. EXPECT_FALSE(IsPageSelected(j));
  70. }
  71. }
  72. }
  73. TEST_F(PageIndicatorViewTest, ExpandAndCollapse) {
  74. int cur_height;
  75. int prev_height;
  76. double expanded_increments[] = {0.90, 0.75, 0.5, 0.25, 0.10};
  77. pagination_model()->SetTotalPages(kPageCount);
  78. // PageIndicatorView has decreasing height as the expanded amount is
  79. // decreased.
  80. prev_height = page_indicator_view()->GetContentsBounds().height();
  81. for (double i : expanded_increments) {
  82. unified_view()->SetExpandedAmount(i);
  83. cur_height = page_indicator_view()->GetContentsBounds().height();
  84. EXPECT_GE(prev_height, cur_height);
  85. prev_height = cur_height;
  86. }
  87. // PageIndicatorView has zero height when collapsed.
  88. unified_view()->SetExpandedAmount(0.00);
  89. EXPECT_EQ(page_indicator_view()->GetContentsBounds().height(), 0);
  90. }
  91. } // namespace ash