@@ -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\t offline\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\t offline\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.
0 commit comments