Guest User

Untitled

a guest
Jan 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. public interface IOService
  2. {
  3. string OpenFileDialog(string defaultPath);
  4.  
  5. //Other similar untestable IO operations
  6. Stream OpenFile(string path);
  7. }
  8.  
  9. public MyViewModel : ViewModel
  10. {
  11. private string _selectedPath;
  12. public string SelectedPath
  13. {
  14. get { return _selectedPath; }
  15. set { _selectedPath = value; OnPropertyChanged("SelectedPath"); }
  16. }
  17.  
  18. private RelayCommand _openCommand;
  19. public RelayCommand OpenCommand
  20. {
  21. //You know the drill.
  22. ...
  23. }
  24.  
  25. private IOService _ioService;
  26. public MyViewModel(IOService ioService)
  27. {
  28. _ioService = ioService;
  29. OpenCommand = new RelayCommand(OpenFile);
  30. }
  31.  
  32. private void OpenFile()
  33. {
  34. SelectedPath = _ioService.OpenFileDialog(@"c:WhereMyFileUsuallyIs.txt");
  35. if(SelectedPath == null)
  36. {
  37. SelectedPath = string.Empty;
  38. }
  39. }
  40. }
  41.  
  42. [Test]
  43. public void OpenFileCommand_UserSelectsInvalidPath_SelectedPathSetToEmpty()
  44. {
  45. Mock<IOService> ioServiceStub = new Mock<IOService>();
  46.  
  47. //We use null to indicate invalid path in our implementation
  48. ioServiceStub.Setup(ioServ => ioServ.OpenFileDialog(It.IsAny<string>()))
  49. .Returns(null);
  50.  
  51. //Setup target and test
  52. MyViewModel target = new MyViewModel(ioServiceStub.Object);
  53. target.OpenCommand.Execute();
  54.  
  55. Assert.IsEqual(string.Empty, target.SelectedPath);
  56. }
  57.  
  58. [Test]
  59. [Isolated]
  60. public void OpenFileCommand_UserSelectsInvalidPath_SelectedPathSetToEmpty()
  61. {
  62. IOService ioServiceStub = Isolate.Fake.Instance<IOService>();
  63.  
  64. //Setup stub arrangements
  65. Isolate.WhenCalled(() => ioServiceStub.OpenFileDialog("blah"))
  66. .WasCalledWithAnyArguments()
  67. .WillReturn(null);
  68.  
  69. //Setup target and test
  70. MyViewModel target = new MyViewModel(ioServiceStub);
  71. target.OpenCommand.Execute();
  72.  
  73. Assert.IsEqual(string.Empty, target.SelectedPath);
  74. }
  75.  
  76. public class OpenFileDialogVM : ViewModelBase
  77. {
  78. public static RelayCommand OpenCommand { get; set; }
  79. private string _selectedPath;
  80. public string SelectedPath
  81. {
  82. get { return _selectedPath; }
  83. set
  84. {
  85. _selectedPath = value;
  86. RaisePropertyChanged("SelectedPath");
  87. }
  88. }
  89.  
  90. private string _defaultPath;
  91.  
  92. public OpenFileDialogVM()
  93. {
  94. RegisterCommands();
  95. }
  96.  
  97. public OpenFileDialogVM(string defaultPath)
  98. {
  99. _defaultPath = defaultPath;
  100. RegisterCommands();
  101. }
  102.  
  103. private void RegisterCommands()
  104. {
  105. OpenCommand = new RelayCommand(ExecuteOpenFileDialog);
  106. }
  107.  
  108. private void ExecuteOpenFileDialog()
  109. {
  110. var dialog = new OpenFileDialog { InitialDirectory = _defaultPath };
  111. dialog.ShowDialog();
  112.  
  113. SelectedPath = dialog.FileName;
  114. }
  115. }
  116.  
  117. <TextBox Text="{Binding SelectedPath}" />
  118. <Button Command="vm:OpenFileDialogVM.OpenCommand" >Browse</Button>
  119.  
  120. DataContext = new OpenFileDialogVM();
  121. InitializeComponent();
  122.  
  123. <Grid>
  124.  
  125. <Grid.ColumnDefinitions>
  126. <ColumnDefinition Width="*"/>
  127. <ColumnDefinition Width="Auto"/>
  128. </Grid.ColumnDefinitions>
  129.  
  130. <TextBox Grid.Column="0" Text="{Binding Text, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
  131. <Button Grid.Column="1"
  132. Click="Button_Click">
  133. <Button.Template>
  134. <ControlTemplate>
  135. <Image Grid.Column="1" Source="../Images/carpeta.png"/>
  136. </ControlTemplate>
  137. </Button.Template>
  138. </Button>
  139.  
  140. </Grid>
  141.  
  142. public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
  143. "Text",
  144. typeof(string),
  145. typeof(customFilePicker),
  146. new FrameworkPropertyMetadata(
  147. null,
  148. FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal));
  149.  
  150. public string Text
  151. {
  152. get
  153. {
  154. return this.GetValue(TextProperty) as String;
  155. }
  156. set
  157. {
  158. this.SetValue(TextProperty, value);
  159. }
  160. }
  161.  
  162. public FilePicker()
  163. {
  164. InitializeComponent();
  165. }
  166.  
  167. private void Button_Click(object sender, RoutedEventArgs e)
  168. {
  169. OpenFileDialog openFileDialog = new OpenFileDialog();
  170.  
  171. if(openFileDialog.ShowDialog() == true)
  172. {
  173. this.Text = openFileDialog.FileName;
  174. }
  175. }
  176.  
  177. <controls:customFilePicker Text="{Binding Text}"}/>
Add Comment
Please, Sign In to add comment