diff options
author | Ted Gould <ted@gould.cx> | 2015-01-28 16:43:18 -0600 |
---|---|---|
committer | Ted Gould <ted@gould.cx> | 2015-01-28 16:43:18 -0600 |
commit | af63886e47b8262189d54712dbb34383ecf83669 (patch) | |
tree | 4be682091795d3477cc4792d8a4d6b618f0b827c | |
parent | 8210778317cd8f8be803f4afa0200bd9cad0f68e (diff) | |
parent | 8fc91cb9823afc3b4dcaa03422c3dc4611ab034f (diff) | |
download | ayatana-indicator-sound-af63886e47b8262189d54712dbb34383ecf83669.tar.gz ayatana-indicator-sound-af63886e47b8262189d54712dbb34383ecf83669.tar.bz2 ayatana-indicator-sound-af63886e47b8262189d54712dbb34383ecf83669.zip |
Bring in the PA Mock
-rw-r--r-- | data/com.canonical.indicator.sound.gschema.xml | 9 | ||||
-rw-r--r-- | debian/changelog | 14 | ||||
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/service.vala | 165 | ||||
-rw-r--r-- | src/sound-menu.vala | 41 | ||||
-rw-r--r-- | src/volume-control.vala | 107 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 26 | ||||
-rw-r--r-- | tests/manual | 99 | ||||
-rw-r--r-- | tests/pa-mock.cpp | 569 | ||||
-rw-r--r-- | tests/volume-control-test.cc | 83 |
10 files changed, 1017 insertions, 98 deletions
diff --git a/data/com.canonical.indicator.sound.gschema.xml b/data/com.canonical.indicator.sound.gschema.xml index 102a1db..a346c0d 100644 --- a/data/com.canonical.indicator.sound.gschema.xml +++ b/data/com.canonical.indicator.sound.gschema.xml @@ -32,15 +32,6 @@ On start up volume should not be muted. </description> </key> - <key name="show-notify-osd-on-scroll" type="b"> - <default>true</default> - <summary>Initial setting for showing notify-osd notification on scroll volume-change</summary> - <description> - When using the mouse scroll-wheel over the indicator-sound icon, the volume changes. - Enabling this setting, every scroll volume-change a notify-osd bubble with the - updated volume value will be shown (if supported by your notification daemon). - </description> - </key> <key name="visible" type="b"> <default>true</default> <summary>Whether or not to show the sound indicator in the menu bar.</summary> diff --git a/debian/changelog b/debian/changelog index 483c78b..93626ad 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,17 @@ +indicator-sound (12.10.2+15.04.20141105-0ubuntu1) vivid; urgency=medium + + [ Ted Gould ] + * Remove various Vala warnings + * Show notifications on volume change (LP: #1378564, #1378961) + * Warn on high audio levels when using headphones (LP: #1232633, #1373404) + * service.vala: don't call set_volume unnecessarily (LP: #1381871) + * Integration test for audio roles + * Integration test for silent mode + * Ensure the greeter menu matches whether song metadata should be shown, + and update the metadata based on the new setting. (LP: #1358340) + + -- Ubuntu daily release <ps-jenkins@lists.canonical.com> Wed, 05 Nov 2014 17:56:29 +0000 + indicator-sound (12.10.2+14.10.20141010-0ubuntu1) utopic; urgency=low [ Ricardo Salveti de Araujo ] diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c18726c..b6f006a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -163,7 +163,6 @@ add_library( target_link_libraries( indicator-sound-service-lib - ${PULSEAUDIO_LIBRARIES} ${SOUNDSERVICE_LIBRARIES} ) @@ -187,6 +186,7 @@ set_target_properties( target_link_libraries( indicator-sound-service-bin indicator-sound-service-lib + ${PULSEAUDIO_LIBRARIES} ) ########################### diff --git a/src/service.vala b/src/service.vala index 2a65492..c7e29e7 100644 --- a/src/service.vala +++ b/src/service.vala @@ -19,6 +19,8 @@ public class IndicatorSound.Service: Object { public Service (MediaPlayerList playerlist) { + sync_notification = new Notify.Notification(_("Volume"), "", "audio-volume-muted"); + this.settings = new Settings ("com.canonical.indicator.sound"); this.sharedsettings = new Settings ("com.ubuntu.sound"); @@ -49,6 +51,7 @@ public class IndicatorSound.Service: Object { this.actions.add_action (this.create_mute_action ()); this.actions.add_action (this.create_volume_action ()); this.actions.add_action (this.create_mic_volume_action ()); + this.actions.add_action (this.create_high_volume_action ()); this.menus = new HashTable<string, SoundMenu> (str_hash, str_equal); this.menus.insert ("desktop_greeter", new SoundMenu (null, SoundMenu.DisplayFlags.SHOW_MUTE | SoundMenu.DisplayFlags.HIDE_PLAYERS | SoundMenu.DisplayFlags.GREETER_PLAYERS)); @@ -60,20 +63,28 @@ public class IndicatorSound.Service: Object { this.volume_control.bind_property ("active-mic", menu, "show-mic-volume", BindingFlags.SYNC_CREATE); }); + this.menus.@foreach ( (profile, menu) => { + this.volume_control.bind_property ("high-volume", menu, "show-high-volume-warning", BindingFlags.SYNC_CREATE); + }); + this.sync_preferred_players (); this.settings.changed["interested-media-players"].connect ( () => { this.sync_preferred_players (); }); - if (settings.get_boolean ("show-notify-osd-on-scroll")) { - List<string> caps = Notify.get_server_caps (); - if (caps.find_custom ("x-canonical-private-synchronous", strcmp) != null) { - this.notification = new Notify.Notification ("indicator-sound", "", ""); - this.notification.set_hint_string ("x-canonical-private-synchronous", "indicator-sound"); - } - } - sharedsettings.bind ("allow-amplified-volume", this, "allow-amplified-volume", SettingsBindFlags.GET); + + /* Hide the notification when the menu is shown */ + var shown_action = actions.lookup_action ("indicator-shown") as SimpleAction; + shown_action.change_state.connect ((state) => { + if (state.get_boolean()) { + try { + sync_notification.close(); + } catch (Error e) { + warning("Unable to close synchronous volume notification: %s", e.message); + } + } + }); } ~Service() { @@ -126,6 +137,9 @@ public class IndicatorSound.Service: Object { } set { + if (this.allow_amplified_volume == value) + return; + if (value) { /* from pulse/volume.h: #define PA_VOLUME_UI_MAX (pa_sw_volume_from_dB(+11.0)) */ this.max_volume = (double)PulseAudio.Volume.sw_from_dB(11.0) / PulseAudio.Volume.NORM; @@ -135,7 +149,7 @@ public class IndicatorSound.Service: Object { } /* Normalize volume, because the volume action's state is [0.0, 1.0], see create_volume_action() */ - this.actions.change_action_state ("volume", this.volume_control.get_volume () / this.max_volume); + this.actions.change_action_state ("volume", this.volume_control.volume / this.max_volume); } } @@ -144,6 +158,7 @@ public class IndicatorSound.Service: Object { { "scroll", activate_scroll_action, "i", null, null }, { "desktop-settings", activate_desktop_settings, null, null, null }, { "phone-settings", activate_phone_settings, null, null, null }, + { "indicator-shown", null, null, "@b false", null }, }; MainLoop loop; @@ -156,10 +171,10 @@ public class IndicatorSound.Service: Object { uint player_action_update_id; bool mute_blocks_sound; uint sound_was_blocked_timeout_id; - Notify.Notification notification; bool syncing_preferred_players = false; AccountsServiceUser? accounts_service = null; bool export_to_accounts_service = false; + private Notify.Notification sync_notification; /* Maximum volume as a scaling factor between the volume action's state and the value in * this.volume_control. See create_volume_action(). @@ -171,29 +186,8 @@ public class IndicatorSound.Service: Object { void activate_scroll_action (SimpleAction action, Variant? param) { int delta = param.get_int32(); /* positive for up, negative for down */ - double v = this.volume_control.get_volume () + volume_step_percentage * delta; - this.volume_control.set_volume (v.clamp (0.0, this.max_volume)); - - if (this.notification != null) { - string icon; - if (v <= 0.0) - icon = "notification-audio-volume-off"; - else if (v <= 0.3) - icon = "notification-audio-volume-low"; - else if (v <= 0.7) - icon = "notification-audio-volume-medium"; - else - icon = "notification-audio-volume-high"; - - this.notification.update ("indicator-sound", "", icon); - this.notification.set_hint_int32 ("value", ((int32) (100 * v / this.max_volume)).clamp (-1, 101)); - try { - this.notification.show (); - } - catch (Error e) { - warning ("unable to show notification: %s", e.message); - } - } + double v = this.volume_control.volume + volume_step_percentage * delta; + this.volume_control.volume = v.clamp (0.0, this.max_volume); } void activate_desktop_settings (SimpleAction action, Variant? param) { @@ -228,7 +222,7 @@ public class IndicatorSound.Service: Object { } void update_root_icon () { - double volume = this.volume_control.get_volume (); + double volume = this.volume_control.volume; string icon; if (this.volume_control.mute) icon = this.mute_blocks_sound ? "audio-volume-muted-blocking-panel" : "audio-volume-muted-panel"; @@ -258,6 +252,63 @@ public class IndicatorSound.Service: Object { root_action.set_state (builder.end()); } + /* TODO: Update these if the notification server leaves the bus and restarts */ + private bool check_sync_notification = false; + private bool support_sync_notification = false; + + void update_sync_notification () { + if (!check_sync_notification) { + List<string> caps = Notify.get_server_caps (); + if (caps.find_custom ("x-canonical-private-synchronous", strcmp) != null) { + support_sync_notification = true; + } + check_sync_notification = true; + } + + if (!support_sync_notification) + return; + + var shown_action = actions.lookup_action ("indicator-shown") as SimpleAction; + if (shown_action != null && shown_action.get_state().get_boolean()) + return; + + /* Determine Label */ + string volume_label = ""; + if (volume_control.high_volume) + volume_label = _("High volume"); + + /* Choose an icon */ + string icon = "audio-volume-muted"; + if (volume_control.volume <= 0.0) + icon = "audio-volume-muted"; + else if (volume_control.volume <= 0.3) + icon = "audio-volume-low"; + else if (volume_control.volume <= 0.7) + icon = "audio-volume-medium"; + else + icon = "audio-volume-high"; + + /* Check tint */ + string tint = "false"; + if (volume_control.high_volume) + tint = "true"; + + /* Put it all into the notification */ + sync_notification.clear_hints (); + sync_notification.update (_("Volume"), volume_label, icon); + sync_notification.set_hint ("value", (int32)(volume_control.volume * 100.0)); + sync_notification.set_hint ("x-canonical-value-bar-tint", tint); + sync_notification.set_hint ("x-canonical-private-synchronous", "true"); + sync_notification.set_hint ("x-canonical-non-shaped-icon", "true"); + + /* Show it */ + try { + sync_notification.show (); + } catch (GLib.Error e) { + warning("Unable to send volume change notification: %s", e.message); + } + } + Action create_silent_mode_action () { bool silentNow = false; if (this.accounts_service != null) { @@ -332,15 +383,6 @@ public class IndicatorSound.Service: Object { return mute_action; } - void volume_changed (double volume) { - var volume_action = this.actions.lookup_action ("volume") as SimpleAction; - - /* Normalize volume, because the volume action's state is [0.0, 1.0], see create_volume_action() */ - volume_action.set_state (new Variant.double (volume / this.max_volume)); - - this.update_root_icon (); - } - Action create_volume_action () { /* The action's state is between be in [0.0, 1.0] instead of [0.0, * max_volume], so that we don't need to update the slider menu item @@ -349,23 +391,31 @@ public class IndicatorSound.Service: Object { * volume_control.set_volume(). */ - double volume = this.volume_control.get_volume () / this.max_volume; + double volume = this.volume_control.volume / this.max_volume; var volume_action = new SimpleAction.stateful ("volume", VariantType.INT32, new Variant.double (volume)); volume_action.change_state.connect ( (action, val) => { double v = val.get_double () * this.max_volume; - volume_control.set_volume (v.clamp (0.0, this.max_volume)); + volume_control.volume = v.clamp (0.0, this.max_volume); }); /* activating this action changes the volume by the amount given in the parameter */ volume_action.activate.connect ( (action, param) => { int delta = param.get_int32 (); - double v = volume_control.get_volume () + volume_step_percentage * delta; - volume_control.set_volume (v.clamp (0.0, this.max_volume)); + double v = volume_control.volume + volume_step_percentage * delta; + volume_control.volume = v.clamp (0.0, this.max_volume); }); - this.volume_control.volume_changed.connect (volume_changed); + this.volume_control.notify["volume"].connect (() => { + var vol_action = this.actions.lookup_action ("volume") as SimpleAction; + + /* Normalize volume, because the volume action's state is [0.0, 1.0], see create_volume_action() */ + vol_action.set_state (new Variant.double (this.volume_control.volume / this.max_volume)); + + this.update_root_icon (); + this.update_sync_notification (); + }); this.volume_control.bind_property ("ready", volume_action, "enabled", BindingFlags.SYNC_CREATE); @@ -373,14 +423,14 @@ public class IndicatorSound.Service: Object { } Action create_mic_volume_action () { - var volume_action = new SimpleAction.stateful ("mic-volume", null, new Variant.double (this.volume_control.get_mic_volume ())); + var volume_action = new SimpleAction.stateful ("mic-volume", null, new Variant.double (this.volume_control.mic_volume)); volume_action.change_state.connect ( (action, val) => { - volume_control.set_mic_volume (val.get_double ()); + volume_control.mic_volume = val.get_double (); }); - this.volume_control.mic_volume_changed.connect ( (volume) => { - volume_action.set_state (new Variant.double (volume)); + this.volume_control.notify["mic-volume"].connect ( () => { + volume_action.set_state (new Variant.double (this.volume_control.mic_volume)); }); this.volume_control.bind_property ("ready", volume_action, "enabled", BindingFlags.SYNC_CREATE); @@ -388,6 +438,17 @@ public class IndicatorSound.Service: Object { return volume_action; } + Action create_high_volume_action () { + var high_volume_action = new SimpleAction.stateful("high-volume", null, new Variant.boolean (this.volume_control.high_volume)); + + this.volume_control.notify["high-volume"].connect( () => { + high_volume_action.set_state(new Variant.boolean (this.volume_control.high_volume)); + update_sync_notification(); + }); + + return high_volume_action; + } + void bus_acquired (DBusConnection connection, string name) { try { connection.export_action_group ("/com/canonical/indicator/sound", this.actions); diff --git a/src/sound-menu.vala b/src/sound-menu.vala index f245a1f..96dd143 100644 --- a/src/sound-menu.vala +++ b/src/sound-menu.vala @@ -60,6 +60,7 @@ public class SoundMenu: Object root_item.set_attribute ("x-canonical-type", "s", "com.canonical.indicator.root"); root_item.set_attribute ("x-canonical-scroll-action", "s", "indicator.scroll"); root_item.set_attribute ("x-canonical-secondary-action", "s", "indicator.mute"); + root_item.set_attribute ("submenu-action", "s", "indicator.indicator-shown"); root_item.set_submenu (this.menu); this.root = new Menu (); @@ -93,12 +94,49 @@ public class SoundMenu: Object this.mic_volume_shown = true; } else if (!value && this.mic_volume_shown) { - this.volume_section.remove (this.volume_section.get_n_items () -1); + int location = -1; + while ((location = find_action(this.volume_section, "indicator.mic-volume")) != -1) { + this.volume_section.remove (location); + } this.mic_volume_shown = false; } } } + public bool show_high_volume_warning { + get { + return this.high_volume_warning_shown; + } + set { + if (value && !this.high_volume_warning_shown) { + /* NOTE: Action doesn't really exist, just used to find below when removing */ + var item = new MenuItem(_("High volume can damage your hearing."), "indicator.high-volume-warning-item"); + volume_section.append_item (item); + this.high_volume_warning_shown = true; + } + else if (!value && this.high_volume_warning_shown) { + int location = -1; + while ((location = find_action(this.volume_section, "indicator.high-volume-warning-item")) != -1) { + this.volume_section.remove (location); + } + this.high_volume_warning_shown = false; + } + } + } + + int find_action (Menu menu, string in_action) { + int n = menu.get_n_items (); + for (int i = 0; i < n; i++) { + string action; + menu.get_item_attribute (i, "action", "s", out action); + if (in_action == action) + return i; + } + + return -1; + } + + public void add_player (MediaPlayer player) { if (this.notify_handlers.contains (player)) return; @@ -141,6 +179,7 @@ public class SoundMenu: Object Menu volume_section; bool mic_volume_shown; bool settings_shown = false; + bool high_volume_warning_shown = false; bool hide_inactive; bool hide_players = false; HashTable<MediaPlayer, ulong> notify_handlers; diff --git a/src/volume-control.vala b/src/volume-control.vala index ad186a7..193e621 100644 --- a/src/volume-control.vala +++ b/src/volume-control.vala @@ -19,6 +19,7 @@ */ using PulseAudio; +using Notify; using Gee; [CCode(cname="pa_cvolume_set", cheader_filename = "pulse/volume.h")] @@ -67,9 +68,7 @@ public class VolumeControl : Object private uint _accountservice_volume_timer = 0; private bool _send_next_local_volume = false; private double _account_service_volume = 0.0; - - public signal void volume_changed (double v); - public signal void mic_volume_changed (double v); + private bool _active_port_headphone = false; /** true when connected to the pulse server */ public bool ready { get; set; } @@ -77,6 +76,13 @@ public class VolumeControl : Object /** true when a microphone is active **/ public bool active_mic { get; private set; default = false; } + /** true when high volume warnings should be shown */ + public bool high_volume { + get { + return this._volume > 0.75 && _active_port_headphone; + } + } + public VolumeControl () { if (loop == null) @@ -84,6 +90,7 @@ public class VolumeControl : Object _mute_cancellable = new Cancellable (); _volume_cancellable = new Cancellable (); + setup_accountsservice.begin (); this.reconnect_to_pulse (); @@ -149,6 +156,8 @@ public class VolumeControl : Object private void sink_info_cb_for_props (Context c, SinkInfo? i, int eol) { + bool old_high_volume = this.high_volume; + if (i == null) return; @@ -165,12 +174,29 @@ public class VolumeControl : Object this.notify_property ("is-playing"); } + /* Check if the current active port is headset/headphone */ + /* There is not easy way to check if the port is a headset/headphone besides + * checking for the port name. On touch (with the pulseaudio droid element) + * the headset/headphone port is called 'output-headset' and 'output-headphone'. + * On the desktop this is usually called 'analog-output-headphones' */ + if (i.active_port.name == "output-wired_headset" || + i.active_port.name == "output-wired_headphone" || + i.active_port.name == "analog-output-headphones") { + _active_port_headphone = true; + } else { + _active_port_headphone = false; + } + if (_pulse_use_stream_restore == false && _volume != volume_to_double (i.volume.max ())) { _volume = volume_to_double (i.volume.max ()); - volume_changed (_volume); + this.notify_property("volume"); start_local_volume_timer(); + } + + if (this.high_volume != old_high_volume) { + this.notify_property("high-volume"); } } @@ -182,7 +208,7 @@ public class VolumeControl : Object if (_mic_volume != volume_to_double (i.volume.values[0])) { _mic_volume = volume_to_double (i.volume.values[0]); - mic_volume_changed (_mic_volume); + this.notify_property ("mic-volume"); } } @@ -238,7 +264,7 @@ public class VolumeControl : Object if (volume != cvolume) { /* Someone else changed the volume for this role, reflect on the indicator */ _volume = volume_to_double (volume); - volume_changed (_volume); + this.notify_property("volume"); start_local_volume_timer(); } } @@ -281,7 +307,7 @@ public class VolumeControl : Object iter.next ("(uu)", &type, &volume); _volume = volume_to_double (volume); - volume_changed (_volume); + this.notify_property("volume"); start_local_volume_timer(); } catch (GLib.Error e) { warning ("unable to get volume for active role %s (%s)", sink_input_objp, e.message); @@ -498,7 +524,7 @@ public class VolumeControl : Object private void set_volume_success_cb (Context c, int success) { if ((bool)success) - volume_changed (_volume); + this.notify_property("volume"); } private void sink_info_set_volume_cb (Context c, SinkInfo? i, int eol) @@ -544,7 +570,7 @@ public class VolumeControl : Object new Variant ("(ssv)", "org.PulseAudio.Ext.StreamRestore1.RestoreEntry", "Volume", volume), null, DBusCallFlags.NONE, -1); - volume_changed (_volume); + this.notify_property("volume"); } catch (GLib.Error e) { lock (_pa_volume_sig_count) { _pa_volume_sig_count--; @@ -559,27 +585,29 @@ public class VolumeControl : Object return false; if (_volume != volume) { + var old_high_volume = this.high_volume; + _volume = volume; if (_pulse_use_stream_restore) set_volume_active_role.begin (); else context.get_server_info (server_info_cb_for_set_volume); + + this.notify_property("volume"); + + if (this.high_volume != old_high_volume) + this.notify_property("high-volume"); + return true; } else { return false; } } - public void set_volume (double volume) - { - if (set_volume_internal (volume)) - start_local_volume_timer(); - } - void set_mic_volume_success_cb (Context c, int success) { if ((bool)success) - mic_volume_changed (_mic_volume); + this.notify_property ("mic-volume"); } void set_mic_volume_get_server_info_cb (PulseAudio.Context c, PulseAudio.ServerInfo? i) { @@ -590,23 +618,28 @@ public class VolumeControl : Object } } - public void set_mic_volume (double volume) - { - return_if_fail (context.get_state () == Context.State.READY); - - _mic_volume = volume; - - context.get_server_info (set_mic_volume_get_server_info_cb); + public double volume { + get { + return _volume; + } + set { + if (set_volume_internal (value)) { + start_local_volume_timer(); + } + } } - public double get_volume () - { - return _volume; - } + public double mic_volume { + get { + return _mic_volume; + } + set { + return_if_fail (context.get_state () == Context.State.READY); - public double get_mic_volume () - { - return _mic_volume; + _mic_volume = value; + + context.get_server_info (set_mic_volume_get_server_info_cb); + } } /* PulseAudio Dbus (Stream Restore) logic */ @@ -689,7 +722,7 @@ public class VolumeControl : Object } /* AccountsService operations */ - private void accountsservice_props_changed_cb (DBusProxy proxy, Variant changed_properties, string[] invalidated_properties) + private void accountsservice_props_changed_cb (DBusProxy proxy, Variant changed_properties, string[]? invalidated_properties) { Variant volume_variant = changed_properties.lookup_value ("Volume", new VariantType ("d")); if (volume_variant != null) { @@ -747,10 +780,14 @@ public class VolumeControl : Object // Get current values and listen for changes _user_proxy.g_properties_changed.connect (accountsservice_props_changed_cb); - var props_variant = yield _user_proxy.get_connection ().call (_user_proxy.get_name (), _user_proxy.get_object_path (), "org.freedesktop.DBus.Properties", "GetAll", new Variant ("(s)", _user_proxy.get_interface_name ()), null, DBusCallFlags.NONE, -1); - Variant props; - props_variant.get ("(@a{sv})", out props); - accountsservice_props_changed_cb(_user_proxy, props, null); + try { + var props_variant = yield _user_proxy.get_connection ().call (_user_proxy.get_name (), _user_proxy.get_object_path (), "org.freedesktop.DBus.Properties", "GetAll", new Variant ("(s)", _user_proxy.get_interface_name ()), null, DBusCallFlags.NONE, -1); + Variant props; + props_variant.get ("(@a{sv})", out props); + accountsservice_props_changed_cb(_user_proxy, props, null); + } catch (GLib.Error e) { + debug("Unable to get properties for user %s at first try: %s", username, e.message); + } } private void greeter_user_changed (string username) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f588d12..c1b4afc 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -83,6 +83,16 @@ target_link_libraries( include_directories(${CMAKE_CURRENT_BINARY_DIR}) ########################### +# Pulse Mock +########################### + +add_library( + pulse-mock + SHARED + pa-mock.cpp +) + +########################### # Name Watch Test ########################### @@ -116,6 +126,22 @@ add_test(accounts-service-user-test-player ) ########################### +# Volume Control +########################### + +include_directories(${CMAKE_SOURCE_DIR}/src) +add_executable (volume-control-test volume-control-test.cc) +target_link_libraries ( + volume-control-test + indicator-sound-service-lib + pulse-mock + gtest + ${TEST_LIBRARIES} +) + +add_test(volume-control-test volume-control-test) + +########################### # Sound Menu ########################### diff --git a/tests/manual b/tests/manual index 201465c..c1cc214 100644 --- a/tests/manual +++ b/tests/manual @@ -22,3 +22,102 @@ Test-case indicator-sound/unity8-items-check <dd>The menu is populated with items</dd> </dl> +Test-case indicator-sound/unity8-sound-notifications +<dl> + <dt>Adjust volume using HW keys if available</dt> + <dd>A notification bubble should appear with the sound volume</dd> + <dd>An audibule sound should play at the level of the audio</dd> + <dt>Adjust volume with slider in sound indicator</dt> + <dd>A notification bubble should appear with the sound volume</dd> + <dd>An audibule sound should play at the level of the audio</dd> + <dt>Open a video with sound and play in media player</dt> + <dd>The video should play and the sound should be audible</dd> + <dt>Adjust volume using HW keys if available</dt> + <dd>A notification bubble should appear with the sound volume</dd> + <dd>No notification sound should be heard</dd> + <dt>Adjust volume with slider in sound indicator</dt> + <dd>A notification bubble should appear with the sound volume</dd> + <dd>No notification sound should be heard</dd> +</dl> + +Test-case indicator-sound/unity8-high-volume +<dl> + <dt>Plug headphones into the headphone jack</dt> + <dt>Adjust volume so that it is at the midpoint of volume range</dt> + <dd>The slider should be in the middle of the scale</dd> + <dt>Increase the volume once using HW keys if available</dt> + <dd>A notification bubble should appear with the sound volume</dd> + <dd>There should be no text on the notification</dd> + <dt>Increase the volume using HW keys until it is roughly 90% of the range</dt> + <dd>A notification bubble should appear with the sound volume</dd> + <dd>The text on the notification should read "High volume"</dd> + <dd>The range on the notification bubble should have a different color signifying the higher volume</dd> + <dt>Decrease the volume using HW keys until it is roughly 50% of the range</dt> + <dd>A notification bubble should appear with the sound volume</dd> + <dd>There should be no text on the notification</dd> + <dd>The range on the notification bubble should have a standard color</dd> +</dl> + +Test-case indicator-sound/unity8-silent-mode +<dl> + <dt>NOTE: This test currently doesn't work because of a bug: http://pad.lv/1336715</dt> + <dt>Open the Sound menu</dt> + <dd>The sound menu includes an item "Silent Mode" which is a check box</dd> + <dd>The checkbox is not checked</dd> + <dt>Enable silent mode</dt> + <dd>Selecting the "Silent Mode" item should cause the box to be checked</dd> + <dt>Open the sound panel in system settings</dt> + <dd>The sound panel includes an item "Silent Mode" which is a check box</dd> + <dd>The checkbox is checked</dd> + <dt>Disable silent mode in system settings</dt> + <dd>The checkbox is not checked</dd> + <dt>Open the Sound menu</dt> + <dd>The sound menu includes an item "Silent Mode" which is a check box</dd> + <dd>The checkbox is not checked</dd> +</dl> + +Test-case indicator-sound/unity8-audio-roles +<dl> + <dt>Without playing anything (no active audio stream), change the volume on the indicator or with the volume buttons and then try playing one of the following audio streams: camera shutter, ringtone, message notification, dtmf</dt> + <dd>The audio stream should reflect the volume set on the indicator</dd> + <dt>Without playing anything (no active audio stream), change the volume on the indicator or with volume buttons and then try playing one of the following audio streams: music-app, webrowser (youtube)</dt> + <dd>The audio stream should not be affected by the volume set on the indicator when there was no other active stream</dt> + <dt>Play a multimedia stream (music-app, webrowser) and change the volume on the indicator when the stream is active</dt> + <dd>The multimedia audio stream should reflect the volume set on the indicator</dd> + <dd>When stopping/closing the multimedia stream, it should automatically show up the volume for the alert role (ringtone, notification, etc)</dd> + <dd>No other role should be affected by the volume level used by the multimedia role</dd> + <dt>Play a alarm stream (clock-app) and change the volume on the indicator when the stream is active</dt> + <dd>The alarm audio stream should reflect the volume set on the indicator</dd> + <dd>When stopping/closing the alarm stream, it should automatically show up the volume for the alert role (ringtone, notification, etc)</dd> + <dd>No other role should be affected by the volume level used by the alarm role</dd> + <dt>Start a voice call using the dialer-app and change the volume on the indicator when the call is active</dt> + <dd>The phone audio stream should reflect the volume set on the indicator</dd> + <dd>When hanging up the voice call it should automatically show up the volume for the alert role (ringtone, notification, etc)</dd> + <dd>No other role should be affected by the volume level used by the phone role</dd> +</dl> + +Test-case indicator-sound/unity8-embedded-greeter +<dl> + <dt>NOTE: Only works with embedded greeter, split greeter will require modifications to this test</dt> + <dt>Ensure System Settings is set to "Show Messages on Greeter"</dt> + <dt>Play a song in the media player</dt> + <dd>The song should be heard</dd> + <dd>There should be an entry in the sound menu with the meta data for the song being played</dd> + <dt>Go to the greeter. This can be done by hitting the lock button twice.</dt> + <dt>Ensure the sound menu has song meta data</dt> + <dd>There should be an entry in the sound menu with the meta data for the song being played</dd> + <dt>Pause the song in the greeter</dt> + <dd>The song should stop playing</dd> + <dt>Resume the song in the greeter</dt> + <dd>The song should continue to play</dd> + <dt>Disable System Settings value "Show Messages on Greeter"</dt> + <dt>Ensure the sound menu has song meta data</dt> + <dd>There should be an entry in the sound menu with the meta data for the song being played</dd> + <dt>Go to the greeter. This can be done by hitting the lock button twice.</dt> + <dt>Ensure the sound menu does not have song meta data</dt> + <dd>There should be an entry for the player but it should have no information on the song being played</dd> + <dt>Pause the song in the greeter</dt> + <dd>The song should stop playing</dd> + <dt>Resume the song in the greeter</dt> + <dd>The song should continue to play</dd> +</dl> diff --git a/tests/pa-mock.cpp b/tests/pa-mock.cpp new file mode 100644 index 0000000..8ca2374 --- /dev/null +++ b/tests/pa-mock.cpp @@ -0,0 +1,569 @@ +/* + * Copyright © 2015 Canonical Ltd. + * + * This program 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; version 3. + * + * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. + * + * Authors: + * Ted Gould <ted@canonical.com> + */ + +#include <atomic> +#include <functional> +#include <vector> + +#include <pulse/pulseaudio.h> +#include <pulse/glib-mainloop.h> +#include <gio/gio.h> +#include <math.h> + +#ifdef G_LOG_DOMAIN +#undef G_LOG_DOMAIN +#endif +#define G_LOG_DOMAIN "PA-Mock" + +/* Core class of the PA Mock state */ +class PAMockContext { +public: + /* Accounting */ + std::atomic<unsigned int> refcnt; + + /* State stuff */ + std::vector<std::function<void(void)>> stateCallbacks; + pa_context_state_t currentState; + pa_context_state_t futureState; + + /* Event stuff */ + std::vector<std::function<void(pa_subscription_event_type_t, uint32_t)>> eventCallbacks; + pa_subscription_mask_t eventMask; + + PAMockContext () + : refcnt(1) + , currentState(PA_CONTEXT_UNCONNECTED) + , futureState(PA_CONTEXT_UNCONNECTED) + , eventMask(PA_SUBSCRIPTION_MASK_NULL) + { + g_debug("Creating Context: %p", this); + } + + ~PAMockContext () { + g_debug("Destroying Context: %p", this); + } + + /* Ref counting */ + void ref () { + refcnt++; + } + + void unref () { + refcnt--; + if (refcnt == 0) + delete this; + } + + /* C/C++ boundry */ + operator pa_context *() { + return reinterpret_cast<pa_context *>(this); + } + + /* State Stuff */ + void setState (pa_context_state_t state) + { + if (state == currentState) + return; + + currentState = state; + for (auto callback : stateCallbacks) { + callback(); + } + } + + void idleOnce (std::function<void(void)> idleFunc) { + auto allocated = new std::function<void(void)>(idleFunc); + + g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, + [](gpointer data) -> gboolean { + std::function<void(void)> * pidleFunc = reinterpret_cast<std::function<void(void)> *>(data); + (*pidleFunc)(); + return G_SOURCE_REMOVE; + }, + allocated, + [](gpointer data) -> void { + std::function<void(void)> * pidleFunc = reinterpret_cast<std::function<void(void)> *>(data); + delete pidleFunc; + }); + } + + void queueState (pa_context_state_t state) + { + idleOnce([this, state](){ + setState(state); + }); + } + + pa_context_state_t getState (void) + { + return currentState; + } + + void addStateCallback (std::function<void(void)> &callback) + { + stateCallbacks.push_back(callback); + } + + /* Event Stuff */ + void setMask (pa_subscription_mask_t mask) + { + eventMask = mask; + } + + void addEventCallback (std::function<void(pa_subscription_event_type_t, uint32_t)> &callback) + { + eventCallbacks.push_back(callback); + } +}; + +/* ******************************* + * context.h + * *******************************/ + +pa_context * +pa_context_new_with_proplist (pa_mainloop_api *mainloop, const char *name, pa_proplist *proplist) +{ + return *(new PAMockContext()); +} + +void +pa_context_unref (pa_context *c) { + reinterpret_cast<PAMockContext*>(c)->unref(); +} + +pa_context * +pa_context_ref (pa_context *c) { + reinterpret_cast<PAMockContext*>(c)->ref(); + return c; +} + +int +pa_context_connect (pa_context *c, const char *server, pa_context_flags_t flags, const pa_spawn_api *api) +{ + reinterpret_cast<PAMockContext*>(c)->queueState(PA_CONTEXT_READY); + return 0; +} + +void +pa_context_disconnect (pa_context *c) +{ + reinterpret_cast<PAMockContext*>(c)->queueState(PA_CONTEXT_UNCONNECTED); +} + +int +pa_context_errno (pa_context *c) +{ + return 0; +} + +void +pa_context_set_state_callback (pa_context *c, pa_context_notify_cb_t cb, void *userdata) +{ + std::function<void(void)> cppcb([c, cb, userdata]() { + cb(c, userdata); + }); + reinterpret_cast<PAMockContext*>(c)->addStateCallback(cppcb); +} + +pa_context_state_t +pa_context_get_state (pa_context *c) +{ + return reinterpret_cast<PAMockContext*>(c)->getState(); +} + +/* ******************************* + * introspect.h + * *******************************/ + +static pa_operation * +dummy_operation (void) +{ + GObject * goper = (GObject *)g_object_new(G_TYPE_OBJECT, nullptr); + pa_operation * oper = (pa_operation *)goper; + return oper; +} + +pa_operation* +pa_context_get_server_info (pa_context *c, pa_server_info_cb_t cb, void *userdata) +{ + reinterpret_cast<PAMockContext*>(c)->idleOnce( + [c, cb, userdata]() { + if (cb == nullptr) + return; + + pa_server_info server{ + .user_name = "user", + .host_name = "host", + .server_version = "1.2.3", + .server_name = "server", + .sample_spec = { + .format = PA_SAMPLE_U8, + .rate = 44100, + .channels = 1 + }, + .default_sink_name = "default-sink", + .default_source_name = "default-source", + .cookie = 1234, + .channel_map = { + .channels = 0 + } + }; + + cb(c, &server, userdata); + }); + + return dummy_operation(); +} + +pa_operation* +pa_context_get_sink_info_by_name (pa_context *c, const gchar * name, pa_sink_info_cb_t cb, void *userdata) +{ + reinterpret_cast<PAMockContext*>(c)->idleOnce( + [c, name, cb, userdata]() { + if (cb == nullptr) + return; + + pa_sink_port_info active_port = {0}; + active_port.name = "speaker"; + + pa_sink_info sink = {0}; + sink.name = "default-sink"; + sink.index = 0; + sink.description = "Default Sink"; + sink.channel_map.channels = 0; + sink.active_port = &active_port; + + cb(c, &sink, 1, userdata); + }); + + return dummy_operation(); +} + +pa_operation* +pa_context_get_sink_info_list (pa_context *c, pa_sink_info_cb_t cb, void *userdata) +{ + /* Only have one today, so this is the same */ + return pa_context_get_sink_info_by_name(c, "default-sink", cb, userdata); +} + +pa_operation * +pa_context_get_sink_input_info (pa_context *c, uint32_t idx, pa_sink_input_info_cb_t cb, void * userdata) +{ + reinterpret_cast<PAMockContext*>(c)->idleOnce( + [c, idx, cb, userdata]() { + if (cb == nullptr) + return; + + pa_sink_input_info sink = { 0 }; + + cb(c, &sink, 1, userdata); + }); + + return dummy_operation(); +} + +pa_operation* +pa_context_get_source_info_by_name (pa_context *c, const char * name, pa_source_info_cb_t cb, void *userdata) +{ + reinterpret_cast<PAMockContext*>(c)->idleOnce( + [c, name, cb, userdata]() { + if (cb == nullptr) + return; + + pa_source_info source = { + .name = "default-source" + }; + + cb(c, &source, 1, userdata); + }); + + return dummy_operation(); +} + +pa_operation* +pa_context_get_source_output_info (pa_context *c, uint32_t idx, pa_source_output_info_cb_t cb, void *userdata) +{ + reinterpret_cast<PAMockContext*>(c)->idleOnce( + [c, idx, cb, userdata]() { + if (cb == nullptr) + return; + + pa_source_output_info source = {0}; + source.name = "default source"; + + cb(c, &source, 1, userdata); + }); + + return dummy_operation(); +} + +pa_operation* +pa_context_set_sink_mute_by_index (pa_context *c, uint32_t idx, int mute, pa_context_success_cb_t cb, void *userdata) +{ + reinterpret_cast<PAMockContext*>(c)->idleOnce( + [c, idx, mute, cb, userdata]() { + if (cb != nullptr) + cb(c, 1, userdata); + }); + + return dummy_operation(); +} + +pa_operation* +pa_context_set_sink_volume_by_index (pa_context *c, uint32_t idx, const pa_cvolume * cvol, pa_context_success_cb_t cb, void *userdata) +{ + reinterpret_cast<PAMockContext*>(c)->idleOnce( + [c, idx, cvol, cb, userdata]() { + if (cb != nullptr) + cb(c, 1, userdata); + }); + + return dummy_operation(); +} + +pa_operation* +pa_context_set_source_volume_by_name (pa_context *c, const char * name, const pa_cvolume * cvol, pa_context_success_cb_t cb, void *userdata) +{ + reinterpret_cast<PAMockContext*>(c)->idleOnce( + [c, name, cvol, cb, userdata]() { + if (cb != nullptr) + cb(c, 1, userdata); + }); + + return dummy_operation(); +} + +/* ******************************* + * subscribe.h + * *******************************/ + +pa_operation * +pa_context_subscribe (pa_context * c, pa_subscription_mask_t mask, pa_context_success_cb_t callback, void * userdata) +{ + reinterpret_cast<PAMockContext*>(c)->idleOnce( + [c, mask, callback, userdata]() { + reinterpret_cast<PAMockContext*>(c)->setMask(mask); + if (callback != nullptr) + callback(c, 1, userdata); + }); + + return dummy_operation(); +} + +void +pa_context_set_subscribe_callback (pa_context * c, pa_context_subscribe_cb_t callback, void * userdata) +{ + std::function<void(pa_subscription_event_type_t, uint32_t)> cppcb([c, callback, userdata](pa_subscription_event_type_t event, uint32_t index) { + if (callback != nullptr) + callback(c, event, index, userdata); + }); + + reinterpret_cast<PAMockContext*>(c)->addEventCallback(cppcb); +} + +/* ******************************* + * glib-mainloop.h + * *******************************/ + +struct pa_glib_mainloop { + GMainContext * context; +}; + +struct pa_mainloop_api mock_mainloop = { 0 }; + +pa_mainloop_api * +pa_glib_mainloop_get_api (pa_glib_mainloop * g) +{ + return &mock_mainloop; +} + +pa_glib_mainloop * +pa_glib_mainloop_new (GMainContext * c) +{ + pa_glib_mainloop * loop = g_new0(pa_glib_mainloop, 1); + + if (c == nullptr) + loop->context = g_main_context_default(); + else + loop->context = c; + + g_main_context_ref(loop->context); + return loop; +} + +void +pa_glib_mainloop_free (pa_glib_mainloop * g) +{ + g_main_context_unref(g->context); + g_free(g); +} + +/* ******************************* + * operation.h + * *******************************/ + +void +pa_operation_unref (pa_operation * operation) +{ + g_return_if_fail(G_IS_OBJECT(operation)); + g_object_unref(G_OBJECT(operation)); +} + +/* ******************************* + * proplist.h + * *******************************/ + +pa_proplist * +pa_proplist_new (void) +{ + return (pa_proplist *)g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); +} + +void +pa_proplist_free (pa_proplist * p) +{ + g_return_if_fail(p != nullptr); + g_hash_table_destroy((GHashTable *)p); +} + +const char * +pa_proplist_gets (pa_proplist * p, const char * key) +{ + g_return_val_if_fail(p != nullptr, nullptr); + g_return_val_if_fail(key != nullptr, nullptr); + return (const char *)g_hash_table_lookup((GHashTable *)p, key); +} + +int +pa_proplist_sets (pa_proplist *p, const char * key, const char * value) +{ + g_return_val_if_fail(p != nullptr, -1); + g_return_val_if_fail(key != nullptr, -1); + g_return_val_if_fail(value != nullptr, -1); + + g_hash_table_insert((GHashTable *)p, g_strdup(key), g_strdup(value)); + return 0; +} + +/* ******************************* + * error.h + * *******************************/ + +const char * +pa_strerror (int error) +{ + return "This is error text"; +} + +/* ******************************* + * volume.h + * *******************************/ + +pa_volume_t +pa_sw_volume_from_dB (double f) +{ + double linear = pow(10.0, f / 20.0); + + pa_volume_t calculated = lround(cbrt(linear) * PA_VOLUME_NORM); + + if (G_UNLIKELY(calculated > PA_VOLUME_MAX)) { + return PA_VOLUME_MAX; + } else if (G_UNLIKELY(calculated < PA_VOLUME_MUTED)) { + return PA_VOLUME_MUTED; + } else { + return calculated; + } +} + +pa_cvolume * +pa_cvolume_init (pa_cvolume * cvol) +{ + g_return_val_if_fail(cvol != nullptr, nullptr); + + cvol->channels = 0; + + unsigned int i; + for (i = 0; i < PA_CHANNELS_MAX; i++) + cvol->values[i] = PA_VOLUME_INVALID; + + return cvol; +} + +pa_cvolume * +pa_cvolume_set (pa_cvolume * cvol, unsigned channels, pa_volume_t volume) +{ + g_return_val_if_fail(cvol != nullptr, nullptr); + g_warn_if_fail(channels > 0); + g_return_val_if_fail(channels <= PA_CHANNELS_MAX, nullptr); + + cvol->channels = channels; + + unsigned int i; + for (i = 0; i < channels; i++) { + if (G_UNLIKELY(volume > PA_VOLUME_MAX)) { + cvol->values[i] = PA_VOLUME_MAX; + } else if (G_UNLIKELY(volume < PA_VOLUME_MUTED)) { + cvol->values[i] = PA_VOLUME_MUTED; + } else { + cvol->values[i] = volume; + } + } + + return cvol; +} + +pa_volume_t +pa_cvolume_max (const pa_cvolume * cvol) +{ + g_return_val_if_fail(cvol != nullptr, PA_VOLUME_MUTED); + pa_volume_t max = PA_VOLUME_MUTED; + + unsigned int i; + for (i = 0; i < cvol->channels; i++) + max = MAX(max, cvol->values[i]); + + return max; +} + +pa_cvolume * +pa_cvolume_scale (pa_cvolume * cvol, pa_volume_t max) +{ + g_return_val_if_fail(cvol != nullptr, nullptr); + + pa_volume_t originalmax = pa_cvolume_max(cvol); + + if (originalmax <= PA_VOLUME_MUTED) + return pa_cvolume_set(cvol, cvol->channels, max); + + unsigned int i; + for (i = 0; i < cvol->channels; i++) { + pa_volume_t calculated = (cvol->values[i] * max) / originalmax; + + if (G_UNLIKELY(calculated > PA_VOLUME_MAX)) { + cvol->values[i] = PA_VOLUME_MAX; + } else if (G_UNLIKELY(calculated < PA_VOLUME_MUTED)) { + cvol->values[i] = PA_VOLUME_MUTED; + } else { + cvol->values[i] = calculated; + } + } + + return cvol; +} + diff --git a/tests/volume-control-test.cc b/tests/volume-control-test.cc new file mode 100644 index 0000000..9970241 --- /dev/null +++ b/tests/volume-control-test.cc @@ -0,0 +1,83 @@ +/* + * Copyright © 2014 Canonical Ltd. + * + * This program 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; version 3. + * + * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. + * + * Authors: + * Ted Gould <ted@canonical.com> + */ + +#include <gtest/gtest.h> +#include <gio/gio.h> +#include <libdbustest/dbus-test.h> + +extern "C" { +#include "indicator-sound-service.h" +} + +class VolumeControlTest : public ::testing::Test +{ + + protected: + DbusTestService * service = NULL; + GDBusConnection * session = NULL; + + virtual void SetUp() { + service = dbus_test_service_new(NULL); + dbus_test_service_start_tasks(service); + + session = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL); + ASSERT_NE(nullptr, session); + g_dbus_connection_set_exit_on_close(session, FALSE); + g_object_add_weak_pointer(G_OBJECT(session), (gpointer *)&session); + } + + virtual void TearDown() { + g_clear_object(&service); + + g_object_unref(session); + + unsigned int cleartry = 0; + while (session != NULL && cleartry < 100) { + loop(100); + cleartry++; + } + + ASSERT_EQ(nullptr, session); + } + + static gboolean timeout_cb (gpointer user_data) { + GMainLoop * loop = static_cast<GMainLoop *>(user_data); + g_main_loop_quit(loop); + return G_SOURCE_REMOVE; + } + + void loop (unsigned int ms) { + GMainLoop * loop = g_main_loop_new(NULL, FALSE); + g_timeout_add(ms, timeout_cb, loop); + g_main_loop_run(loop); + g_main_loop_unref(loop); + } +}; + +TEST_F(VolumeControlTest, BasicObject) { + VolumeControl * control = volume_control_new(); + + /* Setup the PA backend */ + loop(100); + + /* Ready */ + EXPECT_TRUE(volume_control_get_ready(control)); + + g_clear_object(&control); +} |