[NUI] Add TCs of WebView & Update some TCs of NUI.Devel.
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Tests / Tizen.NUI.Devel.Tests / testcase / internal / WebView / TSWebCertificate.cs
1 using global::System;
2 using NUnit.Framework;
3 using NUnit.Framework.TUnit;
4 using Tizen.NUI.Components;
5 using Tizen.NUI.BaseComponents;
6 using System.Threading.Tasks;
7
8 namespace Tizen.NUI.Devel.Tests
9 {
10     using tlog = Tizen.Log;
11
12     [TestFixture]
13     [Description("internal/WebView/WebCertificate")]
14     public class InternalWebCertificateTest
15     {
16         private const string tag = "NUITEST";
17         private static string[] runtimeArgs = { "Tizen.NUI.Devel.Tests", "--enable-dali-window", "--enable-spatial-navigation" };
18         private const string USER_AGENT = "Mozilla/5.0 (SMART-TV; Linux; Tizen 6.0) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/4.0 Chrome/76.0.3809.146 TV Safari/537.36";
19
20         internal class MyWebCertificate : WebCertificate
21         {
22             public MyWebCertificate(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
23             { }
24
25             public void OnReleaseSwigCPtr(global::System.Runtime.InteropServices.HandleRef swigCPtr)
26             {
27                 base.ReleaseSwigCPtr(swigCPtr);
28             }
29         }
30
31         [SetUp]
32         public void Init()
33         {
34             tlog.Info(tag, "Init() is called!");
35
36
37         }
38
39         [TearDown]
40         public void Destroy()
41         {
42             tlog.Info(tag, "Destroy() is called!");
43         }
44
45         [Test]
46         [Category("P1")]
47         [Description("WebCertificate constructor.")]
48         [Property("SPEC", "Tizen.NUI.WebCertificate.WebCertificate C")]
49         [Property("SPEC_URL", "-")]
50         [Property("CRITERIA", "CONSTR")]
51         [Property("COVPARAM", "")]
52         [Property("AUTHOR", "guowei.wang@samsung.com")]
53         public void WebCertificateConstructor()
54         {
55             tlog.Debug(tag, $"WebCertificateConstructor START");
56
57             using (Tizen.NUI.BaseComponents.WebView webview = new Tizen.NUI.BaseComponents.WebView("Shanghai", "Asia/Shanghai"))
58             {
59                 var testingTarget = new WebCertificate(webview.SwigCPtr.Handle, false);
60                 Assert.IsNotNull(testingTarget, "null handle");
61                 Assert.IsInstanceOf<WebCertificate>(testingTarget, "Should return WebCertificate instance.");
62
63                 testingTarget.Dispose();
64             }
65
66             tlog.Debug(tag, $"WebCertificateConstructor END (OK)");
67         }
68
69         [Test]
70         [Category("P1")]
71         [Description("WebCertificate ReleaseSwigCPtr.")]
72         [Property("SPEC", "Tizen.NUI.WebCertificate.ReleaseSwigCPtr M")]
73         [Property("SPEC_URL", "-")]
74         [Property("CRITERIA", "MR")]
75         [Property("COVPARAM", "")]
76         [Property("AUTHOR", "guowei.wang@samsung.com")]
77         public void WebCertificateReleaseSwigCPtr()
78         {
79             tlog.Debug(tag, $"WebCertificateReleaseSwigCPtr START");
80
81             using (Tizen.NUI.BaseComponents.WebView webview = new Tizen.NUI.BaseComponents.WebView("Shanghai", "Asia/Shanghai"))
82             {
83                 var testingTarget = new MyWebCertificate(webview.SwigCPtr.Handle, false);
84                 Assert.IsNotNull(testingTarget, "null handle");
85                 Assert.IsInstanceOf<WebCertificate>(testingTarget, "Should return WebCertificate instance.");
86
87                 try
88                 {
89                     testingTarget.OnReleaseSwigCPtr(testingTarget.SwigCPtr);
90                 }
91                 catch (Exception e)
92                 {
93                     tlog.Debug(tag, e.Message.ToString());
94                     Assert.Fail("Caught Exception : Failed!");
95                 }
96
97                 testingTarget.Dispose();
98             }
99
100             tlog.Debug(tag, $"WebCertificateReleaseSwigCPtr END (OK)");
101         }
102
103         [Test]
104         [Category("P1")]
105         [Description("WebCertificate IsFromMainFrame.")]
106         [Property("SPEC", "Tizen.NUI.WebCertificate.IsFromMainFrame A")]
107         [Property("SPEC_URL", "-")]
108         [Property("CRITERIA", "PRO")]
109         [Property("COVPARAM", "")]
110         [Property("AUTHOR", "guowei.wang@samsung.com")]
111         public async Task WebCertificateIsFromMainFrame()
112         {
113             tlog.Debug(tag, $"WebCertificateIsFromMainFrame START");
114
115             var testingTarget = new Tizen.NUI.BaseComponents.WebView(runtimeArgs)
116             {
117                 Size = new Size(500, 200),
118                 UserAgent = USER_AGENT
119             };
120             Assert.IsNotNull(testingTarget, "null handle");
121             Assert.IsInstanceOf<Tizen.NUI.BaseComponents.WebView>(testingTarget, "Should return Tizen.NUI.BaseComponents.WebView instance.");
122
123             testingTarget.SslCertificateChanged += OnSslCertificateChanged;
124             NUIApplication.GetDefaultWindow().Add(testingTarget);
125             
126             testingTarget.LoadUrl("http://www.baidu.com");
127             await Task.Delay(10000);
128
129             testingTarget.ClearCache();
130             testingTarget.ClearCookies();
131             NUIApplication.GetDefaultWindow().Remove(testingTarget);
132
133             testingTarget.Dispose();
134             tlog.Debug(tag, $"WebCertificateIsFromMainFrame END (OK)");
135         }
136
137         private void OnSslCertificateChanged(object sender, WebViewCertificateReceivedEventArgs e)
138         {
139             tlog.Info(tag, $"ssl certificate changed, PemData: {e.Certificate.PemData}");
140             tlog.Info(tag, $"ssl certificate changed, IsFromMainFrame: {e.Certificate.IsFromMainFrame}");
141             tlog.Info(tag, $"ssl certificate changed, IsContextSecure: {e.Certificate.IsContextSecure}");
142             
143             e.Certificate.Allow(false);
144         }
145     }
146 }