1+ using System . Runtime . Versioning ;
2+
3+ using LogExpert . Core . Config ;
4+ using LogExpert . Core . Interfaces ;
5+ using LogExpert . UI . Services . ToolWindowCoordinatorService ;
6+
7+ using Moq ;
8+
9+ using NUnit . Framework ;
10+
11+ using WeifenLuo . WinFormsUI . Docking ;
12+
13+ namespace LogExpert . Tests . Services ;
14+
15+ [ TestFixture ]
16+ [ Apartment ( ApartmentState . STA ) ]
17+ [ SupportedOSPlatform ( "windows" ) ]
18+ public class ToolWindowCoordinatorTests : IDisposable
19+ {
20+ private Mock < IConfigManager > _configManagerMock ;
21+ private Settings _settings ;
22+ private ToolWindowCoordinator _coordinator ;
23+ private WindowsFormsSynchronizationContext ? _syncContext ;
24+ private bool _disposed ;
25+
26+ [ SetUp ]
27+ public void Setup ( )
28+ {
29+ if ( SynchronizationContext . Current == null )
30+ {
31+ _syncContext = new WindowsFormsSynchronizationContext ( ) ;
32+ SynchronizationContext . SetSynchronizationContext ( _syncContext ) ;
33+ }
34+
35+ _configManagerMock = new Mock < IConfigManager > ( ) ;
36+ _settings = new Settings ( ) ;
37+ _ = _configManagerMock . Setup ( cm => cm . Settings ) . Returns ( _settings ) ;
38+
39+ _coordinator = new ToolWindowCoordinator ( _configManagerMock . Object ) ;
40+ }
41+
42+ [ TearDown ]
43+ public void TearDown ( )
44+ {
45+ _coordinator ? . Dispose ( ) ;
46+ _syncContext ? . Dispose ( ) ;
47+ }
48+
49+ public void Dispose ( )
50+ {
51+ Dispose ( true ) ;
52+ GC . SuppressFinalize ( this ) ;
53+ }
54+
55+ protected virtual void Dispose ( bool disposing )
56+ {
57+ if ( _disposed )
58+ {
59+ return ;
60+ }
61+
62+ if ( disposing )
63+ {
64+ _coordinator ? . Dispose ( ) ;
65+ _syncContext ? . Dispose ( ) ;
66+ }
67+
68+ _disposed = true ;
69+ }
70+
71+ [ Test ]
72+ public void Initialize_CreatesBookmarkWindow ( )
73+ {
74+ // Act
75+ _coordinator . Initialize ( ) ;
76+
77+ // Assert — GetDockContent should return non-null for bookmark persist string
78+ var content = _coordinator . GetDockContent ( "BookmarkWindow" ) ;
79+ Assert . That ( content , Is . Not . Null ) ;
80+ }
81+
82+ [ Test ]
83+ public void Destroy_ClosesBookmarkWindow ( )
84+ {
85+ // Arrange
86+ _coordinator . Initialize ( ) ;
87+
88+ // Act
89+ _coordinator . Destroy ( ) ;
90+
91+ // Assert — GetDockContent should return null after destroy
92+ var content = _coordinator . GetDockContent ( "BookmarkWindow" ) ;
93+ Assert . That ( content , Is . Null ) ;
94+ }
95+
96+ [ Test ]
97+ public void GetDockContent_ReturnsBookmarkWindow_ForMatchingPersistString ( )
98+ {
99+ // Arrange
100+ _coordinator . Initialize ( ) ;
101+
102+ // Act
103+ var result = _coordinator . GetDockContent ( "BookmarkWindow" ) ;
104+
105+ // Assert
106+ Assert . That ( result , Is . Not . Null ) ;
107+ }
108+
109+ [ Test ]
110+ public void GetDockContent_ReturnsNull_ForNonMatchingPersistString ( )
111+ {
112+ // Arrange
113+ _coordinator . Initialize ( ) ;
114+
115+ // Act
116+ var result = _coordinator . GetDockContent ( "SomeOtherWindow" ) ;
117+
118+ // Assert
119+ Assert . That ( result , Is . Null ) ;
120+ }
121+
122+ [ Test ]
123+ public void Disconnect_WithoutConnect_DoesNotThrow ( )
124+ {
125+ // Arrange
126+ _coordinator . Initialize ( ) ;
127+
128+ // Act & Assert
129+ Assert . DoesNotThrow ( _coordinator . Disconnect ) ;
130+ }
131+
132+ [ Test ]
133+ public void ApplyPreferences_DoesNotThrow_WhenInitialized ( )
134+ {
135+ // Arrange
136+ _coordinator . Initialize ( ) ;
137+
138+ // Act & Assert
139+ Assert . DoesNotThrow ( ( ) =>
140+ _coordinator . ApplyPreferences ( "Courier New" , 10f , true , 500 , SettingsFlags . All ) ) ;
141+ }
142+
143+ [ Test ]
144+ public void SetLineColumnVisible_DoesNotThrow_WhenInitialized ( )
145+ {
146+ // Arrange
147+ _coordinator . Initialize ( ) ;
148+
149+ // Act & Assert
150+ Assert . DoesNotThrow ( ( ) => _coordinator . SetLineColumnVisible ( true ) ) ;
151+ Assert . DoesNotThrow ( ( ) => _coordinator . SetLineColumnVisible ( false ) ) ;
152+ }
153+
154+ [ Test ]
155+ public void Dispose_CanBeCalledMultipleTimes ( )
156+ {
157+ // Arrange
158+ _coordinator . Initialize ( ) ;
159+
160+ // Act & Assert
161+ Assert . DoesNotThrow ( ( ) =>
162+ {
163+ _coordinator . Dispose ( ) ;
164+ _coordinator . Dispose ( ) ;
165+ } ) ;
166+
167+ _coordinator = null ; // prevent double dispose in TearDown
168+ }
169+
170+ [ Test ]
171+ public void ToggleBookmarkVisibility_WhenNotInitialized_DoesNotThrow ( )
172+ {
173+ // Arrange — coordinator not initialized (no bookmark window)
174+ using var form = new Form ( ) ;
175+ using var dockPanel = new DockPanel ( ) ;
176+ form . Controls . Add ( dockPanel ) ;
177+
178+ // Act & Assert
179+ Assert . DoesNotThrow ( ( ) => _coordinator . ToggleBookmarkVisibility ( dockPanel ) ) ;
180+ }
181+
182+ [ Test ]
183+ public void GetDockContent_BeforeInitialize_ReturnsNull ( )
184+ {
185+ // Act
186+ var result = _coordinator . GetDockContent ( "BookmarkWindow" ) ;
187+
188+ // Assert
189+ Assert . That ( result , Is . Null ) ;
190+ }
191+
192+ [ Test ]
193+ public void ApplyPreferences_BeforeInitialize_DoesNotThrow ( )
194+ {
195+ // Act & Assert
196+ Assert . DoesNotThrow ( ( ) =>
197+ _coordinator . ApplyPreferences ( "Courier New" , 10f , true , 500 , SettingsFlags . All ) ) ;
198+ }
199+
200+ [ Test ]
201+ public void SetLineColumnVisible_BeforeInitialize_DoesNotThrow ( )
202+ {
203+ // Act & Assert
204+ Assert . DoesNotThrow ( ( ) => _coordinator . SetLineColumnVisible ( true ) ) ;
205+ }
206+ }
0 commit comments