|
9 | 9 | using NUnit.Framework;
|
10 | 10 | using osu.Framework.Allocation;
|
11 | 11 | using osu.Game.Beatmaps;
|
| 12 | +using osu.Game.Collections; |
12 | 13 | using osu.Game.Database;
|
13 | 14 | using osu.Game.Models;
|
14 | 15 | using osu.Game.Overlays.Notifications;
|
@@ -432,6 +433,126 @@ public void TestMetadataTransferred()
|
432 | 433 | });
|
433 | 434 | }
|
434 | 435 |
|
| 436 | + /// <summary> |
| 437 | + /// If all difficulties in the original beatmap set are in a collection, presume the user also wants new difficulties added. |
| 438 | + /// </summary> |
| 439 | + [TestCase(false)] |
| 440 | + [TestCase(true)] |
| 441 | + public void TestCollectionTransferNewBeatmap(bool allOriginalBeatmapsInCollection) |
| 442 | + { |
| 443 | + RunTestWithRealmAsync(async (realm, storage) => |
| 444 | + { |
| 445 | + var importer = new BeatmapImporter(storage, realm); |
| 446 | + using var rulesets = new RealmRulesetStore(realm, storage); |
| 447 | + |
| 448 | + using var __ = getBeatmapArchive(out string pathOriginal); |
| 449 | + using var _ = getBeatmapArchiveWithModifications(out string pathMissingOneBeatmap, directory => |
| 450 | + { |
| 451 | + // remove one difficulty before first import |
| 452 | + directory.GetFiles("*.osu").First().Delete(); |
| 453 | + }); |
| 454 | + |
| 455 | + var importBeforeUpdate = await importer.Import(new ImportTask(pathMissingOneBeatmap)); |
| 456 | + |
| 457 | + Assert.That(importBeforeUpdate, Is.Not.Null); |
| 458 | + Debug.Assert(importBeforeUpdate != null); |
| 459 | + |
| 460 | + int beatmapsToAddToCollection = 0; |
| 461 | + |
| 462 | + importBeforeUpdate.PerformWrite(s => |
| 463 | + { |
| 464 | + var beatmapCollection = s.Realm.Add(new BeatmapCollection("test collection")); |
| 465 | + beatmapsToAddToCollection = s.Beatmaps.Count - (allOriginalBeatmapsInCollection ? 0 : 1); |
| 466 | + |
| 467 | + for (int i = 0; i < beatmapsToAddToCollection; i++) |
| 468 | + beatmapCollection.BeatmapMD5Hashes.Add(s.Beatmaps[i].MD5Hash); |
| 469 | + }); |
| 470 | + |
| 471 | + // Second import matches first but contains one extra .osu file. |
| 472 | + var importAfterUpdate = await importer.ImportAsUpdate(new ProgressNotification(), new ImportTask(pathOriginal), importBeforeUpdate.Value); |
| 473 | + |
| 474 | + Assert.That(importAfterUpdate, Is.Not.Null); |
| 475 | + Debug.Assert(importAfterUpdate != null); |
| 476 | + |
| 477 | + importAfterUpdate.PerformRead(updated => |
| 478 | + { |
| 479 | + updated.Realm.Refresh(); |
| 480 | + |
| 481 | + string[] hashes = updated.Realm.All<BeatmapCollection>().Single().BeatmapMD5Hashes.ToArray(); |
| 482 | + |
| 483 | + if (allOriginalBeatmapsInCollection) |
| 484 | + { |
| 485 | + Assert.That(updated.Beatmaps.Count, Is.EqualTo(beatmapsToAddToCollection + 1)); |
| 486 | + Assert.That(hashes, Has.Length.EqualTo(updated.Beatmaps.Count)); |
| 487 | + } |
| 488 | + else |
| 489 | + { |
| 490 | + // Collection contains one less than the original beatmap, and two less after update (new difficulty included). |
| 491 | + Assert.That(updated.Beatmaps.Count, Is.EqualTo(beatmapsToAddToCollection + 2)); |
| 492 | + Assert.That(hashes, Has.Length.EqualTo(beatmapsToAddToCollection)); |
| 493 | + } |
| 494 | + }); |
| 495 | + }); |
| 496 | + } |
| 497 | + |
| 498 | + /// <summary> |
| 499 | + /// If a difficulty in the original beatmap set is modified, the updated version should remain in any collections it was in. |
| 500 | + /// </summary> |
| 501 | + [Test] |
| 502 | + public void TestCollectionTransferModifiedBeatmap() |
| 503 | + { |
| 504 | + RunTestWithRealmAsync(async (realm, storage) => |
| 505 | + { |
| 506 | + var importer = new BeatmapImporter(storage, realm); |
| 507 | + using var rulesets = new RealmRulesetStore(realm, storage); |
| 508 | + |
| 509 | + using var __ = getBeatmapArchive(out string pathOriginal); |
| 510 | + using var _ = getBeatmapArchiveWithModifications(out string pathModified, directory => |
| 511 | + { |
| 512 | + // Modify one .osu file with different content. |
| 513 | + var firstOsuFile = directory.GetFiles("*[Hard]*.osu").First(); |
| 514 | + |
| 515 | + string existingContent = File.ReadAllText(firstOsuFile.FullName); |
| 516 | + |
| 517 | + File.WriteAllText(firstOsuFile.FullName, existingContent + "\n# I am new content"); |
| 518 | + }); |
| 519 | + |
| 520 | + var importBeforeUpdate = await importer.Import(new ImportTask(pathOriginal)); |
| 521 | + |
| 522 | + Assert.That(importBeforeUpdate, Is.Not.Null); |
| 523 | + Debug.Assert(importBeforeUpdate != null); |
| 524 | + |
| 525 | + string originalHash = string.Empty; |
| 526 | + |
| 527 | + importBeforeUpdate.PerformWrite(s => |
| 528 | + { |
| 529 | + var beatmapCollection = s.Realm.Add(new BeatmapCollection("test collection")); |
| 530 | + originalHash = s.Beatmaps.Single(b => b.DifficultyName == "Hard").MD5Hash; |
| 531 | + |
| 532 | + beatmapCollection.BeatmapMD5Hashes.Add(originalHash); |
| 533 | + }); |
| 534 | + |
| 535 | + // Second import matches first but contains a modified .osu file. |
| 536 | + var importAfterUpdate = await importer.ImportAsUpdate(new ProgressNotification(), new ImportTask(pathModified), importBeforeUpdate.Value); |
| 537 | + |
| 538 | + Assert.That(importAfterUpdate, Is.Not.Null); |
| 539 | + Debug.Assert(importAfterUpdate != null); |
| 540 | + |
| 541 | + importAfterUpdate.PerformRead(updated => |
| 542 | + { |
| 543 | + updated.Realm.Refresh(); |
| 544 | + |
| 545 | + string[] hashes = updated.Realm.All<BeatmapCollection>().Single().BeatmapMD5Hashes.ToArray(); |
| 546 | + string updatedHash = updated.Beatmaps.Single(b => b.DifficultyName == "Hard").MD5Hash; |
| 547 | + |
| 548 | + Assert.That(hashes, Has.Length.EqualTo(1)); |
| 549 | + Assert.That(hashes.First(), Is.EqualTo(updatedHash)); |
| 550 | + |
| 551 | + Assert.That(updatedHash, Is.Not.EqualTo(originalHash)); |
| 552 | + }); |
| 553 | + }); |
| 554 | + } |
| 555 | + |
435 | 556 | private static void checkCount<T>(RealmAccess realm, int expected, Expression<Func<T, bool>>? condition = null) where T : RealmObject
|
436 | 557 | {
|
437 | 558 | var query = realm.Realm.All<T>();
|
|
0 commit comments