File: test-awful-rules.lua

package info (click to toggle)
awesome 4.3-8.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,468 kB
  • sloc: ansic: 14,508; sh: 526; makefile: 46
file content (348 lines) | stat: -rw-r--r-- 8,866 bytes parent folder | download | duplicates (4)
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
local awful = require("awful")
local gears = require("gears")
local beautiful = require("beautiful")
local test_client = require("_client")
local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1)

local callback_called = false
local message_printed = false

-- Magic table to store tests
local tests = {}

local tb_height = gears.math.round(beautiful.get_font_height() * 1.5)
-- local border_width = beautiful.border_width

-- Detect "manage" race conditions
local real_apply = awful.rules.apply
function awful.rules.apply(c)
    assert(#c:tags() == 0)
    return real_apply(c)
end

local function test_rule(rule)
    rule.rule = rule.rule or {}

    -- Create a random class. The number need to be large as "rule1" and "rule10" would collide
    -- The math.ceil is necessary to remove the floating point, this create an invalid dbus name
    local class = string.format("rule%010d", 42+#tests)
    rule.rule.class = rule.rule.class or class

    test_client(rule.rule.class, rule.properties.name  or "Foo")
    if rule.test then
        local t = rule.test
        table.insert(tests, function() return t(rule.rule.class) end)
        rule.test = nil
    end

    table.insert(awful.rules.rules, rule)
end

-- Helper function to search clients
local function get_client_by_class(class)
    for _, c in ipairs(client.get()) do
        if class == c.class then
            return c
        end
    end
end

local orig_error = gears.debug.print_error
function gears.debug.print_error(msg)
    assert(not message_printed, msg)
    assert(msg:find("specified tag = 'does_not_exist', but no such tag exists"), msg)
    message_printed = true
end

-- Test callback and floating
test_rule {
    properties = { floating = true, tag = "does_not_exist" },
    callback   = function(c)
        assert(type(c) == "client")
        callback_called = true
    end,
    test = function(class)
        -- Test if callbacks works
        assert(callback_called)

        -- Test that the "does_not_exist"-tag caused an error
        assert(message_printed)
        gears.debug.print_error = orig_error

        -- Make sure "smart" dynamic properties are applied
        assert(get_client_by_class(class).floating)

        -- The size should not have changed
        local geo = get_client_by_class(class):geometry()
        assert(geo.width == 100 and geo.height == 100+tb_height)

        return true
    end
}

-- Test ontop
test_rule {
    properties = { ontop = true },
    test = function(class)
        -- Make sure C-API properties are applied
        assert(get_client_by_class(class).ontop)

        return true
    end
}

-- Test placement
test_rule {
    properties = {
        floating  = true,
        placement = "bottom_right"
    },
    test = function(class)
        -- Test placement
        local sgeo = mouse.screen.workarea
        local c    = get_client_by_class(class)
        local geo  = c:geometry()

        assert(geo.y+geo.height+2*c.border_width == sgeo.y+sgeo.height)
        assert(geo.x+geo.width+2*c.border_width == sgeo.x+sgeo.width)

        return true
    end
}

-- Create a tag named after the class name
test_rule {
    properties = { new_tag=true },
    test = function(class)
        local c_new_tag1 = get_client_by_class(class)

        assert(#c_new_tag1:tags()[1]:clients() == 1)
        assert(c_new_tag1:tags()[1].name == class)

        return true
    end
}

-- Create a tag named Foo Tag with a magnifier layout
test_rule {
    properties = {
        new_tag = {
            name   = "Foo Tag",
            layout = awful.layout.suit.magnifier
        }
    },
    test = function(class)
        local t_new_tag2 = get_client_by_class(class):tags()[1]

        assert( #t_new_tag2:clients()  == 1           )
        assert( t_new_tag2.name        == "Foo Tag"   )
        assert( t_new_tag2.layout.name == "magnifier" )

        return true
    end
}

-- Create a tag named "Bar Tag"
test_rule {
    properties = { new_tag= "Bar Tag" },
    test = function(class)
        local c_new_tag3 = get_client_by_class(class)
        assert(#c_new_tag3:tags()[1]:clients() == 1)
        assert(c_new_tag3:tags()[1].name == "Bar Tag")

        return true
    end
}

-- Test if setting the geometry work
test_rule {
    properties = {
        floating = true,
        x        = 200,
        y        = 200,
        width    = 200,
        height   = 200,
    },
    test = function(class)
        local c   = get_client_by_class(class)
        local geo = c:geometry()

        -- Give it some time
        if geo.y < 180 then return end

        assert(geo.x      == 200)
        assert(geo.y      == 200)
        assert(geo.width  == 200)
        assert(geo.height == 200)

        return true
    end
}

-- Test if setting a partial geometry preserve the other attributes
test_rule {
    properties = {
        floating = true,
        x        = 200,
        geometry = {
            height = 220
        }
    },
    test = function(class)
        local c   = get_client_by_class(class)
        local geo = c:geometry()

        -- Give it some time
        if geo.height < 200 then return end

        assert(geo.x      == 200)
        assert(geo.width  == 100)
        assert(geo.height == 220)

        return true
    end
}

-- Test maximized_horizontal
test_rule {
    properties = { maximized_horizontal = true },
    test = function(class)
        local c = get_client_by_class(class)
        -- Make sure C-API properties are applied

        assert(c.maximized_horizontal)

        local geo = c:geometry()
        local sgeo = c.screen.workarea

        assert(geo.x==sgeo.x)

        assert(geo.width+2*c.border_width==sgeo.width)

        return true
    end
}

-- Test maximized_vertical
test_rule {
    properties = { maximized_vertical = true },
    test = function(class)
        local c = get_client_by_class(class)
        -- Make sure C-API properties are applied

        assert(c.maximized_vertical)

        local geo = c:geometry()
        local sgeo = c.screen.workarea

        assert(geo.y==sgeo.y)

        assert(geo.height+2*c.border_width==sgeo.height)

        return true
    end
}

-- Test fullscreen
test_rule {
    properties = { fullscreen = true },
    test = function(class)
        local c = get_client_by_class(class)
        -- Make sure C-API properties are applied

        assert(c.fullscreen)

        local geo = c:geometry()
        local sgeo = c.screen.geometry

        assert(geo.x==sgeo.x)
        assert(geo.y==sgeo.y)
        assert(geo.height+2*c.border_width==sgeo.height)
        assert(geo.width+2*c.border_width==sgeo.width)

        return true
    end
}

-- Test the custom sources
assert(awful.rules.add_rule_source("high_priority", function(c, props, _)
    assert(type(c) == "client")
    assert(props.random2)

    props.random1 = true
end, {"awful.spawn"}, {"awful.rules", "low_priority"}))

assert(awful.rules.add_rule_source("before2", function()
    error("This function should not be called")
end, {"awful.spawn"}, {"awful.rules"}))

assert(awful.rules.remove_rule_source("before2"))

assert(awful.rules.add_rule_source("low_priority", function(c, props, _)
    assert(type(c) == "client")
    assert(not props.random1)

    props.random2 = true
end, {"awful.spawn", "high_priority"}, {"void", "awful.rules"}))

local temp = gears.debug.print_warning
gears.debug.print_warning = function() end
assert(not awful.rules.add_rule_source("invalid_source", function()
    assert(false, "This cannot happen")
end, {"awful.rules"}, {"awful.spawn"}))
gears.debug.print_warning = temp

-- Test tag and switch_to_tags
test_rule {
    properties = {
        tag            = "9",
        switch_to_tags = true
    },
    test = function(class)
        local c = get_client_by_class(class)
        -- Make sure C-API properties are applied

        assert(#c:tags() == 1)
        assert(c:tags()[1].name == "9")
        assert(c.screen.selected_tag.name == "9")

        return true
    end
}
test_rule {
    properties = {
        tag            = "8",
        switch_to_tags = false
    },
    test = function(class)
        local c = get_client_by_class(class)
        -- Make sure C-API properties are applied

        assert(#c:tags() == 1)
        assert(c:tags()[1].name == "8")

        assert(c.screen.selected_tag.name ~= "8")

        -- Test the custom sources
        assert(c.random1)
        assert(c.random2)
        assert(not c.random3)

        return true
    end
}



-- Wait until all the auto-generated clients are ready
local function spawn_clients()
    if #client.get() >= #tests then
        -- Set tiled
        awful.layout.inc(1)
        return true
    end
end

require("_runner").run_steps{spawn_clients, unpack(tests)}

-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80