[NUI] Update webview TCs.
[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 using System.IO;
8 using System.Text;
9
10 namespace Tizen.NUI.Devel.Tests
11 {
12     using tlog = Tizen.Log;
13
14     [TestFixture]
15     [Description("internal/WebView/WebHttpRequestInterceptor")]
16     public class InternalWebHttpRequestInterceptorTest
17     {
18         private const string tag = "NUITEST";
19         private string invalidUrl = "https://test/";
20         private BaseComponents.WebView webview;
21
22         [SetUp]
23         public void Init()
24         {
25             tlog.Info(tag, "Init() is called!");
26             webview = new BaseComponents.WebView()
27             {
28                 Size = new Size(500, 200),
29             };
30         }
31
32         [TearDown]
33         public void Destroy()
34         {
35             tlog.Info(tag, "Destroy() is being called!");
36             webview.Dispose();
37             tlog.Info(tag, "Destroy() is called!");
38         }
39
40         [Test]
41         [Category("P1")]
42         [Description("WebHttpRequestInterceptor SetResponseBody.")]
43         [Property("SPEC", "Tizen.NUI.WebHttpRequestInterceptor.SetResponseBody M")]
44         [Property("SPEC_URL", "-")]
45         [Property("CRITERIA", "CONSTR")]
46         [Property("COVPARAM", "")]
47         [Property("AUTHOR", "guowei.wang@samsung.com")]
48         public async Task WebHttpRequestInterceptorSetResponseBody()
49         {
50             tlog.Debug(tag, $"WebHttpRequestInterceptorConstructor START");
51
52             TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(false);
53             WebContext.HttpRequestInterceptedCallback onRequestIntercepted = (interceptor) =>
54             {
55                 tlog.Info(tag, $"onRequestIntercepted is called, url {interceptor.Url}.");
56                 if (interceptor.Url.Equals(invalidUrl))
57                 {
58                     Assert.IsNotNull(interceptor.InterceptedWebView, "InterceptedWebView should not be null.");
59                     Assert.IsNotNull(interceptor.Headers, "Headers should not be null.");
60
61                     byte[] bData = Encoding.UTF8.GetBytes("<html><body><img src='test.jpg'></body></html>");
62                     interceptor.SetResponseStatus(200, "OK");
63                     interceptor.AddResponseHeader("Content-Type", "text/html; charset=UTF-8");
64                     interceptor.AddResponseHeader("Content-Length", bData.Length.ToString());
65                     interceptor.SetResponseBody(bData);
66
67                     tlog.Info(tag, $"http request intercepted set response body end, {interceptor.Method}");
68                 }
69                 else if (interceptor.Url.Equals($"{invalidUrl}test.jpg"))
70                 {
71                     string path = $"{Applications.Application.Current.DirectoryInfo.Resource}webview/tizen.png";
72                     using (FileStream fs = File.OpenRead(path))
73                     {
74                         byte[] bData = new byte[1024];
75                         while (fs.Read(bData, 0, bData.Length) > 0)
76                         {
77                             interceptor.WriteResponseChunk(bData);
78                         }
79                         interceptor.WriteResponseChunk((byte[])null);
80                         tlog.Info(tag, $"http request intercepted write chunk end");
81                     }
82                 }
83                 else
84                 {
85                     tlog.Info(tag, $"http request intercepted end, url {interceptor.Url}");
86                     tcs.TrySetResult(true);
87                 }
88             };
89
90             webview.Context.RegisterHttpRequestInterceptedCallback(onRequestIntercepted);
91
92             webview.LoadUrl(invalidUrl);
93             var result = await tcs.Task;
94             Assert.IsTrue(result, "HttpAuthRequested event should be invoked.");
95
96             // Make current thread (CPU) sleep...
97             await Task.Delay(1);
98
99             webview.Context.RegisterHttpRequestInterceptedCallback(null);
100
101             tlog.Debug(tag, $"WebHttpRequestInterceptorConstructor END (OK)");
102         }
103
104         [Test]
105         [Category("P1")]
106         [Description("WebHttpRequestInterceptor constructor.")]
107         [Property("SPEC", "Tizen.NUI.WebHttpRequestInterceptor.Ignore M")]
108         [Property("SPEC_URL", "-")]
109         [Property("CRITERIA", "CONSTR")]
110         [Property("COVPARAM", "")]
111         [Property("AUTHOR", "guowei.wang@samsung.com")]
112         public async Task WebHttpRequestInterceptorIgnore()
113         {
114             tlog.Debug(tag, $"WebHttpRequestInterceptorIgnore START");
115
116             TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(false);
117             WebContext.HttpRequestInterceptedCallback onRequestIntercepted = (interceptor) =>
118             {
119                 interceptor.Ignore();
120             };
121
122             webview.Context.RegisterHttpRequestInterceptedCallback(onRequestIntercepted);
123
124             EventHandler<WebViewPageLoadErrorEventArgs> onLoadError = (s, e) =>
125             {
126                 tlog.Info(tag, $"http load error!");
127                 tcs.TrySetResult(true);
128             };
129             webview.PageLoadError += onLoadError;
130
131             webview.LoadUrl(invalidUrl);
132             var result = await tcs.Task;
133             Assert.IsTrue(result, "PageLoadError event should be invoked.");
134
135             // Make current thread (CPU) sleep...
136             await Task.Delay(1);
137
138             webview.Context.RegisterHttpRequestInterceptedCallback(null);
139             webview.PageLoadError -= onLoadError;
140
141             tlog.Debug(tag, $"WebHttpRequestInterceptorIgnore END (OK)");
142         }
143     }
144 }