A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System;
5using System.Collections.Generic;
6using System.IO;
7using NUnit.Framework;
8using osu.Framework.Testing;
9
10namespace osu.Framework.Tests.Platform
11{
12 [TestFixture]
13 public class UserStorageLookupTest
14 {
15 private static readonly string path1 = Path.Combine(TestRunHeadlessGameHost.TemporaryTestDirectory, "path1");
16 private static readonly string path2 = Path.Combine(TestRunHeadlessGameHost.TemporaryTestDirectory, "path2");
17
18 private const string game_name = "test_game";
19
20 [TearDown]
21 public void TearDown()
22 {
23 try
24 {
25 File.Delete(path1);
26 File.Delete(path2);
27 }
28 catch
29 {
30 }
31
32 try
33 {
34 Directory.Delete(path1, true);
35 Directory.Delete(path2, true);
36 }
37 catch
38 {
39 }
40 }
41
42 [Test]
43 public void TestFirstBaseExisting()
44 {
45 Directory.CreateDirectory(path1);
46
47 using (var host = new StorageLookupHeadlessGameHost())
48 {
49 runHost(host);
50 Assert.IsTrue(host.Storage.GetFullPath(string.Empty).StartsWith(path1, StringComparison.Ordinal));
51 }
52 }
53
54 [Test]
55 public void TestSecondBaseExistingStillPrefersFirst()
56 {
57 Directory.CreateDirectory(path2);
58
59 using (var host = new StorageLookupHeadlessGameHost())
60 {
61 runHost(host);
62 Assert.IsTrue(host.Storage.GetFullPath(string.Empty).StartsWith(path1, StringComparison.Ordinal));
63 }
64 }
65
66 [Test]
67 public void TestSecondBaseUsedIfFirstFails()
68 {
69 // write a file so directory creation fails.
70 File.WriteAllText(path1, "");
71
72 using (var host = new StorageLookupHeadlessGameHost())
73 {
74 runHost(host);
75 Assert.IsTrue(host.Storage.GetFullPath(string.Empty).StartsWith(path2, StringComparison.Ordinal));
76 }
77 }
78
79 [Test]
80 public void TestFirstDataExisting()
81 {
82 Directory.CreateDirectory(Path.Combine(path1, game_name));
83
84 using (var host = new StorageLookupHeadlessGameHost())
85 {
86 runHost(host);
87 Assert.IsTrue(host.Storage.GetFullPath(string.Empty).StartsWith(path1, StringComparison.Ordinal));
88 }
89 }
90
91 [Test]
92 public void TestSecondDataExisting()
93 {
94 Directory.CreateDirectory(Path.Combine(path2, game_name));
95
96 using (var host = new StorageLookupHeadlessGameHost())
97 {
98 runHost(host);
99 Assert.IsTrue(host.Storage.GetFullPath(string.Empty).StartsWith(path2, StringComparison.Ordinal));
100 }
101 }
102
103 [Test]
104 public void TestPrefersFirstData()
105 {
106 Directory.CreateDirectory(Path.Combine(path1, game_name));
107 Directory.CreateDirectory(Path.Combine(path2, game_name));
108
109 using (var host = new StorageLookupHeadlessGameHost())
110 {
111 runHost(host);
112 Assert.IsTrue(host.Storage.GetFullPath(string.Empty).StartsWith(path1, StringComparison.Ordinal));
113 }
114 }
115
116 [Test]
117 public void TestPrefersSecondDataOverFirstBase()
118 {
119 Directory.CreateDirectory(path1);
120 Directory.CreateDirectory(Path.Combine(path2, game_name));
121
122 using (var host = new StorageLookupHeadlessGameHost())
123 {
124 runHost(host);
125 Assert.IsTrue(host.Storage.GetFullPath(string.Empty).StartsWith(path2, StringComparison.Ordinal));
126 }
127 }
128
129 private static void runHost(StorageLookupHeadlessGameHost host)
130 {
131 TestGame game = new TestGame();
132 game.Schedule(() => game.Exit());
133 host.Run(game);
134 }
135
136 private class StorageLookupHeadlessGameHost : TestRunHeadlessGameHost
137 {
138 public StorageLookupHeadlessGameHost()
139 : base(game_name)
140 {
141 }
142
143 public override IEnumerable<string> UserStoragePaths => new[]
144 {
145 path1,
146 path2,
147 };
148 }
149 }
150}