Exemplo n.º 1
0
        public void TestAddStreamContentType()
        {
            var      fileName    = Path.Combine(TestHelper.ProjectDir, "TestData", "images", "girl.jpg");
            var      contentType = new ContentType("image", "gif");
            var      attachments = new AttachmentCollection();
            MimePart attachment;

            using (var stream = File.OpenRead(fileName))
                attachment = (MimePart)attachments.Add(fileName, stream, contentType);

            Assert.AreEqual(contentType.MimeType, attachment.ContentType.MimeType);
            Assert.AreEqual("girl.jpg", attachment.ContentType.Name);
            Assert.NotNull(attachment.ContentDisposition);
            Assert.AreEqual("attachment", attachment.ContentDisposition.Disposition);
            Assert.AreEqual("girl.jpg", attachment.ContentDisposition.FileName);
            Assert.AreEqual("girl.jpg", attachment.FileName);
            Assert.AreEqual(ContentEncoding.Base64, attachment.ContentTransferEncoding);
            Assert.AreEqual(1, attachments.Count);

            Assert.IsTrue(attachments.Contains(attachment), "Contains");
            Assert.AreEqual(0, attachments.IndexOf(attachment), "IndexOf");
            Assert.IsTrue(attachments.Remove(attachment), "Remove");
            Assert.AreEqual(0, attachments.Count);
            attachments.Clear();
        }
Exemplo n.º 2
0
        public void TestArgumentExceptions()
        {
            var contentType = new ContentType("application", "octet-stream");
            var attachments = new AttachmentCollection();
            var items       = new MimeEntity[10];
            var data        = new byte[1024];

            using (var stream = new MemoryStream()) {
                Assert.Throws <ArgumentException> (() => attachments.Add(string.Empty));
                Assert.Throws <ArgumentNullException> (() => attachments.Add((string)null));
                Assert.Throws <ArgumentNullException> (() => attachments.Add((MimeEntity)null));
                Assert.Throws <ArgumentException> (() => attachments.Add(string.Empty, data));
                Assert.Throws <ArgumentNullException> (() => attachments.Add((string)null, data));
                Assert.Throws <ArgumentException> (() => attachments.Add(string.Empty, stream));
                Assert.Throws <ArgumentNullException> (() => attachments.Add((string)null, stream));
                Assert.Throws <ArgumentException> (() => attachments.Add(string.Empty, contentType));
                Assert.Throws <ArgumentNullException> (() => attachments.Add((string)null, contentType));
                Assert.Throws <ArgumentException> (() => attachments.Add(string.Empty, data, contentType));
                Assert.Throws <ArgumentNullException> (() => attachments.Add((string)null, data, contentType));
                Assert.Throws <ArgumentException> (() => attachments.Add(string.Empty, stream, contentType));
                Assert.Throws <ArgumentNullException> (() => attachments.Add((string)null, stream, contentType));

                Assert.Throws <ArgumentNullException> (() => attachments.Add("file.dat", (byte[])null));
                Assert.Throws <ArgumentNullException> (() => attachments.Add("file.dat", (Stream)null));
                Assert.Throws <ArgumentNullException> (() => attachments.Add("file.dat", (byte[])null, contentType));
                Assert.Throws <ArgumentNullException> (() => attachments.Add("file.dat", (Stream)null, contentType));

                Assert.Throws <ArgumentNullException> (() => attachments.Add("file.dat", (ContentType)null));
                Assert.Throws <ArgumentNullException> (() => attachments.Add("file.dat", data, null));
                Assert.Throws <ArgumentNullException> (() => attachments.Add("file.dat", stream, null));

                Assert.Throws <ArgumentNullException> (() => attachments.Contains(null));

                Assert.Throws <ArgumentNullException> (() => attachments.CopyTo(null, 0));
                Assert.Throws <ArgumentOutOfRangeException> (() => attachments.CopyTo(items, -1));

                Assert.Throws <ArgumentNullException> (() => attachments.IndexOf(null));

                Assert.Throws <ArgumentNullException> (() => attachments.Remove(null));
                Assert.Throws <ArgumentOutOfRangeException> (() => attachments.RemoveAt(0));

                attachments.Add(new TextPart("plain"));
                Assert.Throws <ArgumentOutOfRangeException> (() => { var x = attachments[10]; });
                Assert.Throws <ArgumentOutOfRangeException> (() => attachments[10] = new TextPart("plain"));
                Assert.Throws <ArgumentNullException> (() => attachments[0]        = null);

                Assert.Throws <ArgumentOutOfRangeException> (() => attachments.Insert(-1, new TextPart("plain")));
                Assert.Throws <ArgumentNullException> (() => attachments.Insert(0, null));
            }
        }
