Module 5 Testing, Unit Testing, and Debugging

24
Module 5 Testing, Unit Testing, and Debugging

description

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 - PowerPoint PPT Presentation

Transcript of Module 5 Testing, Unit Testing, and Debugging

Page 1: Module 5 Testing, Unit Testing,  and Debugging

Module 5

Testing, Unit Testing, and Debugging

Page 2: Module 5 Testing, Unit Testing,  and Debugging

Module Overview

• WPF Testing Strategies

• Debugging XAML

• Providing User Feedback for Unhandled Exceptions

• Understanding Security Features

Page 3: Module 5 Testing, Unit Testing,  and Debugging

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

Page 4: Module 5 Testing, Unit Testing,  and Debugging

Understanding How to Test Windows Client Applications

Why test?Why test?

• Reduce the cost of development

• Ensure that your application behaves

predictably

• Reduce the total cost of ownership

Types of Testing:Types of Testing:

• Unit testing

• Integration testing

• Regression testing

• Unit testing

• Integration testing

• Regression testing

Page 5: Module 5 Testing, Unit Testing,  and Debugging

Strategies for Testing Windows Client Applications

Unit testingUnit testing

• Good for testing business logic

• Make unit testing easier by using design patterns:

• MVP

• MVVM

• IoC

• DI

UI AutomationUI Automation

• Good for interactive testing

• Simulates using input devices, such as:

• Keyboard

• Mouse

Page 6: Module 5 Testing, Unit Testing,  and Debugging

Testing by Using Unit Tests in Visual Studio

Different types of tests: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. }}

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]public class CustomerUnitTests{ [TestMethod] public void MyUnitTest() { // Add your test logic here. }}

Page 7: Module 5 Testing, Unit Testing,  and Debugging

Understanding UI Automation

Control patterns: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:UI Automation core concepts:

• Providers

• Clients

Page 8: Module 5 Testing, Unit Testing,  and Debugging

// 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();

// 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();

Testing by Using UI Automation

Steps to invoke a control:Steps to invoke a control:

Create an AutomationElement instanceCreate an AutomationElement instance11

Obtain the pattern object Obtain the pattern object 22

Use the methods that the pattern providesUse the methods that the pattern provides33

Page 9: Module 5 Testing, Unit Testing,  and Debugging

Lesson 2: Debugging XAML

• Understanding Debugging in Visual Studio

• Demonstration: Debugging XAML by Using Visual Studio

• Debugging by Using Tracing

Page 10: Module 5 Testing, Unit Testing,  and Debugging

Understanding Debugging in Visual Studio

Visual Studio debugging capabilities: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:New Visual Studio debugging features:

• WPF Tree Visualizer

• Tracing by using

PresentationTraceSources

Page 11: Module 5 Testing, Unit Testing,  and Debugging

Demonstration: Debugging XAML by UsingVisual Studio

In this demonstration, you will see how to:

•Open the WPF Tree Visualizer

•Explore the visual tree

•Search for properties on an element

Page 12: Module 5 Testing, Unit Testing,  and Debugging

Debugging by Using Tracing

PresentationTraceSources.TraceLevel can be

applied to:

PresentationTraceSources.TraceLevel can be

applied to:

• Classes that are derived from BindingBase

• BindingExpressionBase and its derived classes

• Classes that are derived from DataSourceProvider

Different trace levels:Different trace levels:

• None

• Low

• Medium

• High

Page 13: Module 5 Testing, Unit Testing,  and Debugging

Lesson 3: Providing User Feedback forUnhandled Exceptions

• Understanding Unhandled Exceptions

• Processing Unhandled Exceptions

Page 14: Module 5 Testing, Unit Testing,  and Debugging

Understanding Unhandled Exceptions

What are 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 exceptionOnly catch exceptions you can handle and recover from

Only catch exceptions you can handle and recover from

Why process unhandled exceptions?Why process unhandled exceptions?

• Error logging and reporting

• Improve user experience by presenting

errors to your users consistently

Page 15: Module 5 Testing, Unit Testing,  and Debugging

Processing Unhandled Exceptions

Adding a handler for unhandled exceptions for WPF applicationsAdding a handler for unhandled exceptions for WPF applications

this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler( OnDispatcherUnhandledException);

this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler( OnDispatcherUnhandledException);

Adding a handler for unhandled exceptions for non-WPF applicationsAdding a handler for unhandled exceptions for non-WPF applications

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler( OnUnhandledException);

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler( OnUnhandledException);

Page 16: Module 5 Testing, Unit Testing,  and Debugging

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

Page 17: Module 5 Testing, Unit Testing,  and Debugging

Understanding Security in Windows Client Applications

Available security technologies and aspects: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

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

Page 18: Module 5 Testing, Unit Testing,  and Debugging

Understanding Code Access Security

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

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

Page 19: Module 5 Testing, Unit Testing,  and Debugging

Understanding Full Trust and Partial Trust in WPF

Available in partial trust: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…

Not available in partial trust:Not available in partial trust:

• Window (application-defined

windows and dialog boxes)

• SaveFileDialog

• File system

• Registry access

• Drag-and-drop

• XAML serialization (via

XamlWriter.Save)

• UIAutomation clients

• Many more…

Page 20: Module 5 Testing, Unit Testing,  and Debugging

Understanding User Access Controls

User Account Control has the following benefits: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

Page 21: Module 5 Testing, Unit Testing,  and Debugging

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

Estimated time: 75 minutes

Page 22: Module 5 Testing, Unit Testing,  and Debugging

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.

Page 23: Module 5 Testing, Unit Testing,  and Debugging

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?

Page 24: Module 5 Testing, Unit Testing,  and Debugging

Module Review and Takeaways

• Review Questions

• Best Practices

• Tools