Exemplo n.º 1
0
        public void ShowMockFileDialogService()
        {
            var fileDialogService = new MockFileDialogService();

            var owner     = new object();
            var fileType  = new FileType("description", ".fileExtension");
            var fileTypes = new[] { fileType };
            var result    = new FileDialogResult("selectedName", fileType);

            fileDialogService.Result = result;

            Assert.AreEqual(result, fileDialogService.ShowOpenFileDialog(owner, fileTypes, fileType, "defaultFileName"));

            Assert.AreEqual(FileDialogType.OpenFileDialog, fileDialogService.FileDialogType);
            Assert.AreEqual(owner, fileDialogService.Owner);
            Assert.AreEqual(fileTypes, fileDialogService.FileTypes);
            Assert.AreEqual(fileType, fileDialogService.DefaultFileType);
            Assert.AreEqual("defaultFileName", fileDialogService.DefaultFileName);

            fileDialogService.Clear();

            Assert.AreEqual(FileDialogType.None, fileDialogService.FileDialogType);
            Assert.IsNull(fileDialogService.Owner);
            Assert.IsNull(fileDialogService.FileTypes);
            Assert.IsNull(fileDialogService.DefaultFileType);
            Assert.IsNull(fileDialogService.DefaultFileName);


            fileDialogService.Result = result;
            Assert.AreEqual(result, fileDialogService.ShowSaveFileDialog(owner, fileTypes, fileType, "defaultFileName"));

            Assert.AreEqual(FileDialogType.SaveFileDialog, fileDialogService.FileDialogType);
            Assert.AreEqual(owner, fileDialogService.Owner);
            Assert.AreEqual(fileTypes, fileDialogService.FileTypes);
            Assert.AreEqual(fileType, fileDialogService.DefaultFileType);
            Assert.AreEqual("defaultFileName", fileDialogService.DefaultFileName);
        }
        public void ShowSaveFileDialogExtensionTest()
        {
            var    rtfFileType     = new FileType("RichText Document", ".rtf");
            var    xpsFileType     = new FileType("XPS Document", ".xps");
            var    fileTypes       = new[] { rtfFileType, xpsFileType };
            string defaultFileName = "Document 1.rtf";
            var    result          = new FileDialogResult("Document 2.rtf", rtfFileType);

            object owner   = new object();
            var    service = new MockFileDialogService()
            {
                Result = result
            };

            Assert.AreEqual(result, service.ShowSaveFileDialog(rtfFileType));
            Assert.AreEqual(FileDialogType.SaveFileDialog, service.FileDialogType);
            Assert.AreEqual(rtfFileType, service.FileTypes.Single());
            AssertHelper.ExpectedException <ArgumentNullException>(() => FileDialogServiceExtensions.ShowSaveFileDialog(null !, rtfFileType));
            AssertHelper.ExpectedException <ArgumentNullException>(() => service.ShowSaveFileDialog((FileType)null !));

            service.Clear();
            Assert.AreEqual(result, service.ShowSaveFileDialog(owner, rtfFileType));
            Assert.AreEqual(FileDialogType.SaveFileDialog, service.FileDialogType);
            Assert.AreEqual(owner, service.Owner);
            Assert.AreEqual(rtfFileType, service.FileTypes.Single());
            AssertHelper.ExpectedException <ArgumentNullException>(() => FileDialogServiceExtensions.ShowSaveFileDialog(null !, owner, rtfFileType));
            AssertHelper.ExpectedException <ArgumentNullException>(() => service.ShowSaveFileDialog(owner, (FileType)null !));

            service.Clear();
            Assert.AreEqual(result, service.ShowSaveFileDialog(rtfFileType, defaultFileName));
            Assert.AreEqual(FileDialogType.SaveFileDialog, service.FileDialogType);
            Assert.AreEqual(rtfFileType, service.FileTypes.Single());
            Assert.AreEqual(defaultFileName, service.DefaultFileName);
            AssertHelper.ExpectedException <ArgumentNullException>(() => FileDialogServiceExtensions.ShowSaveFileDialog(null !, rtfFileType, defaultFileName));
            AssertHelper.ExpectedException <ArgumentNullException>(() => service.ShowSaveFileDialog((FileType)null !, defaultFileName));

            service.Clear();
            Assert.AreEqual(result, service.ShowSaveFileDialog(owner, rtfFileType, defaultFileName));
            Assert.AreEqual(FileDialogType.SaveFileDialog, service.FileDialogType);
            Assert.AreEqual(owner, service.Owner);
            Assert.AreEqual(rtfFileType, service.FileTypes.Single());
            Assert.AreEqual(defaultFileName, service.DefaultFileName);
            AssertHelper.ExpectedException <ArgumentNullException>(() => FileDialogServiceExtensions.ShowSaveFileDialog(null !, owner, rtfFileType, defaultFileName));
            AssertHelper.ExpectedException <ArgumentNullException>(() => service.ShowSaveFileDialog(owner, (FileType)null !, defaultFileName));

            service.Clear();
            Assert.AreEqual(result, service.ShowSaveFileDialog(fileTypes));
            Assert.AreEqual(FileDialogType.SaveFileDialog, service.FileDialogType);
            AssertHelper.SequenceEqual(new[] { rtfFileType, xpsFileType }, service.FileTypes);
            AssertHelper.ExpectedException <ArgumentNullException>(() => FileDialogServiceExtensions.ShowSaveFileDialog(null !, fileTypes));

            service.Clear();
            Assert.AreEqual(result, service.ShowSaveFileDialog(owner, fileTypes));
            Assert.AreEqual(FileDialogType.SaveFileDialog, service.FileDialogType);
            Assert.AreEqual(owner, service.Owner);
            AssertHelper.SequenceEqual(new[] { rtfFileType, xpsFileType }, service.FileTypes);
            AssertHelper.ExpectedException <ArgumentNullException>(() => FileDialogServiceExtensions.ShowSaveFileDialog(null !, owner, fileTypes));

            service.Clear();
            Assert.AreEqual(result, service.ShowSaveFileDialog(fileTypes, rtfFileType, defaultFileName));
            Assert.AreEqual(FileDialogType.SaveFileDialog, service.FileDialogType);
            AssertHelper.SequenceEqual(new[] { rtfFileType, xpsFileType }, service.FileTypes);
            Assert.AreEqual(rtfFileType, service.DefaultFileType);
            Assert.AreEqual(defaultFileName, service.DefaultFileName);
            AssertHelper.ExpectedException <ArgumentNullException>(() => FileDialogServiceExtensions.ShowSaveFileDialog(null !, fileTypes, rtfFileType, defaultFileName));
        }