Exemplo n.º 3
0
        public void TestAddEmailMessage()
        {
            var        fileName    = Path.Combine(TestHelper.ProjectDir, "TestData", "messages", "body.1.txt");
            var        attachments = new AttachmentCollection();
            MimeEntity attachment;

            using (var stream = File.OpenRead(fileName))
                attachment = attachments.Add("message.eml", stream);

            Assert.AreEqual("message/rfc822", attachment.ContentType.MimeType);
            Assert.AreEqual(1, attachments.Count);

            Assert.IsTrue(attachments.Contains(attachment), "Contains");
            Assert.AreEqual(0, attachments.IndexOf(attachment), "IndexOf");
            Assert.IsTrue(attachments.Remove(attachment), "Remove");
            Assert.AreEqual(0, attachments.Count);
            attachments.Clear();
        }
Exemplo n.º 4
0
        public void TestAddFileName()
        {
            var      fileName    = Path.Combine(TestHelper.ProjectDir, "TestData", "images", "girl.jpg");
            var      attachments = new AttachmentCollection();
            MimePart attachment;

            attachment = (MimePart)attachments.Add(fileName);
            Assert.AreEqual("image/jpeg", attachment.ContentType.MimeType);
            Assert.AreEqual("girl.jpg", attachment.FileName);
            Assert.AreEqual(ContentEncoding.Base64, attachment.ContentTransferEncoding);
            Assert.AreEqual(1, attachments.Count);

            Assert.IsTrue(attachments.Contains(attachment), "Contains");
            Assert.AreEqual(0, attachments.IndexOf(attachment), "IndexOf");
            Assert.IsTrue(attachments.Remove(attachment), "Remove");
            Assert.AreEqual(0, attachments.Count);
            attachments.Clear();
        }
        public void TestAddTextFileName()
        {
            var      fileName    = Path.Combine(TestHelper.ProjectDir, "TestData", "text", "lorem-ipsum.txt");
            var      attachments = new AttachmentCollection();
            MimePart attachment;

            attachment = (MimePart)attachments.Add(fileName);
            Assert.AreEqual("text/plain", attachment.ContentType.MimeType);
            Assert.AreEqual("lorem-ipsum.txt", attachment.FileName);
            Assert.AreEqual(ContentEncoding.SevenBit, attachment.ContentTransferEncoding);
            Assert.AreEqual(1, attachments.Count);

            Assert.IsTrue(attachments.Contains(attachment), "Contains");
            Assert.AreEqual(0, attachments.IndexOf(attachment), "IndexOf");
            Assert.IsTrue(attachments.Remove(attachment), "Remove");
            Assert.AreEqual(0, attachments.Count);
            attachments.Clear();
        }
Exemplo n.º 6
0
        public void TestAddDataContentType()
        {
            var      fileName    = Path.Combine("..", "..", "TestData", "images", "girl.jpg");
            var      contentType = new ContentType("image", "gif");
            var      attachments = new AttachmentCollection();
            MimePart attachment;

            attachment = (MimePart)attachments.Add(fileName, File.ReadAllBytes(fileName), contentType);
            Assert.AreEqual(contentType.MimeType, attachment.ContentType.MimeType);
            Assert.AreEqual("girl.jpg", attachment.FileName);
            Assert.AreEqual(ContentEncoding.Base64, attachment.ContentTransferEncoding);
            Assert.AreEqual(1, attachments.Count);

            Assert.IsTrue(attachments.Contains(attachment), "Contains");
            Assert.AreEqual(0, attachments.IndexOf(attachment), "IndexOf");
            Assert.IsTrue(attachments.Remove(attachment), "Remove");
            Assert.AreEqual(0, attachments.Count);
            attachments.Clear();
        }
Exemplo n.º 7
0
        public void TestAddStream()
        {
            var      fileName    = Path.Combine("..", "..", "TestData", "images", "girl.jpg");
            var      attachments = new AttachmentCollection();
            MimePart attachment;

            using (var stream = File.OpenRead(fileName))
                attachment = (MimePart)attachments.Add(fileName, stream);

            Assert.AreEqual("image/jpeg", attachment.ContentType.MimeType);
            Assert.AreEqual("girl.jpg", attachment.FileName);
            Assert.AreEqual(ContentEncoding.Base64, attachment.ContentTransferEncoding);
            Assert.AreEqual(1, attachments.Count);

            Assert.IsTrue(attachments.Contains(attachment), "Contains");
            Assert.AreEqual(0, attachments.IndexOf(attachment), "IndexOf");
            Assert.IsTrue(attachments.Remove(attachment), "Remove");
            Assert.AreEqual(0, attachments.Count);
            attachments.Clear();
        }
