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.
5 #include "ash/display/projecting_observer_chromeos.h"
7 #include "base/memory/scoped_vector.h"
8 #include "chromeos/dbus/fake_power_manager_client.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/display/chromeos/test/test_display_snapshot.h"
15 ui::TestDisplaySnapshot
* CreateInternalSnapshot() {
16 ui::TestDisplaySnapshot
* output
= new ui::TestDisplaySnapshot();
17 output
->set_type(ui::DISPLAY_CONNECTION_TYPE_INTERNAL
);
21 ui::TestDisplaySnapshot
* CreateVGASnapshot() {
22 ui::TestDisplaySnapshot
* output
= new ui::TestDisplaySnapshot();
23 output
->set_type(ui::DISPLAY_CONNECTION_TYPE_VGA
);
27 ui::DisplayConfigurator::DisplayStateList
CreateOutputs(
28 const ScopedVector
<ui::TestDisplaySnapshot
>& displays
) {
29 ui::DisplayConfigurator::DisplayStateList outputs
;
30 for (size_t i
= 0; i
< displays
.size(); ++i
) {
31 ui::DisplayConfigurator::DisplayState state
;
32 state
.display
= displays
[i
];
33 outputs
.push_back(state
);
39 class ProjectingObserverTest
: public testing::Test
{
41 ProjectingObserverTest() : observer_(&fake_power_client_
) {}
43 ~ProjectingObserverTest() override
{}
46 chromeos::FakePowerManagerClient fake_power_client_
;
47 ProjectingObserver observer_
;
50 DISALLOW_COPY_AND_ASSIGN(ProjectingObserverTest
);
55 TEST_F(ProjectingObserverTest
, CheckNoDisplay
) {
56 ScopedVector
<ui::TestDisplaySnapshot
> displays
;
57 ui::DisplayConfigurator::DisplayStateList outputs
= CreateOutputs(displays
);
58 observer_
.OnDisplayModeChanged(outputs
);
60 EXPECT_EQ(1, fake_power_client_
.num_set_is_projecting_calls());
61 EXPECT_FALSE(fake_power_client_
.is_projecting());
64 TEST_F(ProjectingObserverTest
, CheckWithoutInternalDisplay
) {
65 ScopedVector
<ui::TestDisplaySnapshot
> displays
;
66 displays
.push_back(CreateVGASnapshot());
67 ui::DisplayConfigurator::DisplayStateList outputs
= CreateOutputs(displays
);
68 observer_
.OnDisplayModeChanged(outputs
);
70 EXPECT_EQ(1, fake_power_client_
.num_set_is_projecting_calls());
71 EXPECT_FALSE(fake_power_client_
.is_projecting());
74 TEST_F(ProjectingObserverTest
, CheckWithInternalDisplay
) {
75 ScopedVector
<ui::TestDisplaySnapshot
> displays
;
76 displays
.push_back(CreateInternalSnapshot());
77 ui::DisplayConfigurator::DisplayStateList outputs
= CreateOutputs(displays
);
78 observer_
.OnDisplayModeChanged(outputs
);
80 EXPECT_EQ(1, fake_power_client_
.num_set_is_projecting_calls());
81 EXPECT_FALSE(fake_power_client_
.is_projecting());
84 TEST_F(ProjectingObserverTest
, CheckWithTwoVGADisplays
) {
85 ScopedVector
<ui::TestDisplaySnapshot
> displays
;
86 displays
.push_back(CreateVGASnapshot());
87 displays
.push_back(CreateVGASnapshot());
88 ui::DisplayConfigurator::DisplayStateList outputs
= CreateOutputs(displays
);
89 observer_
.OnDisplayModeChanged(outputs
);
91 EXPECT_EQ(1, fake_power_client_
.num_set_is_projecting_calls());
92 // We need at least 1 internal display to set projecting to on.
93 EXPECT_FALSE(fake_power_client_
.is_projecting());
96 TEST_F(ProjectingObserverTest
, CheckWithInternalAndVGADisplays
) {
97 ScopedVector
<ui::TestDisplaySnapshot
> displays
;
98 displays
.push_back(CreateInternalSnapshot());
99 displays
.push_back(CreateVGASnapshot());
100 ui::DisplayConfigurator::DisplayStateList outputs
= CreateOutputs(displays
);
101 observer_
.OnDisplayModeChanged(outputs
);
103 EXPECT_EQ(1, fake_power_client_
.num_set_is_projecting_calls());
104 EXPECT_TRUE(fake_power_client_
.is_projecting());
107 TEST_F(ProjectingObserverTest
, CheckWithVGADisplayAndOneCastingSession
) {
108 ScopedVector
<ui::TestDisplaySnapshot
> displays
;
109 displays
.push_back(CreateVGASnapshot());
110 ui::DisplayConfigurator::DisplayStateList outputs
= CreateOutputs(displays
);
111 observer_
.OnDisplayModeChanged(outputs
);
113 observer_
.OnCastingSessionStartedOrStopped(true);
115 EXPECT_EQ(2, fake_power_client_
.num_set_is_projecting_calls());
116 // Need at least one internal display to set projecting state to |true|.
117 EXPECT_FALSE(fake_power_client_
.is_projecting());
120 TEST_F(ProjectingObserverTest
, CheckWithInternalDisplayAndOneCastingSession
) {
121 ScopedVector
<ui::TestDisplaySnapshot
> displays
;
122 displays
.push_back(CreateInternalSnapshot());
123 ui::DisplayConfigurator::DisplayStateList outputs
= CreateOutputs(displays
);
124 observer_
.OnDisplayModeChanged(outputs
);
126 observer_
.OnCastingSessionStartedOrStopped(true);
128 EXPECT_EQ(2, fake_power_client_
.num_set_is_projecting_calls());
129 EXPECT_TRUE(fake_power_client_
.is_projecting());
132 TEST_F(ProjectingObserverTest
, CheckProjectingAfterClosingACastingSession
) {
133 ScopedVector
<ui::TestDisplaySnapshot
> displays
;
134 displays
.push_back(CreateInternalSnapshot());
135 ui::DisplayConfigurator::DisplayStateList outputs
= CreateOutputs(displays
);
136 observer_
.OnDisplayModeChanged(outputs
);
138 observer_
.OnCastingSessionStartedOrStopped(true);
139 observer_
.OnCastingSessionStartedOrStopped(true);
141 EXPECT_EQ(3, fake_power_client_
.num_set_is_projecting_calls());
142 EXPECT_TRUE(fake_power_client_
.is_projecting());
144 observer_
.OnCastingSessionStartedOrStopped(false);
146 EXPECT_EQ(4, fake_power_client_
.num_set_is_projecting_calls());
147 EXPECT_TRUE(fake_power_client_
.is_projecting());
150 TEST_F(ProjectingObserverTest
,
151 CheckStopProjectingAfterClosingAllCastingSessions
) {
152 ScopedVector
<ui::TestDisplaySnapshot
> displays
;
153 displays
.push_back(CreateInternalSnapshot());
154 ui::DisplayConfigurator::DisplayStateList outputs
= CreateOutputs(displays
);
155 observer_
.OnDisplayModeChanged(outputs
);
157 observer_
.OnCastingSessionStartedOrStopped(true);
158 observer_
.OnCastingSessionStartedOrStopped(false);
160 EXPECT_EQ(3, fake_power_client_
.num_set_is_projecting_calls());
161 EXPECT_FALSE(fake_power_client_
.is_projecting());
164 TEST_F(ProjectingObserverTest
,
165 CheckStopProjectingAfterDisconnectingSecondOutput
) {
166 ScopedVector
<ui::TestDisplaySnapshot
> displays
;
167 displays
.push_back(CreateInternalSnapshot());
168 displays
.push_back(CreateVGASnapshot());
169 ui::DisplayConfigurator::DisplayStateList outputs
= CreateOutputs(displays
);
170 observer_
.OnDisplayModeChanged(outputs
);
172 // Remove VGA output.
173 outputs
.erase(outputs
.begin() + 1);
174 observer_
.OnDisplayModeChanged(outputs
);
176 EXPECT_EQ(2, fake_power_client_
.num_set_is_projecting_calls());
177 EXPECT_FALSE(fake_power_client_
.is_projecting());