title: Why is Form.CanFocus false in an unit test only when debugging the test?
body: <p>In an unit test, a <code>Form</code>'s property <code>CanFocus</code> has the value <code>false</code> when debugging the test, and <code>true</code> when running the test.</p>
  
  <p>The code of the test simplified is below:</p>
  
  <pre><code>// Arrange
  Form f = new Form();
  
  // Act
  f.Show();
  Application.DoEvents();
  
  // Assert
  // The following LOC:
  // Debugging the test? Fails. Running the test? Passes.
  Assert.IsTrue(f.CanFocus);
  </code></pre>
  
  <p>I also tried putting these lines directly above <code>f.Show()</code>:</p>
  
  <pre><code>f.Shown += delegate (object sender, EventArgs e)
  {
      f.Activate();
  };
  </code></pre>
  
  <p>From what I've heard, direct UI methods should not be checked, but I am still curious about the situation above.</p>
  
  <p><strong>Update:</strong> I checked and at the moment of the assertion in debug mode, <code>f</code> has the properties <code>IsHandleCreated</code>, <code>Visible</code> and <code>Enabled</code> set to <code>true</code>. I also see that when debugging the test, the window is not actually visible.</p>
  
  <p>The situation is that I am doing an unit test for a method that should focus a control. The method uses the control's own property <code>HasFocus</code> (sets it to <code>true</code>) only when the control is created (<code>!= null</code>) and its property <code>IsDisposed</code> is set to <code>false</code>. I wish to know, in the best case, in what should the unit test for this method consist of.</p>