Beispiel #1
0
        public void AppendPeekAndGetTextWithTextNotFittingFlags()
        {
            // Arrange
            var flags1 = new Flags()
            {
                FontSize = 12,
                Font     = "x"
            };
            var flags2 = new Flags()
            {
                FontSize = 14,
                Font     = "y"
            };
            var initialWidth = 30;
            var constraint   = new DimensionConstraint()
            {
                MaxHeight = 12, MaxWidth = initialWidth
            };
            var text = "A string";

            // Act
            bufferUnderTest.Append(text, flags1);

            var peekedText  = bufferUnderTest.Peek(constraint, flags2);
            var takenText   = bufferUnderTest.Take(constraint, flags2);
            var bufferEmpty = bufferUnderTest.EndOfBuffer();

            // Assert
            Assert.AreEqual(null, peekedText);
            Assert.AreEqual(null, takenText);
            Assert.IsFalse(bufferEmpty, "The buffer should not be empty");
        }
Beispiel #2
0
        public void AppendPeekAndGetTextWithSameFlagsTooLongForTheConstraints()
        {
            // Arrange
            var flags = new Flags()
            {
                FontSize = 12,
                Font     = "x"
            };
            var initialWidth = 25;
            var constraint   = new DimensionConstraint()
            {
                MaxHeight = 12, MaxWidth = initialWidth
            };
            var text       = "A longer text string";
            var firstText  = "A longer";
            var secondText = " text string";

            // The whole text can fit within the given width
            textScoperMock.Setup(ts => ts.GetFittingText(flags.Font, flags.FontSize, text, initialWidth)).Returns(firstText);
            textScoperMock.Setup(ts => ts.GetFittingText(flags.Font, flags.FontSize, secondText, initialWidth)).Returns(secondText);

            // Act
            bufferUnderTest.Append(text, flags);

            var peekedText           = bufferUnderTest.Peek(constraint, flags);
            var takenText            = bufferUnderTest.Take(constraint, flags);
            var additionalPeekedText = bufferUnderTest.Peek(constraint, flags);
            var additionalTakenText  = bufferUnderTest.Take(constraint, flags);
            var bufferEmpty1         = bufferUnderTest.EndOfBuffer();

            bufferUnderTest.Append(text, flags);
            var peekedObject           = (FormattedTextMock)bufferUnderTest.Peek(constraint);
            var takenObject            = (FormattedTextMock)bufferUnderTest.Take(constraint);
            var additionalPeekedObject = (FormattedTextMock)bufferUnderTest.Peek(constraint);
            var additionalTakenObject  = (FormattedTextMock)bufferUnderTest.Take(constraint);
            var bufferEmpty2           = bufferUnderTest.EndOfBuffer();

            // Assert
            Assert.AreEqual(firstText, peekedText);
            Assert.AreEqual(secondText, additionalPeekedText);
            Assert.AreEqual(firstText, takenText);
            Assert.AreEqual(secondText, additionalTakenText);
            Assert.IsTrue(bufferEmpty1, "The buffer should be empty");

            Assert.AreEqual(firstText, peekedObject.Text);
            Assert.AreEqual(false, peekedObject.AllowZeroPosition);
            Assert.AreEqual(flags, peekedObject.Flags);
            Assert.AreEqual(firstText, takenObject.Text);
            Assert.AreEqual(false, takenObject.AllowZeroPosition);
            Assert.AreEqual(flags, takenObject.Flags);
            Assert.AreEqual(secondText, additionalPeekedObject.Text);
            Assert.AreEqual(false, additionalPeekedObject.AllowZeroPosition);
            Assert.AreEqual(flags, additionalPeekedObject.Flags);
            Assert.AreEqual(secondText, additionalTakenObject.Text);
            Assert.AreEqual(false, additionalTakenObject.AllowZeroPosition);
            Assert.AreEqual(flags, additionalTakenObject.Flags);
            Assert.IsTrue(bufferEmpty2, "The buffer should be empty");
        }
