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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/*
* 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 "src/server/input/touchspot_controller.h"
#include "src/server/input/touchspot_image.c"
#include "mir/graphics/graphic_buffer_allocator.h"
#include "mir/graphics/renderable.h"
#include "mir/test/fake_shared.h"
#include "mir/test/doubles/stub_buffer_allocator.h"
#include "mir/test/doubles/stub_buffer.h"
#include "mir/test/doubles/stub_scene.h"
#include "mir/test/doubles/mock_buffer.h"
#include "mir/test/doubles/stub_input_scene.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <vector>
#include <assert.h>
namespace mi = mir::input;
namespace mg = mir::graphics;
namespace mt = mir::test;
namespace mtd = mt::doubles;
namespace geom = mir::geometry;
namespace
{
struct MockBufferAllocator : public mtd::StubBufferAllocator
{
MOCK_METHOD2(alloc_software_buffer, std::shared_ptr<mg::Buffer>(geom::Size, MirPixelFormat));
MOCK_METHOD0(supported_pixel_formats, std::vector<MirPixelFormat>());
};
struct StubScene : public mtd::StubInputScene
{
void add_input_visualization(std::shared_ptr<mg::Renderable> const& overlay) override
{
overlays.push_back(overlay);
}
void remove_input_visualization(std::weak_ptr<mg::Renderable> const& overlay) override
{
auto l = overlay.lock();
assert(l);
auto it = std::find(overlays.begin(), overlays.end(), l);
assert(it != overlays.end());
overlays.erase(it);
}
void expect_spots_centered_at(std::vector<geom::Point> spots)
{
int const touchspot_side_in_pixels = 64;
for (auto overlay : overlays)
{
auto top_left_pos = overlay->screen_position().top_left;
auto center_pos = geom::Point{top_left_pos.x.as_int() + touchspot_side_in_pixels/2,
top_left_pos.y.as_int() + touchspot_side_in_pixels/2};
auto it = std::find(spots.begin(), spots.end(), center_pos);
EXPECT_FALSE(it == spots.end());
spots.erase(it);
}
// If there are left over spots then we didn't have an overlay corresponding to one
EXPECT_EQ(0, spots.size());
}
std::vector<std::shared_ptr<mg::Renderable>> overlays;
};
struct TestTouchspotController : public ::testing::Test
{
TestTouchspotController()
: allocator(std::make_shared<testing::NiceMock<MockBufferAllocator>>()),
scene(std::make_shared<StubScene>())
{
using namespace testing;
ON_CALL(*allocator, supported_pixel_formats())
.WillByDefault(Return(std::vector<MirPixelFormat>{ mir_pixel_format_argb_8888 }));
ON_CALL(*allocator, alloc_software_buffer(_, _))
.WillByDefault(
Invoke([this](auto size, auto pf)
{
return allocator->mtd::StubBufferAllocator::alloc_software_buffer(size, pf);
}));
}
std::shared_ptr<MockBufferAllocator> const allocator;
std::shared_ptr<StubScene> const scene;
};
}
TEST_F(TestTouchspotController, allocates_software_buffer_for_touchspots)
{
using namespace ::testing;
EXPECT_CALL(*allocator, alloc_software_buffer(_, _)).Times(1);
mi::TouchspotController controller(allocator, scene);
}
TEST_F(TestTouchspotController, handles_stride_mismatch_in_buffer)
{
using namespace ::testing;
ON_CALL(*allocator, alloc_software_buffer(_, _))
.WillByDefault(
Invoke(
[](auto size, auto pf)
{
mg::BufferProperties properties{size, pf, mg::BufferUsage::software};
return std::make_shared<mtd::StubBuffer>(
properties,
geom::Stride{size.width.as_uint32_t() * MIR_BYTES_PER_PIXEL(pf) + 29}); // Return a stride != width
}));
mi::TouchspotController controller{allocator, scene};
controller.enable();
controller.visualize_touches({ {{0, 0}, 1} });
ASSERT_THAT(scene->overlays, Not(IsEmpty()));
auto touchspot_buffer = scene->overlays[0]->buffer();
auto const mapping = mir::renderer::software::as_read_mappable_buffer(touchspot_buffer)->map_readable();
// Verify that each row of the touchspot buffer starts with the corresponding row of the touchspot image
// We don't care what else is in the buffer; the content of the padding in the stride doesn't matter.
auto const source_stride = touchspot_image.width * touchspot_image.bytes_per_pixel;
bool difference_found = false;
for (auto y = 0u; y < mapping->size().height.as_uint32_t(); ++y)
{
difference_found |= ::memcmp(
mapping->data() + (y * mapping->stride().as_uint32_t()),
touchspot_image.pixel_data + (y * source_stride),
source_stride) != 0;
}
EXPECT_FALSE(difference_found) << "Image portion of buffer did not match";
}
TEST_F(TestTouchspotController, touches_result_in_renderables_in_stack)
{
using namespace ::testing;
mi::TouchspotController controller(allocator, scene);
controller.enable();
controller.visualize_touches({ {{0,0}, 1} });
scene->expect_spots_centered_at({{0, 0}});
}
TEST_F(TestTouchspotController, spots_move)
{
using namespace ::testing;
mi::TouchspotController controller(allocator, scene);
controller.enable();
controller.visualize_touches({ {{0,0}, 1} });
scene->expect_spots_centered_at({{0, 0}});
controller.visualize_touches({ {{1,1}, 1} });
scene->expect_spots_centered_at({{1, 1}});
}
TEST_F(TestTouchspotController, multiple_spots)
{
using namespace ::testing;
mi::TouchspotController controller(allocator, scene);
controller.enable();
controller.visualize_touches({ {{0,0}, 1}, {{1, 1}, 1}, {{3, 3}, 1} });
scene->expect_spots_centered_at({{0, 0}, {1, 1}, {3, 3}});
controller.visualize_touches({ {{0,0}, 1}, {{1, 1}, 1}, {{3, 3}, 1}, {{5, 5}, 1} });
scene->expect_spots_centered_at({{0, 0}, {1, 1}, {3, 3}, {5, 5}});
controller.visualize_touches({ {{1,1}, 1} });
scene->expect_spots_centered_at({{1, 1}});
controller.visualize_touches({});
scene->expect_spots_centered_at({});
}
// This leaves some semantics undefined, i,e. if the touchspot controller is enabled/disabled
// during a gesture do the spots appear/dissapear? I've been unable to develop a strong opinion
// on this semantic, so I am leaving it unspecified ~racarr
TEST_F(TestTouchspotController, touches_do_not_result_in_renderables_in_stack_when_disabled)
{
using namespace ::testing;
mi::TouchspotController controller(allocator, scene);
controller.enable();
controller.disable();
controller.visualize_touches({ {{0,0}, 1} });
scene->expect_spots_centered_at({});
controller.enable();
controller.visualize_touches({ {{0,0}, 1} });
scene->expect_spots_centered_at({{0, 0}});
}
namespace
{
struct StubSceneWithMockEmission : public StubScene
{
MOCK_METHOD0(emit_scene_changed, void());
};
struct TestTouchspotControllerSceneUpdates : public TestTouchspotController
{
TestTouchspotControllerSceneUpdates()
: scene(std::make_shared<StubSceneWithMockEmission>())
{
}
std::shared_ptr<StubSceneWithMockEmission> const scene;
};
}
TEST_F(TestTouchspotControllerSceneUpdates, does_not_emit_damage_if_nothing_happens)
{
EXPECT_CALL(*scene, emit_scene_changed()).Times(0);
mi::TouchspotController controller(allocator, scene);
// We are disabled so no damage should occur.
controller.visualize_touches({ {{0,0}, 1} });
controller.enable();
// Now we are enabled but do nothing so still no damage should occur.
controller.visualize_touches({});
}
TEST_F(TestTouchspotControllerSceneUpdates, emits_scene_damage)
{
EXPECT_CALL(*scene, emit_scene_changed()).Times(2);
mi::TouchspotController controller(allocator, scene);
controller.enable();
controller.visualize_touches({ {{0,0}, 1} });
controller.visualize_touches({ {{1,1}, 1}});
}
|