From 4b695bacf20b7247f6b16483ede5e5d8b51dd355 Mon Sep 17 00:00:00 2001 From: charles kerr Date: Sat, 19 Dec 2015 16:01:46 -0600 Subject: move max_volume into a reusable 'options' class --- src/CMakeLists.txt | 17 +++++++++++++ src/main.c | 7 ++++-- src/service.vala | 14 +++++++---- src/volume-control-pulse.vala | 34 ++++--------------------- src/volume-control.vala | 9 +++++-- src/volume-warning.vala | 30 +++------------------- tests/CMakeLists.txt | 5 ++++ tests/notifications-test.cc | 56 ++++++++++++++++++++++++++++-------------- tests/volume-control-mock.vala | 4 ++- tests/volume-control-test.cc | 5 +++- 10 files changed, 95 insertions(+), 86 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 51f474b..4ff5646 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -45,22 +45,38 @@ vala_add(indicator-sound-service volume-control volume-control-pulse volume-warning + options + options-gsettings media-player media-player-list mpris2-interfaces accounts-service-user ) +vala_add(indicator-sound-service + options.vala +) +vala_add(indicator-sound-service + options-gsettings.vala + DEPENDS + options + volume-control + volume-control-pulse +) vala_add(indicator-sound-service volume-control.vala + DEPENDS + options ) vala_add(indicator-sound-service volume-control-pulse.vala DEPENDS + options volume-control ) vala_add(indicator-sound-service volume-warning.vala DEPENDS + options volume-control ) vala_add(indicator-sound-service @@ -110,6 +126,7 @@ vala_add(indicator-sound-service DEPENDS media-player volume-control + options ) vala_add(indicator-sound-service accounts-service-user.vala diff --git a/src/main.c b/src/main.c index c368577..01cadf1 100644 --- a/src/main.c +++ b/src/main.c @@ -46,6 +46,7 @@ on_bus_acquired(GDBusConnection *connection, gpointer user_data) { MediaPlayerList * playerlist = NULL; + IndicatorSoundOptions * options = NULL; VolumeControlPulse * volume = NULL; AccountsServiceUser * accounts = NULL; @@ -57,11 +58,13 @@ on_bus_acquired(GDBusConnection *connection, accounts = accounts_service_user_new(); } - volume = volume_control_pulse_new(); + options = indicator_sound_options_gsettings_new(); + volume = volume_control_pulse_new(options); - service = indicator_sound_service_new (playerlist, volume, accounts); + service = indicator_sound_service_new (playerlist, volume, accounts, options); g_clear_object(&playerlist); + g_clear_object(&options); g_clear_object(&volume); g_clear_object(&accounts); } diff --git a/src/service.vala b/src/service.vala index 0a7e108..3ff7ffb 100644 --- a/src/service.vala +++ b/src/service.vala @@ -27,7 +27,10 @@ public class IndicatorSound.Service: Object { */ VolumeControl.Volume _pre_warn_volume = null; - public Service (MediaPlayerList playerlist, VolumeControl volume, AccountsServiceUser? accounts) { + public Service (MediaPlayerList playerlist, VolumeControl volume, AccountsServiceUser? accounts, Options options) { + + _options = options; + try { bus = Bus.get_sync(GLib.BusType.SESSION); } catch (GLib.Error e) { @@ -205,6 +208,7 @@ public class IndicatorSound.Service: Object { bool export_to_accounts_service = false; private Notify.Notification info_notification; private Notify.Notification warn_notification; + private Options _options = null; const double volume_step_percentage = 0.06; @@ -750,10 +754,10 @@ public class IndicatorSound.Service: Object { /* return the current volume in the range of [0.0, 1.0] */ private double get_volume_percent() { - return volume_control.volume.volume / this.volume_control.max_volume; + return volume_control.volume.volume / _options.max_volume; } - /* volume control's range can vary depending on its max_volume property, + /* volume control's range can vary depending on options.max_volume, * but the action always needs to be in [0.0, 1.0]... */ private Variant create_volume_action_state() { return new Variant.double (get_volume_percent()); @@ -768,14 +772,14 @@ public class IndicatorSound.Service: Object { volume_action = new SimpleAction.stateful ("volume", VariantType.INT32, create_volume_action_state()); volume_action.change_state.connect ( (action, val) => { - double v = val.get_double () * this.volume_control.max_volume; + double v = val.get_double () * _options.max_volume; volume_control.set_volume_clamp (v, VolumeControl.VolumeReasons.USER_KEYPRESS); }); /* activating this action changes the volume by the amount given in the parameter */ volume_action.activate.connect ((a,p) => activate_scroll_action(a,p)); - this.volume_control.notify["max-volume"].connect(() => { + _options.notify["max_volume"].connect(() => { update_volume_action_state(); }); diff --git a/src/volume-control-pulse.vala b/src/volume-control-pulse.vala index 4bd3076..6c3ef17 100644 --- a/src/volume-control-pulse.vala +++ b/src/volume-control-pulse.vala @@ -46,7 +46,6 @@ public class VolumeControlPulse : VolumeControl private VolumeControl.Volume _volume = new VolumeControl.Volume(); private double _mic_volume = 0.0; private Settings _settings = new Settings ("com.canonical.indicator.sound"); - private Settings _shared_settings = new Settings ("com.ubuntu.sound"); /* Used by the pulseaudio stream restore extension */ private DBusConnection _pconn; @@ -96,8 +95,10 @@ public class VolumeControlPulse : VolumeControl /** true when a microphone is active **/ public override bool active_mic { get; private set; default = false; } - public VolumeControlPulse () + public VolumeControlPulse (IndicatorSound.Options options) { + base(options); + _volume.volume = 0.0; _volume.reason = VolumeControl.VolumeReasons.PULSE_CHANGE; @@ -116,7 +117,6 @@ public class VolumeControlPulse : VolumeControl private void init_all_properties() { - init_max_volume(); init_high_volume(); init_high_volume_approved(); } @@ -607,13 +607,13 @@ public class VolumeControlPulse : VolumeControl } /* Volume operations */ - private static PulseAudio.Volume double_to_volume (double vol) + public static PulseAudio.Volume double_to_volume (double vol) { double tmp = (double)(PulseAudio.Volume.NORM - PulseAudio.Volume.MUTED) * vol; return (PulseAudio.Volume)tmp + PulseAudio.Volume.MUTED; } - private static double volume_to_double (PulseAudio.Volume vol) + public static double volume_to_double (PulseAudio.Volume vol) { double tmp = (double)(vol - PulseAudio.Volume.MUTED); return tmp / (double)(PulseAudio.Volume.NORM - PulseAudio.Volume.MUTED); @@ -721,30 +721,6 @@ public class VolumeControlPulse : VolumeControl } } - /** MAX VOLUME PROPERTY **/ - - private void init_max_volume() { - _settings.changed["normal-volume-decibels"].connect(() => update_max_volume()); - _settings.changed["amplified-volume-decibels"].connect(() => update_max_volume()); - _shared_settings.changed["allow-amplified-volume"].connect(() => update_max_volume()); - update_max_volume(); - } - private void update_max_volume () { - var new_max_volume = calculate_max_volume(); - if (max_volume != new_max_volume) { - debug("changing max_volume from %f to %f", this.max_volume, new_max_volume); - max_volume = calculate_max_volume(); - } - } - private double calculate_max_volume () { - unowned string decibel_key = _shared_settings.get_boolean("allow-amplified-volume") - ? "amplified-volume-decibels" - : "normal-volume-decibels"; - var volume_dB = _settings.get_double(decibel_key); - var volume_sw = PulseAudio.Volume.sw_from_dB (volume_dB); - return volume_to_double (volume_sw); - } - /** HIGH VOLUME PROPERTY **/ private bool _warning_volume_enabled; diff --git a/src/volume-control.vala b/src/volume-control.vala index 90fc325..f33ae18 100644 --- a/src/volume-control.vala +++ b/src/volume-control.vala @@ -45,6 +45,12 @@ public abstract class VolumeControl : Object public VolumeReasons reason; } + private IndicatorSound.Options _options = null; + + public VolumeControl(IndicatorSound.Options options) { + _options = options; + } + public virtual string stream { get { return ""; } } public virtual bool ready { get { return false; } set { } } public virtual bool active_mic { get { return false; } set { } } @@ -58,7 +64,6 @@ public abstract class VolumeControl : Object private double _pre_clamp_volume; public virtual Volume volume { get { return _volume; } set { } } public virtual double mic_volume { get { return 0.0; } set { } } - public virtual double max_volume { get { return 1.0; } protected set { } } public virtual bool high_volume_approved { get { return false; } protected set { } } public virtual void approve_high_volume() { } @@ -69,7 +74,7 @@ public abstract class VolumeControl : Object public void set_volume_clamp (double unclamped, VolumeControl.VolumeReasons reason) { var v = new VolumeControl.Volume(); - v.volume = unclamped.clamp (0.0, this.max_volume); + v.volume = unclamped.clamp (0.0, _options.max_volume); v.reason = reason; this.volume = v; _pre_clamp_volume = unclamped; diff --git a/src/volume-warning.vala b/src/volume-warning.vala index 73a8ade..b511856 100644 --- a/src/volume-warning.vala +++ b/src/volume-warning.vala @@ -45,7 +45,6 @@ public class VolumeWarning : VolumeControl private VolumeControl.Volume _volume = new VolumeControl.Volume(); private double _mic_volume = 0.0; private Settings _settings = new Settings ("com.canonical.indicator.sound"); - private Settings _shared_settings = new Settings ("com.ubuntu.sound"); /* Used by the pulseaudio stream restore extension */ private DBusConnection _pconn; @@ -87,8 +86,10 @@ public class VolumeWarning : VolumeControl /** true when a microphone is active **/ public override bool active_mic { get; private set; default = false; } - public VolumeWarning () + public VolumeWarning (IndicatorSound.Options options) { + base(options); + _volume.volume = 0.0; _volume.reason = VolumeControl.VolumeReasons.PULSE_CHANGE; @@ -102,7 +103,6 @@ public class VolumeWarning : VolumeControl private void init_all_properties() { - init_max_volume(); init_high_volume(); init_high_volume_approved(); } @@ -648,30 +648,6 @@ public class VolumeWarning : VolumeControl } } - /** MAX VOLUME PROPERTY **/ - - private void init_max_volume() { - _settings.changed["normal-volume-decibels"].connect(() => update_max_volume()); - _settings.changed["amplified-volume-decibels"].connect(() => update_max_volume()); - _shared_settings.changed["allow-amplified-volume"].connect(() => update_max_volume()); - update_max_volume(); - } - private void update_max_volume () { - var new_max_volume = calculate_max_volume(); - if (max_volume != new_max_volume) { - debug("changing max_volume from %f to %f", this.max_volume, new_max_volume); - max_volume = calculate_max_volume(); - } - } - private double calculate_max_volume () { - unowned string decibel_key = _shared_settings.get_boolean("allow-amplified-volume") - ? "amplified-volume-decibels" - : "normal-volume-decibels"; - var volume_dB = _settings.get_double(decibel_key); - var volume_sw = PulseAudio.Volume.sw_from_dB (volume_dB); - return volume_to_double (volume_sw); - } - /** HIGH VOLUME PROPERTY **/ private bool _warning_volume_enabled; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bf0e051..c11e288 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -71,6 +71,10 @@ vala_add(vala-mocks media-player-list-mock.vala ) +vala_add(vala-mocks + options-mock.vala +) + vala_add(vala-mocks volume-control-mock.vala ) @@ -169,6 +173,7 @@ add_executable (volume-control-test volume-control-test.cc gschemas.compiled) target_link_libraries ( volume-control-test indicator-sound-service-lib + vala-mocks-lib pulse-mock gtest-static ${TEST_LIBRARIES} diff --git a/tests/notifications-test.cc b/tests/notifications-test.cc index c5d9770..26864cd 100644 --- a/tests/notifications-test.cc +++ b/tests/notifications-test.cc @@ -119,18 +119,27 @@ class NotificationsTest : public ::testing::Test return playerList; } - std::shared_ptr volumeControlMock () { + std::shared_ptr optionsMock () { + auto options = std::shared_ptr( + INDICATOR_SOUND_OPTIONS(options_mock_new()), + [](IndicatorSoundOptions * options){ + g_clear_object(&options); + }); + return options; + } + + std::shared_ptr volumeControlMock (const std::shared_ptr& optionsMock) { auto volumeControl = std::shared_ptr( - VOLUME_CONTROL(volume_control_mock_new()), + VOLUME_CONTROL(volume_control_mock_new(optionsMock.get())), [](VolumeControl * control){ g_clear_object(&control); }); return volumeControl; } - std::shared_ptr standardService (std::shared_ptr volumeControl, std::shared_ptr playerList) { + std::shared_ptr standardService (std::shared_ptr volumeControl, std::shared_ptr playerList, const std::shared_ptr& options) { auto soundService = std::shared_ptr( - indicator_sound_service_new(playerList.get(), volumeControl.get(), nullptr), + indicator_sound_service_new(playerList.get(), volumeControl.get(), nullptr, options.get()), [](IndicatorSoundService * service){ g_clear_object(&service); }); @@ -168,7 +177,9 @@ class NotificationsTest : public ::testing::Test }; TEST_F(NotificationsTest, BasicObject) { - auto soundService = standardService(volumeControlMock(), playerListMock()); + auto options = optionsMock(); + auto volumeControl = volumeControlMock(options); + auto soundService = standardService(volumeControl, playerListMock(), options); /* Give some time settle */ loop(50); @@ -177,8 +188,9 @@ TEST_F(NotificationsTest, BasicObject) { } TEST_F(NotificationsTest, VolumeChanges) { - auto volumeControl = volumeControlMock(); - auto soundService = standardService(volumeControl, playerListMock()); + auto options = optionsMock(); + auto volumeControl = volumeControlMock(options); + auto soundService = standardService(volumeControl, playerListMock(), options); /* Set a volume */ notifications->clearNotifications(); @@ -216,8 +228,9 @@ TEST_F(NotificationsTest, VolumeChanges) { } TEST_F(NotificationsTest, StreamChanges) { - auto volumeControl = volumeControlMock(); - auto soundService = standardService(volumeControl, playerListMock()); + auto options = optionsMock(); + auto volumeControl = volumeControlMock(options); + auto soundService = standardService(volumeControl, playerListMock(), options); /* Set a volume */ notifications->clearNotifications(); @@ -254,8 +267,9 @@ TEST_F(NotificationsTest, StreamChanges) { } TEST_F(NotificationsTest, IconTesting) { - auto volumeControl = volumeControlMock(); - auto soundService = standardService(volumeControl, playerListMock()); + auto options = optionsMock(); + auto volumeControl = volumeControlMock(options); + auto soundService = standardService(volumeControl, playerListMock(), options); /* Set an initial volume */ notifications->clearNotifications(); @@ -288,8 +302,9 @@ TEST_F(NotificationsTest, IconTesting) { } TEST_F(NotificationsTest, ServerRestart) { - auto volumeControl = volumeControlMock(); - auto soundService = standardService(volumeControl, playerListMock()); + auto options = optionsMock(); + auto volumeControl = volumeControlMock(options); + auto soundService = standardService(volumeControl, playerListMock(), options); /* Set a volume */ notifications->clearNotifications(); @@ -335,8 +350,9 @@ TEST_F(NotificationsTest, ServerRestart) { } TEST_F(NotificationsTest, HighVolume) { - auto volumeControl = volumeControlMock(); - auto soundService = standardService(volumeControl, playerListMock()); + auto options = optionsMock(); + auto volumeControl = volumeControlMock(options); + auto soundService = standardService(volumeControl, playerListMock(), options); /* Set a volume */ notifications->clearNotifications(); @@ -377,8 +393,9 @@ TEST_F(NotificationsTest, HighVolume) { } TEST_F(NotificationsTest, MenuHide) { - auto volumeControl = volumeControlMock(); - auto soundService = standardService(volumeControl, playerListMock()); + auto options = optionsMock(); + auto volumeControl = volumeControlMock(options); + auto soundService = standardService(volumeControl, playerListMock(), options); /* Set a volume */ notifications->clearNotifications(); @@ -407,8 +424,9 @@ TEST_F(NotificationsTest, MenuHide) { } TEST_F(NotificationsTest, DISABLED_ExtendendVolumeNotification) { - auto volumeControl = volumeControlMock(); - auto soundService = standardService(volumeControl, playerListMock()); + auto options = optionsMock(); + auto volumeControl = volumeControlMock(options); + auto soundService = standardService(volumeControl, playerListMock(), options); /* Set a volume */ notifications->clearNotifications(); diff --git a/tests/volume-control-mock.vala b/tests/volume-control-mock.vala index dc11fba..7f958b2 100644 --- a/tests/volume-control-mock.vala +++ b/tests/volume-control-mock.vala @@ -40,7 +40,9 @@ public class VolumeControlMock : VolumeControl } - public VolumeControlMock() { + public VolumeControlMock(IndicatorSound.Options options) { + base(options); + ready = true; this.notify["mock-stream"].connect(() => this.notify_property("stream")); this.notify["mock-high-volume"].connect(() => this.notify_property("high-volume")); diff --git a/tests/volume-control-test.cc b/tests/volume-control-test.cc index 5022245..2d07746 100644 --- a/tests/volume-control-test.cc +++ b/tests/volume-control-test.cc @@ -23,6 +23,7 @@ extern "C" { #include "indicator-sound-service.h" +#include "vala-mocks.h" } class VolumeControlTest : public ::testing::Test @@ -75,7 +76,8 @@ class VolumeControlTest : public ::testing::Test }; TEST_F(VolumeControlTest, BasicObject) { - VolumeControlPulse * control = volume_control_pulse_new(); + auto options = options_mock_new(); + auto control = volume_control_pulse_new(INDICATOR_SOUND_OPTIONS(options)); /* Setup the PA backend */ loop(100); @@ -84,4 +86,5 @@ TEST_F(VolumeControlTest, BasicObject) { EXPECT_TRUE(volume_control_get_ready(VOLUME_CONTROL(control))); g_clear_object(&control); + g_clear_object(&options); } -- cgit v1.2.3