Jelajahi Sumber

Changed Example to use Run<T> because we need more examples of this

Charlie Kindel 2 tahun lalu
induk
melakukan
2cfe7e9695
3 mengubah file dengan 85 tambahan dan 70 penghapusan
  1. 65 50
      Example/Example.cs
  2. 10 8
      Terminal.Gui/Core/Application.cs
  3. 10 12
      UnitTests/ApplicationTests.cs

+ 65 - 50
Example/Example.cs

@@ -5,53 +5,68 @@
 
 using Terminal.Gui;
 
-// Initialize the console
-Application.Init();
-
-// Creates the top-level window with border and title
-var win = new Window("Example App (Ctrl+Q to quit)");
-
-// Create input components and labels
-
-var usernameLabel = new Label("Username:");
-var usernameText = new TextField("")
-{
-    // Position text field adjacent to label
-    X = Pos.Right(usernameLabel) + 1,
-
-    // Fill remaining horizontal space with a margin of 1
-    Width = Dim.Fill(1),
-};
-
-var passwordLabel = new Label(0,2,"Password:");
-var passwordText = new TextField("")
-{
-    Secret = true,
-    // align with the text box above
-    X = Pos.Left(usernameText),
-    Y = 2,
-    Width = Dim.Fill(1),
-};
-
-// Create login button
-var btnLogin = new Button("Login")
-{
-    Y = 4,
-    // center the login button horizontally
-    X = Pos.Center(),
-    IsDefault = true,
-};
-
-// When login button is clicked display a message popup
-btnLogin.Clicked += () => MessageBox.Query("Logging In", "Login Successful", "Ok");
-
-// Add all the views to the window
-win.Add(
-    usernameLabel, usernameText, passwordLabel, passwordText,btnLogin
-);
-
-// Show the application
-Application.Run(win);
-
-// After the application exits, release and reset console for clean shutdown
-Application.Shutdown();
+Application.Run<ExampleWindow> ();
+
+System.Console.WriteLine ($"Username: {((ExampleWindow)Application.Top).usernameText.Text}");
+
+// Before the application exits, reset Terminal.Gui for clean shutdown
+Application.Shutdown ();
+
+// Defines a top-level window with border and title
+public class ExampleWindow : Window {
+	public TextField usernameText;
+	
+	public ExampleWindow ()
+	{
+		Title = "Example App (Ctrl+Q to quit)";
+
+		// Create input components and labels
+		var usernameLabel = new Label () { 
+			Text = "Username:" 
+		};
+
+		usernameText = new TextField ("") {
+			// Position text field adjacent to the label
+			X = Pos.Right (usernameLabel) + 1,
+
+			// Fill remaining horizontal space
+			Width = Dim.Fill (),
+		};
+
+		var passwordLabel = new Label () {
+			Text = "Password:",
+			X = Pos.Left (usernameLabel),
+			Y = Pos.Bottom (usernameLabel) + 1
+		};
+
+		var passwordText = new TextField ("") {
+			Secret = true,
+			// align with the text box above
+			X = Pos.Left (usernameText),
+			Y = Pos.Top (passwordLabel),
+			Width = Dim.Fill (),
+		};
+
+		// Create login button
+		var btnLogin = new Button () {
+			Text = "Login",
+			Y = Pos.Bottom(passwordLabel) + 1,
+			// center the login button horizontally
+			X = Pos.Center (),
+			IsDefault = true,
+		};
+
+		// When login button is clicked display a message popup
+		btnLogin.Clicked += () => {
+			if (usernameText.Text == "admin" && passwordText.Text == "password") {
+				MessageBox.Query ("Logging In", "Login Successful", "Ok");
+				Application.RequestStop ();
+			} else {
+				MessageBox.ErrorQuery ("Logging In", "Incorrect username or password", "Ok");
+			}
+		};
+
+		// Add the views to the Window
+		Add (usernameLabel, usernameText, passwordLabel, passwordText, btnLogin);
+	}
+}

+ 10 - 8
Terminal.Gui/Core/Application.cs

