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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
|
/*
* 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/cursor_controller.h"
#include "mir/thread_safe_list.h"
#include "mir/input/surface.h"
#include "mir/input/scene.h"
#include "mir/scene/observer.h"
#include "mir/scene/surface_observer.h"
#include "mir/graphics/cursor_image.h"
#include "mir/graphics/cursor.h"
#include "mir_toolkit/cursors.h"
#include "mir/test/fake_shared.h"
#include "mir/test/doubles/stub_surface.h"
#include "mir/test/doubles/mock_surface.h"
#include "mir/test/doubles/stub_input_scene.h"
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <initializer_list>
#include <mutex>
#include <algorithm>
#include <assert.h>
namespace mi = mir::input;
namespace mg = mir::graphics;
namespace ms = mir::scene;
namespace geom = mir::geometry;
namespace mt = mir::test;
namespace mtd = mt::doubles;
using namespace ::testing;
namespace
{
struct NamedCursorImage : public mg::CursorImage
{
NamedCursorImage(std::string const& name)
: cursor_name(name)
{
}
void const* as_argb_8888() const override { return nullptr; }
geom::Size size() const override { return geom::Size{16, 16}; }
geom::Displacement hotspot() const override { return geom::Displacement{0, 0}; }
std::string const cursor_name;
};
struct ZeroSizedCursorImage : public mg::CursorImage
{
void const* as_argb_8888() const override { return nullptr; }
geom::Size size() const override { return geom::Size{}; }
geom::Displacement hotspot() const override { return geom::Displacement{0, 0}; }
};
bool cursor_is_named(mg::CursorImage const& i, std::string const& name)
{
auto image = dynamic_cast<NamedCursorImage const*>(&i);
assert(image);
return image->cursor_name == name;
}
MATCHER(DefaultCursorImage, "")
{
return cursor_is_named(arg, mir_default_cursor_name);
}
MATCHER_P(CursorNamed, name, "")
{
return cursor_is_named(arg, name);
}
struct MockCursor : public mg::Cursor
{
MOCK_METHOD0(show, void());
MOCK_METHOD1(show, void(mg::CursorImage const&));
MOCK_METHOD0(hide, void());
MOCK_METHOD1(move_to, void(geom::Point));
};
// TODO: This should only inherit from mi::Surface but to use the Scene observer we need an
// ms::Surface base class.
struct StubInputSurface : public mtd::StubSurface
{
StubInputSurface(geom::Rectangle const& input_bounds, std::shared_ptr<mg::CursorImage> const& cursor_image)
: mtd::StubSurface{},
bounds(input_bounds),
cursor_image_(cursor_image)
{
}
std::string name() const override
{
return std::string();
}
geom::Rectangle input_bounds() const override
{
// We could return bounds here but lets make sure the cursor controller
// is only using input_area_contains.
return geom::Rectangle();
}
bool input_area_contains(geom::Point const& point) const override
{
return bounds.contains(point);
}
mi::InputReceptionMode reception_mode() const override
{
return mi::InputReceptionMode::normal;
}
std::shared_ptr<mg::CursorImage> cursor_image() const override
{
return cursor_image_;
}
void set_cursor_image(std::shared_ptr<mg::CursorImage> const& image) override
{
cursor_image_ = image;
{
std::unique_lock lk(observer_guard);
for (auto o : observers)
o->cursor_image_set_to(this, image);
}
}
void register_interest(std::weak_ptr<ms::SurfaceObserver> const& observer, mir::Executor&) override
{
register_interest(observer);
}
void register_interest(std::weak_ptr<ms::SurfaceObserver> const& observer) override
{
std::unique_lock lk(observer_guard);
observers.push_back(observer.lock());
}
void unregister_interest(ms::SurfaceObserver const& observer) override
{
auto it = std::find_if(observers.begin(), observers.end(), [&observer](auto item)
{
return item.get() == &observer;
});
observers.erase(it);
}
void post_frame()
{
for (auto observer : observers)
{
observer->frame_posted(this, {});
}
}
void set_cursor_image_without_notifications(std::shared_ptr<mg::CursorImage> const& image)
{
cursor_image_ = image;
}
std::optional<mir::geometry::Rectangle> clip_area() const override
{
return std::optional<mir::geometry::Rectangle>();
}
void set_clip_area(std::optional<mir::geometry::Rectangle> const& /*area*/) override
{
}
geom::Rectangle const bounds;
std::shared_ptr<mg::CursorImage> cursor_image_;
std::mutex observer_guard;
std::vector<std::shared_ptr<ms::SurfaceObserver>> observers;
};
struct StubScene : public mtd::StubInputScene
{
StubScene(std::initializer_list<std::shared_ptr<ms::Surface>> const& targets)
: targets(targets.begin(), targets.end())
{
}
auto input_surface_at(geom::Point point) const -> std::shared_ptr<mi::Surface> override
{
std::shared_ptr<mi::Surface> result;
for (auto target : targets)
{
if (target->input_area_contains(point))
{
result = target;
}
}
return result;
}
void add_observer(std::shared_ptr<ms::Observer> const& observer) override
{
observers.add(observer);
for (auto target : targets)
{
observers.for_each([&target](std::shared_ptr<ms::Observer> const& observer)
{
observer->surface_exists(target);
});
}
}
void remove_observer(std::weak_ptr<ms::Observer> const& observer) override
{
auto o = observer.lock();
assert(o);
observers.remove(o);
}
void add_surface(std::shared_ptr<StubInputSurface> const& surface)
{
targets.push_back(surface);
observers.for_each([&surface](std::shared_ptr<ms::Observer> const& observer)
{
observer->surface_added(surface);
});
}
// TODO: Should be mi::Surface. See comment on StubInputSurface.
std::vector<std::shared_ptr<ms::Surface>> targets;
mir::ThreadSafeList<std::shared_ptr<ms::Observer>> observers;
};
struct TestCursorController : public testing::Test
{
TestCursorController()
: default_cursor_image(std::make_shared<NamedCursorImage>(mir_default_cursor_name))
{
}
geom::Rectangle const rect_0_0_1_1{{0, 0}, {1, 1}};
geom::Rectangle const rect_1_1_1_1{{1, 1}, {1, 1}};
std::string const cursor_name_1 = "test-cursor-1";
std::string const cursor_name_2 = "test-cursor-2";
MockCursor cursor;
std::shared_ptr<mg::CursorImage> const default_cursor_image;
geom::Point const initial_cursor_position{0, 0};
geom::Point const another_cursor_position{123, 456};
};
}
TEST_F(TestCursorController, cursor_is_initially_hidden)
{
StubScene targets({});
EXPECT_CALL(cursor, hide()).Times(1);
mi::CursorController controller{ mt::fake_shared(targets), mt::fake_shared(cursor), default_cursor_image};
}
TEST_F(TestCursorController, cursor_is_shown_when_pointing_becomes_usable)
{
StubScene targets({});
EXPECT_CALL(cursor, hide()).Times(1);
mi::CursorController controller{ mt::fake_shared(targets), mt::fake_shared(cursor), default_cursor_image};
Mock::VerifyAndClearExpectations(&cursor);
EXPECT_CALL(cursor, show(_)).Times(1);
controller.pointer_usable();
}
TEST_F(TestCursorController, cursor_is_hidden_when_pointing_becomes_unusable)
{
StubScene targets({});
EXPECT_CALL(cursor, hide()).Times(1);
mi::CursorController controller{ mt::fake_shared(targets), mt::fake_shared(cursor), default_cursor_image};
EXPECT_CALL(cursor, show(_)).Times(1);
controller.pointer_usable();
Mock::VerifyAndClearExpectations(&cursor);
EXPECT_CALL(cursor, hide()).Times(1);
controller.pointer_unusable();
}
TEST_F(TestCursorController, changed_cursor_image_is_not_shown_until_pointing_becomes_usable)
{
StubInputSurface surface{rect_0_0_1_1,
std::make_shared<NamedCursorImage>(cursor_name_1)};
StubScene targets({mt::fake_shared(surface)});
EXPECT_CALL(cursor, hide()).Times(AnyNumber());
mi::CursorController controller{ mt::fake_shared(targets), mt::fake_shared(cursor), default_cursor_image};
surface.set_cursor_image_without_notifications(
std::make_shared<NamedCursorImage>(cursor_name_2));
surface.post_frame();
Mock::VerifyAndClearExpectations(&cursor);
EXPECT_CALL(cursor, show(CursorNamed(cursor_name_2))).Times(1);
controller.pointer_usable();
}
namespace
{
// We wrap mi::CursorController to simplify the setup expected by the majority of tests
struct TestController : mi::CursorController
{
TestController(
mi::Scene& targets,
MockCursor& cursor,
std::shared_ptr<mg::CursorImage> const& default_cursor_image) :
CursorController(
(EXPECT_CALL(cursor, hide()).Times(AnyNumber()), mt::fake_shared(targets)),
mt::fake_shared(cursor),
default_cursor_image)
{
EXPECT_CALL(cursor, show()).Times(AnyNumber());
EXPECT_CALL(cursor, show(_)).Times(AnyNumber());
pointer_usable();
Mock::VerifyAndClearExpectations(&cursor);
}
};
}
TEST_F(TestCursorController, moves_cursor)
{
StubScene targets({});
TestController controller{targets, cursor, default_cursor_image};
InSequence seq;
EXPECT_CALL(cursor, move_to(geom::Point{geom::X{1.0f}, geom::Y{1.0f}}));
EXPECT_CALL(cursor, move_to(geom::Point{geom::X{0.0f}, geom::Y{0.0f}}));
controller.cursor_moved_to(1.0f, 1.0f);
controller.cursor_moved_to(0.0f, 0.0f);
}
TEST_F(TestCursorController, updates_cursor_image_when_entering_surface)
{
StubInputSurface surface{rect_1_1_1_1,
std::make_shared<NamedCursorImage>(cursor_name_1)};
StubScene targets({mt::fake_shared(surface)});
TestController controller{targets, cursor, default_cursor_image};
EXPECT_CALL(cursor, move_to(_)).Times(AtLeast(1));
EXPECT_CALL(cursor, show(CursorNamed(cursor_name_1))).Times(1);
controller.cursor_moved_to(1.0f, 1.0f);
}
TEST_F(TestCursorController, surface_with_no_cursor_image_hides_cursor)
{
StubInputSurface surface{rect_1_1_1_1,
nullptr};
StubScene targets({mt::fake_shared(surface)});
TestController controller{targets, cursor, default_cursor_image};
EXPECT_CALL(cursor, move_to(_)).Times(AtLeast(1));
EXPECT_CALL(cursor, hide()).Times(1);
controller.cursor_moved_to(1.0f, 1.0f);
}
TEST_F(TestCursorController, takes_cursor_image_from_topmost_surface)
{
StubInputSurface surface_1{rect_1_1_1_1, std::make_shared<NamedCursorImage>(cursor_name_1)};
StubInputSurface surface_2{rect_1_1_1_1, std::make_shared<NamedCursorImage>(cursor_name_2)};
StubScene targets({mt::fake_shared(surface_1), mt::fake_shared(surface_2)});
TestController controller{targets, cursor, default_cursor_image};
EXPECT_CALL(cursor, move_to(_)).Times(AtLeast(1));
EXPECT_CALL(cursor, show(CursorNamed(cursor_name_2))).Times(1);
controller.cursor_moved_to(1.0f, 1.0f);
}
TEST_F(TestCursorController, restores_cursor_when_leaving_surface)
{
StubInputSurface surface{rect_1_1_1_1,
std::make_shared<NamedCursorImage>(cursor_name_1)};
StubScene targets({mt::fake_shared(surface)});
TestController controller{targets, cursor, default_cursor_image};
EXPECT_CALL(cursor, move_to(_)).Times(AtLeast(1));
{
InSequence seq;
EXPECT_CALL(cursor, show(CursorNamed(cursor_name_1))).Times(1);
EXPECT_CALL(cursor, show(DefaultCursorImage())).Times(1);
}
controller.cursor_moved_to(1.0f, 1.0f);
controller.cursor_moved_to(2.0f, 2.0f);
}
TEST_F(TestCursorController, change_in_cursor_request_triggers_image_update_without_cursor_motion)
{
StubInputSurface surface{rect_1_1_1_1,
std::make_shared<NamedCursorImage>(cursor_name_1)};
StubScene targets({mt::fake_shared(surface)});
TestController controller{targets, cursor, default_cursor_image};
EXPECT_CALL(cursor, move_to(_)).Times(AtLeast(1));
{
InSequence seq;
EXPECT_CALL(cursor, show(CursorNamed(cursor_name_1))).Times(1);
EXPECT_CALL(cursor, show(CursorNamed(cursor_name_2))).Times(1);
}
controller.cursor_moved_to(1.0f, 1.0f);
surface.set_cursor_image(std::make_shared<NamedCursorImage>(cursor_name_2));
}
TEST_F(TestCursorController, change_in_scene_triggers_image_update)
{
// Here we also demonstrate that the cursor begins at 0,0.
StubInputSurface surface{rect_0_0_1_1,
std::make_shared<NamedCursorImage>(cursor_name_1)};
StubScene targets({});
TestController controller{targets, cursor, default_cursor_image};
EXPECT_CALL(cursor, show(CursorNamed(cursor_name_1))).Times(1);
targets.add_surface(mt::fake_shared(surface));
}
TEST_F(TestCursorController, cursor_image_not_reset_needlessly)
{
auto image = std::make_shared<NamedCursorImage>(cursor_name_1);
// Here we also demonstrate that the cursor begins at 0,0.
StubInputSurface surface1{rect_0_0_1_1, image};
StubInputSurface surface2{rect_0_0_1_1, image};
StubScene targets({});
TestController controller{targets, cursor, default_cursor_image};
EXPECT_CALL(cursor, show(CursorNamed(cursor_name_1))).Times(1);
targets.add_surface(mt::fake_shared(surface1));
targets.add_surface(mt::fake_shared(surface2));
}
TEST_F(TestCursorController, updates_cursor_image_when_surface_posts_first_frame)
{
StubInputSurface surface{rect_0_0_1_1,
std::make_shared<NamedCursorImage>(cursor_name_1)};
StubScene targets({mt::fake_shared(surface)});
TestController controller{targets, cursor, default_cursor_image};
surface.set_cursor_image_without_notifications(
std::make_shared<NamedCursorImage>(cursor_name_2));
// First post should lead to cursor update
EXPECT_CALL(cursor, show(CursorNamed(cursor_name_2))).Times(1);
surface.post_frame();
Mock::VerifyAndClearExpectations(&cursor);
// Second post should have no effect
EXPECT_CALL(cursor, show(_)).Times(0);
surface.post_frame();
}
TEST_F(TestCursorController, zero_sized_image_hides_cursor)
{
auto image = std::make_shared<ZeroSizedCursorImage>();
// Here we also demonstrate that the cursor begins at 0,0.
StubInputSurface surface{rect_0_0_1_1, image};
StubScene targets({});
TestController controller{targets, cursor, default_cursor_image};
EXPECT_CALL(cursor, hide()).Times(1);
targets.add_surface(mt::fake_shared(surface));
}
struct TestCursorControllerDragIcon : public TestCursorController
{
NiceMock<MockCursor> cursor;
StubScene targets{};
TestController controller{targets, cursor, default_cursor_image};
};
TEST_F(TestCursorControllerDragIcon, when_drag_icon_is_set_it_is_moved_to_cursor_location)
{
{
auto const drag_icon = std::make_shared<testing::NiceMock<mtd::MockSurface>>();
EXPECT_CALL(*drag_icon, move_to(initial_cursor_position)).Times(1);
controller.set_drag_icon(drag_icon);
}
controller.cursor_moved_to(another_cursor_position.x.as_int(), another_cursor_position.y.as_int());
{
auto const drag_icon = std::make_shared<testing::NiceMock<mtd::MockSurface>>();
EXPECT_CALL(*drag_icon, move_to(another_cursor_position)).Times(1);
controller.set_drag_icon(drag_icon);
}
}
TEST_F(TestCursorControllerDragIcon, given_drag_icon_is_set_when_cursor_moves_it_moves)
{
auto const drag_icon = std::make_shared<testing::NiceMock<mtd::MockSurface>>();
controller.set_drag_icon(drag_icon);
EXPECT_CALL(*drag_icon, move_to(another_cursor_position)).Times(1);
controller.cursor_moved_to(another_cursor_position.x.as_int(), another_cursor_position.y.as_int());
}
TEST_F(TestCursorControllerDragIcon, given_drag_icon_is_set_when_cursor_hidden_it_hides)
{
auto const drag_icon = std::make_shared<testing::NiceMock<mtd::MockSurface>>();
controller.set_drag_icon(drag_icon);
EXPECT_CALL(*drag_icon, hide()).Times(1);
controller.pointer_unusable();
}
TEST_F(TestCursorControllerDragIcon, given_drag_icon_is_set_when_cursor_unhidden_it_shows)
{
auto const drag_icon = std::make_shared<testing::NiceMock<mtd::MockSurface>>();
controller.pointer_unusable();
controller.set_drag_icon(drag_icon);
EXPECT_CALL(*drag_icon, show()).Times(1);
controller.pointer_usable();
}
|