nearby_share_feature_pod_controller_unittest.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2020 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/nearby_share/nearby_share_feature_pod_controller.h"
  5. #include "ash/public/cpp/test/test_nearby_share_delegate.h"
  6. #include "ash/shell.h"
  7. #include "ash/system/unified/feature_pod_button.h"
  8. #include "ash/system/unified/unified_system_tray_controller.h"
  9. #include "ash/system/unified/unified_system_tray_model.h"
  10. #include "ash/test/ash_test_base.h"
  11. #include "base/memory/scoped_refptr.h"
  12. namespace ash {
  13. // Tests manually control their session state.
  14. class NearbyShareFeaturePodControllerTest : public NoSessionAshTestBase {
  15. public:
  16. NearbyShareFeaturePodControllerTest() = default;
  17. NearbyShareFeaturePodControllerTest(NearbyShareFeaturePodControllerTest&) =
  18. delete;
  19. NearbyShareFeaturePodControllerTest& operator=(
  20. NearbyShareFeaturePodControllerTest&) = delete;
  21. ~NearbyShareFeaturePodControllerTest() override = default;
  22. void SetUp() override {
  23. NoSessionAshTestBase::SetUp();
  24. tray_model_ = base::MakeRefCounted<UnifiedSystemTrayModel>(nullptr);
  25. tray_controller_ =
  26. std::make_unique<UnifiedSystemTrayController>(tray_model_.get());
  27. test_delegate_ = static_cast<TestNearbyShareDelegate*>(
  28. Shell::Get()->nearby_share_delegate());
  29. nearby_share_controller_ = Shell::Get()->nearby_share_controller();
  30. test_delegate_->set_is_pod_button_visible(true);
  31. }
  32. void TearDown() override {
  33. button_.reset();
  34. pod_controller_.reset();
  35. tray_controller_.reset();
  36. tray_model_.reset();
  37. NoSessionAshTestBase::TearDown();
  38. }
  39. protected:
  40. void SetUpButton() {
  41. pod_controller_ = std::make_unique<NearbyShareFeaturePodController>(
  42. tray_controller_.get());
  43. button_.reset(pod_controller_->CreateButton());
  44. }
  45. scoped_refptr<UnifiedSystemTrayModel> tray_model_;
  46. std::unique_ptr<UnifiedSystemTrayController> tray_controller_;
  47. std::unique_ptr<NearbyShareFeaturePodController> pod_controller_;
  48. std::unique_ptr<FeaturePodButton> button_;
  49. TestNearbyShareDelegate* test_delegate_ = nullptr;
  50. NearbyShareController* nearby_share_controller_ = nullptr;
  51. };
  52. TEST_F(NearbyShareFeaturePodControllerTest, ButtonVisibilityNotLoggedIn) {
  53. SetUpButton();
  54. // If not logged in, it should not be visible.
  55. EXPECT_FALSE(button_->GetVisible());
  56. }
  57. TEST_F(NearbyShareFeaturePodControllerTest, ButtonVisibilityLoggedIn) {
  58. CreateUserSessions(1);
  59. SetUpButton();
  60. // If logged in, it should be visible.
  61. EXPECT_TRUE(button_->GetVisible());
  62. }
  63. TEST_F(NearbyShareFeaturePodControllerTest, ButtonVisibilityLocked) {
  64. CreateUserSessions(1);
  65. BlockUserSession(UserSessionBlockReason::BLOCKED_BY_LOCK_SCREEN);
  66. SetUpButton();
  67. // If locked, it should not be visible.
  68. EXPECT_FALSE(button_->GetVisible());
  69. }
  70. TEST_F(NearbyShareFeaturePodControllerTest, ButtonVisibilityLoginScreen) {
  71. CreateUserSessions(1);
  72. BlockUserSession(UserSessionBlockReason::BLOCKED_BY_LOGIN_SCREEN);
  73. SetUpButton();
  74. // If the login screen is showing (e.g. multi-user signin), it should not be
  75. // visible, regardless of whether an active user is signed in.
  76. EXPECT_FALSE(button_->GetVisible());
  77. }
  78. TEST_F(NearbyShareFeaturePodControllerTest, ButtonVisiblilityHiddenByDelegate) {
  79. CreateUserSessions(1);
  80. test_delegate_->set_is_pod_button_visible(false);
  81. SetUpButton();
  82. // If NearbyShareDelegate::IsPodButtonVisible() returns false, it should
  83. // not be visible.
  84. EXPECT_FALSE(button_->GetVisible());
  85. }
  86. TEST_F(NearbyShareFeaturePodControllerTest,
  87. ButtonToggledByHighVisibilityEnabledEvent) {
  88. CreateUserSessions(1);
  89. SetUpButton();
  90. ASSERT_FALSE(button_->IsToggled());
  91. nearby_share_controller_->HighVisibilityEnabledChanged(true);
  92. EXPECT_TRUE(button_->IsToggled());
  93. nearby_share_controller_->HighVisibilityEnabledChanged(false);
  94. EXPECT_FALSE(button_->IsToggled());
  95. }
  96. TEST_F(NearbyShareFeaturePodControllerTest, ButtonPressTogglesHighVisibility) {
  97. CreateUserSessions(1);
  98. SetUpButton();
  99. test_delegate_->method_calls().clear();
  100. test_delegate_->set_is_high_visibility_on(false);
  101. pod_controller_->OnIconPressed();
  102. EXPECT_EQ(1u, test_delegate_->method_calls().size());
  103. EXPECT_EQ(TestNearbyShareDelegate::Method::kEnableHighVisibility,
  104. test_delegate_->method_calls()[0]);
  105. test_delegate_->set_is_high_visibility_on(true);
  106. pod_controller_->OnIconPressed();
  107. EXPECT_EQ(2u, test_delegate_->method_calls().size());
  108. EXPECT_EQ(TestNearbyShareDelegate::Method::kDisableHighVisibility,
  109. test_delegate_->method_calls()[1]);
  110. }
  111. } // namespace ash