Test and profile your Windows Phone 8 App

33
WPReborn 2013 Michele Capra Software Engineer @ OrangeCode ___________________________________________________ Test and profile your application @dnlombardia #wpreborn

description

In this presentation i will give you a deep overview about Windows Phone 8 testing tool and how to profile and monitor your application performance.

Transcript of Test and profile your Windows Phone 8 App

Page 1: Test and profile your Windows Phone 8 App

WPReborn 2013

Michele CapraSoftware Engineer @ OrangeCode

___________________________________________________

Test and profile your application

@dnlombardia#wpreborn

Page 2: Test and profile your Windows Phone 8 App

What we’re going to see:

• Testing in WP7

• Testing tools for WP8

• Profiling tools

Page 3: Test and profile your Windows Phone 8 App

Who am I• Freelance Developer

• C#, Asp.net mvc, WPF , Windows Phone 7 – 8

• Python, Django

• Blog: orangecode.it/blog

Page 4: Test and profile your Windows Phone 8 App

Where we were

Unit / Integration test : - Microsoft Test - Nunit for Windows Phone 7

Mocking framework:- Moq 3.1 for Silverlight

Page 5: Test and profile your Windows Phone 8 App

Test runner for Windows Phone 7

• Nunit runner

• Resharper

• Test Driven.net

Page 6: Test and profile your Windows Phone 8 App

Solution structure in Windows Phone 7WP7 App

WP7 Class Library

TestSuite

View and ViewModel

Page 7: Test and profile your Windows Phone 8 App

Testing in Windows Phone 8

- Windows Phone 8 new feature

- Asyncronous programming style

- Need of an emulator to run the code- No test runner can be used directly in Visual Studio

Page 8: Test and profile your Windows Phone 8 App

Windows Phone Toolkit Test Framework

• Part of the Windows Phone Toolkit

• Provides an easy way to integrate unit tests for your Windows Phone apps

Page 9: Test and profile your Windows Phone 8 App

Windows Phone Toolkit Test Framework

• Test suites are no longer Class Libraries but Applications

• No more automated run

• Can manage asycnronous code

Page 10: Test and profile your Windows Phone 8 App

How to setup WP8 testing tools• Reference via nuget: Windows Phone Toolkit

Test Framework• On your MainPage costructor: public partial class MainPage : PhoneApplicationPage { public MainPage() { InitializeComponent(); Content = UnitTestSystem.CreateTestPage(); } }

Page 11: Test and profile your Windows Phone 8 App

How to setup WP8 testing tools

Page 12: Test and profile your Windows Phone 8 App

Test example scenario

• App loads products informations from db and shows it

• SQLite as db engine

• ModelView-View-Model pattern

Page 13: Test and profile your Windows Phone 8 App

Solution structure

Page 14: Test and profile your Windows Phone 8 App

First test [TestClass] public class MainViewModelFixture { private MainViewModel _viewModel; private Mock<IDbService> _service; public MainViewModelFixture() { _service= new Mock<IDbService>(); _viewModel= new MainViewModel(_service.Object); } [TestMethod] public void Initialize_Should_LoadDataFromDb() { _viewModel.Initialize(); _service.Verify(p=>p.LoadProducts()); } }

Page 15: Test and profile your Windows Phone 8 App

Run test

Page 16: Test and profile your Windows Phone 8 App

Red to Greenusing Caliburn.Micro;using OrangeCode.Wpreborn.SQLIteApp.Services;namespace OrangeCode.Wpreborn.SQLIteApp.ViewModels{ public class MainViewModel :PropertyChangedBase { private readonly IDbService _service;

public MainViewModel(IDbService service) { _service = service; } public void Initialize() { _service.LoadProducts(); } }}

Page 17: Test and profile your Windows Phone 8 App

Red to Green

Page 18: Test and profile your Windows Phone 8 App

Second test [TestMethod] public void Initialize_Should_ShowData() { _service.Setup(p => p.LoadProducts()).Returns(

new List<Product>{new Product {Name = "Product 1", Serial =

"123456"} } );

_viewModel.Initialize();

Assert.AreEqual(_viewModel.Products.Count,1); Assert.AreEqual(_viewModel.Products[0].Serial,"123456"); }

Page 19: Test and profile your Windows Phone 8 App

Second test public class MainViewModel :PropertyChangedBase { private readonly IDbService _service; public IList<Product> Products { get; set; }

public MainViewModel(IDbService service) { _service = service; } public void Initialize() { Products=_service.LoadProducts(); } }

Page 20: Test and profile your Windows Phone 8 App

Second test

Page 21: Test and profile your Windows Phone 8 App

Asynchronous testingHow to test this?

public interface IDbServiceAsync{ Task<IList<Product>> LoadProductsAsync(); } public class DbServiceAsync :IDbServiceAsync{ private readonly SQLiteAsyncConnection _context; public DbService(SQLiteAsyncConnection context) { _context = context; } public async Task<IList<Product>> LoadProductsAsync() { return await _context.Table<Product>().ToListAsync(); } }

Page 22: Test and profile your Windows Phone 8 App

Asynchronous testingThat’s the easy way to test it..

private async void PrepareDb() { SQLiteAsyncConnection connection = new SQLiteAsyncConnection("TestDb.sqlite”); await connection.DropTableAsync<Product>(); await connection.CreateTableAsync<Product>(); await connection.InsertAsync(new Product { Name = "Product 1", Serial = "123456" }); } [TestMethod] public async void LoadProductsAsync_Should_Load_Data_FromDb() { await PrepareDb(); var data= await _service.LoadProductsAsync(); Assert.AreEqual(data.Count,1); }

Page 23: Test and profile your Windows Phone 8 App

Let’s run the test suite

Page 24: Test and profile your Windows Phone 8 App

FeelingsWhat I like: - Test real code- Async / await runner support

What I dislike: - No automated runner- No class library support

Page 25: Test and profile your Windows Phone 8 App

Profiling appVisual Studio 2012 provides the Windows Phone Application Analysis tool.

Main feature:• App Monitoring: you can evaluate the most important

behaviors of your app that contribute to a good user experience, such as start time and responsiveness.

• Profiling: you can evaluate either execution-related or memory-usage aspects of your app.

Page 26: Test and profile your Windows Phone 8 App

Phone application anaysis tool

Page 27: Test and profile your Windows Phone 8 App

Test app

Page 28: Test and profile your Windows Phone 8 App

Running dei profiling

Page 29: Test and profile your Windows Phone 8 App

Phone application report

Page 30: Test and profile your Windows Phone 8 App

Report Details

Page 31: Test and profile your Windows Phone 8 App

Memory good practice

- Implement IDisposable in ViewModel

- Unload all event listener

- Consider using VirtualizingStackPanel instead of StackPanel

Page 32: Test and profile your Windows Phone 8 App

Quick recap

- Windows Phone Toolkit Test Framework

- Windows Phone Application Analysis

Page 33: Test and profile your Windows Phone 8 App

That’s all folks- GitHub repos:

- Testing : https://github.com/piccoloaiutante/WP8TestingExample

- Profiling: https://github.com/piccoloaiutante/WP8ProfilingExample

- Contact:

Email : [email protected] Blog: www.orangecode.it/blog

Twitter: @piccoloaiutante GitHub: github.com/piccoloaiutante