[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 / TSWebHttpRequestInterceptor.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/WebHttpRequestInterceptor")]
14     public class InternalWebHttpRequestInterceptorTest
15     {
16         private const string tag = "NUITEST";
17         private string url = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "picture.png";
18         private static string[] runtimeArgs = { "Tizen.NUI.Devel.Tests", "--enable-dali-window", "--enable-spatial-navigation" };
19         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";
20
21         internal class MyWebHttpRequestInterceptor : WebHttpRequestInterceptor
22         {
23             public MyWebHttpRequestInterceptor(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
24             { }
25
26             public void OnReleaseSwigCPtr(global::System.Runtime.InteropServices.HandleRef swigCPtr)
27             {
28                 base.ReleaseSwigCPtr(swigCPtr);
29             }
30         }
31
32         [SetUp]
33         public void Init()
34         {
35             tlog.Info(tag, "Init() is called!");
36         }
37
38         [TearDown]
39         public void Destroy()
40         {
41             tlog.Info(tag, "Destroy() is called!");
42         }
43
44         [Test]
45         [Category("P1")]
46         [Description("WebHttpRequestInterceptor constructor.")]
47         [Property("SPEC", "Tizen.NUI.WebHttpRequestInterceptor.WebHttpRequestInterceptor C")]
48         [Property("SPEC_URL", "-")]
49         [Property("CRITERIA", "CONSTR")]
50         [Property("COVPARAM", "")]
51         [Property("AUTHOR", "guowei.wang@samsung.com")]
52         public void WebHttpRequestInterceptorConstructor()
53         {
54             tlog.Debug(tag, $"WebHttpRequestInterceptorConstructor START");
55
56             using (Tizen.NUI.BaseComponents.WebView webview = new Tizen.NUI.BaseComponents.WebView("Shanghai", "Asia/Shanghai"))
57             {
58                 var testingTarget = new WebHttpRequestInterceptor(webview.SwigCPtr.Handle, false);
59                 Assert.IsNotNull(testingTarget, "null handle");
60                 Assert.IsInstanceOf<WebHttpRequestInterceptor>(testingTarget, "Should return WebHttpRequestInterceptor instance.");
61
62                 testingTarget.Dispose();
63             }
64
65             tlog.Debug(tag, $"WebHttpRequestInterceptorConstructor END (OK)");
66         }
67
68         [Test]
69         [Category("P1")]
70         [Description("WebHttpRequestInterceptor ReleaseSwigCPtr.")]
71         [Property("SPEC", "Tizen.NUI.WebHttpRequestInterceptor.ReleaseSwigCPtr M")]
72         [Property("SPEC_URL", "-")]
73         [Property("CRITERIA", "MR")]
74         [Property("COVPARAM", "")]
75         [Property("AUTHOR", "guowei.wang@samsung.com")]
76         public void WebHttpRequestInterceptorReleaseSwigCPtr()
77         {
78             tlog.Debug(tag, $"WebHttpRequestInterceptorReleaseSwigCPtr START");
79
80             using (Tizen.NUI.BaseComponents.WebView webview = new Tizen.NUI.BaseComponents.WebView("Shanghai", "Asia/Shanghai"))
81             {
82                 var testingTarget = new MyWebHttpRequestInterceptor(webview.SwigCPtr.Handle, false);
83                 Assert.IsNotNull(testingTarget, "null handle");
84                 Assert.IsInstanceOf<WebHttpRequestInterceptor>(testingTarget, "Should return WebHttpRequestInterceptor instance.");
85
86                 try
87                 {
88                     testingTarget.OnReleaseSwigCPtr(testingTarget.SwigCPtr);
89                 }
90                 catch (Exception e)
91                 {
92                     tlog.Debug(tag, e.Message.ToString());
93                     Assert.Fail("Caught Exception : Failed!");
94                 }
95             }
96
97             tlog.Debug(tag, $"WebHttpRequestInterceptorReleaseSwigCPtr END (OK)");
98         }
99
100         [Test]
101         [Category("P1")]
102         [Description("WebHttpRequestInterceptor Url.")]
103         [Property("SPEC", "Tizen.NUI.WebHttpRequestInterceptor.Url A")]
104         [Property("SPEC_URL", "-")]
105         [Property("CRITERIA", "PRO")]
106         [Property("COVPARAM", "")]
107         [Property("AUTHOR", "guowei.wang@samsung.com")]
108         public async Task WebHttpRequestInterceptorUrl()
109         {
110             tlog.Debug(tag, $"WebHttpRequestInterceptorUrl START");
111
112             var testingTarget = new Tizen.NUI.BaseComponents.WebView(runtimeArgs)
113             {
114                 Size = new Size(500, 200),
115                 UserAgent = USER_AGENT
116             };
117             Assert.IsNotNull(testingTarget, "null handle");
118             Assert.IsInstanceOf<Tizen.NUI.BaseComponents.WebView>(testingTarget, "Should return Tizen.NUI.BaseComponents.WebView instance.");
119
120             testingTarget.HttpRequestIntercepted += OnHttpRequestIntercepted;
121             NUIApplication.GetDefaultWindow().Add(testingTarget);
122
123             testingTarget.LoadUrl("http://www.baidu.com");
124             await Task.Delay(10000);
125
126             testingTarget.ClearCache();
127             testingTarget.ClearCookies();
128             NUIApplication.GetDefaultWindow().Remove(testingTarget);
129
130             testingTarget.Dispose();
131             tlog.Debug(tag, $"WebHttpRequestInterceptorUrl END (OK)");
132         }
133
134         private void OnHttpRequestIntercepted(object sender, WebViewHttpRequestInterceptedEventArgs e)
135         {
136             tlog.Debug(tag, $"HttpRequestInterceptor, Url: {e.HttpRequestInterceptor.Url}");
137                         
138             e.HttpRequestInterceptor.Ignore();
139             e.HttpRequestInterceptor.SetResponseStatus(911, "Internal error.");
140             e.HttpRequestInterceptor.AddResponseHeader("samsung", "webview");
141             e.HttpRequestInterceptor.AddResponseBody("Thank you for using!", 20);
142         }
143     }
144 }