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.
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.