Bug 1582802 - part2 : only run owing multiple audio focus test when we disable audio focus management. r=chunmin

`TestMultipleAudioFocusNums` is used to test whether we can increase the amount of the audio focus when we don't enable audio focus management. As we won't handle the audio competing in this situation, so we allow multiple controllers owing audio focus at the same time.

However, if we enable audio focus management, we would start to handle audio competing and ensure that only one controller can own audio focus at a time. As it involves multiple components, we are not able to test it by simply using `AudioFocusManager`'s APIs.

Therefore, we should have two separate tests to test the behavior of owning audio focus when enabling or disabling audio focus management.

Differential Revision: https://phabricator.services.mozilla.com/D47371

--HG--
extra : moz-landing-system : lando
This commit is contained in:
alwu 2019-10-08 02:48:13 +00:00
parent 6157b4eccc
commit a99f11f839

View File

@ -5,14 +5,70 @@
#include "gtest/gtest.h"
#include "AudioFocusManager.h"
#include "MediaControlService.h"
#include "mozilla/Preferences.h"
using namespace mozilla::dom;
#define FIRST_CONTROLLER_ID 0
#define SECOND_CONTROLLER_ID 1
TEST(AudioFocusManager, TestMultipleAudioFocusNums)
// This RAII class is used to set the audio focus management pref within a test
// and automatically revert the change when a test ends, in order not to
// interfere other tests unexpectedly.
class AudioFocusManagmentPrefSetterRAII {
public:
explicit AudioFocusManagmentPrefSetterRAII(bool aPrefValue) {
mOriginalValue = mozilla::Preferences::GetBool(mPrefName, false);
mozilla::Preferences::SetBool(mPrefName, aPrefValue);
}
~AudioFocusManagmentPrefSetterRAII() {
mozilla::Preferences::SetBool(mPrefName, mOriginalValue);
}
private:
const char* mPrefName = "media.audioFocus.management";
bool mOriginalValue;
};
TEST(AudioFocusManager, TestAudioFocusNumsWhenEnableAudioFocusManagement)
{
// When enabling audio focus management, we only allow one controller owing
// audio focus at a time when the audio competing occurs. As the mechanism of
// handling the audio competing involves multiple components, we can't test it
// simply by using the APIs from AudioFocusManager.
AudioFocusManagmentPrefSetterRAII prefSetter(true);
RefPtr<MediaControlService> service = MediaControlService::GetService();
AudioFocusManager& manager = service->GetAudioFocusManager();
ASSERT_TRUE(manager.GetAudioFocusNums() == 0);
RefPtr<MediaController> controller1 =
new TabMediaController(FIRST_CONTROLLER_ID);
service->AddMediaController(controller1);
manager.RequestAudioFocus(FIRST_CONTROLLER_ID);
ASSERT_TRUE(manager.GetAudioFocusNums() == 1);
// When controller2 starts, it would win the audio focus from controller1. So
// there would only one audio focus existing.
RefPtr<MediaController> controller2 =
new TabMediaController(SECOND_CONTROLLER_ID);
service->AddMediaController(controller2);
manager.RequestAudioFocus(SECOND_CONTROLLER_ID);
ASSERT_TRUE(manager.GetAudioFocusNums() == 1);
manager.RevokeAudioFocus(SECOND_CONTROLLER_ID);
ASSERT_TRUE(manager.GetAudioFocusNums() == 0);
service->RemoveMediaController(controller1);
service->RemoveMediaController(controller2);
}
TEST(AudioFocusManager, TestAudioFocusNumsWhenDisableAudioFocusManagement)
{
// When disabling audio focus management, we won't handle the audio competing,
// so we allow multiple audio focus existing at the same time.
AudioFocusManagmentPrefSetterRAII prefSetter(false);
AudioFocusManager manager(nullptr);
ASSERT_TRUE(manager.GetAudioFocusNums() == 0);