-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
WebViewUnitTests.cs
161 lines (131 loc) · 4.6 KB
/
WebViewUnitTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using NUnit.Framework;
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
using Xamarin.Forms.PlatformConfiguration.WindowsSpecific;
using WindowsOS = Xamarin.Forms.PlatformConfiguration.Windows;
namespace Xamarin.Forms.Core.UnitTests
{
[TestFixture]
public class WebViewUnitTests : BaseTestFixture
{
[Test]
public void TestSourceImplicitConversion ()
{
var web = new WebView ();
Assert.Null (web.Source);
web.Source = "http://www.google.com";
Assert.NotNull (web.Source);
Assert.True (web.Source is UrlWebViewSource);
Assert.AreEqual ("http://www.google.com", ((UrlWebViewSource)web.Source).Url);
}
[Test]
public void TestSourceChangedPropagation ()
{
var source = new UrlWebViewSource {Url ="http://www.google.com"};
var web = new WebView { Source = source };
bool signaled = false;
web.PropertyChanged += (sender, args) => {
if (args.PropertyName == WebView.SourceProperty.PropertyName)
signaled = true;
};
Assert.False (signaled);
source.Url = "http://www.xamarin.com";
Assert.True (signaled);
}
[Test]
public void TestSourceDisconnected ()
{
var source = new UrlWebViewSource {Url="http://www.google.com"};
var web = new WebView { Source = source };
web.Source = new UrlWebViewSource {Url="Foo"};
bool signaled = false;
web.PropertyChanged += (sender, args) => {
if (args.PropertyName == WebView.SourceProperty.PropertyName)
signaled = true;
};
Assert.False (signaled);
source.Url = "http://www.xamarin.com";
Assert.False (signaled);
}
class ViewModel
{
public string HTML { get; set; } = "<html><body><p>This is a WebView!</p></body></html>";
public string URL { get; set; } = "http://xamarin.com";
}
[Test]
public void TestBindingContextPropagatesToSource ()
{
var htmlWebView = new WebView {
};
var urlWebView = new WebView {
};
var htmlSource = new HtmlWebViewSource ();
htmlSource.SetBinding (HtmlWebViewSource.HtmlProperty, "HTML");
htmlWebView.Source = htmlSource;
var urlSource = new UrlWebViewSource ();
urlSource.SetBinding (UrlWebViewSource.UrlProperty, "URL");
urlWebView.Source = urlSource;
var viewModel = new ViewModel ();
var container = new StackLayout {
BindingContext = viewModel,
Padding = new Size (20, 20),
Children = {
htmlWebView,
urlWebView
}
};
Assert.AreEqual ("<html><body><p>This is a WebView!</p></body></html>", htmlSource.Html);
Assert.AreEqual ("http://xamarin.com", urlSource.Url);
}
[Test]
public void TestAndroidMixedContent()
{
var defaultWebView = new WebView();
var mixedContentWebView = new WebView();
mixedContentWebView.On<Android>().SetMixedContentMode(MixedContentHandling.AlwaysAllow);
Assert.AreEqual(defaultWebView.On<Android>().MixedContentMode(), MixedContentHandling.NeverAllow);
Assert.AreEqual(mixedContentWebView.On<Android>().MixedContentMode(), MixedContentHandling.AlwaysAllow);
}
[Test]
public void TestEnableZoomControls()
{
var defaultWebView = new WebView();
var enableZoomControlsWebView = new WebView();
enableZoomControlsWebView.On<Android>().SetEnableZoomControls(true);
Assert.AreEqual(defaultWebView.On<Android>().ZoomControlsEnabled(), false);
Assert.AreEqual(enableZoomControlsWebView.On<Android>().ZoomControlsEnabled(), true);
}
[Test]
public void TestDisplayZoomControls()
{
var defaultWebView = new WebView();
var displayZoomControlsWebView = new WebView();
displayZoomControlsWebView.On<Android>().SetDisplayZoomControls(false);
Assert.AreEqual(defaultWebView.On<Android>().ZoomControlsDisplayed(), true);
Assert.AreEqual(displayZoomControlsWebView.On<Android>().ZoomControlsDisplayed(), false);
}
[Test]
public void TestWindowsSetAllowJavaScriptAlertsFlag()
{
var defaultWebView = new WebView();
var jsAlertsAllowedWebView = new WebView();
jsAlertsAllowedWebView.On<WindowsOS>().SetIsJavaScriptAlertEnabled(true);
Assert.AreEqual(defaultWebView.On<WindowsOS>().IsJavaScriptAlertEnabled(), false);
Assert.AreEqual(jsAlertsAllowedWebView.On<WindowsOS>().IsJavaScriptAlertEnabled(), true);
}
[Test]
public void TestSettingOfCookie()
{
var defaultWebView = new WebView();
var CookieContainer = new CookieContainer();
CookieContainer.Add(new Cookie("TestCookie", "My Test Cookie...", "/", "microsoft.com"));
defaultWebView.Cookies = CookieContainer;
defaultWebView.Source = "http://xamarin.com";
Assert.IsNotNull(defaultWebView.Cookies);
}
}
}