]> git.sur5r.net Git - i3/i3/blob - testcases/t/202-scratchpad-criteria.t
Merge branch 'next' into master
[i3/i3] / testcases / t / 202-scratchpad-criteria.t
1 #!perl
2 # vim:ts=4:sw=4:expandtab
3 #
4 # Please read the following documents before working on tests:
5 # • https://build.i3wm.org/docs/testsuite.html
6 #   (or docs/testsuite)
7 #
8 # • https://build.i3wm.org/docs/lib-i3test.html
9 #   (alternatively: perldoc ./testcases/lib/i3test.pm)
10 #
11 # • https://build.i3wm.org/docs/ipc.html
12 #   (or docs/ipc)
13 #
14 # • http://onyxneon.com/books/modern_perl/modern_perl_a4.pdf
15 #   (unless you are already familiar with Perl)
16 #
17 # Verifies that using criteria to address scratchpad windows works.
18 use i3test;
19
20 my $i3 = i3(get_socket_path());
21
22 #####################################################################
23 # Verify that using scratchpad show with criteria works as expected:
24 # - When matching a scratchpad window which is visible,
25 #   it should hide it.
26 # - When matching a scratchpad window which is on __i3_scratch,
27 #   it should show it.
28 # - When matching a non-scratchpad window, it should be a no-op.
29 # - When matching a scratchpad window,
30 #   non-matching windows shouldn't appear
31 ######################################################################
32
33 my $tmp = fresh_workspace;
34
35 my $third_window = open_window(name => 'scratch-match');
36 cmd 'move scratchpad';
37
38 #####################################################################
39 # Verify that using 'scratchpad show' without any matching windows is
40 # a no-op.
41 #####################################################################
42 my $old_focus = get_focused($tmp);
43
44 cmd '[title="nomatch"] scratchpad show';
45
46 is(get_focused($tmp), $old_focus, 'non-matching criteria have no effect');
47
48 #####################################################################
49 # Verify that we can use criteria to show a scratchpad window.
50 #####################################################################
51 cmd '[title="scratch-match"] scratchpad show';
52
53 my $scratch_focus = get_focused($tmp);
54 isnt($scratch_focus, $old_focus, 'matching criteria works');
55
56 # Check that the window was centered and resized too.
57 my $tree = $i3->get_tree->recv;
58 my $ws = get_ws($tmp);
59 my $scratchrect = $ws->{floating_nodes}->[0]->{rect};
60 my $output = $tree->{nodes}->[1];
61 my $outputrect = $output->{rect};
62
63 is($scratchrect->{width}, $outputrect->{width} * 0.5, 'scratch width is 50%');
64 is($scratchrect->{height}, $outputrect->{height} * 0.75, 'scratch height is 75%');
65 is($scratchrect->{x},
66    ($outputrect->{width} / 2) - ($scratchrect->{width} / 2),
67    'scratch window centered horizontally');
68 is($scratchrect->{y},
69    ($outputrect->{height} / 2 ) - ($scratchrect->{height} / 2),
70    'scratch window centered vertically');
71
72 cmd '[title="scratch-match"] scratchpad show';
73
74 isnt(get_focused($tmp), $scratch_focus, 'matching criteria works');
75 is(get_focused($tmp), $old_focus, 'focus restored');
76
77
78 #####################################################################
79 # Verify that we cannot use criteria to show a non-scratchpad window.
80 #####################################################################
81 my $tmp2 = fresh_workspace;
82 my $non_scratch_window = open_window(name => 'non-scratch');
83 cmd "workspace $tmp";
84 is(get_focused($tmp), $old_focus, 'focus still ok');
85 cmd '[title="non-scratch"] scratchpad show';
86 is(get_focused($tmp), $old_focus, 'focus unchanged');
87
88 #####################################################################
89 # Verify that non-matching windows doesn't appear
90 #####################################################################
91 # Subroutine to clear scratchpad
92 sub clear_scratchpad {
93     while (scalar @{get_ws('__i3_scratch')->{floating_nodes}}) {
94         cmd 'scratchpad show';
95         cmd 'kill';
96     }
97 }
98
99 #Start from an empty fresh workspace
100 my $empty_ws = fresh_workspace;
101 cmd "workspace $empty_ws";
102
103 my $no_focused = get_focused($empty_ws);
104 cmd '[title="nothingmatchthistitle"] scratchpad show';
105 #Check nothing match
106 is(get_focused($empty_ws), $no_focused, "no window to focus on");
107
108 clear_scratchpad;
109
110 open_window(name => "my-scratch-window");
111 my $w1_focus = get_focused($empty_ws);
112 cmd 'move scratchpad';
113 cmd '[title="my-scratch-window"] scratchpad show';
114 #Check we created and shown a scratchpad window
115 is(get_focused($empty_ws), $w1_focus, "focus on scratchpad window");
116
117 #Switching workspace
118 my $empty_ws2 = fresh_workspace;
119 cmd "workspace $empty_ws2";
120 open_window(name => "my-second-scratch-window");
121
122 my $w2_focus = get_focused($empty_ws2);
123 cmd 'move scratchpad';
124 cmd '[title="my-second-scratch-window"] scratchpad show';
125
126 #Check we got the correct window
127 is(get_focused($empty_ws2), $w2_focus, "focus is on second window");
128
129 #####################################################################
130 # Verify that 'scratchpad show' correctly hide multiple scratchpad
131 # windows
132 #####################################################################
133 clear_scratchpad;
134
135 sub check_floating {
136     my($rws, $n) = @_;
137     my $ws = get_ws($rws);
138     is(scalar @{$ws->{nodes}}, 0, 'no windows on ws');
139     is(scalar @{$ws->{floating_nodes}}, $n, "$n floating windows on ws");
140 }
141
142 my $empty_ws3 = fresh_workspace;
143 cmd "workspace $empty_ws3";
144
145 check_floating($empty_ws3, 0);
146
147 #Creating two scratchpad windows
148 open_window(name => "toggle-1");
149 cmd 'move scratchpad';
150 open_window(name => "toggle-2");
151 cmd 'move scratchpad';
152 check_floating($empty_ws3, 0);
153 #Showing both
154 cmd '[title="toggle-"] scratchpad show';
155
156 check_floating($empty_ws3, 2);
157
158 #Hiding both
159 cmd '[title="toggle-"] scratchpad show';
160 check_floating($empty_ws3, 0);
161
162 #Showing both again
163 cmd '[title="toggle-"] scratchpad show';
164 check_floating($empty_ws3, 2);
165
166
167 #Hiding one
168 cmd 'scratchpad show';
169 check_floating($empty_ws3, 1);
170
171 #Hiding the last
172 cmd 'scratchpad show';
173 check_floating($empty_ws3, 0);
174
175 done_testing;