Skip to content

Commit

Permalink
UITest: Extend MultipleNewSaveRestartOpenRecentChangeAskToSave
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Apr 28, 2024
1 parent dc352f8 commit 2b960e3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
66 changes: 49 additions & 17 deletions src/Samples.UITest/Writer.Test/Tests/WriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void NewZoomWritePrintPreviewExitWithoutSave()
Assert.False(startView.IsOffscreen);
startView.NewButton.Click();

Retry.WhileTrue(() => startView.IsOffscreen);
Retry.WhileTrue(() => startView.IsOffscreen, throwOnTimeout: true);
Assert.True(zoomInButton.IsEnabled);
zoomInButton.Click();
var zoomComboBox = window.ZoomComboBox;
Expand Down Expand Up @@ -78,66 +78,98 @@ public void NewZoomWritePrintPreviewExitWithoutSave()
}

[Fact]
public void NewSaveRestartOpenChangeAskToSave()
public void MultipleNewSaveRestartOpenRecentChangeAskToSave()
{
Launch();
var window = GetShellWindow();

var startView = window.StartView;
Assert.False(startView.IsOffscreen);

// Create new document, add text, check tab name with dirty flag indicator '*'
startView.NewButton.Click();

var tab1 = window.DocumentTabItems.Single();
tab1.RichTextView.RichTextBox.Text = "Hello World";
Assert.Equal("Document 1.rtf*", tab1.TabName);

// Create new document #2, add text, check tab name with dirty flag indicator '*'
var fileRibbonMenu = window.FileRibbonMenu;
fileRibbonMenu.MenuButton.Click();
fileRibbonMenu.NewMenuItem.Invoke();

var tab2 = window.DocumentTabItems[1];
Assert.Equal("Document 2.rtf", tab2.TabName);
tab2.CloseButton.Invoke();
tab2.RichTextView.RichTextBox.Text = "Hello World 2";
Assert.Equal("Document 2.rtf*", tab2.TabName);

Assert.True(tab1.IsSelected);
tab1.CloseButton.Invoke();
// Create new document #3, check tab name, close tab - no save dialog comes as text has not been changed
fileRibbonMenu.MenuButton.Click();
fileRibbonMenu.NewMenuItem.Invoke();
var tab3 = window.DocumentTab.SelectedTabItem.As<DocumentTabItem>();
Assert.Equal("Document 3.rtf", tab3.TabName);
tab3.CloseButton.Invoke();

// Select tab #1, close tab, save the document, define custom filename and save
Retry.WhileFalse(() => tab2.IsSelected, throwOnTimeout: true);
tab1.Select();
tab1.CloseButton.Invoke();
var saveChangesWindow = window.FirstModalWindow().As<SaveChangesWindow>();
var firstItem = saveChangesWindow.FilesToSaveList.Items.Single();
Assert.Equal("Document 1.rtf", firstItem.Text);
saveChangesWindow.YesButton.Click();

var saveFileDialog = window.FirstModalWindow().As<SaveFileDialog>();
var fileName = GetTempFileName("rtf");
saveFileDialog.SetFileName(fileName);
Capture.Screen().ToFile(GetScreenshotFile("FullSaveScreen.png"));
saveFileDialog.SaveButton.Click();
Capture.Screen().ToFile(GetScreenshotFile("AfterSaveFile.png"));

// Exit app, save the document, define custom filename and save
fileRibbonMenu.MenuButton.Click();
fileRibbonMenu.ExitMenuItem.Invoke();
saveChangesWindow = window.FirstModalWindow().As<SaveChangesWindow>();
firstItem = saveChangesWindow.FilesToSaveList.Items.Single();
Assert.Equal("Document 2.rtf", firstItem.Text);
saveChangesWindow.YesButton.Click();
saveFileDialog = window.FirstModalWindow().As<SaveFileDialog>();
var fileName2 = GetTempFileName("rtf");
saveFileDialog.SetFileName(fileName2);
saveFileDialog.SaveButton.Click();

// Restart the app

// Restart the app and check recent file list, pin second item -> moved to top
Launch(new LaunchArguments(DefaultSettings: false));
window = GetShellWindow();
startView = window.StartView;
Capture.Screen().ToFile(GetScreenshotFile("RestartScreen.png"));
var firstFileItem = startView.RecentFilesListItems.Single();
Assert.Equal(fileName, firstFileItem.ToolTip);
Assert.False(firstFileItem.PinButton.IsToggled);
firstFileItem.OpenFileButton.Click();
Assert.Equal(new[] { fileName2, fileName }, startView.RecentFileListItems.Select(x => x.ToolTip));
startView.RecentFileListItems[1].PinButton.Click();
Assert.Equal(new[] { fileName, fileName2 }, startView.RecentFileListItems.Select(x => x.ToolTip));
Assert.True(startView.RecentFileListItems[0].PinButton.IsToggled);

// Check that the recent file list within the ribbon menu has the same content
fileRibbonMenu = window.FileRibbonMenu;
fileRibbonMenu.MenuButton.Click();
Assert.Equal(new[] { fileName, fileName2 }, fileRibbonMenu.RecentFileListItems.Select(x => x.ToolTip));
Assert.True(fileRibbonMenu.RecentFileListItems[0].PinButton.IsToggled);
fileRibbonMenu.MenuButton.Toggle();

// Open recent file #1, modify text, check tab name with dirty flag indicator '*'
startView.RecentFileListItems[0].OpenFileButton.Invoke();
tab1 = window.DocumentTabItems.Single();
Assert.Equal(Path.GetFileName(fileName), tab1.TabName);
Assert.Equal("Hello World", tab1.RichTextView.RichTextBox.Text);
tab1.RichTextView.RichTextBox.Text = "- ";

Assert.Equal(Path.GetFileName(fileName) + "*", tab1.TabName);

// Open recent file #2
fileRibbonMenu.MenuButton.Click();
fileRibbonMenu.RecentFileListItems[^1].OpenFileButton.Click();
tab2 = window.DocumentTab.SelectedTabItem.As<DocumentTabItem>();
Assert.Equal(Path.GetFileName(fileName2), tab2.TabName);
Assert.Equal("Hello World 2", tab2.RichTextView.RichTextBox.Text);

// Close app, save dialog shows just file #1, don't save
fileRibbonMenu = window.FileRibbonMenu;
fileRibbonMenu.MenuButton.Click();
fileRibbonMenu.ExitMenuItem.Invoke();

saveChangesWindow = window.FirstModalWindow().As<SaveChangesWindow>();
firstItem = saveChangesWindow.FilesToSaveList.Items.Single();
Assert.Equal(fileName, firstItem.Text);
Expand Down
15 changes: 15 additions & 0 deletions src/Samples.UITest/Writer.Test/Views/ShellWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ public class FileRibbonMenu(FrameworkAutomationElementBase element) : Automation
public MenuItem PrintPreviewMenuItem => this.Find("PrintPreviewMenuItem").AsMenuItem();

public MenuItem ExitMenuItem => this.Find("ExitMenuItem").AsMenuItem();

public ListBox RecentFileList => this.Find("RecentFileList").AsListBox();

public RecentFileMenuItem[] RecentFileListItems => RecentFileList.Items.Select(x => x.As<RecentFileMenuItem>()).ToArray();
}

