Newer Version Available
Testing Your CanvasLifecycleHandler Implementation
You can use the Canvas.Test class to test your Canvas.CanvasLifecycleHandler.onRender() implementation without having to run your canvas app.
Use Canvas.Test to create a test Canvas.RenderContext with mock application and environment context data. Call Canvas.Test.testCanvasLifecycle() with the mock RenderContext and your CanvasLifecycleHandler implementation to verify that your CanvasLifecycleHandler is being invoked correctly.
Use Canvas.Test.mockRenderContext() to create a mock
RenderContext. You can provide initial mock application and environment context data or
let Canvas.Test use a default set of mock data. You provide initial mock application and
environment context data in two Maps of key-value pairs. Use the predefined Canvas.Test
key name constants as your keys. The following example sets the app name in the
application context data and the sublocation in the environment context
data.
For the full list of context keys that Canvas.Test provides, see “Test
Constants” in the Apex Code Developer Guide.
1Map<String,String> appValues = new Map<String,String>();
2appValues.put(Canvas.Test.KEY_NAME,'AppName');
3
4Map<String,String> envValues = new Map<String,String>();
5envValues.put(Canvas.Test.KEY_SUB_LOCATION,'mobileDevice');When you’ve got a mock RenderContext, you can call Canvas.Test.testCanvasLifecycleHandler() with the RenderContext and your CanvasLifecycleHandler. This call invokes the handler’s onRender() method, and passies the mock RenderContext as context data.
Here’s an example test class that uses Canvas.Test. The
test class has three methods.
- testDefaultMockValues() invokes MyCanvasLifecycleHandler, using the default Canvas.Test mock context data.
- testOverriddenAppValues() invokes MyCanvasLifecycleHandler, using a custom mock RenderContext with mock app URL and version application context data.
- testOverriddenEnvironmentValues() invokes MyCanvasLifecycleHandler, using a custom mock RenderContext with mock display location, location URL, and custom parameter environment context data. The custom parameters are set through the Canvas.EnvironmentContext interface after the mock RenderContext is created.
1-@isTest
2global class MyCanvasLifecycleHandlerTest {
3
4 static testMethod void testDefaultMockValues(){
5 // Test handler using the default mock RenderContext Canvas.Test creates
6 MyCanvasLifecycleHandler handler = new MyCanvasLifecycleHandler();
7 Canvas.Test.testCanvasLifecycle(handler,null);
8 }
9
10 static testMethod void testOverriddenAppValues(){
11 // Test handler with some mock application context values
12 Map<String,String> appValues = new Map<String,String>();
13 appValues.put(Canvas.Test.KEY_CANVAS_URL,'https://myserver.com:6000/myAppPath');
14 appValues.put(Canvas.Test.KEY_VERSION,'3.0');
15
16 Canvas.RenderContext mock = Canvas.Test.mockRenderContext(appValues,null);
17 MyCanvasLifecycleHandler handler = new MyCanvasLifecycleHandler();
18 Canvas.Test.testCanvasLifecycle(handler,mock);
19 }
20
21 static testMethod void testOverriddenEnvironmentValues(){
22 // Test handler with some mock environment context values
23 Map<String,String> envValues = new Map<String,String>();
24 envValues.put(Canvas.Test.KEY_DISPLAY_LOCATION,'Chatter');
25 envValues.put(Canvas.Test.KEY_LOCATION_URL,
26 'https://myinstance.salesforce.com/_ui/core/chatter/ui/ChatterPage');
27 MyCanvasLifecycleHandler handler = new MyCanvasLifecycleHandler();
28 Canvas.RenderContext mock = Canvas.Test.mockRenderContext(null,envValues);
29 // Directly update the mock RenderContext and set the custom parameters
30 mock.getEnvironmentContext().setParametersAsJSON(
31 '{\"one\":1,\"two\":2,\"bool\":true,\"stringVal\":\"some string\"}');
32 Canvas.Test.testCanvasLifecycle(handler,mock);
33 }
34
35}