@@ -309,6 +309,9 @@ namespace Terminal.Gui {
 		/// </summary>
 		public static bool UseSystemConsole;
 
+		// For Unit testing - ignores UseSystemConsole
+		internal static bool ForceFakeConsole;
+
 		/// <summary>
 		/// Initializes a new instance of <see cref="Terminal.Gui"/> Application. 
 		/// </summary>
@@ -385,7 +388,11 @@ namespace Terminal.Gui {
 
 			if (Driver == null) {
 				var p = Environment.OSVersion.Platform;
-				if (UseSystemConsole) {
+				if (ForceFakeConsole) {
+					// For Unit Testing only
+					Driver = new FakeDriver ();
+					mainLoopDriver = new FakeMainLoop (() => FakeConsole.ReadKey (true));
+				} else if (UseSystemConsole) {
 					Driver = new NetDriver ();
 					mainLoopDriver = new NetMainLoop (Driver);
 				} else if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows) {
@@ -1262,7 +1269,7 @@ namespace Terminal.Gui {
 		/// Runs the application by calling <see cref="Run(Toplevel, Func{Exception, bool})"/> 
 		/// with a new instance of the specified <see cref="Toplevel"/>-derived class.
 		/// <para>
-		/// Calling <see cref="Init(ConsoleDriver, IMainLoopDriver)"/> first is not needed as this function will initialze the 
+		/// Calling <see cref="Init(ConsoleDriver, IMainLoopDriver)"/> first is not needed as this function will initialze the application.
 		/// </para>
 		/// <para>
 		/// <see cref="Shutdown"/> must be called when the application is closing (typically after Run> has 
@@ -1298,12 +1305,7 @@ namespace Terminal.Gui {
 				}
 			} else {
 				// Init() has NOT been called.
-				if (driver != null) {
-					// Caller has provided a driver so call Init with it (but set calledViaRunT to true so we don't reset Application state).
-					InternalInit (() => new T (), driver, mainLoopDriver, calledViaRunT: true);
-				} else {
-					throw new ArgumentException ("A Driver must be specified when calling Run<T>() when Init() has not been called.");
-				}
+				InternalInit (() => new T (), driver, mainLoopDriver, calledViaRunT: true);
 				Run (Top, errorHandler);
 			}
 		}

+ 10 - 12
UnitTests/ApplicationTests.cs

@@ -287,21 +287,17 @@ namespace Terminal.Gui.Core {
 		[Fact]
 		public void Run_T_After_InitNullDriver_with_TestTopLevel_Throws ()
 		{
-			var p = Environment.OSVersion.Platform;
-			if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows) {
-				Assert.Throws<InvalidOperationException> (() => Application.Init (null, null));
-			} else {
-				Application.Init (null, null);
-				Assert.Equal (typeof (CursesDriver), Application.Driver.GetType ());
-				Application.Shutdown ();
-			}
+			Application.ForceFakeConsole = true;
+
+			Application.Init (null, null);
+			Assert.Equal (typeof (FakeDriver), Application.Driver.GetType ());
 
 			Application.Iteration = () => {
 				Application.RequestStop ();
 			};
 
 			// Init has been called without selecting a driver and we're passing no driver to Run<TestTopLevel>. Bad
-			Assert.Throws<ArgumentException> (() => Application.Run<TestToplevel> ());
+			Application.Run<TestToplevel> ();
 
 			Shutdown ();
 
@@ -332,14 +328,16 @@ namespace Terminal.Gui.Core {
 		}
 
 		[Fact]
-		public void Run_T_NoInit_Throws ()
+		public void Run_T_NoInit_DoesNotThrow ()
 		{
+			Application.ForceFakeConsole = true; 
+			
 			Application.Iteration = () => {
 				Application.RequestStop ();
 			};
 
-			// Init has NOT been called and we're passing no driver to Run<TestToplevel>. This is an error.
-			Assert.Throws<ArgumentException> (() => Application.Run<TestToplevel> (errorHandler: null, driver: null, mainLoopDriver: null));
+			Application.Run<TestToplevel> ();
+			Assert.Equal (typeof (FakeDriver), Application.Driver.GetType ());
 
 			Shutdown ();