Skip to content

Commit 885da9d

Browse files
[Testing] Feature matrix UITest Cases for HybridWebView Control (#30613)
* Added HybridWebView Feature Tests * Changes updated * Added images * Delete the test file * Added the test file after rename the file * Added images --------- Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
1 parent df6583e commit 885da9d

File tree

15 files changed

+1774
-0
lines changed

15 files changed

+1774
-0
lines changed
156 KB
Loading
161 KB
Loading

src/Controls/tests/TestCases.HostApp/CoreViews/CorePageView.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public override string ToString()
9595
new GalleryPageFactory(() => new ImageControlPage(), "Image Feature Matrix"),
9696
new GalleryPageFactory(() => new ImageButtonControlPage(), "ImageButton Feature Matrix"),
9797
new GalleryPageFactory(() => new BoxViewControlPage(), "BoxView Feature Matrix"),
98+
new GalleryPageFactory(() => new HybridWebViewControlPage(), "HybridWebView Feature Matrix"),
9899
new GalleryPageFactory(() => new ScrollViewControlPage(), "ScrollView Feature Matrix"),
99100
new GalleryPageFactory(() => new EditorControlPage(), "Editor Feature Matrix"),
100101
};
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:Maui.Controls.Sample"
5+
x:Class="Maui.Controls.Sample.HybridWebViewControlPage"
6+
x:DataType="local:HybridWebViewViewModel"
7+
Title="HybridWebViewControlPage">
8+
9+
10+
<Grid Padding="10"
11+
RowSpacing="5">
12+
<!-- Define Grid Rows -->
13+
<Grid.RowDefinitions>
14+
<RowDefinition Height="250"/>
15+
<RowDefinition Height="Auto"/>
16+
<RowDefinition Height="Auto"/>
17+
</Grid.RowDefinitions>
18+
19+
<!-- Row 0: HybridWebView at the top -->
20+
<HybridWebView Grid.Row="0"
21+
x:Name="MyHybridWebView"
22+
DefaultFile="{Binding DefaultFile}"
23+
HybridRoot="{Binding HybridRoot}"
24+
IsVisible="{Binding IsVisible}"
25+
Shadow="{Binding Shadow}"
26+
FlowDirection="{Binding FlowDirection}"
27+
RawMessageReceived="OnRawMessageReceived"
28+
AutomationId="HybridWebViewControl"/>
29+
30+
<!-- Row 1: Title and Status Labels -->
31+
<VerticalStackLayout Grid.Row="1"
32+
Spacing="2">
33+
<Label Text="{Binding Status}"
34+
FontSize="14"
35+
TextColor="DarkGreen"
36+
HorizontalOptions="Start"
37+
AutomationId="StatusLabel"/>
38+
39+
<Grid ColumnSpacing="20">
40+
<Grid.ColumnDefinitions>
41+
<ColumnDefinition Width="*"/>
42+
<ColumnDefinition Width="*"/>
43+
</Grid.ColumnDefinitions>
44+
45+
<HorizontalStackLayout Grid.Column="0"
46+
Spacing="2">
47+
<Label Text="HybridRoot:"
48+
FontAttributes="Bold"
49+
FontSize="12"
50+
HorizontalOptions="Start"/>
51+
<Label Text="{Binding HybridRoot}"
52+
FontSize="11"
53+
TextColor="Blue"
54+
AutomationId="HybridRootLabel"/>
55+
</HorizontalStackLayout>
56+
57+
<HorizontalStackLayout Grid.Column="1"
58+
Spacing="2">
59+
<Label Text="DefaultFile:"
60+
FontAttributes="Bold"
61+
FontSize="12"
62+
HorizontalOptions="Start"/>
63+
<Label Text="{Binding DefaultFile}"
64+
FontSize="11"
65+
TextColor="Blue"
66+
AutomationId="DefaultFileLabel"/>
67+
</HorizontalStackLayout>
68+
</Grid>
69+
</VerticalStackLayout>
70+
71+
<!-- Row 2: Action Buttons -->
72+
<VerticalStackLayout Grid.Row="2"
73+
Spacing="2">
74+
<Grid RowSpacing="2"
75+
ColumnSpacing="5">
76+
<Grid.RowDefinitions>
77+
<RowDefinition Height="Auto"/>
78+
<RowDefinition Height="Auto"/>
79+
<RowDefinition Height="Auto"/>
80+
<RowDefinition Height="Auto"/>
81+
<RowDefinition Height="Auto"/>
82+
<RowDefinition Height="Auto"/>
83+
<RowDefinition Height="Auto"/>
84+
<RowDefinition Height="Auto"/>
85+
<RowDefinition Height="Auto"/>
86+
<RowDefinition Height="Auto"/>
87+
<RowDefinition Height="Auto"/>
88+
</Grid.RowDefinitions>
89+
<Grid.ColumnDefinitions>
90+
<ColumnDefinition Width="*"/>
91+
<ColumnDefinition Width="*"/>
92+
</Grid.ColumnDefinitions>
93+
94+
<!-- HybridRoot Selection -->
95+
<Label Grid.Row="0"
96+
Grid.ColumnSpan="2"
97+
Text="Select HybridRoot:"
98+
FontSize="12"
99+
FontAttributes="Bold"
100+
HorizontalOptions="Start"/>
101+
102+
<Button Grid.Row="1"
103+
Grid.Column="0"
104+
x:Name="HybridWebView1Button"
105+
Text="HybridWebView1"
106+
Clicked="OnHybridRootButtonClicked"
107+
AutomationId="HybridWebView1Button"/>
108+
<Button Grid.Row="1"
109+
Grid.Column="1"
110+
x:Name="HybridWebView2Button"
111+
Text="HybridWebView2"
112+
Clicked="OnHybridRootButtonClicked"
113+
AutomationId="HybridWebView2Button"/>
114+
115+
<!-- DefaultFile Selection -->
116+
<Label Grid.Row="2"
117+
Grid.ColumnSpan="2"
118+
Text="Select Default File:"
119+
FontSize="12"
120+
FontAttributes="Bold"
121+
HorizontalOptions="Start"/>
122+
123+
<Button Grid.Row="3"
124+
Grid.Column="0"
125+
x:Name="IndexHtmlButton"
126+
Text="index.html"
127+
Clicked="OnDefaultFileButtonClicked"
128+
AutomationId="IndexHtmlButton"/>
129+
<Button Grid.Row="3"
130+
Grid.Column="1"
131+
x:Name="ImageHtmlButton"
132+
Text="image.html"
133+
Clicked="OnDefaultFileButtonClicked"
134+
AutomationId="ImageHtmlButton"/>
135+
136+
<Button Grid.Row="4"
137+
Grid.Column="0"
138+
x:Name="NavigationHtmlButton"
139+
Text="navigation.html"
140+
Clicked="OnDefaultFileButtonClicked"
141+
AutomationId="NavigationHtmlButton"/>
142+
<Button Grid.Row="4"
143+
Grid.Column="1"
144+
x:Name="WebHtmlButton"
145+
Text="web.html"
146+
Clicked="OnDefaultFileButtonClicked"
147+
AutomationId="WebHtmlButton"/>
148+
149+
<!-- Properties Section -->
150+
<Label Grid.Row="5"
151+
Grid.ColumnSpan="2"
152+
Text="Properties:"
153+
FontSize="12"
154+
FontAttributes="Bold"
155+
HorizontalOptions="Start"/>
156+
157+
<HorizontalStackLayout Grid.Row="6"
158+
Grid.Column="0"
159+
Spacing="5">
160+
<CheckBox x:Name="IsVisibleCheckBox"
161+
IsChecked="{Binding IsVisible}"
162+
AutomationId="IsVisibleCheckBox"/>
163+
<Label Text="IsVisible"
164+
VerticalOptions="Center"
165+
FontSize="12"/>
166+
</HorizontalStackLayout>
167+
168+
<HorizontalStackLayout Grid.Row="6"
169+
Grid.Column="1"
170+
Spacing="5">
171+
<CheckBox x:Name="ShadowCheckBox"
172+
IsChecked="{Binding HasShadow}"
173+
AutomationId="ShadowCheckBox"/>
174+
<Label Text="Shadow"
175+
VerticalOptions="Center"
176+
FontSize="12"/>
177+
</HorizontalStackLayout>
178+
<HorizontalStackLayout Grid.Row="7"
179+
Grid.Column="0"
180+
Spacing="5">
181+
<CheckBox x:Name="FlowDirectionCheckBox"
182+
IsChecked="{Binding IsLeftToRight}"
183+
CheckedChanged="OnFlowDirectionCheckBoxChanged"
184+
AutomationId="FlowDirectionCheckBox"/>
185+
<Label Text="FlowDirection: LTR if checked, RTL if unchecked"
186+
VerticalOptions="Center"
187+
FontSize="12"/>
188+
</HorizontalStackLayout>
189+
190+
191+
<!-- Action Buttons -->
192+
<Label Grid.Row="8"
193+
Grid.ColumnSpan="2"
194+
Text="Action Buttons:"
195+
FontSize="12"
196+
FontAttributes="Bold"
197+
HorizontalOptions="Start"/>
198+
199+
<Button Grid.Row="9"
200+
Grid.Column="0"
201+
Text="Evaluate JS"
202+
Clicked="OnEvaluateJavaScriptClicked"
203+
AutomationId="EvaluateJavaScriptButton"/>
204+
205+
<Button Grid.Row="9"
206+
Grid.Column="1"
207+
Text="Send Message to JS"
208+
Clicked="OnSendMessageToJavaScriptClicked"
209+
FontSize="14"
210+
AutomationId="SendMessageButton"/>
211+
<!-- Reset Button -->
212+
<Button Grid.Row="10"
213+
Grid.ColumnSpan="2"
214+
Text="Reset"
215+
Clicked="OnResetButtonClicked"
216+
BackgroundColor="Orange"
217+
FontSize="12"
218+
AutomationId="ResetButton"/>
219+
</Grid>
220+
</VerticalStackLayout>
221+
</Grid>
222+
</ContentPage>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using Microsoft.Maui.Controls;
3+
4+
namespace Maui.Controls.Sample;
5+
6+
public partial class HybridWebViewControlPage : ContentPage
7+
{
8+
private HybridWebViewViewModel _viewModel;
9+
10+
public HybridWebViewControlPage()
11+
{
12+
InitializeComponent();
13+
_viewModel = new HybridWebViewViewModel();
14+
MyHybridWebView.RawMessageReceived += OnRawMessageReceived;
15+
BindingContext = _viewModel;
16+
}
17+
18+
private async void OnEvaluateJavaScriptClicked(object sender, EventArgs e)
19+
{
20+
var result = await MyHybridWebView.EvaluateJavaScriptAsync("document.title");
21+
_viewModel.Status = $"EvaluateJavaScriptAsync Result: {result}";
22+
}
23+
24+
private async void OnHybridRootButtonClicked(object sender, EventArgs e)
25+
{
26+
var button = (Button)sender;
27+
var selectedRoot = button.Text;
28+
_viewModel.HybridRoot = selectedRoot;
29+
MyHybridWebView.HybridRoot = selectedRoot;
30+
try
31+
{
32+
await MyHybridWebView.EvaluateJavaScriptAsync("window.location.reload();");
33+
34+
}
35+
catch (Exception ex)
36+
{
37+
_viewModel.Status = $"HybridRoot changed to: {selectedRoot} (reload failed: {ex.Message})";
38+
}
39+
}
40+
41+
private async void OnDefaultFileButtonClicked(object sender, EventArgs e)
42+
{
43+
var button = (Button)sender;
44+
var selectedFile = button.Text;
45+
_viewModel.DefaultFile = selectedFile;
46+
MyHybridWebView.DefaultFile = selectedFile;
47+
48+
try
49+
{
50+
await MyHybridWebView.EvaluateJavaScriptAsync("window.location.reload();");
51+
}
52+
catch (Exception ex)
53+
{
54+
_viewModel.Status = $"DefaultFile changed to: {selectedFile} (reload failed: {ex.Message})";
55+
}
56+
}
57+
58+
private int _messageCount = 0;
59+
private void OnRawMessageReceived(object sender, HybridWebViewRawMessageReceivedEventArgs e)
60+
{
61+
_messageCount++;
62+
if (string.IsNullOrEmpty(e.Message))
63+
{
64+
_viewModel.Status = $"Message #{_messageCount}: EMPTY";
65+
}
66+
else
67+
{
68+
_viewModel.Status = $"Message: {e.Message}";
69+
}
70+
}
71+
72+
private async void OnSendMessageToJavaScriptClicked(object sender, EventArgs e)
73+
{
74+
try
75+
{
76+
_viewModel.Status = "Sending message to index.html...";
77+
await Task.Delay(500);
78+
var message = $"Hello from C#";
79+
var jsCode = $@"window.receiveMessageFromCSharp('{message}')";
80+
var result = await MyHybridWebView.EvaluateJavaScriptAsync(jsCode);
81+
_viewModel.Status = $"Message sent successfully. Result: {result}";
82+
83+
}
84+
catch (Exception ex)
85+
{
86+
_viewModel.Status = $"Failed to send message: {ex.Message}";
87+
}
88+
}
89+
90+
private void OnFlowDirectionCheckBoxChanged(object sender, CheckedChangedEventArgs e)
91+
{
92+
_viewModel.FlowDirection = e.Value
93+
? FlowDirection.LeftToRight
94+
: FlowDirection.RightToLeft;
95+
}
96+
97+
private async void OnResetButtonClicked(object sender, EventArgs e)
98+
{
99+
_viewModel = new HybridWebViewViewModel();
100+
BindingContext = _viewModel;
101+
MyHybridWebView.HybridRoot = _viewModel.HybridRoot;
102+
MyHybridWebView.DefaultFile = _viewModel.DefaultFile;
103+
await MyHybridWebView.EvaluateJavaScriptAsync("window.location.reload();");
104+
105+
}
106+
}

0 commit comments

Comments
 (0)