📄 nunitform.cs
字号:
private void fileMenu_Popup(object sender, System.EventArgs e)
{
newMenuItem.Enabled = !IsTestRunning;
openMenuItem.Enabled = !IsTestRunning;
closeMenuItem.Enabled = IsProjectLoaded && !IsTestRunning;
saveMenuItem.Enabled = IsProjectLoaded;
saveAsMenuItem.Enabled = IsProjectLoaded;
reloadMenuItem.Enabled = IsTestLoaded && !IsTestRunning;
recentProjectsMenu.Enabled = !IsTestRunning;
if ( !IsTestRunning )
recentProjectsMenuHandler.Load();
}
private void newMenuItem_Click(object sender, System.EventArgs e)
{
if ( IsProjectLoaded )
TestLoaderUI.CloseProject();
TestLoaderUI.NewProject();
}
private void openMenuItem_Click(object sender, System.EventArgs e)
{
TestLoaderUI.OpenProject();
}
private void closeMenuItem_Click(object sender, System.EventArgs e)
{
TestLoaderUI.CloseProject();
}
private void saveMenuItem_Click(object sender, System.EventArgs e)
{
TestLoaderUI.SaveProject();
}
private void saveAsMenuItem_Click(object sender, System.EventArgs e)
{
TestLoaderUI.SaveProjectAs();
}
private void reloadMenuItem_Click(object sender, System.EventArgs e)
{
using ( new CP.Windows.Forms.WaitCursor() )
{
TestLoader.ReloadTest();
}
}
private void exitMenuItem_Click(object sender, System.EventArgs e)
{
this.Close();
}
#endregion
#region Project Menu Handlers
/// <summary>
/// When the project menu pops up, we populate the
/// submenu for configurations dynamically.
/// </summary>
private void projectMenu_Popup(object sender, System.EventArgs e)
{
int index = 0;
configMenuItem.MenuItems.Clear();
foreach ( ProjectConfig config in TestProject.Configs )
{
string text = string.Format( "&{0} {1}", index+1, config.Name );
MenuItem item = new MenuItem(
text, new EventHandler( configMenuItem_Click ) );
if ( config.Name == TestProject.ActiveConfigName )
item.Checked = true;
configMenuItem.MenuItems.Add( index++, item );
}
configMenuItem.MenuItems.Add( "-" );
configMenuItem.MenuItems.Add( "&Add...",
new System.EventHandler( addConfigurationMenuItem_Click ) );
configMenuItem.MenuItems.Add( "&Edit...",
new System.EventHandler( editConfigurationsMenuItem_Click ) );
addVSProjectMenuItem.Visible = UserSettings.Options.VisualStudioSupport;
}
private void configMenuItem_Click( object sender, EventArgs e )
{
MenuItem item = (MenuItem)sender;
if ( !item.Checked )
TestProject.SetActiveConfig( TestProject.Configs[item.Index].Name );
}
private void addConfigurationMenuItem_Click( object sender, System.EventArgs e )
{
ConfigurationEditor.AddConfiguration( TestProject );
}
private void editConfigurationsMenuItem_Click( object sender, System.EventArgs e )
{
ConfigurationEditor.Edit( TestProject );
}
private void addAssemblyMenuItem_Click(object sender, System.EventArgs e)
{
TestLoaderUI.AddAssembly();
}
private void addVSProjectMenuItem_Click(object sender, System.EventArgs e)
{
TestLoaderUI.AddVSProject();
}
private void editProjectMenuItem_Click(object sender, System.EventArgs e)
{
ProjectEditor editor = new ProjectEditor( TestProject );
editor.ShowDialog( this );
}
#endregion
#region Tools Menu Handlers
private void toolsMenu_Popup(object sender, System.EventArgs e)
{
saveXmlResultsMenuItem.Enabled = IsTestLoaded && TestLoader.Results != null;
exceptionDetailsMenuItem.Enabled = TestLoader.LastException != null;
}
private void saveXmlResultsMenuItem_Click(object sender, System.EventArgs e)
{
TestLoaderUI.SaveLastResult();
}
private void exceptionDetailsMenuItem_Click(object sender, System.EventArgs e)
{
ExceptionDetailsForm details = new ExceptionDetailsForm( TestLoader.LastException );
details.ShowDialog();
}
private void optionsMenuItem_Click(object sender, System.EventArgs e)
{
OptionsDialog.EditOptions();
}
#endregion
#region Help Menu Handlers
/// <summary>
/// Display the about box when menu item is selected
/// </summary>
private void aboutMenuItem_Click(object sender, System.EventArgs e)
{
AboutBox aboutBox = new AboutBox();
aboutBox.ShowDialog();
}
#endregion
#region Form Level Events
/// <summary>
/// Exit application when space key is tapped
/// </summary>
protected override bool ProcessKeyPreview(ref
System.Windows.Forms.Message m)
{
const int SPACE_BAR=32;
const int WM_CHAR = 258;
if (m.Msg == WM_CHAR && m.WParam.ToInt32() == SPACE_BAR )
{
int altKeyBit = (int)m.LParam & 0x10000000;
if ( altKeyBit == 0 )
{
this.Close();
return true;
}
}
return base.ProcessKeyEventArgs( ref m );
}
/// <summary>
/// Form is about to close, first see if we
/// have a test run going on and if so whether
/// we should cancel it. Then unload the
/// test and save the latest form position.
/// </summary>
private void NUnitForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if ( IsTestRunning )
{
DialogResult dialogResult = UserMessage.Ask(
"A test is running, do you want to stop the test and exit?" );
if ( dialogResult == DialogResult.No )
e.Cancel = true;
else
TestLoader.CancelTestRun();
}
if ( !e.Cancel && IsProjectLoaded &&
TestLoaderUI.CloseProject() == DialogResult.Cancel )
e.Cancel = true;
}
/// <summary>
/// Get saved options when form loads
/// </summary>
private void NUnitForm_Load(object sender, System.EventArgs e)
{
LoadFormSettings();
SubscribeToTestEvents();
InitializeControls();
// Load test specified on command line or
// the most recent one if options call for it
if ( commandLineOptions.testFileName != null )
TestLoaderUI.OpenProject( commandLineOptions.testFileName, commandLineOptions.configName, commandLineOptions.testName );
else if( UserSettings.Options.LoadLastProject && !commandLineOptions.noload )
{
string recentProjectName = UserSettings.RecentProjects.RecentFile;
if ( recentProjectName != null )
TestLoaderUI.OpenProject( recentProjectName, commandLineOptions.configName, commandLineOptions.testName );
}
// Run loaded test automatically if called for
if ( commandLineOptions.autorun && TestLoader.IsTestLoaded )
{
// TODO: Temporary fix to avoid problem when /run is used
// with ReloadOnRun turned on. Refactor TestLoader so
// we can just do a run without reload.
bool reload = TestLoader.ReloadOnRun;
try
{
TestLoader.ReloadOnRun = false;
TestLoader.RunLoadedTest();
}
finally
{
TestLoader.ReloadOnRun = reload;
}
}
}
private void LoadFormSettings()
{
// Set position of the form
this.Location = UserSettings.Form.Location;
this.Size = UserSettings.Form.Size;
// Maximize window if that was it's last state
if ( UserSettings.Form.IsMaximized )
this.WindowState = FormWindowState.Maximized;
// Handle changes to form position
this.Move += new System.EventHandler(this.NUnitForm_Move);
this.Resize += new System.EventHandler(this.NUnitForm_Resize);
// Set the splitter positions
this.splitter1.SplitPosition = UserSettings.Form.TreeSplitterPosition;
this.splitter3.SplitPosition = UserSettings.Form.TabSplitterPosition;
// Handle changes in splitter positions
this.splitter1.SplitterMoved += new SplitterEventHandler( splitter1_SplitterMoved );
this.splitter3.SplitterMoved += new SplitterEventHandler( splitter3_SplitterMoved );
}
private void SubscribeToTestEvents()
{
IProjectEvents events = TestLoader.Events;
events.RunStarting += new TestEventHandler( OnRunStarting );
events.RunFinished += new TestEventHandler( OnRunFinished );
events.ProjectLoaded += new TestProjectEventHandler( OnTestProjectLoaded );
events.ProjectLoadFailed+= new TestProjectEventHandler( OnProjectLoadFailure );
events.ProjectUnloaded += new TestProjectEventHandler( OnTestProjectUnloaded );
events.TestLoading += new TestEventHandler( OnTestLoadStarting );
events.TestLoaded += new TestEventHandler( OnTestLoaded );
events.TestLoadFailed += new TestEventHandler( OnTestLoadFailure );
events.TestUnloading += new TestEventHandler( OnTestUnloadStarting );
events.TestUnloaded += new TestEventHandler( OnTestUnloaded );
events.TestReloading += new TestEventHandler( OnReloadStarting );
events.TestReloaded += new TestEventHandler( OnTestChanged );
events.TestReloadFailed += new TestEventHandler( OnTestLoadFailure );
events.TestFinished += new TestEventHandler( OnTestFinished );
events.SuiteFinished += new TestEventHandler( OnSuiteFinished );
events.TestException += new TestEventHandler( OnTestException );
}
private void InitializeControls()
{
// ToDo: Migrate more ui elements to handle events on their own.
this.testTree.Initialize(TestLoader);
this.progressBar.Initialize( TestLoader.Events );
this.statusBar.Initialize( TestLoader.Events );
// Set controls to match option settings. We do this
// here rather than in the controls since there may
// be more than one app that uses the same controls.
testTree.tests.ClearResultsOnChange =
UserSettings.Options.ClearResults;
}
private void NUnitForm_Move(object sender, System.EventArgs e)
{
if ( this.WindowState == FormWindowState.Normal )
{
UserSettings.Form.Location = this.Location;
UserSettings.Form.IsMaximized = false;
this.statusBar.SizingGrip = true;
}
else if ( this.WindowState == FormWindowState.Maximized )
{
UserSettings.Form.IsMaximized = true;
this.statusBar.SizingGrip = false;
}
}
private void NUnitForm_Resize(object sender, System.EventArgs e)
{
if ( this.WindowState == FormWindowState.Normal )
UserSettings.Form.Size = this.Size;
}
private void splitter1_SplitterMoved( object sender, SplitterEventArgs e )
{
UserSettings.Form.TreeSplitterPosition = splitter1.SplitPosition;
}
private void splitter3_SplitterMoved( object sender, SplitterEventArgs e )
{
UserSettings.Form.TabSplitterPosition = splitter3.SplitPosition;
}
#endregion
#region Other UI Event Handlers
/// <summary>
/// When the Run Button is clicked, run the selected test.
/// </summary>
private void runButton_Click(object sender, System.EventArgs e)
{
// if ( testTree.tests.SelectedCategories == null )
// AppUI.TestLoader.SetFilter( null );
// else
// AppUI.TestLoader.SetFilter( new CategoryFilter( testTree.tests.SelectedCategories, testTree.tests.ExcludeSelectedCategories ) );
//
// TestLoader.RunTests( testTree.tests.SelectedTests );
testTree.RunTests();
}
/// <summary>
/// When the Stop Button is clicked, cancel running test
/// </summary>
private void stopButton_Click(object sender, System.EventArgs e)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -