Merge pull request #1691 from esdrubal/exitevent
[mono.git] / mcs / class / System / Test / System.Diagnostics / ProcessTest.cs
index e28fef7bf15b26ded2999181505d9ed696852055..b286100b932504342602c79ed75073df342023dc 100644 (file)
@@ -52,6 +52,15 @@ namespace MonoTests.System.Diagnostics
                        }
                }
 
+               [Test] // Covers #26363
+               public void GetProcesses_StartTime ()
+               {
+                       foreach (var p in Process.GetProcesses ()) {
+                               if (!p.HasExited && p.StartTime.Year < 1800)
+                                       Assert.Fail ("Process should not be started since the 18th century.");
+                       }
+               }
+
                [Test]
                public void PriorityClass_NotStarted ()
                {
@@ -132,7 +141,7 @@ namespace MonoTests.System.Diagnostics
                {
                        if (RunningOnUnix)
                                // on unix, all characters are allowed
-                               return;
+                               Assert.Ignore ("Running on Unix.");
 
                        string systemDir = Environment.GetFolderPath (Environment.SpecialFolder.System);
                        string exe = "\"" + Path.Combine (systemDir, "calc.exe") + "\"";
@@ -593,7 +602,6 @@ namespace MonoTests.System.Diagnostics
                        }
                }
 
-#if NET_2_0            
                [Test]
                public void Start_UseShellExecuteWithEmptyUserName ()
                {
@@ -629,7 +637,6 @@ namespace MonoTests.System.Diagnostics
                        } catch (Win32Exception) {
                        }
                }
-#endif
                
                [Test] // Start (string, string)
                public void Start4_FileName_Null ()
@@ -675,12 +682,12 @@ namespace MonoTests.System.Diagnostics
 
                [Test]
                [NUnit.Framework.Category ("NotDotNet")]
-               [NUnit.Framework.Category ("NotMobile")]
+               [NUnit.Framework.Category ("MobileNotWorking")]
                public void TestRedirectedOutputIsAsync ()
                {
                        // Test requires cygwin, so we just bail out for now.
                        if (Path.DirectorySeparatorChar == '\\')
-                               return;
+                               Assert.Ignore ("Test requires cygwin.");
                        
                        Process p = new Process ();
                        p.StartInfo = new ProcessStartInfo ("/bin/sh", "-c \"sleep 2; echo hello\"");
@@ -722,12 +729,12 @@ namespace MonoTests.System.Diagnostics
                        }
                }
 
-               int bytesRead = -1;
+               public int bytesRead = -1;
 
-#if NET_2_0
 // Not technically a 2.0 only test, but I use lambdas, so I need gmcs
 
                [Test]
+               [NUnit.Framework.Category ("MobileNotWorking")]
                // This was for bug #459450
                public void TestEventRaising ()
                {
@@ -770,11 +777,87 @@ namespace MonoTests.System.Diagnostics
 
                        Assert.AreEqual (true, r, "Null Argument Events Raised");
                }
+
+               [Test]
+               [NUnit.Framework.Category ("MobileNotWorking")]
+               public void TestEnableEventsAfterExitedEvent ()
+               {
+                       Process p = new Process ();
+                       
+                       p.StartInfo = GetCrossPlatformStartInfo ();
+                       p.StartInfo.UseShellExecute = false;
+                       p.StartInfo.RedirectStandardOutput = true;
+                       p.StartInfo.RedirectStandardError = true;
+
+                       var exitedCalledCounter = 0;
+                       p.Exited += (object sender, EventArgs e) => {
+                               exitedCalledCounter++;
+                               Assert.IsTrue (p.HasExited);
+                       };
+
+                       p.EnableRaisingEvents = true;
+
+                       p.Start ();
+                       p.BeginErrorReadLine ();
+                       p.BeginOutputReadLine ();
+                       p.WaitForExit ();
+
+                       Assert.AreEqual (1, exitedCalledCounter);
+                       Thread.Sleep (50);
+                       Assert.AreEqual (1, exitedCalledCounter);
+               }
+
+               [Test]
+               [NUnit.Framework.Category ("MobileNotWorking")]
+               public void TestEnableEventsBeforeExitedEvent ()
+               {
+                       Process p = new Process ();
+                       
+                       p.StartInfo = GetCrossPlatformStartInfo ();
+                       p.StartInfo.UseShellExecute = false;
+                       p.StartInfo.RedirectStandardOutput = true;
+                       p.StartInfo.RedirectStandardError = true;
+
+                       p.EnableRaisingEvents = true;
+
+                       var exitedCalledCounter = 0;
+                       p.Exited += (object sender, EventArgs e) => {
+                               exitedCalledCounter++;
+                               Assert.IsTrue (p.HasExited);
+                       };
+
+                       p.Start ();
+                       p.BeginErrorReadLine ();
+                       p.BeginOutputReadLine ();
+                       p.WaitForExit ();
+
+                       Assert.AreEqual (1, exitedCalledCounter);
+                       Thread.Sleep (50);
+                       Assert.AreEqual (1, exitedCalledCounter);
+               }
+
                
                private ProcessStartInfo GetCrossPlatformStartInfo ()
                {
                        return RunningOnUnix ? new ProcessStartInfo ("/bin/ls", "/") : new ProcessStartInfo ("help", "");
                }
+
+               [Test] // Covers #26362
+               public void TestExitedEvent ()
+               {
+                       var falseExitedEvents = 0;
+                       var cp = Process.GetCurrentProcess ();
+                       foreach (var p in Process.GetProcesses ()) {
+                               if (p.Id != cp.Id && !p.HasExited) {
+                                       p.EnableRaisingEvents = true;
+                                       p.Exited += (s, e) => {
+                                               if (!p.HasExited)
+                                                       falseExitedEvents++;
+                                       };
+                               }
+                       }
+                       Assert.AreEqual (0, falseExitedEvents);
+               }
                
                [Test]
                public void ProcessName_NotStarted ()
@@ -797,6 +880,7 @@ namespace MonoTests.System.Diagnostics
                }
                
                [Test]
+               [NUnit.Framework.Category ("MobileNotWorking")]
                public void ProcessName_AfterExit ()
                {
                        Process p = new Process ();
@@ -825,6 +909,21 @@ namespace MonoTests.System.Diagnostics
                        
                        Assert.IsNull (e.InnerException, "IOE inner exception should be null");
                }
-#endif
+
+               [Test]
+               public void Handle_ThrowsOnNotStarted ()
+               {
+                       Process p = new Process ();
+                       try {
+                               var x = p.Handle;
+                               Assert.Fail ("Handle should throw for unstated procs, but returned " + x);
+                       } catch (InvalidOperationException) {
+                       }
+               }
+
+               [Test]
+               public void HasExitedCurrent () {
+                       Assert.IsFalse (Process.GetCurrentProcess ().HasExited);
+               }
        }
 }