Exemplo n.º 8
0
        public async Task TestAddInlineEmailMessageAsync()
        {
            var        fileName    = Path.Combine(TestHelper.ProjectDir, "TestData", "messages", "body.1.txt");
            var        attachments = new AttachmentCollection(true);
            MimeEntity attachment;

            using (var stream = File.OpenRead(fileName))
                attachment = await attachments.AddAsync("message.eml", stream);

            Assert.AreEqual("message/rfc822", attachment.ContentType.MimeType);
            Assert.AreEqual("message.eml", attachment.ContentType.Name);
            Assert.NotNull(attachment.ContentDisposition);
            Assert.AreEqual("inline", attachment.ContentDisposition.Disposition);
            Assert.AreEqual("message.eml", attachment.ContentDisposition.FileName);
            Assert.AreEqual(1, attachments.Count);

            Assert.IsTrue(attachments.Contains(attachment), "Contains");
            Assert.AreEqual(0, attachments.IndexOf(attachment), "IndexOf");
            Assert.IsTrue(attachments.Remove(attachment), "Remove");
            Assert.AreEqual(0, attachments.Count);
            attachments.Clear();
        }
Exemplo n.º 9
0
        public async Task TestAddInlineFileNameAsync()
        {
            var      fileName    = Path.Combine(TestHelper.ProjectDir, "TestData", "images", "girl.jpg");
            var      attachments = new AttachmentCollection(true);
            MimePart attachment;

            attachment = (MimePart)await attachments.AddAsync(fileName);

            Assert.AreEqual("image/jpeg", attachment.ContentType.MimeType);
            Assert.AreEqual("girl.jpg", attachment.ContentType.Name);
            Assert.NotNull(attachment.ContentDisposition);
            Assert.AreEqual("inline", attachment.ContentDisposition.Disposition);
            Assert.AreEqual("girl.jpg", attachment.ContentDisposition.FileName);
            Assert.AreEqual("girl.jpg", attachment.FileName);
            Assert.AreEqual(ContentEncoding.Base64, attachment.ContentTransferEncoding);
            Assert.AreEqual(1, attachments.Count);

            Assert.IsTrue(attachments.Contains(attachment), "Contains");
            Assert.AreEqual(0, attachments.IndexOf(attachment), "IndexOf");
            Assert.IsTrue(attachments.Remove(attachment), "Remove");
            Assert.AreEqual(0, attachments.Count);
            attachments.Clear();
        }
Exemplo n.º 10
0
 public void RemoveCount()
 {
     ac.Remove(a);
     Assert.Equal(0, ac.Count);
 }
 public void RemoveCount()
 {
     ac.Remove(a);
     Assert.IsTrue(ac.Count == 0);
 }
        /// <summary>
        /// Occurs when a right-tap input stimulus happens while the pointer is over the attachments list.
        /// </summary>
        /// <param name="sender">The object that raised the event (ListView).</param>
        /// <param name="e">The event data (RightTappedRoutedEventArgs).</param>
        private async void MessageAttachments_RightTapped(object sender, RightTappedRoutedEventArgs e)
        {
            // Don't show the appbar
            e.Handled = true;
            if (MainPage.Current.BottomAppBar.IsOpen)
            {
                MainPage.Current.BottomAppBar.IsOpen = false;
            }

            // Get the attachment
            Attachment  attachment     = (Attachment)((FrameworkElement)e.OriginalSource).DataContext;
            StorageFile newStorageFile = await StorageFile.GetFileFromPathAsync(Path.Combine(ApplicationData.Current.TemporaryFolder.Path, attachment.TransferFilename));

            // Create a menu and add commands specifying a callback delegate for each.
            // Since command delegates are unique, no need to specify command Ids.
            PopupMenu menu = new PopupMenu();

            menu.Commands.Add(new UICommand("Open", async(command) =>
            {
                await Launcher.LaunchFileAsync(newStorageFile, new LauncherOptions()
                {
                    TreatAsUntrusted = false
                });
            }));
            menu.Commands.Add(new UICommand("Open With", async(command) =>
            {
                await Launcher.LaunchFileAsync(newStorageFile, new LauncherOptions()
                {
                    DisplayApplicationPicker = true
                });
            }));
            if (Mode == AttachmentListViewMode.ReadOnly)
            {
                menu.Commands.Add(new UICommand("Save", async(command) =>
                {
                    FileSavePicker fileSavePicker = new FileSavePicker();
                    fileSavePicker.FileTypeChoices.Add("File", new List <string>()
                    {
                        attachment.TransferFilenameExtension
                    });
                    fileSavePicker.SuggestedFileName      = newStorageFile.Name;
                    fileSavePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
                    StorageFile saveStorageFile           = await fileSavePicker.PickSaveFileAsync();
                    if (saveStorageFile != null)
                    {
                        await newStorageFile.CopyAndReplaceAsync(saveStorageFile);
                    }
                }));
            }
            else
            {
                menu.Commands.Add(new UICommand("Remove", (command) =>
                {
                    AttachmentCollection attachmentCollection = (AttachmentCollection)ItemsSource;
                    attachmentCollection.Remove(attachment);
                    ItemsSource = null;
                    ItemsSource = attachmentCollection;
                }));
            }

            // Show the menu
            await menu.ShowForSelectionAsync(App.GetElementRect(attachmentRightTapped), Placement.Below);
        }