public void Can_Add_Or_Update_File_In_A_Folder()
        {
            var rootFolder = new FolderDirectoryItem("root", null);

            bool addNotified    = false;
            bool updateNotified = false;

            rootFolder.ItemAdded += (sender, e) =>
            {
                DirectoryItemAddedEventArgs args = e;
                IDirectoryItem newItem           = e.NewItem;
                Assert.Equal("foo.txt", newItem.Name);
                addNotified = true;
            };

            var addedFile = rootFolder.AddOrUpdateFile(new StringFileInfo("hi there", "foo.txt"));

            Assert.True(addNotified);
            Assert.False(updateNotified);
            addNotified = false;


            addedFile.Updated += (sender, e) =>
            {
                Assert.Equal("foo.txt", e.NewItem.Name);
                updateNotified = true;
            };


            var updatedFile = rootFolder.AddOrUpdateFile(new StringFileInfo("hi there modified", "foo.txt"));

            Assert.True(updateNotified);
            Assert.False(addNotified);
        }
        public void Can_Rename_A_Folder_With_Child_Items()
        {
            // arrange
            // build a directory /root/child/grandchild
            var rootFolder       = new FolderDirectoryItem("root", null);
            var childFolder      = rootFolder.GetOrAddFolder("child");
            var grandChildFolder = childFolder.GetOrAddFolder("grandchild");

            // add a file /root/child/foo.txt
            var fileInfo      = new StringFileInfo("contents", "foo.txt");
            var someOtherFile = childFolder.AddFile(fileInfo);

            // add a file /root/child/grandchild/bar.txt
            var barFile = grandChildFolder.AddFile(new StringFileInfo("should trigger", "bar.txt"));

            // Subscribe to event handlers as we want to test if we are notified appropriately when
            // a folder is renamed.
            bool notified     = false;
            bool fileNotified = false;

            // We are going to rename this folder, so we should see if we get notified that it is
            // updated.
            grandChildFolder.Updated += (sender, e) =>
            {
                DirectoryItemUpdatedEventArgs args = e;
                IDirectoryItem oldItem             = e.OldItem;
                IDirectoryItem newItem             = e.NewItem;
                Assert.Equal("grandchild", oldItem.Name);
                Assert.Equal("grandchild-renamed", newItem.Name);
                notified = true;
            };

            // We are going to rename the folder which this file lives in, which means this files
            // path will have changed. We should register to see if we get notified that it was updated.
            barFile.Updated += (sender, e) =>
            {
                DirectoryItemUpdatedEventArgs args = e;
                IDirectoryItem oldItem             = e.OldItem;
                IDirectoryItem newItem             = e.NewItem;
                Assert.Equal("bar.txt", oldItem.Name);
                Assert.Equal("bar.txt", newItem.Name);
                Assert.Equal("root/child/grandchild/bar.txt", oldItem.Path);
                Assert.Equal("root/child/grandchild-renamed/bar.txt", newItem.Path);
                fileNotified = true;
            };

            grandChildFolder.Rename("grandchild-renamed");
            Assert.True(notified);
            Assert.True(fileNotified);
        }
        public void Can_Delete_A_Folder()
        {
            var  rootFolder  = new FolderDirectoryItem("root", null);
            var  childFolder = rootFolder.GetOrAddFolder("child");
            bool notified    = false;

            childFolder.Deleted += (sender, e) =>
            {
                DirectoryItemDeletedEventArgs args = e;
                IDirectoryItem deletedItem         = e.DeletedItem;
                Assert.True(deletedItem.IsFolder);
                Assert.Equal("child", deletedItem.Name);
                notified = true;
            };

            childFolder.Delete();
            Assert.True(notified);
        }
        public void Can_Add_A_File_To_A_Folder()
        {
            var rootFolder = new FolderDirectoryItem("root", null);

            bool notified = false;

            rootFolder.ItemAdded += (sender, e) =>
            {
                DirectoryItemAddedEventArgs args = e;
                IDirectoryItem newItem           = e.NewItem;
                Assert.Equal("child", newItem.Name);
                notified = true;
            };

            var childFolder = rootFolder.GetOrAddFolder("child");

            Assert.True(notified);
        }
        public void Can_Rename_A_Folder()
        {
            var rootFolder  = new FolderDirectoryItem("root", null);
            var childFolder = rootFolder.GetOrAddFolder("child");

            var  fileInfo      = new StringFileInfo("contents", "foo.txt");
            var  someOtherFile = childFolder.AddFile(fileInfo);
            bool notified      = false;

            childFolder.Updated += (sender, e) =>
            {
                DirectoryItemUpdatedEventArgs args = e;
                IDirectoryItem oldItem             = e.OldItem;
                IDirectoryItem newItem             = e.NewItem;
                Assert.Equal("child", oldItem.Name);
                Assert.Equal("child-renamed", newItem.Name);
                notified = true;
            };

            childFolder.Rename("child-renamed");
            Assert.True(notified);
            Assert.Equal("root/child-renamed/foo.txt", someOtherFile.Path);
        }
        public void Can_Delete_A_Folder_Containing_Items()
        {
            // given /root/child/grandchild/foo.txt
            // deleting the "child" folder should also delete all descendents which are
            // "grandchild" folder, and foo.txt.

            var rootFolder       = new FolderDirectoryItem("root", null);
            var childFolder      = rootFolder.GetOrAddFolder("child");
            var grandchildFolder = childFolder.GetOrAddFolder("grandchild");

            var fileInfo = new StringFileInfo("contents", "foo.txt");
            var fileItem = grandchildFolder.AddFile(fileInfo);

            // Should get notified when each folder and file is deleted.
            bool childDeletionNotified = false;

            childFolder.Deleted += (sender, e) =>
            {
                DirectoryItemDeletedEventArgs args = e;
                IDirectoryItem deletedItem         = e.DeletedItem;
                Assert.True(deletedItem.IsFolder);
                Assert.Equal("child", deletedItem.Name);
                childDeletionNotified = true;
            };

            bool grandchildDeletionNotified = false;

            grandchildFolder.Deleted += (sender, e) =>
            {
                DirectoryItemDeletedEventArgs args = e;
                IDirectoryItem deletedItem         = e.DeletedItem;
                Assert.True(deletedItem.IsFolder);
                Assert.Equal("grandchild", deletedItem.Name);
                grandchildDeletionNotified = true;
            };

            bool fileDeletionNotified = false;

            fileItem.Deleted += (sender, e) =>
            {
                DirectoryItemDeletedEventArgs args = e;
                IDirectoryItem deletedItem         = e.DeletedItem;
                Assert.False(deletedItem.IsFolder);
                Assert.Equal("foo.txt", deletedItem.Name);
                fileDeletionNotified = true;
            };

            childFolder.Delete(true);
            Assert.True(childDeletionNotified);
            Assert.True(grandchildDeletionNotified);
            Assert.True(fileDeletionNotified);

            // the deleted items should have had their fileinfo's set to not exist.
            Assert.False(childFolder.FileInfo.Exists);
            Assert.False(grandchildFolder.FileInfo.Exists);
            Assert.False(fileItem.FileInfo.Exists);

            // verify child items are no longer in directory.
            var item = rootFolder.NavigateToNext("child");

            Assert.Null(item);

            item = childFolder.NavigateToNext("grandchild");
            Assert.Null(item);

            item = grandchildFolder.NavigateToNext("foo.txt");
            Assert.Null(item);
        }