1#!/usr/bin/env ruby
2#
3
4require "minitest"
5require "./xdo_test_helper"
6
7class XdotoolSearchTests < MiniTest::Test
8  include XdoTestHelper
9
10  def test_search_pid
11    try do
12      status, lines = xdotool "search --pid #{@windowpid}"
13      assert_equal(0, status, "Exit status should have been 0")
14      assert_equal(1, lines.size, "Expect only one match to our search (only one window running that should match)")
15      assert_equal(@wid, lines[0].to_i, "Expected the correct windowid when searching for its pid")
16    end
17  end
18
19  def test_search_title
20    status, lines = xdotool "search --name #{@title}"
21    try do
22      assert_equal(0, status, "Exit status should have been 0")
23      assert_equal(1, lines.size,
24                   "Expect only one match to our search (only one window " \
25                   "running that should match)")
26      assert_equal(@wid, lines[0].to_i,
27                   "Expected the correct windowid when searching for its pid")
28    end
29  end
30
31  def test_search_onlyvisible_with_pid
32    try do
33      status, lines = xdotool "search --onlyvisible --pid #{@windowpid}"
34      assert_equal(0, status, "Exit status should have been 0")
35      assert_equal(1, lines.size, "Expect only one match to our search (only one window running that should match)")
36      assert_equal(@wid, lines[0].to_i, "Expected the correct windowid when searching for its pid")
37    end
38
39    try do
40      # Hide the window and try searching for it. We shouldn't find it.
41      status, lines = xdotool "windowunmap #{@wid}"
42      assert_equal(0, status, "Exit status should have been 0")
43    end
44
45    try do
46      status, lines = xdotool "search --onlyvisible --pid #{@windowpid}"
47      # search will exit 1 when no matches are found
48      assert_equal(1, status, "Exit status should have been 1")
49      assert_equal(0, lines.size, "Expect no matches with onlyvisible and the only match window is hidden")
50    end
51
52    try do
53      # Bring up the window again and try searching for it.
54      status, lines = xdotool "windowmap #{@wid}"
55      assert_equal(0, status, "Exit status should have been 0")
56
57      status, lines = xdotool "search --onlyvisible --pid #{@windowpid}"
58      assert_equal(0, status, "Exit status should have been 0")
59      assert_equal(1, lines.size,
60                   "Expect only one match to our search (only one window" \
61                   "running that should match)")
62      assert_equal(@wid, lines[0].to_i,
63                   "Expected the correct windowid when searching for its pid")
64    end
65  end
66
67  def test_search_maxdeth_0_has_no_results
68    status, lines = xdotool "search --maxdepth 0 ."
69    assert_equal(1, status, "Exit status should be nonzero (no results expected)")
70    assert_equal(0, lines.length, "Search with --maxdepth 0 should return no results");
71  end
72
73  def test_search_by_name
74    name = "name#{rand}"
75    status, lines = xdotool "search --name '#{name}'"
76    assert_equal(1, status,
77                 "Search for window with name '#{name}' exit nonzero when" \
78                 + " no window with that name exists")
79    assert_equal(0, lines.length,
80                 "Search for window with name '#{name}' should have no results when" \
81                 + " no window with that name exists")
82
83
84    xdotool "set_window --name '#{name}' #{@wid}"
85    try do
86      status, lines = xdotool "search --name '#{name}'"
87      assert_equal(0, status, "Search for name '#{name}' should exit zero")
88      assert_equal(1, lines.size, "Search for name '#{name}' have one result")
89      assert(lines.include?(@wid.to_s),
90             "Searched results should include our expected window")
91    end
92  end
93
94  def test_search_by_class
95    name = "class#{rand}"
96    status, lines = xdotool "search --class '#{name}'"
97    assert_equal(1, status,
98                 "Search for window with class '#{name}' exit nonzero when" \
99                 + " no window with that class exists")
100    assert_equal(0, lines.length,
101                 "Search for window with class '#{name}' should have no results when" \
102                 + " no window with that class exists")
103
104
105    xdotool "set_window --class '#{name}' #{@wid}"
106
107    try do
108      status, lines = xdotool "search --class '#{name}'"
109      assert_equal(0, status, "Search for class '#{name}' should exit zero")
110      assert_equal(1, lines.size, "Search for class '#{name}' have one result")
111      assert(lines.include?(@wid.to_s),
112             "Searched results should include our expected window")
113    end
114  end
115
116  def test_search_by_classname
117    name = "classname#{rand}"
118    status, lines = xdotool "search --classname '#{name}'"
119    assert_equal(1, status,
120                 "Search for window with classname '#{name}' exit nonzero when" \
121                 + " no window with that classname exists")
122    assert_equal(0, lines.length,
123                 "Search for window with classname '#{name}' should have no results when" \
124                 + " no window with that classname exists")
125
126
127    xdotool "set_window --classname '#{name}' #{@wid}"
128
129    try do
130      status, lines = xdotool "search --classname '#{name}'"
131      assert_equal(0, status, "Search for classname '#{name}' should exit zero")
132      assert_equal(1, lines.size, "Search for classname '#{name}' have one result")
133      assert(lines.include?(@wid.to_s),
134             "Searched results should include our expected window")
135    end
136  end
137
138  def test_search_can_find_all_windows
139    name = "searchall#{rand}"
140    windowdata = %x{xwininfo -tree -root}.split("\n") \
141      .grep(/(^ *0x)|Root window id/) \
142      .collect { |l| l[/0x[0-9A-Fa-f]+/].to_i(16) }
143    ["name", "class", "classname"].each do |query|
144      status, lines = xdotool "search --#{query} '^'"
145      assert_equal(0, status,
146                   "Search for window with --#{query} '^' should exit" \
147                   + " with status zero.")
148      #assert_not_equal(0, lines.length,
149                       #"Search for window with --#{query} '^' should have" \
150                       #+ " at least one result.")
151      assert_equal(windowdata.sort, lines.collect { |w| w.to_i }.sort,
152                   "Expected same window list from xwininfo and xdotool")
153    end # ["name" ... ].each
154  end # def test_search_can_find_all_windows
155end # XdotoolSearchTests
156
157class XdotoolRaceSearchTests < MiniTest::Test
158  include XdoTestHelper
159
160  def setup
161    setup_vars
162    setup_ensure_x_is_healthy
163    setup_launch_xterm
164    setup_launch("sh", "-c", "for i in $(seq 100); do xterm -e sleep 0.1 & true; done")
165  end # def setup
166
167  def test_search_title
168    (1 .. 100).each do
169      status, lines = xdotool "search --name #{@title}"
170      assert_equal(0, status, "Exit status should have been 0")
171      assert_equal(1, lines.size,
172                   "Expect only one match to our search (only one window " \
173                   "running that should match)")
174      assert_equal(@wid, lines[0].to_i,
175                   "Expected the correct windowid when searching for its pid")
176    end
177  end
178end # XdotoolRaceSearchTests
179