/V4/MVVM RI/MVVM.Client.Tests/ViewModels/OpenQuestionViewModelFixture.cs

# · C# · 108 lines · 74 code · 17 blank · 17 comment · 0 complexity · b079ce702a241715d565e1d1e85834cf MD5 · raw file

  1. //===================================================================================
  2. // Microsoft patterns & practices
  3. // Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4. //===================================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===================================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===================================================================================
  17. using System;
  18. using System.Linq;
  19. using Microsoft.VisualStudio.TestTools.UnitTesting;
  20. using MVVM.Client.ViewModels;
  21. using MVVM.Questionnaires.Model;
  22. using MVVM.TestSupport;
  23. namespace MVVM.Client.Tests.ViewModels
  24. {
  25. [TestClass]
  26. public class OpenQuestionViewModelFixture
  27. {
  28. [TestMethod]
  29. public void WhenCreatingAViewModelWithANullQuestion_ThenAnExceptionIsThrown()
  30. {
  31. try
  32. {
  33. new OpenQuestionViewModel(null);
  34. Assert.Fail("should have thrown");
  35. }
  36. catch (ArgumentNullException)
  37. {
  38. // expected
  39. }
  40. }
  41. [TestMethod]
  42. public void ViewModelHasMaxLengthAsTheAvailableLength()
  43. {
  44. var question = new OpenQuestionTemplate { QuestionText = "Question", MaxLength = 25 }.CreateNewQuestion() as OpenQuestion;
  45. var viewModel = new OpenQuestionViewModel(question);
  46. Assert.AreEqual(25, viewModel.AvailableLength);
  47. }
  48. [TestMethod]
  49. public void WhenSettingTheResponseTextPropertyOnTheModel_ThenAvailableLengthIsUpdatedOnTheViewModel()
  50. {
  51. var question = new OpenQuestionTemplate { QuestionText = "Question", MaxLength = 25 }.CreateNewQuestion() as OpenQuestion;
  52. var viewModel = new OpenQuestionViewModel(question);
  53. question.Response = "1234567890";
  54. Assert.AreEqual(15, viewModel.AvailableLength);
  55. }
  56. [TestMethod]
  57. public void WhenSettingTheResponseTextPropertyOnTheModel_ThenAChangeOnAvailableLengthIsNotifiedOnTheViewModel()
  58. {
  59. var question = new OpenQuestionTemplate { QuestionText = "Question", MaxLength = 25 }.CreateNewQuestion() as OpenQuestion;
  60. var viewModel = new OpenQuestionViewModel(question);
  61. var changeTracker = new PropertyChangeTracker(viewModel);
  62. question.Response = "1234567890";
  63. Assert.IsTrue(changeTracker.ChangedProperties.Contains("AvailableLength"));
  64. }
  65. [TestMethod]
  66. public void WhenSettingTheResponseTextPropertyOnTheModel_ThenTheViewModelNotifiesAResponseChange()
  67. {
  68. var question = new OpenQuestionTemplate { QuestionText = "Question", MaxLength = 25 }.CreateNewQuestion() as OpenQuestion;
  69. var viewModel = new OpenQuestionViewModel(question);
  70. bool responseChanged = false;
  71. viewModel.ResponseChanged += (s, e) => responseChanged = true;
  72. question.Response = "1234567890";
  73. Assert.IsTrue(responseChanged);
  74. }
  75. [TestMethod]
  76. public void WhenCreatingTheViewModel_ThenHasChangesIsFalse()
  77. {
  78. var question = new OpenQuestionTemplate { QuestionText = "Question", MaxLength = 25 }.CreateNewQuestion() as OpenQuestion;
  79. var viewModel = new OpenQuestionViewModel(question);
  80. Assert.IsFalse(viewModel.HasChanges);
  81. }
  82. [TestMethod]
  83. public void WhenSettingTheResponseOntheModel_ThenViewModelHasChanges()
  84. {
  85. var question = new OpenQuestionTemplate { QuestionText = "Question", MaxLength = 25 }.CreateNewQuestion() as OpenQuestion;
  86. var viewModel = new OpenQuestionViewModel(question);
  87. question.Response = "1234567890";
  88. Assert.IsTrue(viewModel.HasChanges);
  89. }
  90. }
  91. }