1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/*
* Copyright © Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 or 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mir/input/touch_visualizer.h"
#include "mir/input/device_capability.h"
#include "mir/input/input_device_info.h"
#include "mir/test/barrier.h"
#include "mir/test/fake_shared.h"
#include "mir_test_framework/deferred_in_process_server.h"
#include "mir_test_framework/input_testing_server_configuration.h"
#include "mir_test_framework/executable_path.h"
#include "mir_test_framework/fake_input_device.h"
#include "mir_test_framework/stub_server_platform_factory.h"
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <vector>
#include <functional>
namespace mi = mir::input;
namespace mis = mi::synthesis;
namespace mt = mir::test;
namespace geom = mir::geometry;
namespace mtf = mir_test_framework;
namespace
{
ACTION_P(UnblockBarrier, barrier)
{
barrier->ready();
}
MATCHER(NoSpots, "")
{
if (arg.size() > 0)
return false;
return true;
}
static bool
spots_have_position_and_pressure(std::vector<mi::TouchVisualizer::Spot> const& spots,
std::vector<geom::Point> const& positions, float pressure)
{
if (spots.size() != positions.size())
return false;
auto spots_it = spots.begin();
auto positions_it = positions.begin();
while (spots_it != spots.end())
{
auto const& spot = *spots_it;
if (spot.touch_location != *positions_it)
return false;
if (spot.pressure != pressure)
return false;
spots_it++;
positions_it++;
}
return true;
}
MATCHER_P(TouchedSpotsAt, positions, "")
{
return spots_have_position_and_pressure(arg, positions, 1.0);
}
struct MockTouchVisualizer : public mi::TouchVisualizer
{
MOCK_METHOD1(visualize_touches, void(std::vector<mi::TouchVisualizer::Spot> const&));
MOCK_METHOD0(enable, void());
MOCK_METHOD0(disable, void());
};
struct ServerConfiguration : mtf::InputTestingServerConfiguration
{
mt::Barrier& test_complete;
MockTouchVisualizer mock_visualizer;
static geom::Rectangle const display_bounds;
ServerConfiguration(mt::Barrier& test_complete)
: InputTestingServerConfiguration({display_bounds}),
test_complete(test_complete)
{
}
void inject_input() override{}
std::shared_ptr<mi::TouchVisualizer> the_touch_visualizer() override
{
return mt::fake_shared(mock_visualizer);
}
};
geom::Rectangle const ServerConfiguration::display_bounds = {{0, 0}, {1600, 1600}};
struct TestTouchspotVisualizations : mtf::DeferredInProcessServer
{
mt::Barrier test_complete_fence{2};
int env_setup = setenv("MIR_SERVER_PLATFORM_INPUT_LIB", "mir:stub-input", 1);
ServerConfiguration server_configuration{test_complete_fence};
std::unique_ptr<mtf::FakeInputDevice> fake_touch_screen{
mtf::add_fake_input_device(mi::InputDeviceInfo{"ts", "ts-uid" , mi::DeviceCapability::touchscreen|mi::DeviceCapability::multitouch})
};
mir::DefaultServerConfiguration& server_config() override { return server_configuration; }
void start_server()
{
DeferredInProcessServer::start_server();
server_configuration.exec();
}
void TearDown() override
{
test_complete_fence.ready();
server_configuration.on_exit();
DeferredInProcessServer::TearDown();
}
};
}
using namespace ::testing;
TEST_F(TestTouchspotVisualizations, touch_is_given_to_touchspot_visualizer)
{
geom::Point const abs_touch{0, 0};
std::vector<geom::Point> const expected_spots{abs_touch};
InSequence seq;
// First we will see the spots cleared, as this is the start of a new gesture.
EXPECT_CALL(server_configuration.mock_visualizer, visualize_touches(NoSpots())).Times(AtMost(1));
EXPECT_CALL(server_configuration.mock_visualizer, visualize_touches(TouchedSpotsAt(expected_spots)));
EXPECT_CALL(server_configuration.mock_visualizer, visualize_touches(NoSpots())).Times(1).
WillOnce(UnblockBarrier(&server_configuration.test_complete));
start_server();
fake_touch_screen->emit_event(mis::a_touch_event().at_position(abs_touch));
fake_touch_screen->emit_event(mis::a_touch_event().at_position(abs_touch)
.with_action(mis::TouchParameters::Action::Release));
}
TEST_F(TestTouchspotVisualizations, touchspots_follow_gesture)
{
auto const display_size{ServerConfiguration::display_bounds.size};
geom::Point const abs_touch{0, 0};
geom::Point const abs_touch_2{display_size.width.as_uint32_t() - 1,
display_size.height.as_uint32_t() - 1};
std::vector<geom::Point> expected_spots_1{abs_touch};
std::vector<geom::Point> expected_spots_2{abs_touch_2};
InSequence seq;
EXPECT_CALL(server_configuration.mock_visualizer, visualize_touches(NoSpots())).Times(AtMost(1));
EXPECT_CALL(server_configuration.mock_visualizer, visualize_touches(TouchedSpotsAt(expected_spots_1))).Times(1);
EXPECT_CALL(server_configuration.mock_visualizer, visualize_touches(TouchedSpotsAt(expected_spots_2))).Times(1);
EXPECT_CALL(server_configuration.mock_visualizer, visualize_touches(NoSpots())).
Times(1).WillOnce(UnblockBarrier(&server_configuration.test_complete));
start_server();
fake_touch_screen->emit_event(mis::a_touch_event().at_position(abs_touch));
fake_touch_screen->emit_event(mis::a_touch_event().at_position(abs_touch_2)
.with_action(mis::TouchParameters::Action::Move));
fake_touch_screen->emit_event(mis::a_touch_event().at_position(abs_touch_2)
.with_action(mis::TouchParameters::Action::Release));
}
|