From 8f7750e6f2e3b7fd2d0ca64669ca4430e6f9a4b0 Mon Sep 17 00:00:00 2001 From: Brian Sobulefsky Date: Mon, 22 Aug 2022 21:26:28 -0700 Subject: [PATCH] Add tests for the snipper tool and the snip (defined geometry) routine to increase test coverage. --- lib/awful/screenshot.lua | 14 ++--- tests/test-screenshot.lua | 110 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 7 deletions(-) diff --git a/lib/awful/screenshot.lua b/lib/awful/screenshot.lua index 66c4e7427..d0550a65f 100644 --- a/lib/awful/screenshot.lua +++ b/lib/awful/screenshot.lua @@ -390,7 +390,7 @@ function default_on_success_cb(ss) end -- Internal function exected when a root window screenshot is taken. -function screenshot_methods.root_screenshot(ss) +function screenshot_methods.root(ss) local w, h = root.size() ss._private.geometry = {x = 0, y = 0, width = w, height = h} ss:filepath_builder() @@ -399,7 +399,7 @@ function screenshot_methods.root_screenshot(ss) end -- Internal function executed when a physical screen screenshot is taken. -function screenshot_methods.screen_screenshot(ss) +function screenshot_methods.screen(ss) -- note the use of _private because screen has no setter if ss.screen then @@ -419,7 +419,7 @@ function screenshot_methods.screen_screenshot(ss) end -- Internal function executed when a client window screenshot is taken. -function screenshot_methods.client_screenshot(ss) +function screenshot_methods.client(ss) -- -- note the use of _private becuase client has no setter if not ss.client then @@ -433,7 +433,7 @@ function screenshot_methods.client_screenshot(ss) end -- Internal function executed when a snipper screenshot tool is launched. -function screenshot_methods.snipper_screenshot(ss) +function screenshot_methods.snipper(ss) if type(ss._private.on_success_cb) ~= "function" then ss._private.on_success_cb = default_on_success_cb -- the cb has no setter @@ -448,7 +448,7 @@ end -- Internal function executed when a snip screenshow (a defined geometry) is -- taken. -function screenshot_methods.snip_screenshot(ss) +function screenshot_methods.snip(ss) local root_w, root_h local root_intrsct @@ -483,7 +483,7 @@ function screenshot_methods.snip_screenshot(ss) end -- Default method is root -screenshot_methods.default = root_screenshot +screenshot_methods.default = screenshot_methods.root local default_method_name = "root" -- Module routines @@ -845,7 +845,7 @@ local function new(_, args) end elseif method_name == "snip" then - geom = args.geometry + geometry = args.geometry end ss._private = { diff --git a/tests/test-screenshot.lua b/tests/test-screenshot.lua index fc2f845f8..7e946d2e3 100644 --- a/tests/test-screenshot.lua +++ b/tests/test-screenshot.lua @@ -108,6 +108,17 @@ local function get_pixel(img, x, y) return "#" .. bytes:gsub('.', function(c) return ('%02x'):format(c:byte()) end) end +local snipper_success = nil +local function snipper_cb(ss) + local img = ss.surface + if img and get_pixel(img, 10, 10) == "#00ff00" then + snipper_success = "true" + return + else + snipper_success = "false" + end +end + local steps = {} -- Check the whole root window with root.content() @@ -247,4 +258,103 @@ table.insert(steps, function() end) +--Check the snipper toop with awful.screenshot.snipper() method +table.insert(steps, function() + --Make sure client from last test is gone + if #client.get() ~= 0 then return end + --Ensure mousegrabber is satisfied + root.fake_input("button_press",1) + return true +end) + +table.insert(steps, function() + root.fake_input("button_release",1) + return true +end) + +table.insert(steps, function() + --Ensure prior mouse presses go through + local t0 = os.time() + while os.time() - t0 < 1 do end + return true +end) + +table.insert(steps, function() + awful.screenshot.snipper({directory = fake_screenshot_dir, on_success_cb = snipper_cb}) + return true +end) + +table.insert(steps, function() + mouse.coords {x = 110, y = 110} + return true +end) + +table.insert(steps, function() + root.fake_input("button_press",1) + return true +end) + +table.insert(steps, function() + root.fake_input("button_release",1) + return true +end) + +table.insert(steps, function() + mouse.coords {x = 190, y = 190} + return true +end) + +table.insert(steps, function() + --Ensure prior mouse presses and movements go through + local t0 = os.time() + while os.time() - t0 < 1 do end + return true +end) + +table.insert(steps, function() + root.fake_input("button_press",1) + return true +end) + +table.insert(steps, function() + root.fake_input("button_release",1) + return true +end) + +table.insert(steps, function() + --Ensure prior mouse presses go through and callback runs + local t0 = os.time() + while os.time() - t0 < 1 do end + return true +end) + +table.insert(steps, function() + + --Check for success + if snipper_success then + if snipper_success == "true" then + return true + else + return false + end + else + return + end + + return true + +end) + +table.insert(steps, function() + + local ss = awful.screenshot.snip({geometry = {x = 100, y = 100, width = 100, height = 100}, + directory = fake_screenshot_dir}) + local img = ss.surface + if get_pixel(img, 10, 10) == "#00ff00" then + return true + else + return false + end +end) + require("_runner").run_steps(steps)