lib/Android/ElectricSheep/Automator/XMLParsers.pm

filter: Statementstmt 31.1% 19 / 61 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 75.0% 6 / 8 Podpod 0.0% 0 / 2 totaltotal 19.6% 25 / 127 CC 20 SCAR 54.4
CC
20
Cov
18%
XMLParser_find_all_apps
342.0
xpath_matches
12.0
BEGIN
1.0
CRAP 229.6
↑ Summary lib/Android/ElectricSheep/Automator/ADB.pm »
1 package Android::ElectricSheep::Automator::XMLParsers;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.09';
8  
9 1 ▶use Mojo::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Data::Roundtrip qw/perl2dump no-unicode-escape-permanently/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # This parser is for parsing UI Automator dump of the screen
15 # which comes up when you swipe from the bottom up
16 # which brings all the installed apps, we will find all
17 # those apps (name+coordinates) and return them as a hash
18 # keyed on app name.
19 # e.g. myappname => {'name' => myappname, 'bounds' => [x1,y1,x2,y2]}
20 # It takes in a UI Automator XML dump either from:
21 # a file : 'xml-filename' => ...
22 # or from a string: 'xml-string' => ...
23 # It returns the apps as a HASHref (keyed on appname) on success
24 # or undef on failure.
25 # my $xmlstring = $self->dump_current_screen_ui();
26 # my $ret = Android::ElectricSheep::Automator::XMLParsers::XMLParser_find_all_apps(
27 # {'xml-string' => $xmlstring}
28 # );
29 # will call fromXML();
30 # Or make it yourself: adb shell uiautomator dump outfile
31 # or adb exec-out uiautomator dump /dev/tty | awk '{gsub("UI hierchary dumped to: /dev/tty", "");print}'
32 0 ▶sub XMLParser_find_all_apps {
Subroutine XMLParser_find_all_apps: not called CC=18 SCAR=58.3
Pod undocumented
33 0 my $params = $_[0];
34 0 ▶ $params //= {};
Condition0%0/2
$params //= {}
l!l
00
MC/DC0%0/1
$params //= {}
$params
Truth table0%0/2
A
A= $params
Aresult
11
00
35  
36 0 ▶ my $parent = ( caller(1) )[3] || "N/A";
Condition0%0/2
(caller 1)[3] || 'N/A'
l!l
00
MC/DC0%0/1
(caller 1)[3] || 'N/A'
(caller 1)[3]
Truth table0%0/2
A
A= (caller 1)[3]
Aresult
11
00
37 0 my $whoami = ( caller(0) )[3];
38  
39 0 ▶ my $log = (exists($params->{'log'}) && defined($params->{'log'})) ? $params->{'log'} : Mojo::Log->new;
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{'log'} && defined $params->{'log'} ? : 0 0 0
Condition0%0/3
exists $params->{"log"} and defined $params->{"log"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $params->{"log"} and defined $params->{"log"}
exists $params->{"log"} defined $params->{"log"}
Truth table0%0/3
A and B
A= exists $params->{"log"}B= defined $params->{"log"}
ABresult
0X0
100
111
40 0 ▶ my $verbosity = (exists($params->{'verbosity'}) && defined($params->{'verbosity'})) ? $params->{'verbosity'} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{'verbosity'} && defined $params->{'verbosity'} ? : 0 0 0
Condition0%0/3
exists $params->{"verbosity"} and defined $params->{"verbosity"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $params->{"verbosity"} and defined $params->{"verbosity"}
exists $params->{"verbosity"} defined $params->{"verbosity"}
Truth table0%0/3
A and B
A= exists $params->{"verbosity"}B= defined $params->{"verbosity"}
ABresult
0X0
100
111
41  
42 0 my $doc;
43 0 ▶ if( exists($params->{'xml-filename'}) && defined($params->{'xml-filename'}) ){
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params->{'xml-filename'} and defined $params->{'xml-filename'}) { } 0 0 0
elsif (exists $params->{'xml-string'} and defined $params->{'xml-string'}) { } 0 0 0
Condition0%0/3
exists $params->{"xml-filename"} and defined $params->{"xml-filename"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $params->{"xml-filename"} and defined $params->{"xml-filename"}
exists $params->{"xml-filename"} defined $params->{"xml-filename"}
Truth table0%0/3
A and B
A= exists $params->{"xml-filename"}B= defined $params->{"xml-filename"}
ABresult
0X0
100
111
44 0 $doc = eval { XML::LibXML->load_xml(location => $params->{'xml-filename'}) };
45 0 ▶ if( $@ || (! defined $doc) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'XML::LibXML->load_xml()'." has failed for file '".$params->{'xml-filename'}."' : $@"); return undef }
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not defined $doc) 0 0 0
Condition0%0/3
$@ or not defined $doc
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not defined $doc
$@ not defined $doc
Truth table0%0/3
A or not(B)
A= $@B= not defined $doc
ABresult
1X1
011
000
46 ▶ } elsif( exists($params->{'xml-string'}) && defined($params->{'xml-string'}) ){
Condition0%0/3
exists $params->{"xml-string"} and defined $params->{"xml-string"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $params->{"xml-string"} and defined $params->{"xml-string"}
exists $params->{"xml-string"} defined $params->{"xml-string"}
Truth table0%0/3
A and B
A= exists $params->{"xml-string"}B= defined $params->{"xml-string"}
ABresult
0X0
100
111
47 0 $doc = eval { XML::LibXML->load_xml(string => $params->{'xml-string'}) };
48 0 ▶ if( $@ || (! defined $doc) ){ $log->error($params->{'xml-string'}."\n${whoami} (via $parent), line ".__LINE__." : error, call to ".'XML::LibXML->load_xml()'." has failed for above XML string: $@"); return undef }
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not defined $doc) 0 0 0
Condition0%0/3
$@ or not defined $doc
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not defined $doc
$@ not defined $doc
Truth table0%0/3
A or not(B)
A= $@B= not defined $doc
ABresult
1X1
011
000
49 0 } else { $log->error("${whoami} (via $parent), line ".__LINE__." : error, either 'xml-filename' or 'xml-string' must be specified in the input parameters."); return undef }
50  
51 # find a node AT THE TOP for setting the width and height
52 # text=""
53 # content-desc=""
54 # resource-id=""
55 # class="android.view.FrameLayout"
56 0 my $xpath =
57 '/hierarchy/node'
58 . '//node[contains(@resource-id, \'id/apps_view\')]'
59 . '/node[contains(@resource-id, \'id/apps_list_view\')]'
60 . '/node[contains(@resource-id, \'id/icon\') and @class=\'android.widget.TextView\']'
61 ;
62 0 my @nodes = eval { $doc->findnodes($xpath) };
63 0 ▶ if( $@ ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'findnodes()'." has failed for this xpath: $xpath : $@"); return undef };
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
64 0 my %ret;
65 0 foreach my $anode (@nodes){
66 0 my $appname = $anode->getAttribute('text');
67 0 my $bounds = $anode->getAttribute('bounds');
68 0 $bounds =~ s/\s+//;
69 0 my %item = (
70 'name' => $appname
71 );
72 0 ▶ if( $bounds =~ /^\[(\d+),(\d+)\]\[(\d+),(\d+)\]$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($bounds =~ /^\[(\d+),(\d+)\]\[(\d+),(\d+)\]$/) { } 0 0 0
73 0 $item{'bounds'} = [$1, $2, $3, $4];
74 0 } else { $log->error($anode."\n${whoami} (via $parent), line ".__LINE__." : error, above node has invalid bounds/1."); return undef }
75 0 $ret{$appname} = \%item;
76 }
77 0 return \%ret;
78 }
79  
80 # this can be registered with XML::LibXML in order to
81 # allow regex searches
82 # using
83 # my $dom = XML::LibXML->load_xml(xml=>string);
84 # my $xc = XML::LibXML::XPathContext->new($dom);
85 # $xc->registerFunction('matches', \&grep_nodes);
86 # $xc->findnodes('//div[@id="aa" and matches(@text,".*[tT]oWn$","i")]');
87 # see https://stackoverflow.com/a/73270935
88 0 ▶sub xpath_matches {
Subroutine xpath_matches: not called CC=3 SCAR=24.8
Pod undocumented
89 0 my ($input,$pattern,$flg) = @_;
90 0 ▶ $flg = '' if !defined ($flg);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flg 0 0 0
91 0 ▶ return 1 if $input =~ /(?$flg)$pattern/;
Branch: 1 branch
BranchTrueFalseTotal
if $input =~ /(?$flg)$pattern/ 0 0 0
92 0 return undef;
93 }
94  
95 # only pod below
96 =pod
97  
98 =head1 NAME
99  
100 Android::ElectricSheep::Automator - The great new Android::ElectricSheep::Automator!
101  
102 =head1 VERSION
103  
104 Version 0.09
105  
106  
107 =head1 SYNOPSIS
108  
109 Quick summary of what the module does.
110  
111 Perhaps a little code snippet.
112  
113 use Android::ElectricSheep::Automator;
114  
115 my $foo = Android::ElectricSheep::Automator->new();
116 ...
117  
118 =head1 EXPORT
119  
120 A list of functions that can be exported. You can delete this section
121 if you don't export anything, such as for a purely object-oriented module.
122  
123 =head1 SUBROUTINES/METHODS
124  
125 =head2 function1
126  
127  
128  
129 =head2 function2
130  
131  
132 =head1 AUTHOR
133  
134 Andreas Hadjiprocopis, C<< <bliako at cpan.org> >>
135  
136 =head1 BUGS
137  
138 Please report any bugs or feature requests to C<bug-android-adb-automator at rt.cpan.org>, or through
139 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Android-ADB-Automator>. I will be notified, and then you'll
140 automatically be notified of progress on your bug as I make changes.
141  
142  
143  
144  
145 =head1 SUPPORT
146  
147 You can find documentation for this module with the perldoc command.
148  
149 perldoc Android::ElectricSheep::Automator
150  
151  
152 You can also look for information at:
153  
154 =over 4
155  
156 =item * RT: CPAN's request tracker (report bugs here)
157  
158 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Android-ADB-Automator>
159  
160 =item * CPAN Ratings
161  
162 L<https://cpanratings.perl.org/d/Android-ADB-Automator>
163  
164 =item * Search CPAN
165  
166 L<https://metacpan.org/release/Android-ADB-Automator>
167  
168 =back
169  
170  
171 =head1 ACKNOWLEDGEMENTS
172  
173  
174 =head1 LICENSE AND COPYRIGHT
175  
176 This software is Copyright (c) 2025 by Andreas Hadjiprocopis.
177  
178 This is free software, licensed under:
179  
180 The Artistic License 2.0 (GPL Compatible)
181  
182  
183 =cut
184  
185 1 1; # End of Android::ElectricSheep::Automator
↑ Summary lib/Android/ElectricSheep/Automator/ADB.pm »