dom/audiochannel/tests/TestAudioChannelService.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 #ifdef XP_WIN
michael@0 5 #include <windows.h>
michael@0 6 #else
michael@0 7 #include <unistd.h>
michael@0 8 #endif
michael@0 9
michael@0 10 #include "TestHarness.h"
michael@0 11
michael@0 12 #include "nsWeakReference.h"
michael@0 13 #include "AudioChannelService.h"
michael@0 14 #include "AudioChannelAgent.h"
michael@0 15
michael@0 16 #define TEST_ENSURE_BASE(_test, _msg) \
michael@0 17 PR_BEGIN_MACRO \
michael@0 18 if (!(_test)) { \
michael@0 19 fail(_msg); \
michael@0 20 return NS_ERROR_FAILURE; \
michael@0 21 } else { \
michael@0 22 passed(_msg); \
michael@0 23 } \
michael@0 24 PR_END_MACRO
michael@0 25
michael@0 26 using namespace mozilla::dom;
michael@0 27
michael@0 28 class Agent : public nsIAudioChannelAgentCallback,
michael@0 29 public nsSupportsWeakReference
michael@0 30 {
michael@0 31 public:
michael@0 32 NS_DECL_ISUPPORTS
michael@0 33
michael@0 34 Agent(AudioChannel aChannel)
michael@0 35 : mChannel(aChannel)
michael@0 36 , mWaitCallback(false)
michael@0 37 , mRegistered(false)
michael@0 38 , mCanPlay(AUDIO_CHANNEL_STATE_MUTED)
michael@0 39 {
michael@0 40 mAgent = do_CreateInstance("@mozilla.org/audiochannelagent;1");
michael@0 41 }
michael@0 42
michael@0 43 virtual ~Agent()
michael@0 44 {
michael@0 45 if (mRegistered) {
michael@0 46 StopPlaying();
michael@0 47 }
michael@0 48 }
michael@0 49
michael@0 50 nsresult Init(bool video=false)
michael@0 51 {
michael@0 52 nsresult rv = NS_OK;
michael@0 53 if (video) {
michael@0 54 rv = mAgent->InitWithVideo(nullptr, static_cast<int32_t>(mChannel),
michael@0 55 this, true);
michael@0 56 }
michael@0 57 else {
michael@0 58 rv = mAgent->InitWithWeakCallback(nullptr, static_cast<int32_t>(mChannel),
michael@0 59 this);
michael@0 60 }
michael@0 61 NS_ENSURE_SUCCESS(rv, rv);
michael@0 62
michael@0 63 return mAgent->SetVisibilityState(false);
michael@0 64 }
michael@0 65
michael@0 66 nsresult StartPlaying(AudioChannelState *_ret)
michael@0 67 {
michael@0 68 if (mRegistered) {
michael@0 69 StopPlaying();
michael@0 70 }
michael@0 71
michael@0 72 nsresult rv = mAgent->StartPlaying((int32_t *)_ret);
michael@0 73 mRegistered = true;
michael@0 74 return rv;
michael@0 75 }
michael@0 76
michael@0 77 nsresult StopPlaying()
michael@0 78 {
michael@0 79 mRegistered = false;
michael@0 80 int loop = 0;
michael@0 81 while (mWaitCallback) {
michael@0 82 #ifdef XP_WIN
michael@0 83 Sleep(1000);
michael@0 84 #else
michael@0 85 sleep(1);
michael@0 86 #endif
michael@0 87 if (loop++ == 5) {
michael@0 88 TEST_ENSURE_BASE(false, "StopPlaying timeout");
michael@0 89 }
michael@0 90 }
michael@0 91 return mAgent->StopPlaying();
michael@0 92 }
michael@0 93
michael@0 94 nsresult SetVisibilityState(bool visible)
michael@0 95 {
michael@0 96 if (mRegistered) {
michael@0 97 mWaitCallback = true;
michael@0 98 }
michael@0 99 return mAgent->SetVisibilityState(visible);
michael@0 100 }
michael@0 101
michael@0 102 NS_IMETHODIMP CanPlayChanged(int32_t canPlay)
michael@0 103 {
michael@0 104 mCanPlay = static_cast<AudioChannelState>(canPlay);
michael@0 105 mWaitCallback = false;
michael@0 106 return NS_OK;
michael@0 107 }
michael@0 108
michael@0 109 NS_IMETHODIMP WindowVolumeChanged()
michael@0 110 {
michael@0 111 return NS_OK;
michael@0 112 }
michael@0 113
michael@0 114 nsresult GetCanPlay(AudioChannelState *_ret)
michael@0 115 {
michael@0 116 int loop = 0;
michael@0 117 while (mWaitCallback) {
michael@0 118 #ifdef XP_WIN
michael@0 119 Sleep(1000);
michael@0 120 #else
michael@0 121 sleep(1);
michael@0 122 #endif
michael@0 123 if (loop++ == 5) {
michael@0 124 TEST_ENSURE_BASE(false, "GetCanPlay timeout");
michael@0 125 }
michael@0 126 }
michael@0 127 *_ret = mCanPlay;
michael@0 128 return NS_OK;
michael@0 129 }
michael@0 130
michael@0 131 nsCOMPtr<nsIAudioChannelAgent> mAgent;
michael@0 132 AudioChannel mChannel;
michael@0 133 bool mWaitCallback;
michael@0 134 bool mRegistered;
michael@0 135 AudioChannelState mCanPlay;
michael@0 136 };
michael@0 137
michael@0 138 NS_IMPL_ISUPPORTS(Agent, nsIAudioChannelAgentCallback,
michael@0 139 nsISupportsWeakReference)
michael@0 140
michael@0 141 nsresult
michael@0 142 TestDoubleStartPlaying()
michael@0 143 {
michael@0 144 nsRefPtr<Agent> agent = new Agent(AudioChannel::Normal);
michael@0 145
michael@0 146 nsresult rv = agent->Init();
michael@0 147 NS_ENSURE_SUCCESS(rv, rv);
michael@0 148
michael@0 149 AudioChannelState playable;
michael@0 150 rv = agent->mAgent->StartPlaying((int32_t *)&playable);
michael@0 151 NS_ENSURE_SUCCESS(rv, rv);
michael@0 152
michael@0 153 rv = agent->mAgent->StartPlaying((int32_t *)&playable);
michael@0 154 TEST_ENSURE_BASE(NS_FAILED(rv),
michael@0 155 "Test0: StartPlaying calling twice must return error");
michael@0 156
michael@0 157 return NS_OK;
michael@0 158 }
michael@0 159
michael@0 160 nsresult
michael@0 161 TestOneNormalChannel()
michael@0 162 {
michael@0 163 nsRefPtr<Agent> agent = new Agent(AudioChannel::Normal);
michael@0 164 nsresult rv = agent->Init();
michael@0 165 NS_ENSURE_SUCCESS(rv, rv);
michael@0 166
michael@0 167 AudioChannelState playable;
michael@0 168 rv = agent->StartPlaying(&playable);
michael@0 169 NS_ENSURE_SUCCESS(rv, rv);
michael@0 170 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
michael@0 171 "Test1: A normal channel unvisible agent must be muted");
michael@0 172
michael@0 173 rv = agent->SetVisibilityState(true);
michael@0 174 NS_ENSURE_SUCCESS(rv, rv);
michael@0 175
michael@0 176 rv = agent->GetCanPlay(&playable);
michael@0 177 NS_ENSURE_SUCCESS(rv, rv);
michael@0 178 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 179 "Test1: A normal channel visible agent must be playable");
michael@0 180
michael@0 181 return rv;
michael@0 182 }
michael@0 183
michael@0 184 nsresult
michael@0 185 TestTwoNormalChannels()
michael@0 186 {
michael@0 187 nsRefPtr<Agent> agent1 = new Agent(AudioChannel::Normal);
michael@0 188 nsresult rv = agent1->Init();
michael@0 189 NS_ENSURE_SUCCESS(rv, rv);
michael@0 190
michael@0 191 nsRefPtr<Agent> agent2 = new Agent(AudioChannel::Normal);
michael@0 192 rv = agent2->Init();
michael@0 193 NS_ENSURE_SUCCESS(rv, rv);
michael@0 194
michael@0 195 AudioChannelState playable;
michael@0 196 rv = agent1->StartPlaying(&playable);
michael@0 197 NS_ENSURE_SUCCESS(rv, rv);
michael@0 198 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
michael@0 199 "Test2: A normal channel unvisible agent1 must be muted");
michael@0 200
michael@0 201 rv = agent2->StartPlaying(&playable);
michael@0 202 NS_ENSURE_SUCCESS(rv, rv);
michael@0 203 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
michael@0 204 "Test2: A normal channel unvisible agent2 must be muted");
michael@0 205
michael@0 206 rv = agent1->SetVisibilityState(true);
michael@0 207 NS_ENSURE_SUCCESS(rv, rv);
michael@0 208
michael@0 209 rv = agent2->SetVisibilityState(true);
michael@0 210 NS_ENSURE_SUCCESS(rv, rv);
michael@0 211
michael@0 212 rv = agent1->GetCanPlay(&playable);
michael@0 213 NS_ENSURE_SUCCESS(rv, rv);
michael@0 214 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 215 "Test2: A normal channel visible agent1 must be playable");
michael@0 216
michael@0 217 rv = agent2->GetCanPlay(&playable);
michael@0 218 NS_ENSURE_SUCCESS(rv, rv);
michael@0 219 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 220 "Test2: A normal channel visible agent2 must be playable");
michael@0 221
michael@0 222 return rv;
michael@0 223 }
michael@0 224
michael@0 225 nsresult
michael@0 226 TestContentChannels()
michael@0 227 {
michael@0 228 nsRefPtr<Agent> agent1 = new Agent(AudioChannel::Content);
michael@0 229 nsresult rv = agent1->Init();
michael@0 230 NS_ENSURE_SUCCESS(rv, rv);
michael@0 231
michael@0 232 nsRefPtr<Agent> agent2 = new Agent(AudioChannel::Content);
michael@0 233 rv = agent2->Init();
michael@0 234 NS_ENSURE_SUCCESS(rv, rv);
michael@0 235
michael@0 236 // All content channels in the foreground can be allowed to play
michael@0 237 rv = agent1->SetVisibilityState(true);
michael@0 238 NS_ENSURE_SUCCESS(rv, rv);
michael@0 239
michael@0 240 rv = agent2->SetVisibilityState(true);
michael@0 241 NS_ENSURE_SUCCESS(rv, rv);
michael@0 242
michael@0 243 AudioChannelState playable;
michael@0 244 rv = agent1->StartPlaying(&playable);
michael@0 245 NS_ENSURE_SUCCESS(rv, rv);
michael@0 246 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 247 "Test3: A content channel visible agent1 must be playable");
michael@0 248
michael@0 249 rv = agent2->StartPlaying(&playable);
michael@0 250 NS_ENSURE_SUCCESS(rv, rv);
michael@0 251 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 252 "Test3: A content channel visible agent2 must be playable");
michael@0 253
michael@0 254 // Test the transition state of one content channel tried to set non-visible
michael@0 255 // state first when app is going to background.
michael@0 256 rv = agent1->SetVisibilityState(false);
michael@0 257 NS_ENSURE_SUCCESS(rv, rv);
michael@0 258
michael@0 259 rv = agent1->GetCanPlay(&playable);
michael@0 260 NS_ENSURE_SUCCESS(rv, rv);
michael@0 261 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 262 "Test3: A content channel unvisible agent1 must be playable from "
michael@0 263 "foreground to background");
michael@0 264
michael@0 265 // Test all content channels set non-visible already
michael@0 266 rv = agent2->SetVisibilityState(false);
michael@0 267 NS_ENSURE_SUCCESS(rv, rv);
michael@0 268
michael@0 269 rv = agent2->GetCanPlay(&playable);
michael@0 270 NS_ENSURE_SUCCESS(rv, rv);
michael@0 271 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 272 "Test3: A content channel unvisible agent2 must be playable from "
michael@0 273 "foreground to background");
michael@0 274
michael@0 275 // Clear the content channels & mActiveContentChildIDs in AudioChannelService.
michael@0 276 // If agent stop playable in the background, we will reserve it's childID in
michael@0 277 // mActiveContentChildIDs, then it can allow to play next song. So we set agents
michael@0 278 // to foreground first then stopping to play
michael@0 279 rv = agent1->SetVisibilityState(true);
michael@0 280 NS_ENSURE_SUCCESS(rv, rv);
michael@0 281 rv = agent2->SetVisibilityState(true);
michael@0 282 NS_ENSURE_SUCCESS(rv, rv);
michael@0 283 rv = agent1->StopPlaying();
michael@0 284 NS_ENSURE_SUCCESS(rv, rv);
michael@0 285 rv = agent2->StopPlaying();
michael@0 286 NS_ENSURE_SUCCESS(rv, rv);
michael@0 287
michael@0 288 // Test that content channels can be allow to play when they starts from
michael@0 289 // the background state
michael@0 290 rv = agent1->SetVisibilityState(false);
michael@0 291 NS_ENSURE_SUCCESS(rv, rv);
michael@0 292 rv = agent2->SetVisibilityState(false);
michael@0 293 NS_ENSURE_SUCCESS(rv, rv);
michael@0 294
michael@0 295 rv = agent1->StartPlaying(&playable);
michael@0 296 NS_ENSURE_SUCCESS(rv, rv);
michael@0 297 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 298 "Test3: A content channel unvisible agent1 must be playable "
michael@0 299 "from background state");
michael@0 300
michael@0 301 rv = agent2->StartPlaying(&playable);
michael@0 302 NS_ENSURE_SUCCESS(rv, rv);
michael@0 303 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 304 "Test3: A content channel unvisible agent2 must be playable "
michael@0 305 "from background state");
michael@0 306
michael@0 307 return rv;
michael@0 308 }
michael@0 309
michael@0 310 nsresult
michael@0 311 TestFadedState()
michael@0 312 {
michael@0 313 nsRefPtr<Agent> normalAgent = new Agent(AudioChannel::Normal);
michael@0 314 nsresult rv = normalAgent->Init();
michael@0 315 NS_ENSURE_SUCCESS(rv, rv);
michael@0 316
michael@0 317 nsRefPtr<Agent> contentAgent = new Agent(AudioChannel::Content);
michael@0 318 rv = contentAgent->Init();
michael@0 319 NS_ENSURE_SUCCESS(rv, rv);
michael@0 320
michael@0 321 nsRefPtr<Agent> notificationAgent = new Agent(AudioChannel::Notification);
michael@0 322 rv = notificationAgent->Init();
michael@0 323 NS_ENSURE_SUCCESS(rv, rv);
michael@0 324
michael@0 325 rv = normalAgent->SetVisibilityState(true);
michael@0 326 NS_ENSURE_SUCCESS(rv, rv);
michael@0 327
michael@0 328 rv = contentAgent->SetVisibilityState(true);
michael@0 329 NS_ENSURE_SUCCESS(rv, rv);
michael@0 330
michael@0 331 rv = notificationAgent->SetVisibilityState(true);
michael@0 332 NS_ENSURE_SUCCESS(rv, rv);
michael@0 333
michael@0 334 AudioChannelState playable;
michael@0 335 rv = normalAgent->StartPlaying(&playable);
michael@0 336 NS_ENSURE_SUCCESS(rv, rv);
michael@0 337 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 338 "Test4: A normal channel visible agent must be playable");
michael@0 339
michael@0 340 rv = contentAgent->StartPlaying(&playable);
michael@0 341 NS_ENSURE_SUCCESS(rv, rv);
michael@0 342 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 343 "Test4: A content channel visible agent must be playable");
michael@0 344
michael@0 345 rv = notificationAgent->StartPlaying(&playable);
michael@0 346 NS_ENSURE_SUCCESS(rv, rv);
michael@0 347 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 348 "Test4: A notification channel visible agent must be playable");
michael@0 349
michael@0 350 rv = contentAgent->GetCanPlay(&playable);
michael@0 351 NS_ENSURE_SUCCESS(rv, rv);
michael@0 352 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_FADED,
michael@0 353 "Test4: A content channel unvisible agent must be faded because of "
michael@0 354 "notification channel is playing");
michael@0 355
michael@0 356 rv = contentAgent->SetVisibilityState(false);
michael@0 357 NS_ENSURE_SUCCESS(rv, rv);
michael@0 358
michael@0 359 rv = contentAgent->GetCanPlay(&playable);
michael@0 360 NS_ENSURE_SUCCESS(rv, rv);
michael@0 361 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_FADED,
michael@0 362 "Test4: A content channel unvisible agent must be faded because of "
michael@0 363 "notification channel is playing");
michael@0 364
michael@0 365 rv = notificationAgent->SetVisibilityState(false);
michael@0 366 NS_ENSURE_SUCCESS(rv, rv);
michael@0 367
michael@0 368 rv = notificationAgent->GetCanPlay(&playable);
michael@0 369 NS_ENSURE_SUCCESS(rv, rv);
michael@0 370 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 371 "Test4: A notification channel unvisible agent must be playable from "
michael@0 372 "foreground to background");
michael@0 373
michael@0 374 rv = notificationAgent->StopPlaying();
michael@0 375 NS_ENSURE_SUCCESS(rv, rv);
michael@0 376
michael@0 377 rv = contentAgent->GetCanPlay(&playable);
michael@0 378 NS_ENSURE_SUCCESS(rv, rv);
michael@0 379 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 380 "Test4: A content channel unvisible agent must be playable "
michael@0 381 "because of notification channel is stopped");
michael@0 382
michael@0 383 rv = contentAgent->SetVisibilityState(true);
michael@0 384 NS_ENSURE_SUCCESS(rv, rv);
michael@0 385
michael@0 386 return rv;
michael@0 387 }
michael@0 388
michael@0 389 nsresult
michael@0 390 TestPriorities()
michael@0 391 {
michael@0 392 nsRefPtr<Agent> normalAgent = new Agent(AudioChannel::Normal);
michael@0 393 nsresult rv = normalAgent->Init();
michael@0 394 NS_ENSURE_SUCCESS(rv, rv);
michael@0 395
michael@0 396 nsRefPtr<Agent> contentAgent = new Agent(AudioChannel::Content);
michael@0 397 rv = contentAgent->Init();
michael@0 398 NS_ENSURE_SUCCESS(rv, rv);
michael@0 399
michael@0 400 nsRefPtr<Agent> notificationAgent = new Agent(AudioChannel::Notification);
michael@0 401 rv = notificationAgent->Init();
michael@0 402 NS_ENSURE_SUCCESS(rv, rv);
michael@0 403
michael@0 404 nsRefPtr<Agent> alarmAgent = new Agent(AudioChannel::Alarm);
michael@0 405 rv = alarmAgent->Init();
michael@0 406 NS_ENSURE_SUCCESS(rv, rv);
michael@0 407
michael@0 408 nsRefPtr<Agent> telephonyAgent = new Agent(AudioChannel::Telephony);
michael@0 409 rv = telephonyAgent->Init();
michael@0 410 NS_ENSURE_SUCCESS(rv, rv);
michael@0 411
michael@0 412 nsRefPtr<Agent> ringerAgent = new Agent(AudioChannel::Ringer);
michael@0 413 rv = ringerAgent->Init();
michael@0 414 NS_ENSURE_SUCCESS(rv, rv);
michael@0 415
michael@0 416 nsRefPtr<Agent> pNotificationAgent =
michael@0 417 new Agent(AudioChannel::Publicnotification);
michael@0 418 rv = pNotificationAgent->Init();
michael@0 419 NS_ENSURE_SUCCESS(rv, rv);
michael@0 420
michael@0 421 AudioChannelState playable;
michael@0 422
michael@0 423 rv = normalAgent->StartPlaying(&playable);
michael@0 424 NS_ENSURE_SUCCESS(rv, rv);
michael@0 425 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
michael@0 426 "Test5: A normal channel unvisible agent must be muted");
michael@0 427
michael@0 428 rv = contentAgent->StartPlaying(&playable);
michael@0 429 NS_ENSURE_SUCCESS(rv, rv);
michael@0 430 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 431 "Test5: A content channel unvisible agent must be playable while "
michael@0 432 "playing from background state");
michael@0 433
michael@0 434 rv = notificationAgent->StartPlaying(&playable);
michael@0 435 NS_ENSURE_SUCCESS(rv, rv);
michael@0 436 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 437 "Test5: A notification channel unvisible agent must be playable");
michael@0 438
michael@0 439 rv = alarmAgent->StartPlaying(&playable);
michael@0 440 NS_ENSURE_SUCCESS(rv, rv);
michael@0 441 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 442 "Test5: An alarm channel unvisible agent must be playable");
michael@0 443
michael@0 444 rv = notificationAgent->StartPlaying(&playable);
michael@0 445 NS_ENSURE_SUCCESS(rv, rv);
michael@0 446 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
michael@0 447 "Test5: A notification channel unvisible agent must be muted when an "
michael@0 448 "alarm is playing");
michael@0 449
michael@0 450 rv = telephonyAgent->StartPlaying(&playable);
michael@0 451 NS_ENSURE_SUCCESS(rv, rv);
michael@0 452 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 453 "Test5: A telephony channel unvisible agent must be playable");
michael@0 454
michael@0 455 rv = alarmAgent->StartPlaying(&playable);
michael@0 456 NS_ENSURE_SUCCESS(rv, rv);
michael@0 457 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
michael@0 458 "Test5: An alarm channel unvisible agent must be muted when a telephony "
michael@0 459 "is playing");
michael@0 460
michael@0 461 rv = ringerAgent->StartPlaying(&playable);
michael@0 462 NS_ENSURE_SUCCESS(rv, rv);
michael@0 463 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 464 "Test5: A ringer channel unvisible agent must be playable");
michael@0 465
michael@0 466 rv = telephonyAgent->StartPlaying(&playable);
michael@0 467 NS_ENSURE_SUCCESS(rv, rv);
michael@0 468 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
michael@0 469 "Test5: A telephony channel unvisible agent must be muted when a ringer "
michael@0 470 "is playing");
michael@0 471
michael@0 472 rv = pNotificationAgent->StartPlaying(&playable);
michael@0 473 NS_ENSURE_SUCCESS(rv, rv);
michael@0 474 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 475 "Test5: A pNotification channel unvisible agent must be playable");
michael@0 476
michael@0 477 rv = ringerAgent->StartPlaying(&playable);
michael@0 478 NS_ENSURE_SUCCESS(rv, rv);
michael@0 479 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
michael@0 480 "Test5: A ringer channel unvisible agent must be muted when a public "
michael@0 481 "notification is playing");
michael@0 482
michael@0 483 // Stop to play notification channel or normal/content will be faded.
michael@0 484 // Which already be tested on Test 4.
michael@0 485 rv = notificationAgent->StopPlaying();
michael@0 486 NS_ENSURE_SUCCESS(rv, rv);
michael@0 487
michael@0 488 // Settings visible the normal channel.
michael@0 489 rv = normalAgent->SetVisibilityState(true);
michael@0 490 NS_ENSURE_SUCCESS(rv, rv);
michael@0 491
michael@0 492 rv = normalAgent->GetCanPlay(&playable);
michael@0 493 NS_ENSURE_SUCCESS(rv, rv);
michael@0 494 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 495 "Test5: A normal channel visible agent must be playable");
michael@0 496
michael@0 497 // Set the content channel as visible .
michael@0 498 rv = contentAgent->SetVisibilityState(true);
michael@0 499 NS_ENSURE_SUCCESS(rv, rv);
michael@0 500
michael@0 501 // Content must be playable because visible.
michael@0 502 rv = contentAgent->GetCanPlay(&playable);
michael@0 503 NS_ENSURE_SUCCESS(rv, rv);
michael@0 504 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 505 "Test5: A content channel visible agent must be playable");
michael@0 506
michael@0 507 // Set the alarm channel as visible.
michael@0 508 rv = alarmAgent->SetVisibilityState(true);
michael@0 509 NS_ENSURE_SUCCESS(rv, rv);
michael@0 510
michael@0 511 rv = alarmAgent->GetCanPlay(&playable);
michael@0 512 NS_ENSURE_SUCCESS(rv, rv);
michael@0 513 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 514 "Test5: An alarm channel visible agent must be playable");
michael@0 515
michael@0 516 // Set the telephony channel as visible.
michael@0 517 rv = telephonyAgent->SetVisibilityState(true);
michael@0 518 NS_ENSURE_SUCCESS(rv, rv);
michael@0 519
michael@0 520 rv = telephonyAgent->GetCanPlay(&playable);
michael@0 521 NS_ENSURE_SUCCESS(rv, rv);
michael@0 522 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 523 "Test5: A telephony channel visible agent must be playable");
michael@0 524
michael@0 525 // Set the ringer channel as visible.
michael@0 526 rv = ringerAgent->SetVisibilityState(true);
michael@0 527 NS_ENSURE_SUCCESS(rv, rv);
michael@0 528
michael@0 529 rv = ringerAgent->GetCanPlay(&playable);
michael@0 530 NS_ENSURE_SUCCESS(rv, rv);
michael@0 531 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 532 "Test5: A ringer channel visible agent must be playable");
michael@0 533
michael@0 534 // Set the public notification channel as visible.
michael@0 535 rv = pNotificationAgent->SetVisibilityState(true);
michael@0 536 NS_ENSURE_SUCCESS(rv, rv);
michael@0 537
michael@0 538 rv = pNotificationAgent->GetCanPlay(&playable);
michael@0 539 NS_ENSURE_SUCCESS(rv, rv);
michael@0 540 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 541 "Test5: A pNotification channel visible agent must be playable");
michael@0 542
michael@0 543 return rv;
michael@0 544 }
michael@0 545
michael@0 546 nsresult
michael@0 547 TestOneVideoNormalChannel()
michael@0 548 {
michael@0 549 nsRefPtr<Agent> agent1 = new Agent(AudioChannel::Normal);
michael@0 550 nsresult rv = agent1->Init(true);
michael@0 551 NS_ENSURE_SUCCESS(rv, rv);
michael@0 552
michael@0 553 nsRefPtr<Agent> agent2 = new Agent(AudioChannel::Content);
michael@0 554 rv = agent2->Init(false);
michael@0 555 NS_ENSURE_SUCCESS(rv, rv);
michael@0 556
michael@0 557 AudioChannelState playable;
michael@0 558 rv = agent1->StartPlaying(&playable);
michael@0 559 NS_ENSURE_SUCCESS(rv, rv);
michael@0 560 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
michael@0 561 "Test6: A video normal channel invisible agent1 must be muted");
michael@0 562
michael@0 563 rv = agent2->StartPlaying(&playable);
michael@0 564 NS_ENSURE_SUCCESS(rv, rv);
michael@0 565 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 566 "Test6: A content channel invisible agent2 must be playable");
michael@0 567
michael@0 568 // one video normal channel in foreground and one content channel in background
michael@0 569 rv = agent1->SetVisibilityState(true);
michael@0 570 NS_ENSURE_SUCCESS(rv, rv);
michael@0 571
michael@0 572 rv = agent1->GetCanPlay(&playable);
michael@0 573 NS_ENSURE_SUCCESS(rv, rv);
michael@0 574 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 575 "Test6: A video normal channel visible agent1 must be playable");
michael@0 576
michael@0 577 rv = agent2->GetCanPlay(&playable);
michael@0 578 NS_ENSURE_SUCCESS(rv, rv);
michael@0 579 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
michael@0 580 "Test6: A content channel invisible agent2 must be muted");
michael@0 581
michael@0 582 // both one video normal channel and one content channel in foreground
michael@0 583 rv = agent2->SetVisibilityState(true);
michael@0 584 NS_ENSURE_SUCCESS(rv, rv);
michael@0 585
michael@0 586 rv = agent1->GetCanPlay(&playable);
michael@0 587 NS_ENSURE_SUCCESS(rv, rv);
michael@0 588 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 589 "Test6: A video normal channel visible agent1 must be playable");
michael@0 590
michael@0 591 rv = agent2->GetCanPlay(&playable);
michael@0 592 NS_ENSURE_SUCCESS(rv, rv);
michael@0 593 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 594 "Test6: A content channel visible agent2 must be playable");
michael@0 595
michael@0 596 // one video normal channel in background and one content channel in foreground
michael@0 597 rv = agent1->SetVisibilityState(false);
michael@0 598 NS_ENSURE_SUCCESS(rv, rv);
michael@0 599
michael@0 600 rv = agent1->GetCanPlay(&playable);
michael@0 601 NS_ENSURE_SUCCESS(rv, rv);
michael@0 602 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
michael@0 603 "Test6: A video normal channel invisible agent1 must be muted");
michael@0 604
michael@0 605 rv = agent2->GetCanPlay(&playable);
michael@0 606 NS_ENSURE_SUCCESS(rv, rv);
michael@0 607 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 608 "Test6: A content channel visible agent2 must be playable");
michael@0 609
michael@0 610 // both one video normal channel and one content channel in background
michael@0 611 rv = agent2->SetVisibilityState(false);
michael@0 612 NS_ENSURE_SUCCESS(rv, rv);
michael@0 613
michael@0 614 rv = agent1->GetCanPlay(&playable);
michael@0 615 NS_ENSURE_SUCCESS(rv, rv);
michael@0 616 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_MUTED,
michael@0 617 "Test6: A video normal channel invisible agent1 must be muted");
michael@0 618
michael@0 619 rv = agent2->GetCanPlay(&playable);
michael@0 620 NS_ENSURE_SUCCESS(rv, rv);
michael@0 621 TEST_ENSURE_BASE(playable == AUDIO_CHANNEL_STATE_NORMAL,
michael@0 622 "Test6: A content channel invisible agent2 must be playable");
michael@0 623
michael@0 624 return rv;
michael@0 625 }
michael@0 626
michael@0 627 int main(int argc, char** argv)
michael@0 628 {
michael@0 629 ScopedXPCOM xpcom("AudioChannelService");
michael@0 630 if (xpcom.failed()) {
michael@0 631 return 1;
michael@0 632 }
michael@0 633
michael@0 634 if (NS_FAILED(TestDoubleStartPlaying())) {
michael@0 635 return 1;
michael@0 636 }
michael@0 637
michael@0 638 if (NS_FAILED(TestOneNormalChannel())) {
michael@0 639 return 1;
michael@0 640 }
michael@0 641
michael@0 642 if (NS_FAILED(TestTwoNormalChannels())) {
michael@0 643 return 1;
michael@0 644 }
michael@0 645
michael@0 646 if (NS_FAILED(TestContentChannels())) {
michael@0 647 return 1;
michael@0 648 }
michael@0 649
michael@0 650 if (NS_FAILED(TestFadedState())) {
michael@0 651 return 1;
michael@0 652 }
michael@0 653
michael@0 654 // Channel type with AudioChannel::Telephony cannot be unregistered until the
michael@0 655 // main thread has chances to process 1500 millisecond timer. In order to
michael@0 656 // skip ambiguous return value of ChannelsActiveWithHigherPriorityThan(), new
michael@0 657 // test cases are added before any test case that registers the channel type
michael@0 658 // with AudioChannel::Telephony channel.
michael@0 659 if (NS_FAILED(TestOneVideoNormalChannel())) {
michael@0 660 return 1;
michael@0 661 }
michael@0 662
michael@0 663 if (NS_FAILED(TestPriorities())) {
michael@0 664 return 1;
michael@0 665 }
michael@0 666
michael@0 667 return 0;
michael@0 668 }
michael@0 669

mercurial