Exemplo n.º 3
0
        public void ShowMockFileDialogService()
        {
            var fileDialogService = new MockFileDialogService();

            var owner     = new object();
            var fileType  = new FileType("description", ".fileExtension");
            var fileTypes = new[] { fileType };
            var result    = new FileDialogResult("selectedName", fileType);

            fileDialogService.Result = result;

            Assert.AreEqual(result, fileDialogService.ShowOpenFileDialog(owner, fileTypes, fileType, "defaultFileName"));

            Assert.AreEqual(FileDialogType.OpenFileDialog, fileDialogService.FileDialogType);
            Assert.AreEqual(owner, fileDialogService.Owner);
            Assert.AreEqual(fileTypes, fileDialogService.FileTypes);
            Assert.AreEqual(fileType, fileDialogService.DefaultFileType);
            Assert.AreEqual("defaultFileName", fileDialogService.DefaultFileName);

            fileDialogService.Clear();

            Assert.AreEqual(FileDialogType.None, fileDialogService.FileDialogType);
            Assert.IsNull(fileDialogService.Owner);
            Assert.IsFalse(fileDialogService.FileTypes.Any());
            Assert.IsNull(fileDialogService.DefaultFileType);
            Assert.IsNull(fileDialogService.DefaultFileName);


            fileDialogService.Result = result;
            Assert.AreEqual(result, fileDialogService.ShowSaveFileDialog(owner, fileTypes, fileType, "defaultFileName"));

            Assert.AreEqual(FileDialogType.SaveFileDialog, fileDialogService.FileDialogType);
            Assert.AreEqual(owner, fileDialogService.Owner);
            Assert.AreEqual(fileTypes, fileDialogService.FileTypes);
            Assert.AreEqual(fileType, fileDialogService.DefaultFileType);
            Assert.AreEqual("defaultFileName", fileDialogService.DefaultFileName);

            var showOpenFileDialogCalled = false;

            fileDialogService.ShowOpenFileDialogStub = (view, fTypes, defaultFType, defaultFName) =>
            {
                showOpenFileDialogCalled = true;
                Assert.AreSame(owner, view);
                Assert.AreSame(fTypes, fileTypes);
                Assert.AreSame(defaultFType, fileType);
                Assert.AreEqual("defaultFileName", defaultFName);
                return(new FileDialogResult());
            };
            Assert.IsFalse(fileDialogService.ShowOpenFileDialog(owner, fileTypes, fileType, "defaultFileName").IsValid);
            Assert.IsTrue(showOpenFileDialogCalled);

            var showSaveFileDialogCalled = false;

            fileDialogService.ShowSaveFileDialogStub = (view, fTypes, defaultFType, defaultFName) =>
            {
                showSaveFileDialogCalled = true;
                Assert.AreSame(owner, view);
                Assert.AreSame(fTypes, fileTypes);
                Assert.AreSame(defaultFType, fileType);
                Assert.AreEqual("defaultFileName", defaultFName);
                return(new FileDialogResult());
            };
            Assert.IsFalse(fileDialogService.ShowSaveFileDialog(owner, fileTypes, fileType, "defaultFileName").IsValid);
            Assert.IsTrue(showSaveFileDialogCalled);
        }
        public void ShowSaveFileDialogExtensionTest()
        {
            FileType rtfFileType = new FileType("RichText Document", ".rtf");
            FileType xpsFileType = new FileType("XPS Document", ".xps");
            IEnumerable<FileType> fileTypes = new[] { rtfFileType, xpsFileType };
            string defaultFileName = "Document 1.rtf";
            FileDialogResult result = new FileDialogResult("Document 2.rtf", rtfFileType);

            object owner = new object();
            MockFileDialogService service = new MockFileDialogService();
            service.Result = result;

            Assert.AreEqual(result, service.ShowSaveFileDialog(rtfFileType));
            Assert.AreEqual(FileDialogType.SaveFileDialog, service.FileDialogType);
            Assert.AreEqual(rtfFileType, service.FileTypes.Single());
            AssertHelper.ExpectedException<ArgumentNullException>(() => FileDialogServiceExtensions.ShowSaveFileDialog(null, rtfFileType));
            AssertHelper.ExpectedException<ArgumentNullException>(() => service.ShowSaveFileDialog((FileType)null));

            service.Clear();
            Assert.AreEqual(result, service.ShowSaveFileDialog(owner, rtfFileType));
            Assert.AreEqual(FileDialogType.SaveFileDialog, service.FileDialogType);
            Assert.AreEqual(owner, service.Owner);
            Assert.AreEqual(rtfFileType, service.FileTypes.Single());
            AssertHelper.ExpectedException<ArgumentNullException>(() => FileDialogServiceExtensions.ShowSaveFileDialog(null, owner, rtfFileType));
            AssertHelper.ExpectedException<ArgumentNullException>(() => service.ShowSaveFileDialog(owner, (FileType)null));

            service.Clear();
            Assert.AreEqual(result, service.ShowSaveFileDialog(rtfFileType, defaultFileName));
            Assert.AreEqual(FileDialogType.SaveFileDialog, service.FileDialogType);
            Assert.AreEqual(rtfFileType, service.FileTypes.Single());
            Assert.AreEqual(defaultFileName, service.DefaultFileName);
            AssertHelper.ExpectedException<ArgumentNullException>(() => FileDialogServiceExtensions.ShowSaveFileDialog(null, rtfFileType, defaultFileName));
            AssertHelper.ExpectedException<ArgumentNullException>(() => service.ShowSaveFileDialog((FileType)null, defaultFileName));

            service.Clear();
            Assert.AreEqual(result, service.ShowSaveFileDialog(owner, rtfFileType, defaultFileName));
            Assert.AreEqual(FileDialogType.SaveFileDialog, service.FileDialogType);
            Assert.AreEqual(owner, service.Owner);
            Assert.AreEqual(rtfFileType, service.FileTypes.Single());
            Assert.AreEqual(defaultFileName, service.DefaultFileName);
            AssertHelper.ExpectedException<ArgumentNullException>(() => FileDialogServiceExtensions.ShowSaveFileDialog(null, owner, rtfFileType, defaultFileName));
            AssertHelper.ExpectedException<ArgumentNullException>(() => service.ShowSaveFileDialog(owner, (FileType)null, defaultFileName));

            service.Clear();
            Assert.AreEqual(result, service.ShowSaveFileDialog(fileTypes));
            Assert.AreEqual(FileDialogType.SaveFileDialog, service.FileDialogType);
            Assert.IsTrue(service.FileTypes.SequenceEqual(new[] { rtfFileType, xpsFileType }));
            AssertHelper.ExpectedException<ArgumentNullException>(() => FileDialogServiceExtensions.ShowSaveFileDialog(null, fileTypes));

            service.Clear();
            Assert.AreEqual(result, service.ShowSaveFileDialog(owner, fileTypes));
            Assert.AreEqual(FileDialogType.SaveFileDialog, service.FileDialogType);
            Assert.AreEqual(owner, service.Owner);
            Assert.IsTrue(service.FileTypes.SequenceEqual(new[] { rtfFileType, xpsFileType }));
            AssertHelper.ExpectedException<ArgumentNullException>(() => FileDialogServiceExtensions.ShowSaveFileDialog(null, owner, fileTypes));

            service.Clear();
            Assert.AreEqual(result, service.ShowSaveFileDialog(fileTypes, rtfFileType, defaultFileName));
            Assert.AreEqual(FileDialogType.SaveFileDialog, service.FileDialogType);
            Assert.IsTrue(service.FileTypes.SequenceEqual(new[] { rtfFileType, xpsFileType }));
            Assert.AreEqual(rtfFileType, service.DefaultFileType);
            Assert.AreEqual(defaultFileName, service.DefaultFileName);
            AssertHelper.ExpectedException<ArgumentNullException>(() => FileDialogServiceExtensions.ShowSaveFileDialog(null, fileTypes, rtfFileType, defaultFileName));
        }
        public void ShowOpenFileDialogExtensionTest()
        {
            FileType rtfFileType             = new FileType("RichText Document", ".rtf");
            FileType xpsFileType             = new FileType("XPS Document", ".xps");
            IEnumerable <FileType> fileTypes = new[] { rtfFileType, xpsFileType };
            string           defaultFileName = "Document 1.rtf";
            FileDialogResult result          = new FileDialogResult("Document 2.rtf", rtfFileType);

            object owner = new object();
            MockFileDialogService service = new MockFileDialogService();

            service.Result = result;

            Assert.AreEqual(result, service.ShowOpenFileDialog(rtfFileType));
            Assert.AreEqual(FileDialogType.OpenFileDialog, service.FileDialogType);
            Assert.AreEqual(rtfFileType, service.FileTypes.Single());
            AssertHelper.ExpectedException <ArgumentNullException>(() => FileDialogServiceExtensions.ShowOpenFileDialog(null, rtfFileType));
            AssertHelper.ExpectedException <ArgumentNullException>(() => service.ShowOpenFileDialog((FileType)null));

            service.Clear();
            Assert.AreEqual(result, service.ShowOpenFileDialog(owner, rtfFileType));
            Assert.AreEqual(FileDialogType.OpenFileDialog, service.FileDialogType);
            Assert.AreEqual(owner, service.Owner);
            Assert.AreEqual(rtfFileType, service.FileTypes.Single());
            AssertHelper.ExpectedException <ArgumentNullException>(() => FileDialogServiceExtensions.ShowOpenFileDialog(null, owner, rtfFileType));
            AssertHelper.ExpectedException <ArgumentNullException>(() => service.ShowOpenFileDialog(owner, (FileType)null));

            service.Clear();
            Assert.AreEqual(result, service.ShowOpenFileDialog(rtfFileType, defaultFileName));
            Assert.AreEqual(FileDialogType.OpenFileDialog, service.FileDialogType);
            Assert.AreEqual(rtfFileType, service.FileTypes.Single());
            Assert.AreEqual(defaultFileName, service.DefaultFileName);
            AssertHelper.ExpectedException <ArgumentNullException>(() => FileDialogServiceExtensions.ShowOpenFileDialog(null, rtfFileType, defaultFileName));
            AssertHelper.ExpectedException <ArgumentNullException>(() => service.ShowOpenFileDialog((FileType)null, defaultFileName));

            service.Clear();
            Assert.AreEqual(result, service.ShowOpenFileDialog(owner, rtfFileType, defaultFileName));
            Assert.AreEqual(FileDialogType.OpenFileDialog, service.FileDialogType);
            Assert.AreEqual(owner, service.Owner);
            Assert.AreEqual(rtfFileType, service.FileTypes.Single());
            Assert.AreEqual(defaultFileName, service.DefaultFileName);
            AssertHelper.ExpectedException <ArgumentNullException>(() => FileDialogServiceExtensions.ShowOpenFileDialog(null, owner, rtfFileType, defaultFileName));
            AssertHelper.ExpectedException <ArgumentNullException>(() => service.ShowOpenFileDialog(owner, (FileType)null, defaultFileName));

            service.Clear();
            Assert.AreEqual(result, service.ShowOpenFileDialog(fileTypes));
            Assert.AreEqual(FileDialogType.OpenFileDialog, service.FileDialogType);
            Assert.IsTrue(service.FileTypes.SequenceEqual(new[] { rtfFileType, xpsFileType }));
            AssertHelper.ExpectedException <ArgumentNullException>(() => FileDialogServiceExtensions.ShowOpenFileDialog(null, fileTypes));

            service.Clear();
            Assert.AreEqual(result, service.ShowOpenFileDialog(owner, fileTypes));
            Assert.AreEqual(FileDialogType.OpenFileDialog, service.FileDialogType);
            Assert.AreEqual(owner, service.Owner);
            Assert.IsTrue(service.FileTypes.SequenceEqual(new[] { rtfFileType, xpsFileType }));
            AssertHelper.ExpectedException <ArgumentNullException>(() => FileDialogServiceExtensions.ShowOpenFileDialog(null, owner, fileTypes));

            service.Clear();
            Assert.AreEqual(result, service.ShowOpenFileDialog(fileTypes, rtfFileType, defaultFileName));
            Assert.AreEqual(FileDialogType.OpenFileDialog, service.FileDialogType);
            Assert.IsTrue(service.FileTypes.SequenceEqual(new[] { rtfFileType, xpsFileType }));
            Assert.AreEqual(rtfFileType, service.DefaultFileType);
            Assert.AreEqual(defaultFileName, service.DefaultFileName);
            AssertHelper.ExpectedException <ArgumentNullException>(() => FileDialogServiceExtensions.ShowOpenFileDialog(null, fileTypes, rtfFileType, defaultFileName));
        }