Add a test for Creation event on FileSystemWatcher.
authorCody Russell <cody@jhu.edu>
Tue, 24 Jun 2014 16:28:18 +0000 (11:28 -0500)
committerAlexis Christoforides <alexis@thenull.net>
Wed, 16 Jul 2014 21:27:21 +0000 (17:27 -0400)
mcs/class/System/Test/System.IO/FileSystemWatcherTest.cs

index 4ad27e1a8be28feb1e41738090cee783dec5b624..5d3f0fd1e570c7f71e72c9ce0d495b52507aa1ba 100644 (file)
 using NUnit.Framework;
 using System;
 using System.IO;
+using System.Threading;
 
 namespace MonoTests.System.IO
 {
        [TestFixture]
        public class FileSystemWatcherTest
        {
+               string base_path;
+               string path_a;
+
+               FileSystemWatcher SetupWatcher ()
+               {
+                       var fsw = new FileSystemWatcher ();
+                       fsw.Path = base_path;
+                       fsw.IncludeSubdirectories = true;
+                       fsw.EnableRaisingEvents = true;
+
+                       return fsw;
+               }
+
+               [SetUp]
+               public void Setup ()
+               {
+                       base_path = Path.GetTempPath ();
+                       path_a = Path.Combine (base_path, "a.txt");
+               }
+
+               [TearDown]
+               public void TearDown ()
+               {
+                       if (File.Exists (path_a))
+                               File.Delete (path_a);
+               }
+
                [Test]
                public void CheckDefaults ()
                {
@@ -83,7 +111,26 @@ namespace MonoTests.System.IO
                        FileSystemWatcher fw = new FileSystemWatcher (Path.GetTempPath (), "*");
                        fw.Path = "*";
                }
+
+               [Test]
+               public void TestWatchPathForFileCreation ()
+               {
+                       bool created = false;
+                       var fsw = SetupWatcher ();
+
+                       fsw.Created += (object sender, FileSystemEventArgs e) => {
+                               created = true;
+                       };
+
+                       Assert.IsFalse (created);
+
+                       File.WriteAllText (path_a, "this should create the file");
+
+                       Thread.Sleep (20);
+
+                       Assert.IsTrue (created);
+               }
        }
 }
 
-#endif
\ No newline at end of file
+#endif