Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / test / automated / dbus-tests.el
blobc9d9c72ec1d480e78d57a22512d3014b8e23f076
1 ;;; dbus-tests.el --- Tests of D-Bus integration into Emacs
3 ;; Copyright (C) 2013-2014 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
7 ;; This program is free software: you can redistribute it and/or
8 ;; modify it under the terms of the GNU General Public License as
9 ;; published by the Free Software Foundation, either version 3 of the
10 ;; License, or (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see `http://www.gnu.org/licenses/'.
20 ;;; Code:
22 (require 'ert)
23 (require 'dbus)
25 (setq dbus-debug nil)
27 (defvar dbus--test-enabled-session-bus
28 (and (featurep 'dbusbind)
29 (dbus-ignore-errors (dbus-get-unique-name :session)))
30 "Check, whether we are registered at the session bus.")
32 (defvar dbus--test-enabled-system-bus
33 (and (featurep 'dbusbind)
34 (dbus-ignore-errors (dbus-get-unique-name :system)))
35 "Check, whether we are registered at the system bus.")
37 (defun dbus--test-availability (bus)
38 "Test availability of D-Bus BUS."
39 (should (dbus-list-names bus))
40 (should (dbus-list-activatable-names bus))
41 (should (dbus-list-known-names bus))
42 (should (dbus-get-unique-name bus)))
44 (ert-deftest dbus-test00-availability-session ()
45 "Test availability of D-Bus `:session'."
46 :expected-result (if dbus--test-enabled-session-bus :passed :failed)
47 (dbus--test-availability :session))
49 (ert-deftest dbus-test00-availability-system ()
50 "Test availability of D-Bus `:system'."
51 :expected-result (if dbus--test-enabled-system-bus :passed :failed)
52 (dbus--test-availability :system))
54 (ert-deftest dbus-test01-type-conversion ()
55 "Check type conversion functions."
56 (let ((ustr "0123abc_xyz\x01\xff")
57 (mstr "Grüß Göttin"))
58 (should
59 (string-equal
60 (dbus-byte-array-to-string (dbus-string-to-byte-array "")) ""))
61 (should
62 (string-equal
63 (dbus-byte-array-to-string (dbus-string-to-byte-array ustr)) ustr))
64 (should
65 (string-equal
66 (dbus-byte-array-to-string (dbus-string-to-byte-array mstr) 'multibyte)
67 mstr))
68 ;; Should not work for multibyte strings.
69 (should-not
70 (string-equal
71 (dbus-byte-array-to-string (dbus-string-to-byte-array mstr)) mstr))
73 (should
74 (string-equal
75 (dbus-unescape-from-identifier (dbus-escape-as-identifier "")) ""))
76 (should
77 (string-equal
78 (dbus-unescape-from-identifier (dbus-escape-as-identifier ustr)) ustr))
79 ;; Should not work for multibyte strings.
80 (should-not
81 (string-equal
82 (dbus-unescape-from-identifier (dbus-escape-as-identifier mstr)) mstr))))
84 (defun dbus--test-register-service (bus)
85 "Check service registration at BUS."
86 ;; Cleanup.
87 (dbus-ignore-errors (dbus-unregister-service bus dbus-service-emacs))
89 ;; Register an own service.
90 (should (eq (dbus-register-service bus dbus-service-emacs) :primary-owner))
91 (should (dbus-ping bus dbus-service-emacs 100))
92 (should (eq (dbus-register-service bus dbus-service-emacs) :already-owner))
93 (should (dbus-ping bus dbus-service-emacs 100))
95 ;; Unregister the service.
96 (should (eq (dbus-unregister-service bus dbus-service-emacs) :released))
97 (should-not (dbus-ping bus dbus-service-emacs 100))
98 (should (eq (dbus-unregister-service bus dbus-service-emacs) :non-existent))
99 (should-not (dbus-ping bus dbus-service-emacs 100))
101 ;; `dbus-service-dbus' is reserved for the BUS itself.
102 (should-error (dbus-register-service bus dbus-service-dbus))
103 (should-error (dbus-unregister-service bus dbus-service-dbus)))
105 (ert-deftest dbus-test02-register-service-session ()
106 "Check service registration at `:session'."
107 (skip-unless (and dbus--test-enabled-session-bus
108 (dbus-register-service :session dbus-service-emacs)))
109 (dbus--test-register-service :session)
111 (let ((service "org.freedesktop.Notifications"))
112 (when (dbus-ping :session service 100)
113 ;; Cleanup.
114 (dbus-ignore-errors (dbus-unregister-service :session service))
116 (should (eq (dbus-register-service :session service) :in-queue))
117 (should (eq (dbus-unregister-service :session service) :released))
119 (should
120 (eq (dbus-register-service :session service :do-not-queue) :exists))
121 (should (eq (dbus-unregister-service :session service) :not-owner)))))
123 (ert-deftest dbus-test02-register-service-system ()
124 "Check service registration at `:system'."
125 (skip-unless (and dbus--test-enabled-system-bus
126 (dbus-register-service :system dbus-service-emacs)))
127 (dbus--test-register-service :system))
129 (defun dbus-test-all (&optional interactive)
130 "Run all tests for \\[dbus]."
131 (interactive "p")
132 (funcall
133 (if interactive 'ert-run-tests-interactively 'ert-run-tests-batch) "^dbus"))
135 (provide 'dbus-tests)
136 ;;; dbus-tests.el ends here