Index: Core/Common/src/Core.Common.Controls/TextEditor/RichTextBoxControl.cs =================================================================== diff -u -r9f1e95f305bbc982a11f7a8917a95bf6caa18544 -r895e4cf56972494e9e363f0af9cd9e21d55e9004 --- Core/Common/src/Core.Common.Controls/TextEditor/RichTextBoxControl.cs (.../RichTextBoxControl.cs) (revision 9f1e95f305bbc982a11f7a8917a95bf6caa18544) +++ Core/Common/src/Core.Common.Controls/TextEditor/RichTextBoxControl.cs (.../RichTextBoxControl.cs) (revision 895e4cf56972494e9e363f0af9cd9e21d55e9004) @@ -21,8 +21,6 @@ using System; using System.Drawing; -using System.IO; -using System.Text; using System.Windows.Forms; namespace Core.Common.Controls.TextEditor @@ -32,6 +30,9 @@ /// public partial class RichTextBoxControl : UserControl { + private bool loaded; + private string rtfToSetAfterLoad; + /// /// The event which is send when the text changes. /// @@ -46,27 +47,57 @@ richTextBox.TextChanged += OnTextChanged; richTextBox.KeyDown += OnKeyDown; + + Load += OnLoad; } + /// + /// This is needed for the RichTextBox to apply styling. + /// + private void OnLoad(object sender, EventArgs eventArgs) + { + loaded = true; + + if (!string.IsNullOrEmpty(rtfToSetAfterLoad)) + { + richTextBox.Rtf = rtfToSetAfterLoad; + } + } + + /// + /// Gets or sets the Rtf from the . + /// public string Rtf { get { - return richTextBox.Rtf; + return loaded ? richTextBox.Rtf : rtfToSetAfterLoad; } set { - richTextBox.Rtf = value; + if (loaded) + { + richTextBox.Rtf = value; + } + else + { + rtfToSetAfterLoad = value; + } } } #region Event handling private void OnTextChanged(object sender, EventArgs e) { + OnTextBoxValueChanged(e); + } + + private void OnTextBoxValueChanged(EventArgs e) + { if (TextBoxValueChanged != null) { - TextBoxValueChanged(sender, e); + TextBoxValueChanged(this, e); } } Index: Core/Common/test/Core.Common.Controls.Test/TextEditor/RichTextBoxControlTest.cs =================================================================== diff -u -r9f1e95f305bbc982a11f7a8917a95bf6caa18544 -r895e4cf56972494e9e363f0af9cd9e21d55e9004 --- Core/Common/test/Core.Common.Controls.Test/TextEditor/RichTextBoxControlTest.cs (.../RichTextBoxControlTest.cs) (revision 9f1e95f305bbc982a11f7a8917a95bf6caa18544) +++ Core/Common/test/Core.Common.Controls.Test/TextEditor/RichTextBoxControlTest.cs (.../RichTextBoxControlTest.cs) (revision 895e4cf56972494e9e363f0af9cd9e21d55e9004) @@ -21,20 +21,94 @@ } [Test] - public void Data_ValueSet_TextAsExpected() + public void Rtf_ValueSetWhenLoaded_TextAsExpected() { // Setup - var data = ""; - var control = new RichTextBoxControl(); + using (var form = new Form()) + { + // Show the view + var control = new RichTextBoxControl(); + form.Controls.Add(control); + form.Show(); - // Call - control.Rtf = GetValidRtfString(data); + var data = ""; - // Assert - Assert.AreEqual(data, control.Controls[0].Text); + // Call + control.Rtf = GetValidRtfString(data); + + // Assert + Assert.AreEqual(data, control.Controls[0].Text); + } } [Test] + public void Rtf_ValueSetWhenFormNotLoaded_TextAsExpected() + { + // Setup + using (var form = new Form()) + { + // Show the view + var control = new RichTextBoxControl(); + + var data = ""; + + control.Rtf = GetValidRtfString(data); + form.Controls.Add(control); + + // Call + form.Show(); + + // Assert + Assert.AreEqual(data, control.Controls[0].Text); + } + } + + [Test] + public void Rtf_ValueSetWhenLoaded_ReturnsValue() + { + // Setup + using (var form = new Form()) + { + // Show the view + var control = new RichTextBoxControl(); + form.Controls.Add(control); + form.Show(); + + var data = ""; + var validRtfString = GetValidRtfString(data); + + // Call + control.Rtf = validRtfString; + + // Assert + Assert.AreEqual(validRtfString, control.Rtf); + } + } + + [Test] + public void Rtf_ValueSetWhenFormNotLoaded_ReturnsValue() + { + // Setup + using (var form = new Form()) + { + // Show the view + var control = new RichTextBoxControl(); + + var data = ""; + var validRtfString = GetValidRtfString(data); + + control.Rtf = validRtfString; + form.Controls.Add(control); + + // Call + form.Show(); + + // Assert + Assert.AreEqual(validRtfString, control.Rtf); + } + } + + [Test] public void RichTextBoxControl_RichTextBoxTextChanged_TextBoxValueChangedEventRaised() { // Setup @@ -65,7 +139,7 @@ // Assert Assert.AreEqual(1, eventCounter); Assert.AreSame(eventArgs, sendEventArgs); - Assert.AreSame(richTextBox, eventSender); + Assert.IsInstanceOf(eventSender); } } Index: Ringtoets/Integration/src/Ringtoets.Integration.Forms/Views/AssessmentSectionCommentView.cs =================================================================== diff -u -r9f1e95f305bbc982a11f7a8917a95bf6caa18544 -r895e4cf56972494e9e363f0af9cd9e21d55e9004 --- Ringtoets/Integration/src/Ringtoets.Integration.Forms/Views/AssessmentSectionCommentView.cs (.../AssessmentSectionCommentView.cs) (revision 9f1e95f305bbc982a11f7a8917a95bf6caa18544) +++ Ringtoets/Integration/src/Ringtoets.Integration.Forms/Views/AssessmentSectionCommentView.cs (.../AssessmentSectionCommentView.cs) (revision 895e4cf56972494e9e363f0af9cd9e21d55e9004) @@ -70,10 +70,10 @@ }; Controls.Add(richTextEditor); - richTextEditor.TextBoxValueChanged += RichTextEditorOnTextChanged; + richTextEditor.TextBoxValueChanged += OnTextBoxValueChanged; } - private void RichTextEditorOnTextChanged(object sender, EventArgs eventArgs) + private void OnTextBoxValueChanged(object sender, EventArgs eventArgs) { data.Comments = richTextEditor.Rtf; } Index: Ringtoets/Integration/test/Ringtoets.Integration.Forms.Test/Views/AssessmentSectionCommentViewTest.cs =================================================================== diff -u -r9f1e95f305bbc982a11f7a8917a95bf6caa18544 -r895e4cf56972494e9e363f0af9cd9e21d55e9004 --- Ringtoets/Integration/test/Ringtoets.Integration.Forms.Test/Views/AssessmentSectionCommentViewTest.cs (.../AssessmentSectionCommentViewTest.cs) (revision 9f1e95f305bbc982a11f7a8917a95bf6caa18544) +++ Ringtoets/Integration/test/Ringtoets.Integration.Forms.Test/Views/AssessmentSectionCommentViewTest.cs (.../AssessmentSectionCommentViewTest.cs) (revision 895e4cf56972494e9e363f0af9cd9e21d55e9004) @@ -19,9 +19,11 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using System; using System.Windows.Forms; using Core.Common.Controls.TextEditor; using Core.Common.Controls.Views; +using NUnit.Extensions.Forms; using NUnit.Framework; using Rhino.Mocks; using Ringtoets.Common.Data; @@ -79,26 +81,61 @@ } [Test] - [RequiresSTA] public void Data_AssessmentSectionContainsComment_CommentSetOnRichTextEditor() { // Setup - var expectedText = ""; var mocks = new MockRepository(); var view = new AssessmentSectionCommentView(); var data = mocks.Stub(); - data.Comments = GetValidRtfString(expectedText); + var expectedText = ""; + var validRtfString = GetValidRtfString(expectedText); + data.Comments = validRtfString; mocks.ReplayAll(); // Call view.Data = data; // Assert - var textBoxControl = view.Controls[0].Controls[0]; - Assert.AreEqual(expectedText, textBoxControl.Text); + var textBoxControl = view.Controls[0] as RichTextBoxControl; + Assert.IsNotNull(textBoxControl); + Assert.AreEqual(validRtfString, textBoxControl.Rtf); } + [Test] + public void RichTextEditorOnTextChanged_Always_SetsAssessmentSectionComments() + { + // Setup + using (var form = new Form()) + { + var expectedText = ""; + var validRtfString = GetValidRtfString(expectedText); + + var view = new AssessmentSectionCommentView(); + form.Controls.Add(view); + form.Show(); + + var richTextBoxControl = (RichTextBoxControl)new ControlTester("RichTextBoxControl").TheObject; + + var mocks = new MockRepository(); + var data = mocks.Stub(); + mocks.ReplayAll(); + + view.Data = data; + + // Precondition + Assert.AreEqual(GetValidRtfString(""), data.Comments); + + richTextBoxControl.Rtf = validRtfString; + + // Call + EventHelper.RaiseEvent(richTextBoxControl, "TextBoxValueChanged", EventArgs.Empty); + + // Assert + Assert.AreEqual(validRtfString, data.Comments); + } + } + private static string GetValidRtfString(string value) { RichTextBox richTextBox = new RichTextBox