[NUI] Fix ImageView Dispose crash (#2933)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.WebViewTest / WebViewApp.cs
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 using System;
18 using Tizen.NUI.BaseComponents;
19
20 namespace Tizen.NUI.WebViewTest
21 {
22     public class WebViewApplication : NUIApplication
23     {
24         private WebView simpleWebView = null;
25         private TextField addressBar = null;
26
27         private int index = 0;
28         private const int WEBSITES_COUNT = 93;
29         private string[] websites = 
30         {
31             "http://www.baidu.com","http://www.Google.com","http://www.Facebook.com","http://www.Youtube.com","http://www.Yahoo.com",
32             "http://www.Amazon.com","http://www.Wikipedia.org","http://www.Google.co.in","http://www.Qq.com","http://www.Twitter.com",
33             "http://www.Live.com","http://www.Taobao.com","http://www.Msn.com","http://www.Yahoo.co.jp","http://www.Linkedin.com",
34             "http://www.Google.co.jp","http://www.Sina.com.cn","http://www.Bing.com","http://www.Weibo.com","http://www.Yandex.ru",
35             "http://www.Vk.com","http://www.Instagram.com","http://www.Hao123.com","http://www.Ebay.com","http://www.Google.de",
36             "http://www.Amazon.co.jp","http://www.Mail.ru","http://www.Google.co.uk","http://www.Google.ru","http://www.Pinterest.com",
37             "http://www.360.cn","http://www.T.co","http://www.Reddit.com","http://www.Google.com.br","http://www.Netflix.com",
38             "http://www.Tmall.com","http://www.Google.fr","http://www.Paypal.com","http://www.Microsoft.com","http://www.Wordpress.com",
39             "http://www.Sohu.com","http://www.Blogspot.com","http://www.Google.it","http://www.Google.es","http://www.Onclickads.net",
40             "http://www.Tumblr.com","http://www.Imgur.com","http://www.Gmw.cn","http://www.Ok.ru","http://www.Aliexpress.com",
41             "http://www.Apple.com","http://www.Imdb.com","http://www.Stackoverflow.com","http://www.Fc2.com","http://www.Google.com.mx",
42             "http://www.Ask.com","http://www.Amazon.de","http://www.Google.com.hk","http://www.Google.com.tr","http://www.Alibaba.com",
43             "http://www.Google.ca","http://www.Office.com","http://www.Rakuten.co.jp","http://www.Google.co.id","http://www.Tianya.cn",
44             "http://www.Xinhuanet.com","http://www.Github.com","http://www.Craigslist.org","http://www.Nicovideo.jp","http://www.Soso.com",
45             "http://www.Amazon.co.uk","http://www.Amazon.in","http://www.Blogger.com","http://www.Kat.cr","http://www.Outbrain.com",
46             "http://www.Pixnet.net","http://www.Cnn.com","http://www.Go.com","http://www.Google.pl","http://www.Dropbox.com",
47             "http://www.Google.com.au","http://www.360.com","http://www.Haosou.com","http://www.Naver.com","http://www.Jd.com",
48             "http://www.Adobe.com","http://www.Flipkart.com","http://www.Whatsapp.com","http://www.Nytimes.com","http://www.Coccoc.com",
49             "http://www.Chase.com","http://www.Chinadaily.com.cn","http://www.bbc.co.uk"
50         };
51
52         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";
53
54         private const int ADDRESSBAR_HEIGHT = 100;
55
56         private const int SCREEN_WIDTH = 1920;
57         private const int SCREEN_HEIGHT = 1080;
58
59         private const int MIN_WEBVIEW_WIDTH = 1000;
60         private const int MIN_WEBVIEW_HEIGHT = 600;
61
62         private const int WEBVIEW_WIDTH = SCREEN_WIDTH;
63         private const int WEBVIEW_HEIGHT = SCREEN_HEIGHT - ADDRESSBAR_HEIGHT;
64
65         private int blueKeyPressedCount = 0;
66         private int yellowKeyPressedCount = 0;
67
68         private static long startTime = 0;
69
70         private TextLabel messageLabel = null;
71         private Timer messageTimer = null;
72
73         protected override void OnCreate()
74         {
75             base.OnCreate();
76
77             GetDefaultWindow().BackgroundColor = new Color((float)189 /255, (float)179 /255, (float)204 /255, 1.0f);
78             //EnvironmentVariable.SetEnvironmentVariable("DALI_WEB_ENGINE_NAME", "lwe");
79
80             addressBar = new TextField()
81             {
82                 BackgroundColor = Color.White,
83                 Size = new Size(SCREEN_WIDTH, ADDRESSBAR_HEIGHT),
84                 EnableGrabHandlePopup = false,
85                 EnableGrabHandle = false,
86                 EnableSelection = true,
87                 Focusable = true,
88                 PlaceholderText = "Please input url here like Www.baidu.com.",
89             };
90             addressBar.FocusGained += OnTextEditorFocusGained;
91             addressBar.FocusLost += OnTextEditorFocusLost;
92             addressBar.KeyEvent += OnAddressBarKeyEvent;
93             addressBar.TouchEvent += OnAddressBarTouchEvent;
94             GetDefaultWindow().Add(addressBar);
95
96             simpleWebView = new WebView()
97             {
98                 Position = new Position(0, ADDRESSBAR_HEIGHT),
99                 Size = new Size(WEBVIEW_WIDTH, WEBVIEW_HEIGHT),
100                 UserAgent = USER_AGENT,
101                 Focusable = true,
102             };
103             simpleWebView.FocusGained += OnWebViewFocusGained;
104             simpleWebView.FocusLost += OnWebViewFocusLost;
105             simpleWebView.KeyEvent += OnWebViewKeyEvent;
106             simpleWebView.TouchEvent += OnWebViewTouchEvent;
107             simpleWebView.PageLoadStarted += OnPageLoadStarted;
108             simpleWebView.ScrollEdgeReached += OnScrollEdgeReached;
109             GetDefaultWindow().Add(simpleWebView);
110
111             GetDefaultWindow().KeyEvent += Instance_KeyEvent;
112             simpleWebView.LoadUrl(websites[index]);
113             FocusManager.Instance.SetCurrentFocusView(simpleWebView);
114
115             messageTimer = new Timer(10000);
116             messageTimer.Tick += OnTick;
117         }
118
119         protected override void OnTerminate()
120         {
121             GetDefaultWindow().Remove(simpleWebView);
122             GetDefaultWindow().Remove(addressBar);
123
124             messageTimer.Tick -= OnTick;
125             messageTimer.Dispose();
126             messageTimer = null;
127
128             base.OnTerminate();
129         }
130
131         private bool OnTick(object sender, EventArgs e)
132         {
133             GetDefaultWindow().Remove(messageLabel);
134             messageLabel.Dispose();
135             messageLabel = null;
136             return false;
137         }
138
139         private bool OnWebViewTouchEvent(object source, View.TouchEventArgs args)
140         {
141             if (!simpleWebView.HasFocus())
142             {
143                 FocusManager.Instance.SetCurrentFocusView(simpleWebView);
144             }
145             return false;
146         }
147
148         private void OnScrollEdgeReached(object sender, WebViewScrollEdgeReachedEventArgs e)
149         {
150             Log.Info("WebView", $"------------scroll edge reached: {e.ScrollEdge}-------");
151         }
152
153         private void OnPageLoadStarted(object sender, WebViewPageLoadEventArgs e)
154         {
155             Log.Info("WebView", $"------------web view start to load time: {DateTime.Now.Ticks - startTime}-------");
156         }
157
158         private void OnWebViewFocusGained(object source, EventArgs args)
159         {
160             Log.Info("WebView", $"------------web view focus is gained-------");
161         }
162
163         private void OnWebViewFocusLost(object source, EventArgs args)
164         {
165             Log.Info("WebView", $"------------web view focus is lost-------");
166         }
167
168         private bool OnWebViewKeyEvent(object source, View.KeyEventArgs args)
169         {
170             bool result = false;
171
172             Log.Info("WebView", $"----web view key is {args.Key.KeyPressedName}, state is {args.Key.State}-------");
173
174             if (args.Key.State == Key.StateType.Up)
175             {
176                 if (args.Key.KeyPressedName == "XF86RaiseChannel")
177                 {
178                     Log.Info("WebView", $"old url is {simpleWebView.Url}.");
179                     if (index != 0)
180                     {
181                         simpleWebView.Url = websites[--index].ToLowerInvariant();
182                     }
183                     else
184                     {
185                         simpleWebView.Url = websites[WEBSITES_COUNT - 1].ToLowerInvariant();
186                     }
187                     result = true;
188                     Log.Info("WebView", $"new url is {simpleWebView.Url}.");
189                 }
190                 else if (args.Key.KeyPressedName == "XF86LowerChannel")
191                 {
192                     Log.Info("WebView", $"old url is {simpleWebView.Url}.");
193                     if (index != WEBSITES_COUNT - 1)
194                     {
195                         simpleWebView.Url = websites[++index].ToLowerInvariant();
196                     }
197                     else
198                     {
199                         simpleWebView.Url = websites[0].ToLowerInvariant();
200                     }
201                     result = true;
202                     Log.Info("WebView", $"new url is {simpleWebView.Url}.");
203                 }
204                 else if (args.Key.KeyPressedName == "XF86Back")
205                 {
206                     simpleWebView.GoBack();
207                     result = true;
208                 }
209                 else if (args.Key.KeyPressedName == "XF86Red")
210                 {
211                     FocusManager.Instance.SetCurrentFocusView(addressBar);
212                     result = true;
213                 }
214                 else if (args.Key.KeyPressedName == "XF86Blue")
215                 {
216                     blueKeyPressedCount++;
217                     if (blueKeyPressedCount % 6 == 0)
218                     {
219                         simpleWebView.Position = new Position(0, ADDRESSBAR_HEIGHT);
220                         simpleWebView.Orientation = new Rotation(new Radian(new Degree(60 * blueKeyPressedCount)), Vector3.ZAxis);
221                         blueKeyPressedCount = 0;
222                     }
223                     else
224                     {
225                         simpleWebView.Orientation = new Rotation(new Radian(new Degree(60 * blueKeyPressedCount)), Vector3.ZAxis);
226                     }
227                     result = true;
228                 }
229                 else if (args.Key.KeyPressedName == "XF86Yellow")
230                 {
231                     yellowKeyPressedCount++;
232                     int wdistance = (SCREEN_WIDTH - MIN_WEBVIEW_WIDTH) / 5;
233                     int hdistance = (SCREEN_HEIGHT - MIN_WEBVIEW_HEIGHT - ADDRESSBAR_HEIGHT) / 5;
234                     simpleWebView.Position = new Position((SCREEN_WIDTH - MIN_WEBVIEW_WIDTH - yellowKeyPressedCount * wdistance) / 2, ADDRESSBAR_HEIGHT);
235                     simpleWebView.Size = new Size(MIN_WEBVIEW_WIDTH + yellowKeyPressedCount * wdistance, MIN_WEBVIEW_HEIGHT + hdistance * yellowKeyPressedCount);
236                     if (yellowKeyPressedCount % 5 == 0)
237                     {
238                         yellowKeyPressedCount = 0;
239                     }
240                     result = true;
241                 }
242                 else if (args.Key.KeyPressedName == "XF86Green")
243                 {
244                     if (messageLabel != null)
245                         return result;
246
247                     Log.Info("WebView", $"key XF86Green is pressed.");
248
249                     simpleWebView.ScrollPosition = new Position(0, 200);
250                     simpleWebView.ScrollBy(0, 50);
251                     Log.Info("WebView", $"scroll position is ({simpleWebView.ScrollPosition.X}, {simpleWebView.ScrollPosition.Y}).");
252                     Log.Info("WebView", $"scroll size is ({simpleWebView.ScrollSize.Width}, {simpleWebView.ScrollSize.Height}).");
253                     Log.Info("WebView", $"content size is ({simpleWebView.ContentSize.Width}, {simpleWebView.ContentSize.Height}).");
254
255                     result = true;
256                 }
257             }
258
259             return result;
260         }
261
262         private bool OnAddressBarTouchEvent(object source, View.TouchEventArgs args)
263         {
264             FocusManager.Instance.SetCurrentFocusView(addressBar);
265             return false;
266         }
267
268         private void OnTextEditorFocusGained(object source, EventArgs args)
269         {
270             Log.Info("WebView", $"------------address bar focus is gained-------");
271
272             addressBar.Text = "";
273
274             addressBar.GetInputMethodContext().Activate();
275             addressBar.GetInputMethodContext().ShowInputPanel();
276         }
277
278         private void OnTextEditorFocusLost(object source, EventArgs args)
279         {
280             Log.Info("WebView", $"------------address bar focus lost-------");
281         }
282
283         private bool OnAddressBarKeyEvent(object source, View.KeyEventArgs args)
284         {
285             Log.Info("WebView", $"------------address bar key is {args.Key.KeyPressedName}-------");
286
287             if (args.Key.State == Key.StateType.Up)
288             {
289                 if (args.Key.KeyPressedName == "Select")
290                 {
291                     Log.Info("WebView", $"------------address bar text is {addressBar.Text}-------");
292
293                     if (addressBar.Text.Length > 0)
294                     {
295                         addressBar.GetInputMethodContext().HideInputPanel();
296                         addressBar.GetInputMethodContext().Deactivate();
297
298                         simpleWebView.Url = $"http://{addressBar.Text.ToLowerInvariant()}";
299
300                         // set focus to webview.
301                         FocusManager.Instance.SetCurrentFocusView(simpleWebView);
302                     }
303                 }
304                 else if (args.Key.KeyPressedName == "BackSpace")
305                 {
306                     if (addressBar.Text.Length > 0)
307                     {
308                         addressBar.Text = addressBar.Text.Substring(0, addressBar.Text.Length - 1);
309                     }
310                 }
311                 else if (args.Key.KeyPressedName == "XF86Red")
312                 {
313                     FocusManager.Instance.SetCurrentFocusView(simpleWebView);
314                 }
315             }
316
317             return true;
318         }
319
320         private void Instance_KeyEvent(object sender, Window.KeyEventArgs args)
321         {
322             if (args.Key.State == Key.StateType.Up)
323             {
324                 Log.Info("WebView", $"window key is {args.Key.KeyPressedName}.");
325             }
326         }
327
328         [STAThread]
329         static void Main(string[] args)
330         {
331             startTime = DateTime.Now.Ticks;
332             Log.Info("WebView", $"------------web view start time: {startTime}-------");
333             new WebViewApplication().Run(args);
334         }
335     }
336 }
337