-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathAdbServerTests.Async.cs
More file actions
234 lines (184 loc) · 9.35 KB
/
AdbServerTests.Async.cs
File metadata and controls
234 lines (184 loc) · 9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using System;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace AdvancedSharpAdbClient.Tests
{
public partial class AdbServerTests
{
/// <summary>
/// Tests the <see cref="AdbServer.GetStatusAsync(CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task GetStatusAsyncNotRunningTest()
{
IAdbSocket adbSocketMock = Substitute.For<IAdbSocket>();
adbSocketMock.SendAdbRequestAsync("host:version", Arg.Any<CancellationToken>()).Throws(new AggregateException(new SocketException(AdbServer.ConnectionRefused)));
AdbServer adbServer = new(endPoint => adbSocketMock, adbCommandLineClientFactory);
AdbServerStatus status = await adbServer.GetStatusAsync(TestContext.Current.CancellationToken);
Assert.False(status.IsRunning);
Assert.Null(status.Version);
}
/// <summary>
/// Tests the <see cref="AdbServer.GetStatusAsync(CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task GetStatusAsyncRunningTest()
{
socket.Responses.Enqueue(AdbResponse.OK);
socket.ResponseMessages.Enqueue("0020");
AdbServerStatus status = await adbServer.GetStatusAsync(TestContext.Current.CancellationToken);
Assert.Empty(socket.Responses);
Assert.Empty(socket.ResponseMessages);
Assert.Single(socket.Requests);
Assert.Equal("host:version", socket.Requests[0]);
Assert.True(status.IsRunning);
Assert.Equal(new Version(1, 0, 32), status.Version);
}
/// <summary>
/// Tests the <see cref="AdbServer.GetStatusAsync(CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task GetStatusAsyncOtherSocketExceptionTest()
{
adbSocketFactory = endPoint => throw new SocketException();
adbServer = new AdbServer(adbSocketFactory, adbCommandLineClientFactory);
_ = await Assert.ThrowsAsync<SocketException>(() => adbServer.GetStatusAsync(TestContext.Current.CancellationToken));
}
/// <summary>
/// Tests the <see cref="AdbServer.GetStatusAsync(CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task GetStatusAsyncOtherExceptionTest()
{
adbSocketFactory = endPoint => throw new Exception();
adbServer = new AdbServer(adbSocketFactory, adbCommandLineClientFactory);
_ = await Assert.ThrowsAsync<Exception>(() => adbServer.GetStatusAsync(TestContext.Current.CancellationToken));
}
/// <summary>
/// Tests the <see cref="AdbServer.StartServerAsync(string, bool, CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task StartServerAsyncAlreadyRunningTest()
{
commandLineClient.Version = AdbCommandLineStatus.GetVersionFromOutput(["Android Debug Bridge version 1.0.20"]);
socket.Responses.Enqueue(AdbResponse.OK);
socket.ResponseMessages.Enqueue("0020");
StartServerResult result = await adbServer.StartServerAsync(null, false, TestContext.Current.CancellationToken);
Assert.Equal(StartServerResult.AlreadyRunning, result);
Assert.Single(socket.Requests);
Assert.Equal("host:version", socket.Requests[0]);
}
/// <summary>
/// Tests the <see cref="AdbServer.StartServerAsync(string, bool, CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task StartServerAsyncOutdatedRunningNoExecutableTest()
{
socket.Responses.Enqueue(AdbResponse.OK);
socket.ResponseMessages.Enqueue("0010");
AggregateException exception = await Assert.ThrowsAsync<AggregateException>(() => adbServer.StartServerAsync(null, false, TestContext.Current.CancellationToken));
Assert.IsType<AdbException>(exception.InnerException);
}
/// <summary>
/// Tests the <see cref="AdbServer.StartServerAsync(string, bool, CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task StartServerAsyncNotRunningNoExecutableTest()
{
adbSocketFactory = endPoint => throw new SocketException(AdbServer.ConnectionRefused);
adbServer = new AdbServer(adbSocketFactory, adbCommandLineClientFactory);
AggregateException exception = await Assert.ThrowsAsync<AggregateException>(() => adbServer.StartServerAsync(null, false, TestContext.Current.CancellationToken));
Assert.IsType<AdbException>(exception.InnerException);
}
/// <summary>
/// Tests the <see cref="AdbServer.StartServerAsync(string, bool, CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task StartServerAsyncOutdatedRunningTest()
{
socket.Responses.Enqueue(AdbResponse.OK);
socket.ResponseMessages.Enqueue("0010");
commandLineClient.Version = AdbCommandLineStatus.GetVersionFromOutput(["Android Debug Bridge version 1.0.32"]);
Assert.False(commandLineClient.ServerStarted);
_ = await adbServer.StartServerAsync(ServerName, false, TestContext.Current.CancellationToken);
Assert.True(commandLineClient.ServerStarted);
Assert.Equal(2, socket.Requests.Count);
Assert.Equal("host:version", socket.Requests[0]);
Assert.Equal("host:kill", socket.Requests[1]);
}
/// <summary>
/// Tests the <see cref="AdbServer.StartServerAsync(string, bool, CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task StartServerAsyncNotRunningTest()
{
adbSocketFactory = endPoint => throw new SocketException(AdbServer.ConnectionRefused);
adbServer = new AdbServer(adbSocketFactory, adbCommandLineClientFactory);
commandLineClient.Version = AdbCommandLineStatus.GetVersionFromOutput(["Android Debug Bridge version 1.0.32"]);
Assert.False(commandLineClient.ServerStarted);
StartServerResult result = await adbServer.StartServerAsync(ServerName, false, TestContext.Current.CancellationToken);
Assert.True(commandLineClient.ServerStarted);
}
/// <summary>
/// Tests the <see cref="AdbServer.StartServerAsync(string, bool, CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task StartServerAsyncIntermediateRestartRequestedRunningTest()
{
socket.Responses.Enqueue(AdbResponse.OK);
socket.ResponseMessages.Enqueue("001f");
commandLineClient.Version = AdbCommandLineStatus.GetVersionFromOutput(["Android Debug Bridge version 1.0.32"]);
Assert.False(commandLineClient.ServerStarted);
_ = await adbServer.StartServerAsync(ServerName, true, TestContext.Current.CancellationToken);
Assert.True(commandLineClient.ServerStarted);
Assert.Equal(2, socket.Requests.Count);
Assert.Equal("host:version", socket.Requests[0]);
Assert.Equal("host:kill", socket.Requests[1]);
}
/// <summary>
/// Tests the <see cref="AdbServer.StartServerAsync(string, bool, CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task StartServerAsyncIntermediateRestartNotRequestedRunningTest()
{
socket.Responses.Enqueue(AdbResponse.OK);
socket.ResponseMessages.Enqueue("001f");
commandLineClient.Version = AdbCommandLineStatus.GetVersionFromOutput(["Android Debug Bridge version 1.0.32"]);
Assert.False(commandLineClient.ServerStarted);
_ = await adbServer.StartServerAsync(ServerName, false, TestContext.Current.CancellationToken);
Assert.False(commandLineClient.ServerStarted);
Assert.Single(socket.Requests);
Assert.Equal("host:version", socket.Requests[0]);
}
/// <summary>
/// Tests the <see cref="AdbServer.RestartServerAsync(string, CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task RestartServerAsyncTest()
{
socket.Responses.Enqueue(AdbResponse.OK);
socket.ResponseMessages.Enqueue("001f");
commandLineClient.Version = AdbCommandLineStatus.GetVersionFromOutput(["Android Debug Bridge version 1.0.32"]);
Assert.False(commandLineClient.ServerStarted);
_ = await adbServer.RestartServerAsync(ServerName, TestContext.Current.CancellationToken);
Assert.True(commandLineClient.ServerStarted);
Assert.Equal(2, socket.Requests.Count);
Assert.Equal("host:version", socket.Requests[0]);
Assert.Equal("host:kill", socket.Requests[1]);
}
/// <summary>
/// Tests the <see cref="AdbServer.StopServerAsync(CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task StopServerAsyncTest()
{
await adbServer.StopServerAsync(TestContext.Current.CancellationToken);
Assert.Single(socket.Requests);
Assert.Equal("host:kill", socket.Requests[0]);
}
}
}