Transcript Document

Module 5 Testing, Unit Testing, and Debugging

Module Overview • WPF Testing Strategies • Debugging XAML • Providing User Feedback for Unhandled Exceptions • Understanding Security Features

Lesson 1: WPF Testing Strategies • Understanding How to Test Windows Client Applications • Strategies for Testing Windows Client Applications • Testing by Using Unit Tests in Visual Studio • Understanding UI Automation • Testing by Using UI Automation

Understanding How to Test Windows Client Applications Why test?

• Reduce the cost of development • Ensure that your application behaves predictably • Reduce the total cost of ownership Types of Testing: • Unit testing • Integration testing • Regression testing

Strategies for Testing Windows Client Applications Unit testing • Good for testing business logic • Make unit testing easier by using design patterns: • MVP • MVVM • IoC • DI UI Automation • Good for interactive testing • Simulates using input devices, such as: • Keyboard • Mouse

Testing by Using Unit Tests in Visual Studio Different types of tests: • Unit test • Coded UI test • Database unit test using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] public class CustomerUnitTests { [TestMethod] public void MyUnitTest() { // Add your test logic here.

} }

Understanding UI Automation Control patterns: • ExpandCollapse control pattern • Invoke control pattern • Toggle control pattern • Grid control pattern • RangeValue control pattern • Window control pattern • More… UI Automation core concepts: • Providers • Clients

Testing by Using UI Automation Steps to invoke a control:

1

Create an AutomationElement instance

2

Obtain the pattern object

3

Use the methods that the pattern provides // Create an Automation instance from Button1.

AutomationElement button = AutomationElement.RootElement.FindFirst( TreeScope.Descendants, new PropertyCondition( AutomationElement.AutomationIdProperty, "Button1")); // Obtain the pattern object.

InvokePattern pattern = (InvokePattern) button.GetCurrentPattern(InvokePattern.Pattern); // Click the Button by using the pattern.

pattern.Invoke();

Lesson 2: Debugging XAML • Understanding Debugging in Visual Studio • Demonstration: Debugging XAML by Using Visual Studio • Debugging by Using Tracing

Understanding Debugging in Visual Studio Visual Studio debugging capabilities: • Setting break points in your code • Inspecting memory and register values • Viewing and changing variables values • Observing message traffic New Visual Studio debugging features: • WPF Tree Visualizer • Tracing by using

PresentationTraceSources

Demonstration: Debugging XAML by Using Visual Studio In this demonstration, you will see how to: • Open the WPF Tree Visualizer • Explore the visual tree • Search for properties on an element

Debugging by Using Tracing PresentationTraceSources.TraceLevel can be applied to: • Classes that are derived from BindingBaseBindingExpressionBase and its derived classes • Classes that are derived from DataSourceProvider Different trace levels: •

None

Low

Medium

High

Lesson 3: Providing User Feedback for Unhandled Exceptions • Understanding Unhandled Exceptions • Processing Unhandled Exceptions

Understanding Unhandled Exceptions What are unhandled exceptions?

• Exceptions are a standard mechanism for reporting errors • An exception that you do not handle in your code is an unhandled exception

Only catch exceptions you can handle and recover from

Why process unhandled exceptions?

• Error logging and reporting • Improve user experience by presenting errors to your users consistently

Processing Unhandled Exceptions Adding a handler for unhandled exceptions for WPF applications this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler( OnDispatcherUnhandledException); Adding a handler for unhandled exceptions for non-WPF applications AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler( OnUnhandledException);

Lesson 4: Understanding Security Features • Understanding Security in Windows Client Applications • Understanding Code Access Security • Understanding Full Trust and Partial Trust in WPF • Understanding User Access Controls

Understanding Security in Windows Client Applications Available security technologies and aspects: • SRP • CAS • Partial trust and full trust in WPF • UAC In the .NET Framework version 4, the CLR is moving away from providing security policy for computers Microsoft is recommending the use of Windows Software Restriction Policies as a replacement for CLR security policy

Understanding Code Access Security Code Access Security: • Protects you from malicious code • Enables code to be trusted to varying degrees • Enforces varying levels of trust • Reduces the likelihood that malicious code can misuse your code Major changes have been made to CAS in the .NET Framework version 4 The most notable change has been security transparency, but there are also other significant changes that affect CAS

Understanding Full Trust and Partial Trust in WPF Available in partial trust: Not available in partial trust: • Browser window • Site-of-origin access • Isolated storage (512 KB limit) • UI Automation providers • Commanding • IMEs • Tablet stylus and ink • Simulated drag and drop • Many more… • Window (application-defined windows and dialog boxes) •

SaveFileDialog

• File system • Registry access • Drag-and-drop • XAML serialization (via XamlWriter.Save) • UIAutomation clients • Many more…

Understanding User Access Controls User Account Control has the following benefits: • It reduces the number of programs that run with elevated privileges • It enables you to be productive when you run as a Standard user by removing unnecessary restrictions • It gives you the ability to ask an administrator to give you permission to perform an administrative task in their current session

Lab: Testing and Debugging WPF Applications • Exercise 1: Unit Testing Strategy • Exercise 2: Unit Testing in WPF Applications • Exercise 3: Debugging Applications in Visual Studio 2010 • Exercise 4: Advanced Exception Handling Logon information Virtual machine User name Password 10262A-GEN-DEV Student Pa$$w0rd

Estimated time: 75 minutes

Lab Scenario You have been asked to create unit tests for the UI code that you have written so far. First, you must decide on the best approach to use to test each class, and then you must write the necessary code.

During testing, you discover that you have a couple of hard to-find bugs, and you must use the tooling support that Visual Studio 2010 provides to track down and resolve these bugs.

Lab Review Review Questions • What is the most appropriate strategy for testing UI elements?

• What is the name of the new debugging visualizer in Visual Studio 2010 for inspecting WPF elements?

• What is the name of the event on the Application class for processing unhandled exceptions?

Module Review and Takeaways • Review Questions • Best Practices • Tools