~ubuntu-branches/ubuntu/vivid/mir/vivid

« back to all changes in this revision

Viewing changes to tests/unit-tests/shell/test_default_focus_mechanism.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-10-10 14:01:26 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20141010140126-n1czko8na1kuz4ll
Tags: upstream-0.8.0+14.10.20141010
Import upstream version 0.8.0+14.10.20141010

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2012 Canonical Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authored By: Robert Carr <racarr@canonical.com>
 
17
 */
 
18
 
 
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"
 
25
 
 
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"
 
33
 
 
34
#include <gmock/gmock.h>
 
35
#include <gtest/gtest.h>
 
36
#include <string>
 
37
 
 
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;
 
44
 
 
45
TEST(DefaultFocusMechanism, mechanism_notifies_default_surface_of_focus_changes)
 
46
{
 
47
    using namespace ::testing;
 
48
 
 
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>();
 
53
 
 
54
    ON_CALL(app1, default_surface()).WillByDefault(Return(mock_surface1));
 
55
    ON_CALL(app2, default_surface()).WillByDefault(Return(mock_surface2));
 
56
 
 
57
    msh::DefaultFocusMechanism focus_mechanism(
 
58
        std::make_shared<mtd::StubInputTargeter>(),
 
59
        surface_coordinator);
 
60
 
 
61
    InSequence seq;
 
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);
 
67
 
 
68
    focus_mechanism.set_focus_to(mt::fake_shared(app1));
 
69
    focus_mechanism.set_focus_to(mt::fake_shared(app2));
 
70
}
 
71
 
 
72
TEST(DefaultFocusMechanism, sets_input_focus)
 
73
{
 
74
    using namespace ::testing;
 
75
 
 
76
    NiceMock<mtd::MockSceneSession> app1;
 
77
    NiceMock<mtd::MockSurface> mock_surface;
 
78
    auto const surface_coordinator = std::make_shared<mtd::MockSurfaceCoordinator>();
 
79
 
 
80
    {
 
81
        InSequence seq;
 
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>()));
 
86
    }
 
87
 
 
88
    NiceMock<mtd::MockInputTargeter> targeter;
 
89
 
 
90
    msh::DefaultFocusMechanism focus_mechanism(mt::fake_shared(targeter), surface_coordinator);
 
91
 
 
92
    {
 
93
        InSequence seq;
 
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);
 
99
    }
 
100
    EXPECT_CALL(*surface_coordinator, raise(_)).Times(1);
 
101
 
 
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>());
 
105
}
 
106
 
 
107
TEST(DefaultFocusMechanism, notifies_surface_of_focus_change_after_it_has_taken_the_focus)
 
108
{
 
109
    using namespace ::testing;
 
110
 
 
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>>();
 
114
 
 
115
    ON_CALL(app, default_surface()).WillByDefault(Return(mock_surface));
 
116
 
 
117
    msh::DefaultFocusMechanism focus_mechanism(
 
118
        std::make_shared<mtd::StubInputTargeter>(),
 
119
        surface_coordinator);
 
120
 
 
121
    InSequence seq;
 
122
    EXPECT_CALL(*mock_surface, take_input_focus(_));
 
123
    EXPECT_CALL(*mock_surface, configure(mir_surface_attrib_focus, mir_surface_focused)).Times(1);
 
124
 
 
125
    focus_mechanism.set_focus_to(mt::fake_shared(app));
 
126
}