[PATCH 3/4] tests: promote subsurface-shot-tests's check_screen() to a utility function

Micah Fedke micah.fedke at collabora.co.uk
Tue Feb 7 14:42:03 UTC 2017


This function takes a screenshot of a provided client, compares it to a
reference image, outputs the result as a PNG, and returns a value
indicating whether the two images matched or not.  A difference image is
generated if the screenshot and reference do not match.
---
 tests/subsurface-shot-test.c      | 39 ----------------------------
 tests/weston-test-client-helper.c | 54 +++++++++++++++++++++++++++++++++++++++
 tests/weston-test-client-helper.h |  4 +++
 3 files changed, 58 insertions(+), 39 deletions(-)

diff --git a/tests/subsurface-shot-test.c b/tests/subsurface-shot-test.c
index a8dfebad..bae4cd87 100644
--- a/tests/subsurface-shot-test.c
+++ b/tests/subsurface-shot-test.c
@@ -96,45 +96,6 @@ color(pixman_color_t *tmp, uint8_t r, uint8_t g, uint8_t b)
 	return tmp;
 }
 
-static int
-check_screen(struct client *client,
-	     const char *ref_image,
-	     int ref_seq_no,
-	     const struct rectangle *clip,
-	     int seq_no)
-{
-	const char *test_name = get_test_name();
-	struct buffer *shot;
-	pixman_image_t *ref;
-	char *ref_fname;
-	char *shot_fname;
-	bool match;
-
-	ref_fname = screenshot_reference_filename(ref_image, ref_seq_no);
-	shot_fname = screenshot_output_filename(test_name, seq_no);
-
-	ref = load_image_from_png(ref_fname);
-	assert(ref);
-
-	shot = capture_screenshot_of_output(client);
-	assert(shot);
-
-	match = check_images_match(shot->image, ref, clip);
-	printf("ref %s vs. shot %s: %s\n", ref_fname, shot_fname,
-	       match ? "PASS" : "FAIL");
-
-	write_image_as_png(shot->image, shot_fname);
-	if (!match)
-		write_visual_diff(ref, shot, clip, test_name, seq_no);
-
-	buffer_destroy(shot);
-	pixman_image_unref(ref);
-	free(ref_fname);
-	free(shot_fname);
-
-	return match ? 0 : -1;
-}
-
 static struct buffer *
 surface_commit_color(struct client *client, struct wl_surface *surface,
 		     pixman_color_t *color, int width, int height)
diff --git a/tests/weston-test-client-helper.c b/tests/weston-test-client-helper.c
index 43a76eb3..63a7ad21 100644
--- a/tests/weston-test-client-helper.c
+++ b/tests/weston-test-client-helper.c
@@ -1415,3 +1415,57 @@ capture_screenshot_of_output(struct client *client)
 	return buffer;
 }
 
+/**
+ * Takes a screenshot of a provided client, compares it to a reference image,
+ * outputs the result as a PNG, and returns a value indicating whether the two
+ * images matched or not.  A difference image is generated if the screenshot
+ * and reference do not match.
+ *
+ * \param client The client whose output will be compared to the reference image.
+ * \param ref_image The reference image.
+ * \param ref_seq_no The sequence number of the reference image to use.
+ * \param clip The region of interest, or NULL for comparing the whole
+ * image.
+ * \param seq_no The sequence number to append to the output diff file name
+ * (usually matches the ref_seq_no, unless multiple tests are being compared to
+ * a single reference image)
+ * \return 0 if the screenshot and reference image are equal, -1 otherwise.
+ */
+int
+check_screen(struct client *client,
+	     const char *ref_image,
+	     int ref_seq_no,
+	     const struct rectangle *clip,
+	     int seq_no)
+{
+	const char *test_name = get_test_name();
+	struct buffer *shot;
+	pixman_image_t *ref;
+	char *ref_fname;
+	char *shot_fname;
+	bool match;
+
+	ref_fname = screenshot_reference_filename(ref_image, ref_seq_no);
+	shot_fname = screenshot_output_filename(test_name, seq_no);
+
+	ref = load_image_from_png(ref_fname);
+	assert(ref);
+
+	shot = capture_screenshot_of_output(client);
+	assert(shot);
+
+	match = check_images_match(shot->image, ref, clip);
+	printf("ref %s vs. shot %s: %s\n", ref_fname, shot_fname,
+	       match ? "PASS" : "FAIL");
+
+	write_image_as_png(shot->image, shot_fname);
+	if (!match)
+		write_visual_diff(ref, shot, clip, test_name, seq_no);
+
+	buffer_destroy(shot);
+	pixman_image_unref(ref);
+	free(ref_fname);
+	free(shot_fname);
+
+	return match ? 0 : -1;
+}
diff --git a/tests/weston-test-client-helper.h b/tests/weston-test-client-helper.h
index 8c497e24..218e79dc 100644
--- a/tests/weston-test-client-helper.h
+++ b/tests/weston-test-client-helper.h
@@ -217,4 +217,8 @@ load_image_from_png(const char *fname);
 struct buffer *
 capture_screenshot_of_output(struct client *client);
 
+int
+check_screen(struct client *client, const char *ref_image, int ref_seq_no,
+	     const struct rectangle *clip, int seq_no);
+
 #endif
-- 
2.11.0



More information about the wayland-devel mailing list