;;; eww-tests.el --- tests for eww.el -*- lexical-binding: t; -*- ;; Copyright (C) 2024-2026 Free Software Foundation, Inc. ;; This file is part of GNU Emacs. ;; GNU Emacs is free software: you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; GNU Emacs is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs. If not, see . ;;; Commentary: ;;; Code: (require 'ert) (require 'eww) (defvar eww-test--response-function (lambda (url) (concat "\n" url)) "A function for returning a mock response for URL. The default just returns an empty list of headers and the URL as the body.") (defvar eww-test--lots-of-words (string-join (make-list 20 "All work and no play makes Jack a dull boy.") " ") "A long enough run of words to satisfy EWW's readable mode cutoff.") (defvar eww-test--wordy-page (concat "" "" "Welcome to my home page" "" "" "" "This is an uninteresting sentence." "
" eww-test--lots-of-words "
" "")) (defmacro eww-test--with-mock-retrieve (&rest body) "Evaluate BODY with a mock implementation of `eww-retrieve'. This avoids network requests during our tests. Additionally, prepare a temporary EWW buffer for our tests." (declare (indent 0)) `(cl-letf (((symbol-function 'eww-retrieve) (lambda (url callback args) (with-temp-buffer (insert (funcall eww-test--response-function url)) (apply callback nil args))))) (with-temp-buffer (eww-mode) ,@body))) (defun eww-test--history-urls () (mapcar (lambda (elem) (plist-get elem :url)) eww-history)) ;;; Tests: (ert-deftest eww-test/display/html () "Test displaying a simple HTML page." (skip-unless (libxml-available-p)) (eww-test--with-mock-retrieve (let ((eww-test--response-function (lambda (url) (concat "Content-Type: text/html\n\n" (format "

Hello

%s" url))))) (eww "example.invalid") ;; Check that the buffer contains the rendered HTML. (should (equal (buffer-string) "Hello\n\n\nhttp://example.invalid/\n")) (should (equal (get-text-property (point-min) 'face) '(shr-text shr-h1))) ;; Check that the DOM includes the `base'. (should (equal (pcase (plist-get eww-data :dom) (`(base ((href . ,url)) ,_) url)) "http://example.invalid/"))))) (ert-deftest eww-test/tag/textarea/starts-on-new-line () "Test that inserting a newline before a " "")))) (eww "example.invalid") ;; Make sure the text before the " "
this is the end
" "")))) (eww "example.invalid") ;; Make sure that text after the