projecting_observer_unittest.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2014 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/display/projecting_observer.h"
  5. #include <memory>
  6. #include <vector>
  7. #include "chromeos/dbus/power/fake_power_manager_client.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. #include "ui/display/fake/fake_display_snapshot.h"
  10. namespace ash {
  11. namespace {
  12. std::unique_ptr<display::DisplaySnapshot> CreateInternalSnapshot() {
  13. return display::FakeDisplaySnapshot::Builder()
  14. .SetId(123)
  15. .SetNativeMode(gfx::Size(1024, 768))
  16. .SetType(display::DISPLAY_CONNECTION_TYPE_INTERNAL)
  17. .Build();
  18. }
  19. std::unique_ptr<display::DisplaySnapshot> CreateVGASnapshot() {
  20. return display::FakeDisplaySnapshot::Builder()
  21. .SetId(456)
  22. .SetNativeMode(gfx::Size(1024, 768))
  23. .SetType(display::DISPLAY_CONNECTION_TYPE_VGA)
  24. .Build();
  25. }
  26. display::DisplayConfigurator::DisplayStateList GetPointers(
  27. const std::vector<std::unique_ptr<display::DisplaySnapshot>>& displays) {
  28. display::DisplayConfigurator::DisplayStateList result;
  29. for (const auto& display : displays)
  30. result.push_back(display.get());
  31. return result;
  32. }
  33. } // namespace
  34. class ProjectingObserverTest : public testing::Test {
  35. public:
  36. ProjectingObserverTest() = default;
  37. void SetUp() override {
  38. chromeos::PowerManagerClient::InitializeFake();
  39. observer_ = std::make_unique<ProjectingObserver>(nullptr);
  40. }
  41. void TearDown() override {
  42. observer_.reset();
  43. chromeos::PowerManagerClient::Shutdown();
  44. }
  45. ProjectingObserverTest(const ProjectingObserverTest&) = delete;
  46. ProjectingObserverTest& operator=(const ProjectingObserverTest&) = delete;
  47. ~ProjectingObserverTest() override = default;
  48. protected:
  49. chromeos::FakePowerManagerClient* power_client() {
  50. return chromeos::FakePowerManagerClient::Get();
  51. }
  52. std::unique_ptr<ProjectingObserver> observer_;
  53. };
  54. TEST_F(ProjectingObserverTest, CheckNoDisplay) {
  55. std::vector<std::unique_ptr<display::DisplaySnapshot>> displays;
  56. observer_->OnDisplayModeChanged(GetPointers(displays));
  57. EXPECT_EQ(1, power_client()->num_set_is_projecting_calls());
  58. EXPECT_FALSE(power_client()->is_projecting());
  59. }
  60. TEST_F(ProjectingObserverTest, CheckWithoutInternalDisplay) {
  61. std::vector<std::unique_ptr<display::DisplaySnapshot>> displays;
  62. displays.push_back(CreateVGASnapshot());
  63. observer_->OnDisplayModeChanged(GetPointers(displays));
  64. EXPECT_EQ(1, power_client()->num_set_is_projecting_calls());
  65. EXPECT_FALSE(power_client()->is_projecting());
  66. }
  67. TEST_F(ProjectingObserverTest, CheckWithInternalDisplay) {
  68. std::vector<std::unique_ptr<display::DisplaySnapshot>> displays;
  69. displays.push_back(CreateInternalSnapshot());
  70. observer_->OnDisplayModeChanged(GetPointers(displays));
  71. EXPECT_EQ(1, power_client()->num_set_is_projecting_calls());
  72. EXPECT_FALSE(power_client()->is_projecting());
  73. }
  74. TEST_F(ProjectingObserverTest, CheckWithTwoVGADisplays) {
  75. std::vector<std::unique_ptr<display::DisplaySnapshot>> displays;
  76. displays.push_back(CreateVGASnapshot());
  77. displays.push_back(CreateVGASnapshot());
  78. observer_->OnDisplayModeChanged(GetPointers(displays));
  79. EXPECT_EQ(1, power_client()->num_set_is_projecting_calls());
  80. // We need at least 1 internal display to set projecting to on.
  81. EXPECT_FALSE(power_client()->is_projecting());
  82. }
  83. TEST_F(ProjectingObserverTest, CheckWithInternalAndVGADisplays) {
  84. std::vector<std::unique_ptr<display::DisplaySnapshot>> displays;
  85. displays.push_back(CreateInternalSnapshot());
  86. displays.push_back(CreateVGASnapshot());
  87. observer_->OnDisplayModeChanged(GetPointers(displays));
  88. EXPECT_EQ(1, power_client()->num_set_is_projecting_calls());
  89. EXPECT_TRUE(power_client()->is_projecting());
  90. }
  91. TEST_F(ProjectingObserverTest, CheckWithVGADisplayAndOneCastingSession) {
  92. std::vector<std::unique_ptr<display::DisplaySnapshot>> displays;
  93. displays.push_back(CreateVGASnapshot());
  94. observer_->OnDisplayModeChanged(GetPointers(displays));
  95. observer_->OnCastingSessionStartedOrStopped(true);
  96. EXPECT_EQ(2, power_client()->num_set_is_projecting_calls());
  97. // Need at least one internal display to set projecting state to |true|.
  98. EXPECT_FALSE(power_client()->is_projecting());
  99. }
  100. TEST_F(ProjectingObserverTest, CheckWithInternalDisplayAndOneCastingSession) {
  101. std::vector<std::unique_ptr<display::DisplaySnapshot>> displays;
  102. displays.push_back(CreateInternalSnapshot());
  103. observer_->OnDisplayModeChanged(GetPointers(displays));
  104. observer_->OnCastingSessionStartedOrStopped(true);
  105. EXPECT_EQ(2, power_client()->num_set_is_projecting_calls());
  106. EXPECT_TRUE(power_client()->is_projecting());
  107. }
  108. TEST_F(ProjectingObserverTest, CheckProjectingAfterClosingACastingSession) {
  109. std::vector<std::unique_ptr<display::DisplaySnapshot>> displays;
  110. displays.push_back(CreateInternalSnapshot());
  111. observer_->OnDisplayModeChanged(GetPointers(displays));
  112. observer_->OnCastingSessionStartedOrStopped(true);
  113. observer_->OnCastingSessionStartedOrStopped(true);
  114. EXPECT_EQ(3, power_client()->num_set_is_projecting_calls());
  115. EXPECT_TRUE(power_client()->is_projecting());
  116. observer_->OnCastingSessionStartedOrStopped(false);
  117. EXPECT_EQ(4, power_client()->num_set_is_projecting_calls());
  118. EXPECT_TRUE(power_client()->is_projecting());
  119. }
  120. TEST_F(ProjectingObserverTest,
  121. CheckStopProjectingAfterClosingAllCastingSessions) {
  122. std::vector<std::unique_ptr<display::DisplaySnapshot>> displays;
  123. displays.push_back(CreateInternalSnapshot());
  124. observer_->OnDisplayModeChanged(GetPointers(displays));
  125. observer_->OnCastingSessionStartedOrStopped(true);
  126. observer_->OnCastingSessionStartedOrStopped(false);
  127. EXPECT_EQ(3, power_client()->num_set_is_projecting_calls());
  128. EXPECT_FALSE(power_client()->is_projecting());
  129. }
  130. TEST_F(ProjectingObserverTest,
  131. CheckStopProjectingAfterDisconnectingSecondOutput) {
  132. std::vector<std::unique_ptr<display::DisplaySnapshot>> displays;
  133. displays.push_back(CreateInternalSnapshot());
  134. displays.push_back(CreateVGASnapshot());
  135. observer_->OnDisplayModeChanged(GetPointers(displays));
  136. // Remove VGA output.
  137. displays.erase(displays.begin() + 1);
  138. observer_->OnDisplayModeChanged(GetPointers(displays));
  139. EXPECT_EQ(2, power_client()->num_set_is_projecting_calls());
  140. EXPECT_FALSE(power_client()->is_projecting());
  141. }
  142. } // namespace ash