phone_hub_recent_apps_view_unittest.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2021 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/phonehub/phone_hub_recent_apps_view.h"
  5. #include "ash/components/phonehub/fake_recent_apps_interaction_handler.h"
  6. #include "ash/components/phonehub/notification.h"
  7. #include "ash/services/multidevice_setup/public/mojom/multidevice_setup.mojom.h"
  8. #include "ash/system/phonehub/phone_hub_recent_app_button.h"
  9. #include "ash/test/ash_test_base.h"
  10. #include "ui/events/test/test_event.h"
  11. #include "ui/gfx/image/image.h"
  12. #include "ui/views/test/button_test_api.h"
  13. namespace ash {
  14. const char16_t kAppName[] = u"Test App";
  15. const char kPackageName[] = "com.google.testapp";
  16. const int64_t kUserId = 0;
  17. namespace {
  18. using FeatureState = multidevice_setup::mojom::FeatureState;
  19. } // namespace
  20. class RecentAppButtonsViewTest : public AshTestBase {
  21. public:
  22. RecentAppButtonsViewTest() = default;
  23. ~RecentAppButtonsViewTest() override = default;
  24. // AshTestBase:
  25. void SetUp() override {
  26. AshTestBase::SetUp();
  27. phone_hub_recent_apps_view_ = std::make_unique<PhoneHubRecentAppsView>(
  28. &fake_recent_apps_interaction_handler_);
  29. }
  30. void TearDown() override {
  31. phone_hub_recent_apps_view_.reset();
  32. AshTestBase::TearDown();
  33. }
  34. protected:
  35. PhoneHubRecentAppsView* recent_apps_view() {
  36. return phone_hub_recent_apps_view_.get();
  37. }
  38. void NotifyRecentAppAddedOrUpdated() {
  39. fake_recent_apps_interaction_handler_.NotifyRecentAppAddedOrUpdated(
  40. phonehub::Notification::AppMetadata(
  41. kAppName, kPackageName,
  42. /*icon=*/gfx::Image(), /*icon_color =*/absl::nullopt,
  43. /*icon_is_monochrome =*/true, kUserId),
  44. base::Time::Now());
  45. }
  46. size_t PackageNameToClickCount(const std::string& package_name) {
  47. return fake_recent_apps_interaction_handler_.HandledRecentAppsCount(
  48. package_name);
  49. }
  50. void FeatureStateChanged(FeatureState feature_state) {
  51. fake_recent_apps_interaction_handler_.OnFeatureStateChanged(feature_state);
  52. }
  53. private:
  54. std::unique_ptr<PhoneHubRecentAppsView> phone_hub_recent_apps_view_;
  55. phonehub::FakeRecentAppsInteractionHandler
  56. fake_recent_apps_interaction_handler_;
  57. };
  58. TEST_F(RecentAppButtonsViewTest, TaskViewVisibility) {
  59. // The recent app view is not visible if the NotifyRecentAppAddedOrUpdated
  60. // function never be called, e.g. device boot.
  61. EXPECT_FALSE(recent_apps_view()->GetVisible());
  62. // The feature state is enabled but no recent app has been added yet, we
  63. // should not show the recent app buttons view.
  64. FeatureStateChanged(FeatureState::kEnabledByUser);
  65. recent_apps_view()->Update();
  66. EXPECT_TRUE(recent_apps_view()->GetVisible());
  67. EXPECT_FALSE(recent_apps_view()->recent_app_buttons_view_->GetVisible());
  68. // The feature state is disabled so we should not show all recent apps view.
  69. FeatureStateChanged(FeatureState::kDisabledByUser);
  70. recent_apps_view()->Update();
  71. EXPECT_FALSE(recent_apps_view()->GetVisible());
  72. }
  73. TEST_F(RecentAppButtonsViewTest, SingleRecentAppButtonsView) {
  74. NotifyRecentAppAddedOrUpdated();
  75. FeatureStateChanged(FeatureState::kEnabledByUser);
  76. recent_apps_view()->Update();
  77. size_t expected_recent_app_button = 1;
  78. EXPECT_TRUE(recent_apps_view()->GetVisible());
  79. EXPECT_EQ(expected_recent_app_button,
  80. recent_apps_view()->recent_app_buttons_view_->children().size());
  81. }
  82. TEST_F(RecentAppButtonsViewTest, MultipleRecentAppButtonsView) {
  83. NotifyRecentAppAddedOrUpdated();
  84. NotifyRecentAppAddedOrUpdated();
  85. NotifyRecentAppAddedOrUpdated();
  86. FeatureStateChanged(FeatureState::kEnabledByUser);
  87. recent_apps_view()->Update();
  88. size_t expected_recent_app_button = 3;
  89. EXPECT_EQ(expected_recent_app_button,
  90. recent_apps_view()->recent_app_buttons_view_->children().size());
  91. for (auto* child : recent_apps_view()->recent_app_buttons_view_->children()) {
  92. PhoneHubRecentAppButton* recent_app =
  93. static_cast<PhoneHubRecentAppButton*>(child);
  94. // Simulate clicking button using placeholder event.
  95. views::test::ButtonTestApi(recent_app).NotifyClick(ui::test::TestEvent());
  96. }
  97. size_t expected_number_of_button_be_clicked = 3;
  98. EXPECT_EQ(expected_number_of_button_be_clicked,
  99. PackageNameToClickCount(kPackageName));
  100. }
  101. } // namespace ash