Skip to content

Commit 6ece9be

Browse files
authored
Merge pull request #52 from ztamas83/fix_monitor
Only trigger DeviceChanged event if there is a change in device state
2 parents 486ffa6 + 0793be0 commit 6ece9be

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

AdvancedSharpAdbClient.Tests/DeviceMonitorTests.cs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public void StartInitialDeviceListTest()
157157
}
158158

159159
[Fact]
160-
public void DeviceChangedTest()
160+
public void DeviceChanged_TriggeredWhenStatusChanged()
161161
{
162162
Socket.WaitForNewData = true;
163163

@@ -186,6 +186,8 @@ public void DeviceChangedTest()
186186
Socket.Responses.Clear();
187187
Socket.Requests.Clear();
188188

189+
sink.ResetSignals();
190+
189191
// Device disconnects
190192
ManualResetEvent eventWaiter = sink.CreateEventSignal();
191193

@@ -199,13 +201,64 @@ public void DeviceChangedTest()
199201

200202
Assert.Equal(1, monitor.Devices.Count);
201203
Assert.Equal(DeviceState.Online, monitor.Devices.ElementAt(0).State);
202-
Assert.Single(sink.ConnectedEvents);
204+
Assert.Empty(sink.ConnectedEvents);
203205
Assert.Single(sink.ChangedEvents);
204206
Assert.Empty(sink.DisconnectedEvents);
205207
Assert.Equal("169.254.109.177:5555", sink.ChangedEvents[0].Device.Serial);
206208
});
207209
}
208210

211+
[Fact]
212+
public void DeviceChanged_NoTriggerIfStatusIsSame()
213+
{
214+
Socket.WaitForNewData = true;
215+
216+
using DeviceMonitor monitor = new(Socket);
217+
DeviceMonitorSink sink = new(monitor);
218+
219+
Assert.Equal(0, monitor.Devices.Count);
220+
221+
// Start the monitor, detect the initial device.
222+
RunTest(
223+
OkResponse,
224+
ResponseMessages("169.254.109.177:5555\toffline\n"),
225+
Requests("host:track-devices"),
226+
() =>
227+
{
228+
monitor.Start();
229+
230+
Assert.Equal(1, monitor.Devices.Count);
231+
Assert.Equal(DeviceState.Offline, monitor.Devices.ElementAt(0).State);
232+
Assert.Single(sink.ConnectedEvents);
233+
Assert.Empty(sink.ChangedEvents);
234+
Assert.Empty(sink.DisconnectedEvents);
235+
});
236+
237+
Socket.ResponseMessages.Clear();
238+
Socket.Responses.Clear();
239+
Socket.Requests.Clear();
240+
241+
sink.ResetSignals();
242+
243+
// Something happens but device does not change
244+
ManualResetEvent eventWaiter = sink.CreateEventSignal();
245+
246+
RunTest(
247+
NoResponses,
248+
ResponseMessages("169.254.109.177:5555\toffline\n"),
249+
Requests(),
250+
() =>
251+
{
252+
eventWaiter.WaitOne(1000);
253+
254+
Assert.Equal(1, monitor.Devices.Count);
255+
Assert.Equal(DeviceState.Offline, monitor.Devices.ElementAt(0).State);
256+
Assert.Empty(sink.ConnectedEvents);
257+
Assert.Empty(sink.ChangedEvents);
258+
Assert.Empty(sink.DisconnectedEvents);
259+
});
260+
}
261+
209262
/// <summary>
210263
/// Tests the <see cref="DeviceMonitor"/> in a case where the adb server dies in the middle of the monitor
211264
/// loop. The <see cref="DeviceMonitor"/> should detect this condition and restart the adb server.

AdvancedSharpAdbClient.Tests/Dummys/DeviceMonitorSink.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public DeviceMonitorSink(DeviceMonitor monitor)
1818
ConnectedEvents = new Collection<DeviceDataEventArgs>();
1919
}
2020

21+
public void ResetSignals()
22+
{
23+
ChangedEvents.Clear();
24+
DisconnectedEvents.Clear();
25+
ConnectedEvents.Clear();
26+
}
27+
2128
public Collection<DeviceDataEventArgs> DisconnectedEvents { get; private set; }
2229

2330
public Collection<DeviceDataEventArgs> ConnectedEvents { get; private set; }

AdvancedSharpAdbClient/DeviceMonitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ private void UpdateDevices(List<DeviceData> devices)
324324
this.devices.Add(device);
325325
OnDeviceConnected(new DeviceDataEventArgs(device));
326326
}
327-
else
327+
else if (existingDevice.State != device.State)
328328
{
329329
existingDevice.State = device.State;
330330
OnDeviceChanged(new DeviceDataEventArgs(existingDevice));

0 commit comments

Comments
 (0)