2
* Copyright © 2012 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored By: Robert Carr <racarr@canonical.com>
19
#include "src/server/scene/application_session.h"
20
#include "src/server/shell/default_focus_mechanism.h"
21
#include "mir/scene/session.h"
22
#include "mir/scene/surface_creation_parameters.h"
23
#include "src/server/scene/basic_surface.h"
24
#include "mir/graphics/display_configuration.h"
26
#include "mir_test/fake_shared.h"
27
#include "mir_test_doubles/mock_buffer_stream.h"
28
#include "mir_test_doubles/mock_scene_session.h"
29
#include "mir_test_doubles/mock_surface.h"
30
#include "mir_test_doubles/mock_surface_coordinator.h"
31
#include "mir_test_doubles/stub_input_targeter.h"
32
#include "mir_test_doubles/mock_input_targeter.h"
34
#include <gmock/gmock.h>
35
#include <gtest/gtest.h>
38
namespace mc = mir::compositor;
39
namespace msh = mir::shell;
40
namespace ms = mir::scene;
41
namespace mf = mir::frontend;
42
namespace mt = mir::test;
43
namespace mtd = mir::test::doubles;
45
TEST(DefaultFocusMechanism, mechanism_notifies_default_surface_of_focus_changes)
47
using namespace ::testing;
49
NiceMock<mtd::MockSceneSession> app1, app2;
50
auto const mock_surface1 = std::make_shared<NiceMock<mtd::MockSurface>>();
51
auto const mock_surface2 = std::make_shared<NiceMock<mtd::MockSurface>>();
52
auto const surface_coordinator = std::make_shared<mtd::MockSurfaceCoordinator>();
54
ON_CALL(app1, default_surface()).WillByDefault(Return(mock_surface1));
55
ON_CALL(app2, default_surface()).WillByDefault(Return(mock_surface2));
57
msh::DefaultFocusMechanism focus_mechanism(
58
std::make_shared<mtd::StubInputTargeter>(),
62
EXPECT_CALL(*surface_coordinator, raise(_)).Times(1);
63
EXPECT_CALL(*mock_surface1, configure(mir_surface_attrib_focus, mir_surface_focused)).Times(1);
64
EXPECT_CALL(*surface_coordinator, raise(_)).Times(1);
65
EXPECT_CALL(*mock_surface1, configure(mir_surface_attrib_focus, mir_surface_unfocused)).Times(1);
66
EXPECT_CALL(*mock_surface2, configure(mir_surface_attrib_focus, mir_surface_focused)).Times(1);
68
focus_mechanism.set_focus_to(mt::fake_shared(app1));
69
focus_mechanism.set_focus_to(mt::fake_shared(app2));
72
TEST(DefaultFocusMechanism, sets_input_focus)
74
using namespace ::testing;
76
NiceMock<mtd::MockSceneSession> app1;
77
NiceMock<mtd::MockSurface> mock_surface;
78
auto const surface_coordinator = std::make_shared<mtd::MockSurfaceCoordinator>();
82
EXPECT_CALL(app1, default_surface()).Times(1)
83
.WillOnce(Return(mt::fake_shared(mock_surface)));
84
EXPECT_CALL(app1, default_surface()).Times(1)
85
.WillOnce(Return(std::shared_ptr<ms::Surface>()));
88
NiceMock<mtd::MockInputTargeter> targeter;
90
msh::DefaultFocusMechanism focus_mechanism(mt::fake_shared(targeter), surface_coordinator);
94
EXPECT_CALL(mock_surface, take_input_focus(_)).Times(1);
95
// When we have no default surface.
96
EXPECT_CALL(targeter, focus_cleared()).Times(1);
97
// When we have no session.
98
EXPECT_CALL(targeter, focus_cleared()).Times(1);
100
EXPECT_CALL(*surface_coordinator, raise(_)).Times(1);
102
focus_mechanism.set_focus_to(mt::fake_shared(app1));
103
focus_mechanism.set_focus_to(mt::fake_shared(app1));
104
focus_mechanism.set_focus_to(std::shared_ptr<ms::Session>());
107
TEST(DefaultFocusMechanism, notifies_surface_of_focus_change_after_it_has_taken_the_focus)
109
using namespace ::testing;
111
NiceMock<mtd::MockSceneSession> app;
112
auto const mock_surface = std::make_shared<NiceMock<mtd::MockSurface>>();
113
auto const surface_coordinator = std::make_shared<NiceMock<mtd::MockSurfaceCoordinator>>();
115
ON_CALL(app, default_surface()).WillByDefault(Return(mock_surface));
117
msh::DefaultFocusMechanism focus_mechanism(
118
std::make_shared<mtd::StubInputTargeter>(),
119
surface_coordinator);
122
EXPECT_CALL(*mock_surface, take_input_focus(_));
123
EXPECT_CALL(*mock_surface, configure(mir_surface_attrib_focus, mir_surface_focused)).Times(1);
125
focus_mechanism.set_focus_to(mt::fake_shared(app));