/usr/lib/python2.7/dist-packages/unity_voice/dbus_interface/tests/test_voice.py is in unity-voice-autopilot 0.1+14.04.20140304-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
# Copyright 2013 Canonical
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
from autopilot.testcase import AutopilotTestCase
from os.path import abspath, dirname, join, isdir
from autopilot.matchers import Eventually
from testtools.matchers import Equals
import dbus
import dbus.service
import dbus.mainloop.glib
import gobject
import glob
import subprocess
import sys
from unity_voice import DBusTestCase
DBUS_NAME = "com.canonical.Unity.Voice"
DBUS_PATH = "/com/canonical/Unity/Voice"
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
class DBusInterfaceTests(AutopilotTestCase, DBusTestCase):
@classmethod
def setUpClass(klass):
klass.start_session_bus()
klass.dbus_con = klass.get_dbus()
def setUp(self):
super(DBusInterfaceTests, self).setUp()
self.sound_device = "/tmp/unity-voice-test.source"
args = ["pactl", "load-module",
"module-pipe-source",
"source_name=unity-voice-test",
"file=/tmp/unity-voice-test.source",
"channels=1",
"format=s16le",
"rate=16000"]
self.module_id = subprocess.check_output(args).strip()
self.mainloop = gobject.MainLoop()
self.app = self.launch_application()
self.wait_for_bus_object(DBUS_NAME, DBUS_PATH)
self.interface = dbus.Interface(self.dbus_con
.get_object(DBUS_NAME, DBUS_PATH), DBUS_NAME)
self.result = ""
self.sound_file = ""
# First try the in-source path
self.sound_dir = abspath(join(dirname(__file__), '..', '..', '..', '..', 'data', 'sounds'))
# Now try the install path
if not isdir(self.sound_dir):
self.sound_dir = join(dirname(sys.modules['unity_voice'].__file__), 'sounds')
def application_binary(self):
cmds = glob.glob('/usr/lib/*/unity-voice-service')
self.assertThat(len(cmds), Equals(1))
return cmds[0]
def launch_application(self):
return subprocess.Popen([self.application_binary(), "unity-voice-test"])
def tearDown(self):
super(DBusInterfaceTests, self).tearDown()
args = ["pactl", "unload-module", self.module_id]
subprocess.check_call(args)
self.mainloop.quit()
self.app.terminate()
def get_result(self):
return self.result
def listen(self, commands, sound_file, expected_text):
self.sound_file = sound_file
self.interface.connect_to_signal("Listening", self.play_sound)
self.interface.listen(commands, reply_handler=self.listen_result,
error_handler=self.listen_error)
self.mainloop.run()
self.assertThat(self.get_result, Eventually(Equals(expected_text)))
def copy_file(self, src, dst):
with open(src, 'r') as fp:
content = fp.read()
with open(dst, 'w') as fp:
fp.write(content)
def play_sound(self):
# Note: Can't use shutil.copyfile, as that doesn't work on block devices
self.copy_file(join(self.sound_dir, self.sound_file) + ".raw", self.sound_device)
self.copy_file(join(self.sound_dir, "silence.raw"), self.sound_device)
def listen_result(self, result):
self.result = result
self.mainloop.quit()
def listen_error(self, result):
self.mainloop.quit()
self.fail()
def test_sounds(self):
commands = [["auto", "adjust"], ["color", "balance"], ["open", "tab"],
["open", "terminal"], ["system", "settings"]]
self.listen(commands, "auto-adjust", "auto adjust");
self.listen(commands, "color-balance", "color balance");
self.listen(commands, "open-tab", "open tab");
self.listen(commands, "open-terminal", "open terminal");
self.listen(commands, "system-settings", "system settings");
|