Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / WPF

Catel - Part 4 of n: Unit testing with Catel

Rate me:
Please Sign up or sign in to vote.
4.55/5 (10 votes)
28 Jan 2011CPOL11 min read 48.6K   572   11  
This article explains how to write unit tests for MVVM using Catel.
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Rtf.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   Class defining all the Rtf constants and methods.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using Catel.Windows.Controls;

namespace Catel.Windows.Documents
{
    /// <summary>
    /// Class defining all the Rtf constants and methods.
    /// </summary>
    public static class Rtf
    {
        #region Constants
        /// <summary>
        /// Rtf marker for font style.
        /// </summary>
        public const string RtfFontStyleMarker = "{\\fonttbl";

        /// <summary>
        /// Rtf marker for font size.
        /// </summary>
        public const string RtfFontSizeMarker = "\\fs";

        private const string CarriageReturn = "\r";
        private const string LineFeed = "\n";
        private const string CarriageReturnLineFeed = "\r\n";

        private const string CustomCarriageReturn = "[cr]";
        private const string CustomLineFeed = "[lf]";
        private const string CustomCarriageReturnLineFeed = "[crlf]";
        #endregion

        #region Methods

        /// <summary>
        /// Converts plain text to Rtf with the newline bug fix.
        /// </summary>
        /// <param name="text">Text to convert.</param>
        /// <param name="fontSize">Size of the font.</param>
        /// <param name="fontFamily">The font family.</param>
        /// <returns>Rtf version of the text.</returns>
        public static string ConvertPlainTextToRtf(this string text, double fontSize, FontFamily fontFamily)
        {
            const string RtfCarriageReturn = "\\line ";

            // Replace all known feeds to a custom constant
            text = text.Replace(CarriageReturnLineFeed, CustomCarriageReturnLineFeed).Replace(CarriageReturn, CustomCarriageReturn).Replace(LineFeed, CustomLineFeed);

            // Now convert all working feeds back
            text = text.Replace(CustomCarriageReturnLineFeed, CarriageReturnLineFeed).Replace(CustomLineFeed, CarriageReturnLineFeed);

            // Convert using a control
            RichTextBox rtfControl = CreateRichTextBoxWithText(text, fontSize, fontFamily);

            // Read the text again (now it should be rtf)
            text = rtfControl.GetText();

            // Now replace all known bugs
            text = text.Replace(CustomCarriageReturn, RtfCarriageReturn);

            return text;
        }

        /// <summary>
        /// Converts the RTF to plain text.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <returns></returns>
        public static string ConvertRtfToPlainText(this string text)
        {
            // Fix the newline bug by replacing \par by \par\par and \line by \par
            text = text.Replace("\\par", "\\par\\par").Replace("\\line", "\\par").Replace("\\par\\pard", "\\pard");

            // Convert using a control
            RichTextBox rtfControl = CreateRichTextBoxWithText(text);

            // Get the text range
            var textRange = new TextRange(rtfControl.Document.ContentStart, rtfControl.Document.ContentEnd);

            // Get the plain text from the text range
            text = textRange.Text;

            // Remove last linefeeds
            while (text.EndsWith(Environment.NewLine))
            {
                text = text.Substring(0, text.Length - Environment.NewLine.Length);
            }

            // Replace all known feeds to a custom constant
            text = text.Replace(CarriageReturnLineFeed + CarriageReturnLineFeed, CustomCarriageReturnLineFeed).Replace(CarriageReturnLineFeed, CustomCarriageReturn).Replace(LineFeed, CustomLineFeed);

            // Now convert all working feeds back
            text = text.Replace(CustomCarriageReturnLineFeed, CarriageReturnLineFeed).Replace(CustomCarriageReturn, CarriageReturn).Replace(CustomLineFeed, CarriageReturnLineFeed);

            // Return text
            return text;
        }
        #endregion

        #region Private helper methods

        private static RichTextBox CreateRichTextBoxWithText(string text)
        {
            return CreateRichTextBoxWithText(text, null, null);
        }

        private static RichTextBox CreateRichTextBoxWithText(string text, double? fontSize, FontFamily fontFamily)
        {
            var rtfControl = new RichTextBox();
            rtfControl.Width = 300;
            rtfControl.Height = 300;

            var textRange = new TextRange(rtfControl.Document.ContentStart, rtfControl.Document.ContentEnd);
            textRange.SetText(text);

            if (fontSize.HasValue)
            {
                rtfControl.FontSize = fontSize.Value;
            }

            if (fontFamily != null)
            {
                rtfControl.FontFamily = fontFamily;
            }

            textRange.ClearTextMargins();

            return rtfControl;
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions