Mock Components
Purpose and Scope
This document describes the mock implementations of WebView platform components used for testing the mesh-flutter-sdk without requiring actual WebView functionality. These mocks enable unit testing of the SDK's core logic, event processing, and controller behavior in a controlled environment.
Mock Architecture Overview
The SDK provides a complete mock implementation of the webview_flutter_platform_interface to support testing without platform dependencies. The mock system consists of four components that mirror the real WebView platform architecture:
Simulation Capabilities
SDK Components Under Test
Mock Platform Components
Test Environment
Creates and configures
Creates
Creates
Creates
Injects mocks into
Uses
Uses
Displays
Calls
Triggers callbacks in
Provides
Asserts against
Test Cases
MockWebViewPlatform
MockWebViewController
MockNavigationDelegate
MockWebViewWidget
MeshLinkController
MeshLinkPage
simulateJsMessage()
State Tracking
MockWebViewController
MockWebViewController is the central mock component that simulates PlatformWebViewController behavior. It provides mechanisms to simulate JavaScript messages and track state changes for assertions.
Class Structure
«interface»
PlatformWebViewController
MockWebViewController
-_onMessageReceived Function
-_backgroundColor Color
-_javaScriptMode JavaScriptMode
-_javaScriptChannel String
-_requestUri Uri
-_lastJavaScript String
+backgroundColor Color
+javaScriptMode JavaScriptMode
+javaScriptChannel String
+requestUri Uri
+lastJavaScript String
+simulateJsMessage(String)
+setBackgroundColor(Color)
+setJavaScriptMode(JavaScriptMode)
+addJavaScriptChannel(JavaScriptChannelParams)
+setPlatformNavigationDelegate(PlatformNavigationDelegate)
+loadRequest(LoadRequestParams)
+runJavaScript(String)
Key Properties and Methods
| Property/Method | Type | Purpose |
|---|---|---|
simulateJsMessage(String) |
Method | Simulates receiving a JavaScript message through the JSBridge channel |
backgroundColor |
Getter | Returns the currently set background color |
javaScriptMode |
Getter | Returns the currently set JavaScript execution mode |
javaScriptChannel |
Getter | Returns the name of the registered JavaScript channel |
requestUri |
Getter | Returns the last URI loaded via loadRequest() |
lastJavaScript |
Getter | Returns the last JavaScript code executed via runJavaScript() |
JavaScript Message Simulation
The simulateJsMessage() method enables tests to inject JavaScript messages as if they came from the web content:
"MeshLinkController""onMessageReceived""MockWebViewController""Test Case""MeshLinkController""onMessageReceived""MockWebViewController""Test Case""addJavaScriptChannel(params)""Store _onMessageReceived""simulateJsMessage(json)""Call with JavaScriptMessage""Process message""Check state changes"
State Tracking
The mock tracks all configuration changes, enabling tests to verify that the controller properly configures the WebView:
| Tracked State | Setter Method | Getter Property | Storage Location |
|---|---|---|---|
| Background Color | setBackgroundColor() |
backgroundColor |
_backgroundColor |
| JavaScript Mode | setJavaScriptMode() |
javaScriptMode |
_javaScriptMode |
| JavaScript Channel | addJavaScriptChannel() |
javaScriptChannel |
_javaScriptChannel |
| Loaded URI | loadRequest() |
requestUri |
_requestUri |
| Executed JavaScript | runJavaScript() |
lastJavaScript |
_lastJavaScript |
Channel Registration Validation
The mock enforces that only one JavaScript channel can be registered, throwing a StateError if a second channel is added. This matches the expected behavior of the real WebView platform.
MockNavigationDelegate
MockNavigationDelegate provides stub implementations of PlatformNavigationDelegate methods. Unlike MockWebViewController, this mock does not track state or provide simulation capabilities—it simply accepts callback registrations without error.
Interface Implementation
«interface»
PlatformNavigationDelegate
+setOnNavigationRequest()
+setOnWebResourceError()
+setOnUrlChange()
+setOnHttpAuthRequest()
+setOnHttpError()
MockNavigationDelegate
+setOnNavigationRequest()
+setOnWebResourceError()
+setOnUrlChange()
+setOnHttpAuthRequest()
+setOnHttpError()
Supported Callbacks
All callback registration methods are implemented as no-ops:
| Method | Purpose | Implementation |
|---|---|---|
setOnNavigationRequest() |
Register navigation request handler | Empty async method |
setOnWebResourceError() |
Register resource error handler | Empty async method |
setOnUrlChange() |
Register URL change handler | Empty async method |
setOnHttpAuthRequest() |
Register HTTP auth handler | Empty async method |
setOnHttpError() |
Register HTTP error handler | Empty async method |
MockWebViewWidget
MockWebViewWidget is a minimal Flutter widget that renders as plain text instead of an actual WebView. This allows UI tests to verify widget structure without requiring WebView rendering capabilities.
Widget Implementation
The widget extends PlatformWebViewWidget and returns a simple Text widget:
returns
«abstract»
PlatformWebViewWidget
+build(BuildContext) : Widget
MockWebViewWidget
+build(BuildContext) : Widget
«Flutter Widget»
Text
MockWebViewPlatform
MockWebViewPlatform acts as a factory that returns pre-configured mock instances instead of creating real platform implementations. This component ties the entire mock system together.
Constructor and Fields
The constructor requires all three mock instances to be provided:
MockWebViewPlatform({
required this.webViewController,
required this.navigationDelegate,
required this.mockWebViewWidget,
})
Factory Methods
| Factory Method | Returns | Implementation |
|---|---|---|
createPlatformWebViewController() |
PlatformWebViewController |
Returns stored webViewController field |
createPlatformNavigationDelegate() |
PlatformNavigationDelegate |
Returns stored navigationDelegate field |
createPlatformWebViewWidget() |
PlatformWebViewWidget |
Returns stored mockWebViewWidget field |
Mock Component Limitations
The mock implementations have intentional limitations that align with their testing purpose:
| Component | Limitation | Rationale |
|---|---|---|
MockNavigationDelegate |
Does not store or invoke callbacks | Navigation logic is tested indirectly through controller behavior |
MockWebViewWidget |
Does not render actual web content | Widget structure tests don't require rendering |
MockWebViewController |
No automatic page load simulation | Tests must explicitly call simulateJsMessage() to trigger events |
MockWebViewController |
Single channel enforcement | Prevents test setup errors where multiple channels are registered |