aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorJim Porter2024-06-19 20:59:59 -0700
committerJim Porter2024-06-22 23:09:00 -0700
commit5f9b5803bea0f360a91e00cd85d72ea7f56d6095 (patch)
treef54e14a1871a2fd5c5f276f299058e4a78749f4a /test
parent6f2036243f24369b0b4c35c9b323eb19dad4e4cd (diff)
downloademacs-5f9b5803bea0f360a91e00cd85d72ea7f56d6095.tar.gz
emacs-5f9b5803bea0f360a91e00cd85d72ea7f56d6095.zip
Fix zooming images in SHR
Previously, for images with no alt-text, the zoomed image wouldn't get properly inserted. For images with alt-text, both the zoomed and unzoomed image would be displayed at once (bug#71666). * lisp/net/shr.el (shr-sliced-image): New face. (shr-zoom-image): Reimplement using 'next/previous-single-property-change', and don't bother deleting any of the text. (shr-image-fetched): Clean up any overlays when deleting the old region. (shr-put-image): Ensure we always have a non-empty string to put the image on. For sliced images, just use "*", since we'll repeat it, so we can't preserve the original buffer text exactly anyway. Apply an overlay to sliced images to prevent unsightly text decorations. (shr-tag-img): Move the placeholder space insertion where it should be and explain what it's doing. * test/lisp/net/shr-tests.el (shr-test--max-wait-time) (shr-test-wait-for): New helper functions. (shr-test/zoom-image): New test.
Diffstat (limited to 'test')
-rw-r--r--test/lisp/net/shr-tests.el64
1 files changed, 64 insertions, 0 deletions
diff --git a/test/lisp/net/shr-tests.el b/test/lisp/net/shr-tests.el
index 17138053450..b6552674b27 100644
--- a/test/lisp/net/shr-tests.el
+++ b/test/lisp/net/shr-tests.el
@@ -29,6 +29,22 @@
29 29
30(declare-function libxml-parse-html-region "xml.c") 30(declare-function libxml-parse-html-region "xml.c")
31 31
32(defvar shr-test--max-wait-time 5
33 "The maximum amount of time to wait for a condition to resolve, in seconds.
34See `shr-test-wait-for'.")
35
36(defun shr-test-wait-for (predicate &optional message)
37 "Wait until PREDICATE returns non-nil.
38If this takes longer than `shr-test--max-wait-time', raise an error.
39MESSAGE is an optional message to use if this times out."
40 (let ((start (current-time))
41 (message (or message "timed out waiting for condition")))
42 (while (not (funcall predicate))
43 (when (> (float-time (time-since start))
44 shr-test--max-wait-time)
45 (error message))
46 (sit-for 0.1))))
47
32(defun shr-test--rendering-check (name &optional context) 48(defun shr-test--rendering-check (name &optional context)
33 "Render NAME.html and compare it to NAME.txt. 49 "Render NAME.html and compare it to NAME.txt.
34Raise a test failure if the rendered buffer does not match NAME.txt. 50Raise a test failure if the rendered buffer does not match NAME.txt.
@@ -68,6 +84,8 @@ validate for the NAME testcase.
68The `rendering' testcase will test NAME once without altering any 84The `rendering' testcase will test NAME once without altering any
69settings, then once more for each (OPTION . VALUE) pair.") 85settings, then once more for each (OPTION . VALUE) pair.")
70 86
87;;; Tests:
88
71(ert-deftest rendering () 89(ert-deftest rendering ()
72 (skip-unless (fboundp 'libxml-parse-html-region)) 90 (skip-unless (fboundp 'libxml-parse-html-region))
73 (dolist (file (directory-files (ert-resource-directory) nil "\\.html\\'")) 91 (dolist (file (directory-files (ert-resource-directory) nil "\\.html\\'"))
@@ -114,6 +132,52 @@ settings, then once more for each (OPTION . VALUE) pair.")
114 (should (equal (shr--parse-srcset "https://example.org/1,2\n\n 10w , https://example.org/2 20w ") 132 (should (equal (shr--parse-srcset "https://example.org/1,2\n\n 10w , https://example.org/2 20w ")
115 '(("https://example.org/2" 20) ("https://example.org/1,2" 10))))) 133 '(("https://example.org/2" 20) ("https://example.org/1,2" 10)))))
116 134
135(ert-deftest shr-test/zoom-image ()
136 "Test that `shr-zoom-image' properly replaces the original image."
137 (let ((image (expand-file-name "data/image/blank-100x200.png"
138 (getenv "EMACS_TEST_DIRECTORY"))))
139 (dolist (alt '(nil "" "nothing to see here"))
140 (with-temp-buffer
141 (ert-info ((format "image with alt=%S" alt))
142 (let ((attrs (if alt (format " alt=\"%s\"" alt) "")))
143 (insert (format "<img src=\"file://%s\" %s" image attrs)))
144 (cl-letf* (;; Pretend we're a graphical display.
145 ((symbol-function 'display-graphic-p) #'always)
146 ((symbol-function 'url-queue-retrieve)
147 (lambda (&rest args)
148 (apply #'run-at-time 0 nil #'url-retrieve args)))
149 (put-image-calls 0)
150 (shr-put-image-function
151 (lambda (&rest args)
152 (cl-incf put-image-calls)
153 (apply #'shr-put-image args)))
154 (shr-width 80)
155 (shr-use-fonts nil)
156 (shr-image-animate nil)
157 (inhibit-message t)
158 (dom (libxml-parse-html-region (point-min) (point-max))))
159 ;; Render the document.
160 (erase-buffer)
161 (shr-insert-document dom)
162 (shr-test-wait-for (lambda () (= put-image-calls 1)))
163 ;; Now zoom the image.
164 (goto-char (point-min))
165 (shr-zoom-image)
166 (shr-test-wait-for (lambda () (= put-image-calls 2)))
167 ;; Check that we got a sliced image.
168 (let ((slice-count 0))
169 (goto-char (point-min))
170 (while (< (point) (point-max))
171 (when-let ((display (get-text-property (point) 'display)))
172 ;; If this is nil, we found a non-sliced image, but we
173 ;; should have replaced that!
174 (should (assq 'slice display))
175 (cl-incf slice-count))
176 (goto-char (or (next-single-property-change (point) 'display)
177 (point-max))))
178 ;; Make sure we actually saw a slice.
179 (should (> slice-count 1)))))))))
180
117(require 'shr) 181(require 'shr)
118 182
119;;; shr-tests.el ends here 183;;; shr-tests.el ends here