Beispiel #3
0
        /// <summary>
        /// Insers the content of the document buffer into itself and sub containers.
        /// </summary>
        /// <param name="documentBuffer">The document buffer.</param>
        /// <param name="dimensionConstraint">The constraining dimensions.</param>
        public void Insert(IDocumentBuffer documentBuffer, IDimensionConstraint dimensionConstraint)
        {
            if (dimensionConstraint.MaxWidth == null)
            {
                throw new Exception("The formatted text object must be given a width constraint on insert.");
            }

            var textBeforeCursor   = Strings.strsubutf8(this.text, 0, this.cursorPos);
            var xBeforeCursor      = this.textScoper.GetWidth(this.flags.Font, this.flags.FontSize, textBeforeCursor);
            var availableDimension = new DimensionConstraint()
            {
                MaxWidth  = (double)dimensionConstraint.MaxWidth - xBeforeCursor,
                MaxHeight = dimensionConstraint.MaxHeight,
            };

            var addedText = documentBuffer.Take(availableDimension, this.GetCurrentFlags());

            if (addedText == String.Empty)
            {
                return;
            }

            var addedTextSize = Strings.strlenutf8(addedText);

            if (this.cursorPos < this.GetLength()) // The cursor is not and the end. The inserted text is thus put in at the cursor position
            {
                var textAfterCursor = Strings.strsubutf8(this.text, this.cursorPos);
                this.text = Strings.strsubutf8(this.text, 0, this.cursorPos);

                var bufferEmpty = documentBuffer.EndOfBuffer();

                documentBuffer.Append(textAfterCursor, this.GetCurrentFlags());

                if (bufferEmpty)
                {
                    availableDimension = new DimensionConstraint()
                    {
                        MaxWidth  = (double)dimensionConstraint.MaxWidth - this.textScoper.GetWidth(this.flags.Font, this.flags.FontSize, this.text + addedText),
                        MaxHeight = dimensionConstraint.MaxHeight,
                    };

                    addedText += documentBuffer.Take(availableDimension, this.GetCurrentFlags());
                }
            }

            if (this.cursor != null)
            {
                this.cursorPos += addedTextSize;
                this.CursorChanged();
            }

            this.text += addedText;
            this.TextChanged();
        }
        public MainPage()
        {
            InitializeComponent();

            var mainCollection = (ConstraintCollection)Resources["Constraints"];
            var altCollection  = (ConstraintCollection)Resources["AltConstraints"];

            mainCollection.Add(PositionConstraint.CenterX(Toggle).ToCenterX(Layout));
            mainCollection.Add(PositionConstraint.CenterY(Toggle).ToCenterY(Layout));
            mainCollection.Add(DimensionConstraint.Width(Toggle, -20).ToWidth(Layout, 0.5f));
            mainCollection.Add(DimensionConstraint.Height(Toggle, -20).ToHeight(Layout, 0.3f));

            altCollection.Add(PositionConstraint.Left(Toggle).ToLeft(LeftLabel));
            altCollection.Add(DimensionConstraint.Width(Toggle, 400));
            altCollection.Add(PositionConstraint.Top(Toggle).ToBottom(BottomLabel));
            altCollection.Add(PositionConstraint.Bottom(Toggle).ToBottom(Layout));
        }
Beispiel #5
0
        public void AppendPeekAndGetTextWithTextLargerThanHeight()
        {
            // Arrange
            var flags = new Flags()
            {
                FontSize = 12,
                Font     = "x"
            };

            var constraint = new DimensionConstraint()
            {
                MaxHeight = 12, MaxWidth = 30
            };

            // The whole text can fit within the given width
            var elementMock = new Mock <IElement>();

            elementMock.Setup(e => e.GetHeight()).Returns(14);
            elementMock.Setup(e => e.GetWidth()).Returns(14);

            // Act
            bufferUnderTest.Append(elementMock.Object);

            var peekedText   = bufferUnderTest.Peek(constraint, flags);
            var takenText    = bufferUnderTest.Take(constraint, flags);
            var bufferEmpty1 = bufferUnderTest.EndOfBuffer();

            var peekedObject = bufferUnderTest.Peek(constraint);
            var takenObject  = bufferUnderTest.Take(constraint);
            var bufferEmpty2 = bufferUnderTest.EndOfBuffer();

            // Assert
            Assert.AreEqual(null, peekedText);
            Assert.AreEqual(null, takenText);
            Assert.IsFalse(bufferEmpty1, "The buffer should not be empty");

            Assert.AreEqual(null, peekedObject);
            Assert.AreEqual(null, takenObject);
            Assert.IsFalse(bufferEmpty2, "The buffer should not be empty");
        }