[NUI] Add an API for getting web view when request is intercepted.
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / WebViewInterceptTest.cs
1 
2 using System;
3 using Tizen.NUI;
4 using Tizen.NUI.BaseComponents;
5 using Tizen.NUI.Components;
6 using System.IO;
7 using System.Text;
8
9 namespace Tizen.NUI.Samples
10 {
11     using tlog = Tizen.Log;
12     public class WebViewInterceptTest : IExample
13     {
14         const string tag = "NUITEST";
15         private View root;
16         private Window win;
17         private Button btn1, btn2, btn3;
18         private WebView webView1, webView2;
19         private string invalidUrl = "https://test/";
20
21         public void Activate()
22         {
23             win = NUIApplication.GetDefaultWindow();
24
25             root = new View()
26             {
27                 Size = new Size(win.Size.Width, win.Size.Height, 0),
28                 BackgroundColor = Color.Green,
29                 Layout = new LinearLayout()
30                 {
31                     LinearOrientation = LinearLayout.Orientation.Vertical,
32                     Padding = new Extents(3, 3, 3, 3),
33                 },
34             };
35             win.Add(root);
36
37             webView1 = new WebView();
38             webView1.Url = "https://m.naver.com/";
39             webView1.WidthSpecification = LayoutParamPolicies.MatchParent;
40             webView1.HeightSpecification = LayoutParamPolicies.MatchParent;
41             root.Add(webView1);
42
43             webView2 = new WebView();
44             webView2.Url = "https://m.google.com/";
45             webView2.WidthSpecification = LayoutParamPolicies.MatchParent;
46             webView2.HeightSpecification = LayoutParamPolicies.MatchParent;
47             root.Add(webView2);
48
49             btn1 = new Button()
50             {
51                 HeightSpecification = 50,
52                 WidthSpecification = 300,
53                 Text = "register intercept callback",
54             };
55             btn1.Clicked += (s, e) =>
56             {
57                 webView1.Context.RegisterHttpRequestInterceptedCallback(Callback);
58                 // only this Callback2 will be invoked for all created WebViews, because the Context is global(Callback will be ignored).
59                 webView2.Context.RegisterHttpRequestInterceptedCallback(Callback2);
60             };
61             root.Add(btn1);
62
63             btn2 = new Button()
64             {
65                 HeightSpecification = 50,
66                 WidthSpecification = 300,
67                 Text = "WebView1 load invalid url",
68             };
69             btn2.Clicked += (s, e) =>
70             {
71                 webView1.LoadUrl(invalidUrl);
72             };
73             root.Add(btn2);
74
75             btn3 = new Button()
76             {
77                 HeightSpecification = 50,
78                 WidthSpecification = 300,
79                 Text = "WebView2 load invalid url",
80             };
81             btn3.Clicked += (s, e) =>
82             {
83                 webView2.LoadUrl(invalidUrl);
84             };
85             root.Add(btn3);
86
87         }
88
89         private void Callback(WebHttpRequestInterceptor interceptor)
90         {
91             tlog.Debug(tag, $"callback: http request intercepted start, Url: {interceptor.Url}");
92
93             //interceptor.Ignore();
94             if (interceptor.Url.Equals(invalidUrl))
95             {
96                 byte[] bData = Encoding.UTF8.GetBytes("<html><body><img src='test.jpg'></body></html>");
97                 interceptor.SetResponseStatus(200, "OK");
98                 interceptor.AddResponseHeader("Content-Type", "text/html; charset=UTF-8");
99                 interceptor.AddResponseHeader("Content-Length", bData.Length.ToString());
100                 interceptor.SetResponseBody(bData);
101                 tlog.Debug(tag, $"http request intercepted set response body end");
102             }
103             else if (interceptor.Url.Equals($"{invalidUrl}test.jpg"))
104             {
105                 string path = Tizen.Applications.Application.Current.DirectoryInfo.SharedResource + "soundcloud.png";
106                 using (FileStream fs = File.OpenRead(path))
107                 {
108                     byte[] bData = new byte[1024];
109                     while (fs.Read(bData, 0, bData.Length) > 0)
110                     {
111                         interceptor.WriteResponseChunk(bData);
112                     }
113                     interceptor.WriteResponseChunk((byte[])null);
114                     tlog.Debug(tag, $"http request intercepted write chunk end");
115                 }
116             }
117             
118             if (interceptor.InterceptedWebView != null)
119             {
120                 tlog.Debug(tag, $"http request intercepted web view is not null");
121                 if(webView2 == interceptor.InterceptedWebView)
122                 {
123                     tlog.Debug(tag, $"InterceptedWebView is webView2!");
124                 }
125                 else if(webView1 == interceptor.InterceptedWebView)
126                 {
127                     tlog.Debug(tag, $"InterceptedWebView is webView1!");
128                 }
129                 else
130                 {
131                     tlog.Debug(tag, $"InterceptedWebView is not either webView1 or webView2");
132                 }
133             }
134
135             tlog.Debug(tag, $"http request intercepted end");
136             tlog.Debug(tag, $"");
137         }
138
139         private void Callback2(WebHttpRequestInterceptor interceptor)
140         {
141             tlog.Debug(tag, $"callback2: http request intercepted start, Url: {interceptor.Url}");
142
143             //interceptor.Ignore();
144             if (interceptor.Url.Equals(invalidUrl))
145             {
146                 byte[] bData = Encoding.UTF8.GetBytes("<html><body><img src='test.jpg'></body></html>");
147                 interceptor.SetResponseStatus(200, "OK");
148                 interceptor.AddResponseHeader("Content-Type", "text/html; charset=UTF-8");
149                 interceptor.AddResponseHeader("Content-Length", bData.Length.ToString());
150                 interceptor.SetResponseBody(bData);
151                 tlog.Debug(tag, $"http request intercepted set response body end");
152             }
153             else if (interceptor.Url.Equals($"{invalidUrl}test.jpg"))
154             {
155                 string path = Tizen.Applications.Application.Current.DirectoryInfo.SharedResource + "netflix.png";
156
157                 if (interceptor.InterceptedWebView == webView2)
158                 {
159                     path = Tizen.Applications.Application.Current.DirectoryInfo.SharedResource + "hbo.png";
160                 }
161                 
162                 using (FileStream fs = File.OpenRead(path))
163                 {
164                     byte[] bData = new byte[1024];
165                     while (fs.Read(bData, 0, bData.Length) > 0)
166                     {
167                         interceptor.WriteResponseChunk(bData);
168                     }
169                     interceptor.WriteResponseChunk((byte[])null);
170                     tlog.Debug(tag, $"http request intercepted write chunk end");
171                 }
172             }
173
174             if (interceptor.InterceptedWebView != null)
175             {
176                 tlog.Debug(tag, $"http request intercepted web view is not null");
177                 if (webView2 == interceptor.InterceptedWebView)
178                 {
179                     tlog.Debug(tag, $"InterceptedWebView is webView2!");
180                 }
181                 else if (webView1 == interceptor.InterceptedWebView)
182                 {
183                     tlog.Debug(tag, $"InterceptedWebView is webView1!");
184                 }
185                 else
186                 {
187                     tlog.Debug(tag, $"InterceptedWebView is not either webView1 or webView2");
188                 }
189             }
190
191             tlog.Debug(tag, $"http request intercepted end");
192             tlog.Debug(tag, $"");
193         }
194
195         public void Deactivate()
196         {
197             btn3.Unparent();
198             btn2.Unparent();
199             btn1.Unparent();
200             webView2.Unparent();
201             webView1.Unparent();
202             root.Unparent();
203
204             btn3.Dispose();
205             btn2.Dispose();
206             btn1.Dispose();
207             webView2.Dispose();
208             webView1.Dispose();
209             root.Dispose();
210         }
211     }
212 }