public class RecentFileMenuItem(FrameworkAutomationElementBase element) : ListBoxItem(element)
{
public string FileName => OpenFileButton.Name;

public string ToolTip => OpenFileButton.HelpText;

public Button OpenFileButton => this.Find("OpenItemButton").AsButton();

public ToggleButton PinButton => this.Find("PinToggleButton").AsToggleButton();
}

public class ViewTab(FrameworkAutomationElementBase element) : TabItem(element)
Expand Down
6 changes: 3 additions & 3 deletions src/Samples.UITest/Writer.Test/Views/StartView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ public class StartView(FrameworkAutomationElementBase element) : AutomationEleme
{
public Button NewButton => this.Find("NewButton").AsButton();

public ListBox RecentFilesList => this.Find("RecentFilesList").AsListBox();
public ListBox RecentFileList => this.Find("RecentFileList").AsListBox();

public RecentFilesListItem[] RecentFilesListItems => RecentFilesList.Items.Select(x => x.As<RecentFilesListItem>()).ToArray();
public RecentFileListItem[] RecentFileListItems => RecentFileList.Items.Select(x => x.As<RecentFileListItem>()).ToArray();
}

public class RecentFilesListItem(FrameworkAutomationElementBase element) : ListBoxItem(element)
public class RecentFileListItem(FrameworkAutomationElementBase element) : ListBoxItem(element)
{
public string FileName => Label.Name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</Button>

<ItemsControl ItemsSource="{Binding FileService.RecentFileList.RecentFiles}" Focusable="False" Grid.Column="1" Grid.Row="1" Grid.RowSpan="3" Margin="100,22,22,22"
AutomationProperties.AutomationId="RecentFilesList">
AutomationProperties.AutomationId="RecentFileList">
<ItemsControl.ItemTemplate>
<DataTemplate>
<DockPanel Margin="0,0,0,15">
Expand Down

0 comments on commit 2b960e3

Please sign in to comment.