dom/audiochannel/tests/TestAudioChannelService.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/audiochannel/tests/TestAudioChannelService.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,669 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +#ifdef XP_WIN
     1.8 +#include <windows.h>
     1.9 +#else
    1.10 +#include <unistd.h>
    1.11 +#endif
    1.12 +
    1.13 +#include "TestHarness.h"
    1.14 +
    1.15 +#include "nsWeakReference.h"
    1.16 +#include "AudioChannelService.h"
    1.17 +#include "AudioChannelAgent.h"
    1.18 +
    1.19 +#define TEST_ENSURE_BASE(_test, _msg)       \
    1.20 +  PR_BEGIN_MACRO                            \
    1.21 +    if (!(_test)) {                         \
    1.22 +      fail(_msg);                           \
    1.23 +      return NS_ERROR_FAILURE;              \
    1.24 +    } else {                                \
    1.25 +      passed(_msg);                         \
    1.26 +    }                                       \
    1.27 +  PR_END_MACRO
    1.28 +
    1.29 +using namespace mozilla::dom;
    1.30 +
    1.31 +class Agent : public nsIAudioChannelAgentCallback,
    1.32 +              public nsSupportsWeakReference
    1.33 +{
    1.34 +public:
    1.35 +  NS_DECL_ISUPPORTS
    1.36 +
    1.37 +  Agent(AudioChannel aChannel)
    1.38 +  : mChannel(aChannel)
    1.39 +  , mWaitCallback(false)
    1.40 +  , mRegistered(false)
    1.41 +  , mCanPlay(AUDIO_CHANNEL_STATE_MUTED)
    1.42 +  {
    1.43 +    mAgent = do_CreateInstance("@mozilla.org/audiochannelagent;1");
    1.44 +  }
    1.45 +
    1.46 +  virtual ~Agent()
    1.47 +  {
    1.48 +    if (mRegistered) {
    1.49 +      StopPlaying();
    1.50 +    }
    1.51 +  }
    1.52 +
    1.53 +  nsresult Init(bool video=false)
    1.54 +  {
    1.55 +    nsresult rv = NS_OK;
    1.56 +    if (video) {
    1.57 +      rv = mAgent->InitWithVideo(nullptr, static_cast<int32_t>(mChannel),
    1.58 +                                 this, true);
    1.59 +    }
    1.60 +    else {
    1.61 +      rv = mAgent->InitWithWeakCallback(nullptr, static_cast<int32_t>(mChannel),
    1.62 +                                        this);
    1.63 +    }
    1.64 +    NS_ENSURE_SUCCESS(rv, rv);
    1.65 +
    1.66 +    return mAgent->SetVisibilityState(false);
    1.67 +  }
    1.68 +
    1.69 +  nsresult StartPlaying(AudioChannelState *_ret)
    1.70 +  {
    1.71 +    if (mRegistered) {
    1.72 +      StopPlaying();
    1.73 +    }
    1.74 +
    1.75 +    nsresult rv = mAgent->StartPlaying((int32_t *)_ret);
    1.76 +    mRegistered = true;
    1.77 +    return rv;
    1.78 +  }
    1.79 +
    1.80 +  nsresult StopPlaying()
    1.81 +  {
    1.82 +    mRegistered = false;
    1.83 +    int loop = 0;
    1.84 +    while (mWaitCallback) {
    1.85 +      #ifdef XP_WIN
    1.86 +      Sleep(1000);
    1.87 +      #else
    1.88 +      sleep(1);
    1.89 +      #endif
    1.90 +      if (loop++ == 5) {
    1.91 +        TEST_ENSURE_BASE(false, "StopPlaying timeout");
    1.92 +      }
    1.93 +    }
    1.94 +    return mAgent->StopPlaying();
    1.95 +  }
    1.96 +
    1.97 +  nsresult SetVisibilityState(bool visible)
    1.98 +  {
    1.99 +    if (mRegistered) {
   1.100 +      mWaitCallback = true;
   1.101 +    }
   1.102 +    return mAgent->SetVisibilityState(visible);
   1.103 +  }
   1.104 +
   1.105 +  NS_IMETHODIMP CanPlayChanged(int32_t canPlay)
   1.106 +  {
   1.107 +    mCanPlay = static_cast<AudioChannelState>(canPlay);
   1.108 +    mWaitCallback = false;
   1.109 +    return NS_OK;
   1.110 +  }
   1.111 +
   1.112 +  NS_IMETHODIMP WindowVolumeChanged()
   1.113 +  {
   1.114 +    return NS_OK;
   1.115 +  }
   1.116 +
   1.117 +  nsresult GetCanPlay(AudioChannelState *_ret)
   1.118 +  {
   1.119 +    int loop = 0;
   1.120 +    while (mWaitCallback) {
   1.121 +      #ifdef XP_WIN
   1.122 +      Sleep(1000);
   1.123 +      #else
   1.124 +      sleep(1);
   1.125 +      #endif
   1.126 +      if (loop++ == 5) {
   1.127 +        TEST_ENSURE_BASE(false, "GetCanPlay timeout");
   1.128 +      }
   1.129 +    }
   1.130 +    *_ret = mCanPlay;
   1.131 +    return NS_OK;
   1.132 +  }
   1.133 +
   1.134 +  nsCOMPtr<nsIAudioChannelAgent> mAgent;
   1.135 +  AudioChannel mChannel;
   1.136 +  bool mWaitCallback;
   1.137 +  bool mRegistered;
   1.138 +  AudioChannelState mCanPlay;
   1.139 +};
   1.140 +
   1.141 +NS_IMPL_ISUPPORTS(Agent, nsIAudioChannelAgentCallback,
   1.142 +                  nsISupportsWeakReference)
   1.143 +
   1.144 +nsresult
   1.145 +TestDoubleStartPlaying()
   1.146 +{
   1.147 +  nsRefPtr<Agent> agent = new Agent(AudioChannel::Normal);
   1.148 +
   1.149 +  nsresult rv = agent->Init();
   1.150 +  NS_ENSURE_SUCCESS(rv, rv);
   1.151 +
   1.152 +  AudioChannelState playable;
   1.153 +  rv = agent->mAgent->StartPlaying((int32_t *)&playable);
   1.154 +  NS_ENSURE_SUCCESS(rv, rv);
   1.155 +
   1.156 +  rv = agent->mAgent->StartPlaying((int32_t *)&playable);
   1.157 +  TEST_ENSURE_BASE(NS_FAILED(rv),
   1.158 +    "Test0: StartPlaying calling twice must return error");
   1.159 +
   1.160 +  return NS_OK;
   1.161 +}
   1.162 +
   1.163 +nsresult
   1.164 +TestOneNormalChannel()
   1.165 +{
   1.166 +  nsRefPtr<Agent> agent = new Agent(AudioChannel::Normal);
   1.167 +  nsresult rv = agent->Init();
   1.168 +  NS_ENSURE_SUCCESS(rv, rv);
   1.169 +
   1.170 +  AudioChannelState playable;
   1.171 +  rv = agent->StartPlaying(&playable);
   1.172 +  NS_ENSURE_SUCCESS(rv, rv);
   1.173 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
   1.174 +    "Test1: A normal channel unvisible agent must be muted");
   1.175 +
   1.176 +  rv = agent->SetVisibilityState(true);
   1.177 +  NS_ENSURE_SUCCESS(rv, rv);
   1.178 +
   1.179 +  rv = agent->GetCanPlay(&playable);
   1.180 +  NS_ENSURE_SUCCESS(rv, rv);
   1.181 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.182 +    "Test1: A normal channel visible agent must be playable");
   1.183 +
   1.184 +  return rv;
   1.185 +}
   1.186 +
   1.187 +nsresult
   1.188 +TestTwoNormalChannels()
   1.189 +{
   1.190 +  nsRefPtr<Agent> agent1 = new Agent(AudioChannel::Normal);
   1.191 +  nsresult rv = agent1->Init();
   1.192 +  NS_ENSURE_SUCCESS(rv, rv);
   1.193 +
   1.194 +  nsRefPtr<Agent> agent2 = new Agent(AudioChannel::Normal);
   1.195 +  rv = agent2->Init();
   1.196 +  NS_ENSURE_SUCCESS(rv, rv);
   1.197 +
   1.198 +  AudioChannelState playable;
   1.199 +  rv = agent1->StartPlaying(&playable);
   1.200 +  NS_ENSURE_SUCCESS(rv, rv);
   1.201 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
   1.202 +    "Test2: A normal channel unvisible agent1 must be muted");
   1.203 +
   1.204 +  rv = agent2->StartPlaying(&playable);
   1.205 +  NS_ENSURE_SUCCESS(rv, rv);
   1.206 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
   1.207 +    "Test2: A normal channel unvisible agent2 must be muted");
   1.208 +
   1.209 +  rv = agent1->SetVisibilityState(true);
   1.210 +  NS_ENSURE_SUCCESS(rv, rv);
   1.211 +
   1.212 +  rv = agent2->SetVisibilityState(true);
   1.213 +  NS_ENSURE_SUCCESS(rv, rv);
   1.214 +
   1.215 +  rv = agent1->GetCanPlay(&playable);
   1.216 +  NS_ENSURE_SUCCESS(rv, rv);
   1.217 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.218 +    "Test2: A normal channel visible agent1 must be playable");
   1.219 +
   1.220 +  rv = agent2->GetCanPlay(&playable);
   1.221 +  NS_ENSURE_SUCCESS(rv, rv);
   1.222 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.223 +    "Test2: A normal channel visible agent2 must be playable");
   1.224 +
   1.225 +  return rv;
   1.226 +}
   1.227 +
   1.228 +nsresult
   1.229 +TestContentChannels()
   1.230 +{
   1.231 +  nsRefPtr<Agent> agent1 = new Agent(AudioChannel::Content);
   1.232 +  nsresult rv = agent1->Init();
   1.233 +  NS_ENSURE_SUCCESS(rv, rv);
   1.234 +
   1.235 +  nsRefPtr<Agent> agent2 = new Agent(AudioChannel::Content);
   1.236 +  rv = agent2->Init();
   1.237 +  NS_ENSURE_SUCCESS(rv, rv);
   1.238 +
   1.239 +  // All content channels in the foreground can be allowed to play
   1.240 +  rv = agent1->SetVisibilityState(true);
   1.241 +  NS_ENSURE_SUCCESS(rv, rv);
   1.242 +
   1.243 +  rv = agent2->SetVisibilityState(true);
   1.244 +  NS_ENSURE_SUCCESS(rv, rv);
   1.245 +
   1.246 +  AudioChannelState playable;
   1.247 +  rv = agent1->StartPlaying(&playable);
   1.248 +  NS_ENSURE_SUCCESS(rv, rv);
   1.249 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.250 +    "Test3: A content channel visible agent1 must be playable");
   1.251 +
   1.252 +  rv = agent2->StartPlaying(&playable);
   1.253 +  NS_ENSURE_SUCCESS(rv, rv);
   1.254 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.255 +    "Test3: A content channel visible agent2 must be playable");
   1.256 +
   1.257 +  // Test the transition state of one content channel tried to set non-visible
   1.258 +  // state first when app is going to background.
   1.259 +  rv = agent1->SetVisibilityState(false);
   1.260 +  NS_ENSURE_SUCCESS(rv, rv);
   1.261 +
   1.262 +  rv = agent1->GetCanPlay(&playable);
   1.263 +  NS_ENSURE_SUCCESS(rv, rv);
   1.264 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.265 +    "Test3: A content channel unvisible agent1 must be playable from "
   1.266 +    "foreground to background");
   1.267 +
   1.268 +  // Test all content channels set non-visible already
   1.269 +  rv = agent2->SetVisibilityState(false);
   1.270 +  NS_ENSURE_SUCCESS(rv, rv);
   1.271 +
   1.272 +  rv = agent2->GetCanPlay(&playable);
   1.273 +  NS_ENSURE_SUCCESS(rv, rv);
   1.274 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.275 +    "Test3: A content channel unvisible agent2 must be playable from "
   1.276 +    "foreground to background");
   1.277 +
   1.278 +  // Clear the content channels & mActiveContentChildIDs in AudioChannelService.
   1.279 +  // If agent stop playable in the background, we will reserve it's childID in
   1.280 +  // mActiveContentChildIDs, then it can allow to play next song. So we set agents
   1.281 +  // to foreground first then stopping to play
   1.282 +  rv = agent1->SetVisibilityState(true);
   1.283 +  NS_ENSURE_SUCCESS(rv, rv);
   1.284 +  rv = agent2->SetVisibilityState(true);
   1.285 +  NS_ENSURE_SUCCESS(rv, rv);
   1.286 +  rv = agent1->StopPlaying();
   1.287 +  NS_ENSURE_SUCCESS(rv, rv);
   1.288 +  rv = agent2->StopPlaying();
   1.289 +  NS_ENSURE_SUCCESS(rv, rv);
   1.290 +
   1.291 +  // Test that content channels can be allow to play when they starts from
   1.292 +  // the background state
   1.293 +  rv = agent1->SetVisibilityState(false);
   1.294 +  NS_ENSURE_SUCCESS(rv, rv);
   1.295 +  rv = agent2->SetVisibilityState(false);
   1.296 +  NS_ENSURE_SUCCESS(rv, rv);
   1.297 +
   1.298 +  rv = agent1->StartPlaying(&playable);
   1.299 +  NS_ENSURE_SUCCESS(rv, rv);
   1.300 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.301 +    "Test3: A content channel unvisible agent1 must be playable "
   1.302 +    "from background state");
   1.303 +
   1.304 +  rv = agent2->StartPlaying(&playable);
   1.305 +  NS_ENSURE_SUCCESS(rv, rv);
   1.306 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.307 +    "Test3: A content channel unvisible agent2 must be playable "
   1.308 +    "from background state");
   1.309 +
   1.310 +  return rv;
   1.311 +}
   1.312 +
   1.313 +nsresult
   1.314 +TestFadedState()
   1.315 +{
   1.316 +  nsRefPtr<Agent> normalAgent = new Agent(AudioChannel::Normal);
   1.317 +  nsresult rv = normalAgent->Init();
   1.318 +  NS_ENSURE_SUCCESS(rv, rv);
   1.319 +
   1.320 +  nsRefPtr<Agent> contentAgent = new Agent(AudioChannel::Content);
   1.321 +  rv = contentAgent->Init();
   1.322 +  NS_ENSURE_SUCCESS(rv, rv);
   1.323 +
   1.324 +  nsRefPtr<Agent> notificationAgent = new Agent(AudioChannel::Notification);
   1.325 +  rv = notificationAgent->Init();
   1.326 +  NS_ENSURE_SUCCESS(rv, rv);
   1.327 +
   1.328 +  rv = normalAgent->SetVisibilityState(true);
   1.329 +  NS_ENSURE_SUCCESS(rv, rv);
   1.330 +
   1.331 +  rv = contentAgent->SetVisibilityState(true);
   1.332 +  NS_ENSURE_SUCCESS(rv, rv);
   1.333 +
   1.334 +  rv = notificationAgent->SetVisibilityState(true);
   1.335 +  NS_ENSURE_SUCCESS(rv, rv);
   1.336 +
   1.337 +  AudioChannelState playable;
   1.338 +  rv = normalAgent->StartPlaying(&playable);
   1.339 +  NS_ENSURE_SUCCESS(rv, rv);
   1.340 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.341 +    "Test4: A normal channel visible agent must be playable");
   1.342 +
   1.343 +  rv = contentAgent->StartPlaying(&playable);
   1.344 +  NS_ENSURE_SUCCESS(rv, rv);
   1.345 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.346 +    "Test4: A content channel visible agent must be playable");
   1.347 +
   1.348 +  rv = notificationAgent->StartPlaying(&playable);
   1.349 +  NS_ENSURE_SUCCESS(rv, rv);
   1.350 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.351 +    "Test4: A notification channel visible agent must be playable");
   1.352 +
   1.353 +    rv = contentAgent->GetCanPlay(&playable);
   1.354 +  NS_ENSURE_SUCCESS(rv, rv);
   1.355 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_FADED,
   1.356 +    "Test4: A content channel unvisible agent must be faded because of "
   1.357 +    "notification channel is playing");
   1.358 +
   1.359 +  rv = contentAgent->SetVisibilityState(false);
   1.360 +  NS_ENSURE_SUCCESS(rv, rv);
   1.361 +
   1.362 +  rv = contentAgent->GetCanPlay(&playable);
   1.363 +  NS_ENSURE_SUCCESS(rv, rv);
   1.364 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_FADED,
   1.365 +    "Test4: A content channel unvisible agent must be faded because of "
   1.366 +    "notification channel is playing");
   1.367 +
   1.368 +  rv = notificationAgent->SetVisibilityState(false);
   1.369 +  NS_ENSURE_SUCCESS(rv, rv);
   1.370 +
   1.371 +  rv = notificationAgent->GetCanPlay(&playable);
   1.372 +  NS_ENSURE_SUCCESS(rv, rv);
   1.373 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.374 +    "Test4: A notification channel unvisible agent must be playable from "
   1.375 +    "foreground to background");
   1.376 +
   1.377 +  rv = notificationAgent->StopPlaying();
   1.378 +  NS_ENSURE_SUCCESS(rv, rv);
   1.379 +
   1.380 +  rv = contentAgent->GetCanPlay(&playable);
   1.381 +  NS_ENSURE_SUCCESS(rv, rv);
   1.382 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.383 +    "Test4: A content channel unvisible agent must be playable "
   1.384 +    "because of notification channel is stopped");
   1.385 +
   1.386 +  rv = contentAgent->SetVisibilityState(true);
   1.387 +  NS_ENSURE_SUCCESS(rv, rv);
   1.388 +
   1.389 +  return rv;
   1.390 +}
   1.391 +
   1.392 +nsresult
   1.393 +TestPriorities()
   1.394 +{
   1.395 +  nsRefPtr<Agent> normalAgent = new Agent(AudioChannel::Normal);
   1.396 +  nsresult rv = normalAgent->Init();
   1.397 +  NS_ENSURE_SUCCESS(rv, rv);
   1.398 +
   1.399 +  nsRefPtr<Agent> contentAgent = new Agent(AudioChannel::Content);
   1.400 +  rv = contentAgent->Init();
   1.401 +  NS_ENSURE_SUCCESS(rv, rv);
   1.402 +
   1.403 +  nsRefPtr<Agent> notificationAgent = new Agent(AudioChannel::Notification);
   1.404 +  rv = notificationAgent->Init();
   1.405 +  NS_ENSURE_SUCCESS(rv, rv);
   1.406 +
   1.407 +  nsRefPtr<Agent> alarmAgent = new Agent(AudioChannel::Alarm);
   1.408 +  rv = alarmAgent->Init();
   1.409 +  NS_ENSURE_SUCCESS(rv, rv);
   1.410 +
   1.411 +  nsRefPtr<Agent> telephonyAgent = new Agent(AudioChannel::Telephony);
   1.412 +  rv = telephonyAgent->Init();
   1.413 +  NS_ENSURE_SUCCESS(rv, rv);
   1.414 +
   1.415 +  nsRefPtr<Agent> ringerAgent = new Agent(AudioChannel::Ringer);
   1.416 +  rv = ringerAgent->Init();
   1.417 +  NS_ENSURE_SUCCESS(rv, rv);
   1.418 +
   1.419 +  nsRefPtr<Agent> pNotificationAgent =
   1.420 +    new Agent(AudioChannel::Publicnotification);
   1.421 +  rv = pNotificationAgent->Init();
   1.422 +  NS_ENSURE_SUCCESS(rv, rv);
   1.423 +
   1.424 +  AudioChannelState playable;
   1.425 +
   1.426 +  rv = normalAgent->StartPlaying(&playable);
   1.427 +  NS_ENSURE_SUCCESS(rv, rv);
   1.428 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
   1.429 +    "Test5: A normal channel unvisible agent must be muted");
   1.430 +
   1.431 +  rv = contentAgent->StartPlaying(&playable);
   1.432 +  NS_ENSURE_SUCCESS(rv, rv);
   1.433 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.434 +    "Test5: A content channel unvisible agent must be playable while "
   1.435 +    "playing from background state");
   1.436 +
   1.437 +  rv = notificationAgent->StartPlaying(&playable);
   1.438 +  NS_ENSURE_SUCCESS(rv, rv);
   1.439 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.440 +    "Test5: A notification channel unvisible agent must be playable");
   1.441 +
   1.442 +  rv = alarmAgent->StartPlaying(&playable);
   1.443 +  NS_ENSURE_SUCCESS(rv, rv);
   1.444 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.445 +    "Test5: An alarm channel unvisible agent must be playable");
   1.446 +
   1.447 +  rv = notificationAgent->StartPlaying(&playable);
   1.448 +  NS_ENSURE_SUCCESS(rv, rv);
   1.449 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
   1.450 +    "Test5: A notification channel unvisible agent must be muted when an "
   1.451 +    "alarm is playing");
   1.452 +
   1.453 +  rv = telephonyAgent->StartPlaying(&playable);
   1.454 +  NS_ENSURE_SUCCESS(rv, rv);
   1.455 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.456 +    "Test5: A telephony channel unvisible agent must be playable");
   1.457 +
   1.458 +  rv = alarmAgent->StartPlaying(&playable);
   1.459 +  NS_ENSURE_SUCCESS(rv, rv);
   1.460 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
   1.461 +    "Test5: An alarm channel unvisible agent must be muted when a telephony "
   1.462 +    "is playing");
   1.463 +
   1.464 +  rv = ringerAgent->StartPlaying(&playable);
   1.465 +  NS_ENSURE_SUCCESS(rv, rv);
   1.466 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.467 +    "Test5: A ringer channel unvisible agent must be playable");
   1.468 +
   1.469 +  rv = telephonyAgent->StartPlaying(&playable);
   1.470 +  NS_ENSURE_SUCCESS(rv, rv);
   1.471 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
   1.472 +    "Test5: A telephony channel unvisible agent must be muted when a ringer "
   1.473 +    "is playing");
   1.474 +
   1.475 +  rv = pNotificationAgent->StartPlaying(&playable);
   1.476 +  NS_ENSURE_SUCCESS(rv, rv);
   1.477 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.478 +    "Test5: A pNotification channel unvisible agent must be playable");
   1.479 +
   1.480 +  rv = ringerAgent->StartPlaying(&playable);
   1.481 +  NS_ENSURE_SUCCESS(rv, rv);
   1.482 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
   1.483 +    "Test5: A ringer channel unvisible agent must be muted when a public "
   1.484 +    "notification is playing");
   1.485 +
   1.486 +  // Stop to play notification channel or normal/content will be faded.
   1.487 +  // Which already be tested on Test 4.
   1.488 +  rv = notificationAgent->StopPlaying();
   1.489 +  NS_ENSURE_SUCCESS(rv, rv);
   1.490 +
   1.491 +  // Settings visible the normal channel.
   1.492 +  rv = normalAgent->SetVisibilityState(true);
   1.493 +  NS_ENSURE_SUCCESS(rv, rv);
   1.494 +
   1.495 +  rv = normalAgent->GetCanPlay(&playable);
   1.496 +  NS_ENSURE_SUCCESS(rv, rv);
   1.497 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.498 +    "Test5: A normal channel visible agent must be playable");
   1.499 +
   1.500 +  // Set the content channel as visible .
   1.501 +  rv = contentAgent->SetVisibilityState(true);
   1.502 +  NS_ENSURE_SUCCESS(rv, rv);
   1.503 +
   1.504 +  // Content must be playable because visible.
   1.505 +  rv = contentAgent->GetCanPlay(&playable);
   1.506 +  NS_ENSURE_SUCCESS(rv, rv);
   1.507 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.508 +    "Test5: A content channel visible agent must be playable");
   1.509 +
   1.510 +  // Set the alarm channel as visible.
   1.511 +  rv = alarmAgent->SetVisibilityState(true);
   1.512 +  NS_ENSURE_SUCCESS(rv, rv);
   1.513 +
   1.514 +  rv = alarmAgent->GetCanPlay(&playable);
   1.515 +  NS_ENSURE_SUCCESS(rv, rv);
   1.516 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.517 +    "Test5: An alarm channel visible agent must be playable");
   1.518 +
   1.519 +  // Set the telephony channel as visible.
   1.520 +  rv = telephonyAgent->SetVisibilityState(true);
   1.521 +  NS_ENSURE_SUCCESS(rv, rv);
   1.522 +
   1.523 +  rv = telephonyAgent->GetCanPlay(&playable);
   1.524 +  NS_ENSURE_SUCCESS(rv, rv);
   1.525 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.526 +    "Test5: A telephony channel visible agent must be playable");
   1.527 +
   1.528 +  // Set the ringer channel as visible.
   1.529 +  rv = ringerAgent->SetVisibilityState(true);
   1.530 +  NS_ENSURE_SUCCESS(rv, rv);
   1.531 +
   1.532 +  rv = ringerAgent->GetCanPlay(&playable);
   1.533 +  NS_ENSURE_SUCCESS(rv, rv);
   1.534 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.535 +    "Test5: A ringer channel visible agent must be playable");
   1.536 +
   1.537 +  // Set the public notification channel as visible.
   1.538 +  rv = pNotificationAgent->SetVisibilityState(true);
   1.539 +  NS_ENSURE_SUCCESS(rv, rv);
   1.540 +
   1.541 +  rv = pNotificationAgent->GetCanPlay(&playable);
   1.542 +  NS_ENSURE_SUCCESS(rv, rv);
   1.543 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.544 +    "Test5: A pNotification channel visible agent must be playable");
   1.545 +
   1.546 +  return rv;
   1.547 +}
   1.548 +
   1.549 +nsresult
   1.550 +TestOneVideoNormalChannel()
   1.551 +{
   1.552 +  nsRefPtr<Agent> agent1 = new Agent(AudioChannel::Normal);
   1.553 +  nsresult rv = agent1->Init(true);
   1.554 +  NS_ENSURE_SUCCESS(rv, rv);
   1.555 +
   1.556 +  nsRefPtr<Agent> agent2 = new Agent(AudioChannel::Content);
   1.557 +  rv = agent2->Init(false);
   1.558 +  NS_ENSURE_SUCCESS(rv, rv);
   1.559 +
   1.560 +  AudioChannelState playable;
   1.561 +  rv = agent1->StartPlaying(&playable);
   1.562 +  NS_ENSURE_SUCCESS(rv, rv);
   1.563 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
   1.564 +    "Test6: A video normal channel invisible agent1 must be muted");
   1.565 +
   1.566 +  rv = agent2->StartPlaying(&playable);
   1.567 +  NS_ENSURE_SUCCESS(rv, rv);
   1.568 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.569 +    "Test6: A content channel invisible agent2 must be playable");
   1.570 +
   1.571 +  // one video normal channel in foreground and one content channel in background
   1.572 +  rv = agent1->SetVisibilityState(true);
   1.573 +  NS_ENSURE_SUCCESS(rv, rv);
   1.574 +
   1.575 +  rv = agent1->GetCanPlay(&playable);
   1.576 +  NS_ENSURE_SUCCESS(rv, rv);
   1.577 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.578 +    "Test6: A video normal channel visible agent1 must be playable");
   1.579 +
   1.580 +  rv = agent2->GetCanPlay(&playable);
   1.581 +  NS_ENSURE_SUCCESS(rv, rv);
   1.582 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
   1.583 +    "Test6: A content channel invisible agent2 must be muted");
   1.584 +
   1.585 +  // both one video normal channel and one content channel in foreground
   1.586 +  rv = agent2->SetVisibilityState(true);
   1.587 +  NS_ENSURE_SUCCESS(rv, rv);
   1.588 +
   1.589 +  rv = agent1->GetCanPlay(&playable);
   1.590 +  NS_ENSURE_SUCCESS(rv, rv);
   1.591 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.592 +    "Test6: A video normal channel visible agent1 must be playable");
   1.593 +
   1.594 +  rv = agent2->GetCanPlay(&playable);
   1.595 +  NS_ENSURE_SUCCESS(rv, rv);
   1.596 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.597 +    "Test6: A content channel visible agent2 must be playable");
   1.598 +
   1.599 +  // one video normal channel in background and one content channel in foreground
   1.600 +  rv = agent1->SetVisibilityState(false);
   1.601 +  NS_ENSURE_SUCCESS(rv, rv);
   1.602 +
   1.603 +  rv = agent1->GetCanPlay(&playable);
   1.604 +  NS_ENSURE_SUCCESS(rv, rv);
   1.605 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
   1.606 +    "Test6: A video normal channel invisible agent1 must be muted");
   1.607 +
   1.608 +  rv = agent2->GetCanPlay(&playable);
   1.609 +  NS_ENSURE_SUCCESS(rv, rv);
   1.610 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.611 +    "Test6: A content channel visible agent2 must be playable");
   1.612 +
   1.613 +  // both one video normal channel and one content channel in background
   1.614 +  rv = agent2->SetVisibilityState(false);
   1.615 +  NS_ENSURE_SUCCESS(rv, rv);
   1.616 +
   1.617 +  rv = agent1->GetCanPlay(&playable);
   1.618 +  NS_ENSURE_SUCCESS(rv, rv);
   1.619 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
   1.620 +    "Test6: A video normal channel invisible agent1 must be muted");
   1.621 +
   1.622 +  rv = agent2->GetCanPlay(&playable);
   1.623 +  NS_ENSURE_SUCCESS(rv, rv);
   1.624 +  TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
   1.625 +    "Test6: A content channel invisible agent2 must be playable");
   1.626 +
   1.627 +  return rv;
   1.628 +}
   1.629 +
   1.630 +int main(int argc, char** argv)
   1.631 +{
   1.632 +  ScopedXPCOM xpcom("AudioChannelService");
   1.633 +  if (xpcom.failed()) {
   1.634 +    return 1;
   1.635 +  }
   1.636 +
   1.637 +  if (NS_FAILED(TestDoubleStartPlaying())) {
   1.638 +    return 1;
   1.639 +  }
   1.640 +
   1.641 +  if (NS_FAILED(TestOneNormalChannel())) {
   1.642 +    return 1;
   1.643 +  }
   1.644 +
   1.645 +  if (NS_FAILED(TestTwoNormalChannels())) {
   1.646 +    return 1;
   1.647 +  }
   1.648 +
   1.649 +  if (NS_FAILED(TestContentChannels())) {
   1.650 +    return 1;
   1.651 +  }
   1.652 +
   1.653 +  if (NS_FAILED(TestFadedState())) {
   1.654 +    return 1;
   1.655 +  }
   1.656 +
   1.657 +  // Channel type with AudioChannel::Telephony cannot be unregistered until the
   1.658 +  // main thread has chances to process 1500 millisecond timer. In order to
   1.659 +  // skip ambiguous return value of ChannelsActiveWithHigherPriorityThan(), new
   1.660 +  // test cases are added before any test case that registers the channel type
   1.661 +  // with AudioChannel::Telephony channel.
   1.662 +  if (NS_FAILED(TestOneVideoNormalChannel())) {
   1.663 +    return 1;
   1.664 +  }
   1.665 +
   1.666 +  if (NS_FAILED(TestPriorities())) {
   1.667 +    return 1;
   1.668 +  }
   1.669 +
   1.670 +  return 0;
   1.671 +}
   1.672 +

mercurial