New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add mouse_drag
subroutine to support click and drag in tests
#1508
Conversation
8de037f
to
f96596f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there's a related ticket, please mention it in the commit message. It would also be appreciated to extend the unit test the new code (e.g. t/03-testapi.t
). Besides the mentioned points the PR looks good.
438c124
to
57a0593
Compare
Looks like all issues I've mentioned have been addressed but tests are still missing. Not that the failing ./18-qemu.t in the OBS build is unrelated. (It fails on my own PR as well and I'll have to look into it.) Seems like the behavior is still messed up when there's a needle with the click point explicitly set to
|
dfae2db
to
4cbb3d8
Compare
Codecov Report
@@ Coverage Diff @@
## master #1508 +/- ##
==========================================
+ Coverage 56.37% 57.79% +1.42%
==========================================
Files 54 55 +1
Lines 6386 6499 +113
==========================================
+ Hits 3600 3756 +156
+ Misses 2786 2743 -43
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code looks good now beside one style issue. I've restarted the CI because it failed to upload coverage data. But even without the report is is clear that the PR would decrease test coverage because not tests for the new test API function are provided. Having a test for the mouse_drag
function would be desirable.
1. This PR adds the `mouse_drag` subroutine that enables the mouse and drag support for OpenQA tests. To use the subroutine, it is possible to either provide a "starting" and "ending" needle from which the starting and finishing points will be calculated (using the centres of the needle tag), or coordinates might be provided directly. If both needles and coordinates are provided at the same time, then the coordinates precede over the needles. Combinations are possible, too, which means that one of the points can be passed as a needle and the other as coordinates. 2. In order not to repete code, the part responsible for calculating the center of needle was removed from the `click_lastmatch` subroutine and moved into a standalone non-exported subroutine called `calculate_clickpoint` to be also used by the `mouse_drag` subroutine, as well as by the original `click_lastmatch`. 3. This PR solves action #39335
I am happy that the PR now passes the required test. I will try to provide the tests for the |
5ddacbb
to
a77e482
Compare
t/03-testapi.t
Outdated
|
||
$cmds = []; | ||
# Everything is provided. | ||
my ($x, $y) = testapi::_calculate_clickpoint('foo', %fake_needle_area, %fake_click_point)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like there's one )
too much. Besides, you should likely pass the hashes as reference, e.g. … \%fake_needle_area, \%fake_click_point …
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the difference between this suggestion and the original one? What does "passing as reference" means? Sorry, told you I am not quite experienced in Perl :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in perl, when e.g. calling a subroutine with an arg that's an array or hash, you can either send the actual array or hash itself, or you can send a reference to it. A reference is a scalar which refers to a hash or an array (or other things - you can have a coderef, for instance, a reference to a block of code).
You usually don't have to worry about this in Python because Python takes care of it for you and doesn't give you any choices.
https://perldoc.perl.org/perlreftut.html is a good starting point on this stuff. The example @Martchus gave is the way to create a reference to an existing hash: if you have a hash %foo
, then \%foo
creates a reference to that hash. You can pass an anonymous reference when calling a subroutine - e.g. mysub(\%foo);
calls mysub
with an anonymous reference to %foo
as the arg - or you can assign it to a scalar: my $fooref = \%foo;
and then do stuff with the scalar, e.g. mysub($fooref);
.
If you want a subroutine to take arrays or hashes as args it's almost always best to use references, because passing actual arrays and hashes as subroutine args doesn't really work the way you might expect. Especially if the sub takes more than one arg.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you look at _calculate_checkpoint
itself, you can see it's expecting to be passed scalars:
sub _calculate_clickpoint {
my ($needle_to_use, $needle_area, $click_point) = @_;
and the rest of the code actually shows that $needle_area
and $click_point
are (mostly) expected to be hash refs, once you understand references.
if (!$click_point || $click_point eq 'center') {
$click_point = {
xpos => $needle_area->{w} / 2,
ypos => $needle_area->{h} / 2,
};
}
First off, see that use of ->
? That is the convenient way to get a value out of a hash reference. It's not how you'd get the value out of a hash. If you had a hash %needle_area
you'd use $needle_area{w}
.
You can also see that here we're handling the case where $click_point
is not passed at all or is passed not as a hash ref but as the special string value 'center', and what we're actually doing in those cases is redefining $click_point
as a hash ref. Note we use curly braces to define $click_point
here: that is a way to create a hash ref directly, as opposed to creating a named hash then creating a reference to it. If you were just creating a hash you'd do %click_point = (<stuff>)
- you use regular braces. Doing $click_point = {<stuff>}
, with curly braces, creates a new hash ref.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much, Adam, for this explanation.
t/03-testapi.t
Outdated
ok($x == 120, 'xpoint correct'); | ||
ok($y == 110, 'ypoint correct'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use is
here (for better output when the check fails).
f37b800
to
7556fe0
Compare
5dc9a92
to
20a834e
Compare
I think the problem you're having with the test failures is because you're doing Per the docs, Why's it failing? Because If you look, this is what all the other tests in the file do. The |
Technically it returns the return value of the last statement but that appears to be undef and is nothing we want to check here. |
right, sorry, I elided that a bit :) |
934b01c
to
725c23c
Compare
t/03-testapi.t
Outdated
button => 'left', | ||
cmd => 'backend_mouse_button' | ||
}, | ||
], 'mouse drag (using coordinates only) was succesful') or diag explain $cmds; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess successful is written differently but I'd actually remove it completely because that's kind of implied for a test description. The same counts for the other occurrences.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All right.
This might be ready to merge. Thanks for all your support. |
This PR adds the
mouse_drag
subroutine that enables the mouse and dragsupport for OpenQA tests. To use the subroutine, it is possible to either
provide a "starting" and "ending" needle from which the starting and finishing
points will be calculated (using the centres of the needle tag), or
coordinates might be provided directly. If both needles and coordinates are
provided at the same time, then the coordinates precede over the needles.
Combinations are possible, too, which means that one of the points can
be passed as a needle and the other as coordinates.
In order not to repete code, the part responsible for calculating the
center of needle was removed from the
click_lastmatch
subroutine and movedinto a standalone non-exported subroutine called
calculate_clickpoint
to bealso used by the
mouse_drag
subroutine, as well as by the originalclick_lastmatch
.