|
| 1 | +import copy |
1 | 2 | import os |
2 | 3 | import pathlib |
3 | 4 | import platform |
@@ -1330,6 +1331,114 @@ def __init__(self): |
1330 | 1331 |
|
1331 | 1332 | self.assertRaises(RuntimeError, incorrect.get_volume) |
1332 | 1333 |
|
| 1334 | + def test_snd_copy(self): |
| 1335 | + class SubSound(mixer.Sound): |
| 1336 | + def __init__(self, *args, **kwargs): |
| 1337 | + super().__init__(*args, **kwargs) |
| 1338 | + |
| 1339 | + mixer.init() |
| 1340 | + |
| 1341 | + filenames = [ |
| 1342 | + "house_lo.ogg", |
| 1343 | + "house_lo.wav", |
| 1344 | + "house_lo.flac", |
| 1345 | + "house_lo.opus", |
| 1346 | + "surfonasinewave.xm", |
| 1347 | + ] |
| 1348 | + old_volumes = [0.1, 0.2, 0.5, 0.7, 1.0] |
| 1349 | + new_volumes = [0.2, 0.3, 0.7, 1.0, 0.1] |
| 1350 | + if pygame.mixer.get_sdl_mixer_version() >= (2, 6, 0): |
| 1351 | + filenames.append("house_lo.mp3") |
| 1352 | + old_volumes.append(0.9) |
| 1353 | + new_volumes.append(0.5) |
| 1354 | + |
| 1355 | + for f, old_vol, new_vol in zip(filenames, old_volumes, new_volumes): |
| 1356 | + filename = example_path(os.path.join("data", f)) |
| 1357 | + try: |
| 1358 | + sound = mixer.Sound(file=filename) |
| 1359 | + sound.set_volume(old_vol) |
| 1360 | + except pygame.error: |
| 1361 | + continue |
| 1362 | + sound_copy = sound.copy() |
| 1363 | + self.assertEqual(sound.get_length(), sound_copy.get_length()) |
| 1364 | + self.assertEqual(sound.get_num_channels(), sound_copy.get_num_channels()) |
| 1365 | + self.assertEqual(sound.get_volume(), sound_copy.get_volume()) |
| 1366 | + self.assertEqual(sound.get_raw(), sound_copy.get_raw()) |
| 1367 | + |
| 1368 | + sound.set_volume(new_vol) |
| 1369 | + self.assertNotEqual(sound.get_volume(), sound_copy.get_volume()) |
| 1370 | + |
| 1371 | + del sound |
| 1372 | + |
| 1373 | + # Test on the copy for playable sounds |
| 1374 | + channel = sound_copy.play() |
| 1375 | + if channel is None: |
| 1376 | + continue |
| 1377 | + self.assertTrue(channel.get_busy()) |
| 1378 | + sound_copy.stop() |
| 1379 | + self.assertFalse(channel.get_busy()) |
| 1380 | + sound_copy.play() |
| 1381 | + self.assertEqual(sound_copy.get_num_channels(), 1) |
| 1382 | + |
| 1383 | + # Test __copy__ |
| 1384 | + for f, old_vol, new_vol in zip(filenames, old_volumes, new_volumes): |
| 1385 | + filename = example_path(os.path.join("data", f)) |
| 1386 | + try: |
| 1387 | + sound = mixer.Sound(file=filename) |
| 1388 | + sound.set_volume(old_vol) |
| 1389 | + except pygame.error: |
| 1390 | + continue |
| 1391 | + sound_copy = copy.copy(sound) |
| 1392 | + self.assertEqual(sound.get_length(), sound_copy.get_length()) |
| 1393 | + self.assertEqual(sound.get_num_channels(), sound_copy.get_num_channels()) |
| 1394 | + self.assertEqual(sound.get_volume(), sound_copy.get_volume()) |
| 1395 | + self.assertEqual(sound.get_raw(), sound_copy.get_raw()) |
| 1396 | + |
| 1397 | + sound.set_volume(new_vol) |
| 1398 | + self.assertNotEqual(sound.get_volume(), sound_copy.get_volume()) |
| 1399 | + |
| 1400 | + del sound |
| 1401 | + |
| 1402 | + # Test on the copy for playable sounds |
| 1403 | + channel = sound_copy.play() |
| 1404 | + if channel is None: |
| 1405 | + continue |
| 1406 | + self.assertTrue(channel.get_busy()) |
| 1407 | + sound_copy.stop() |
| 1408 | + self.assertFalse(channel.get_busy()) |
| 1409 | + sound_copy.play() |
| 1410 | + self.assertEqual(sound_copy.get_num_channels(), 1) |
| 1411 | + |
| 1412 | + # Test copying a subclass of Sound |
| 1413 | + for f, old_vol, new_vol in zip(filenames, old_volumes, new_volumes): |
| 1414 | + filename = example_path(os.path.join("data", f)) |
| 1415 | + try: |
| 1416 | + sound = SubSound(file=filename) |
| 1417 | + sound.set_volume(old_vol) |
| 1418 | + except pygame.error: |
| 1419 | + continue |
| 1420 | + sound_copy = sound.copy() |
| 1421 | + self.assertIsInstance(sound_copy, SubSound) |
| 1422 | + self.assertEqual(sound.get_length(), sound_copy.get_length()) |
| 1423 | + self.assertEqual(sound.get_num_channels(), sound_copy.get_num_channels()) |
| 1424 | + self.assertEqual(sound.get_volume(), sound_copy.get_volume()) |
| 1425 | + self.assertEqual(sound.get_raw(), sound_copy.get_raw()) |
| 1426 | + |
| 1427 | + sound.set_volume(new_vol) |
| 1428 | + self.assertNotEqual(sound.get_volume(), sound_copy.get_volume()) |
| 1429 | + |
| 1430 | + del sound |
| 1431 | + |
| 1432 | + # Test on the copy for playable sounds |
| 1433 | + channel = sound_copy.play() |
| 1434 | + if channel is None: |
| 1435 | + continue |
| 1436 | + self.assertTrue(channel.get_busy()) |
| 1437 | + sound_copy.stop() |
| 1438 | + self.assertFalse(channel.get_busy()) |
| 1439 | + sound_copy.play() |
| 1440 | + self.assertEqual(sound_copy.get_num_channels(), 1) |
| 1441 | + |
1333 | 1442 |
|
1334 | 1443 | ##################################### MAIN ##################################### |
1335 | 1444 |
|
|
0 commit comments