[NUI] Add manual test for webview.
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Tests / Tizen.NUI.Devel.Manual.Tests / testcase / TSWebView.cs
1 /*
2  *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
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 System.Threading.Tasks;
19 using NUnit.Framework;
20 using NUnit.Framework.TUnit;
21 using Tizen.NUI;
22 using Tizen.NUI.Components;
23 using Tizen.NUI.BaseComponents;
24
25 namespace Tizen.NUI.Tests
26 {
27     [TestFixture]
28     [Description("Tizen.NUI.View test")]
29     public class WebViewTests
30     {
31         private const string tag = "NUITEST";
32         private WearableManualTestNUI _wearTestPage;
33         private TextLabel _label;
34
35         private BaseComponents.WebView _webView = null;
36         private Window _window = null;
37         private string url = $"file://{Tizen.Applications.Application.Current.DirectoryInfo.Resource}webview/index.html";
38         private string urlForContextMenu = $"file://{Tizen.Applications.Application.Current.DirectoryInfo.Resource}webview/contextmenu.html";
39         //private string urlForFormSubmit = $"file://{Tizen.Applications.Application.Current.DirectoryInfo.Resource}webview/webform.html";
40         private string urlForScrollEdge = $"file://{Tizen.Applications.Application.Current.DirectoryInfo.Resource}webview/webscrolledge.html";
41         private string urlForOpenWindow = $"file://{Tizen.Applications.Application.Current.DirectoryInfo.Resource}webview/openwindow.html";
42         private Button _menu = null;
43         private WebContextMenu _contextMenu;
44
45         [SetUp]
46         public void Init()
47         {
48             Log.Info(tag, "Preconditions for each TEST");
49             if (ManualTest.IsWearable())
50             {
51                 _label = ManualTest.CreateLabel("This case is unsuitable for wearable, please press PASS button to continue!");
52                 _wearTestPage = WearableManualTestNUI.GetInstance();
53             }
54             else
55             {
56                 _webView = new BaseComponents.WebView()
57                 {
58                     Size = new Size(1920, 540),
59                     ParentOrigin = ParentOrigin.Center,
60                     PivotPoint = PivotPoint.Center,
61                     PositionUsesPivotPoint = true,
62                 };
63                 _window = Window.Instance;
64             }
65         }
66
67         [TearDown]
68         public void Destroy()
69         {
70             Log.Info(tag, "Postconditions for each TEST, webview is being destroyed.");
71             _webView.Dispose();
72             Log.Info(tag, "Postconditions for each TEST");
73         }
74
75         private void OnContextMenuShown(object sender, WebViewContextMenuShownEventArgs e)
76         {
77             Log.Info(tag, "TUnit WebView.WebViewContextMenuShownEvent!!!!!!!!!!!!!!!!!!!!!");
78             ManualTest.Confirm();
79         }
80
81         private void OnMenuClicked(object sender, ClickedEventArgs e)
82         {
83             Log.Info(tag, "TUnit OnMenuClicked!!!!!!!!!!!!!!!!!!!!!");
84             _contextMenu.Hide();
85         }
86
87         private void OnContextMenuHidden(object sender, WebViewContextMenuHiddenEventArgs e)
88         {
89             Log.Info(tag, "TUnit WebView.WebViewContextMenuShownEvent!!!!!!!!!!!!!!!!!!!!!");
90             ManualTest.Confirm();
91         }
92
93         [Test]
94         [Category("P1")]
95         [Description("Test: Handle event. Try to long-press to trigger the event.")]
96         [Property("SPEC", "Tizen.NUI.BaseComponents.WebView.ContextMenuShown E")]
97         [Property("SPEC_URL", "-")]
98         [Property("CRITERIA", "EVL")]
99         [Property("AUTHOR", "guowei.wang@samsung.com")]
100         [Precondition(1, "In case of Emulator, only use PC keyboard or remote controller of emulator itself.")]
101         [Precondition(2, "If test on TV, prepare mouse and connect to TV.")]
102         [Step(1, "Click to runt TC.")]
103         [Step(2, "Long press image of test page.")]
104         [Step(3, "If there is no exception, test will be automatically passed.")]
105         [Postcondition(1, "NA")]
106         public async Task ContextMenuShown_CB()
107         {
108             if (ManualTest.IsWearable())
109             {
110                 _wearTestPage.ExecuteTC(_label);
111                 await ManualTest.WaitForConfirm();
112                 _wearTestPage.ClearTestCase(_label);
113             }
114             else
115             {
116                 _window.GetDefaultLayer().Add(_webView);
117                 FocusManager.Instance.SetCurrentFocusView(_webView);
118
119                 TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(false);
120                 EventHandler<WebViewPageLoadEventArgs> onLoadFinished = (s, e) =>
121                 {
122                     Log.Info(tag, "onLoadFinished is called.");
123                     tcs.TrySetResult(true);
124                 };
125                 Log.Info(tag, "onLoadFinished is called.");
126                 _webView.PageLoadFinished += onLoadFinished;
127                 _webView.ContextMenuShown += OnContextMenuShown;
128                 Log.Info(tag, "onLoadFinished is called.");
129                 _webView.LoadUrl(urlForContextMenu);
130
131                 Log.Info(tag, "onLoadFinished is called.");
132                 var result = await tcs.Task;
133                 Assert.IsTrue(result, "PageLoadFinished event should be invoked");
134
135                 Log.Info(tag, "onLoadFinished is called.");
136
137                 // Waits for user confirmation.
138                 await ManualTest.WaitForConfirm();
139                 _webView.PageLoadFinished -= onLoadFinished;
140                 _webView.ContextMenuShown -= OnContextMenuShown;
141                 _window.GetDefaultLayer().Remove(_webView);
142             }
143         }
144
145         [Test]
146         [Category("P1")]
147         [Description("Test: Handle event. Try to long-press to trigger the event.")]
148         [Property("SPEC", "Tizen.NUI.BaseComponents.WebView.ContextMenuShown E")]
149         [Property("SPEC_URL", "-")]
150         [Property("CRITERIA", "EVL")]
151         [Property("AUTHOR", "guowei.wang@samsung.com")]
152         [Precondition(1, "In case of Emulator, only use PC keyboard or remote controller of emulator itself.")]
153         [Precondition(2, "If test on TV, prepare mouse and connect to TV.")]
154         [Step(1, "Click to runt TC.")]
155         [Step(2, "Long press image of test page.")]
156         [Step(3, "If there is no exception, test will be automatically passed.")]
157         [Postcondition(1, "NA")]
158         public async Task ContextMenuHidden_CB()
159         {
160             if (ManualTest.IsWearable())
161             {
162                 _wearTestPage.ExecuteTC(_label);
163                 await ManualTest.WaitForConfirm();
164                 _wearTestPage.ClearTestCase(_label);
165             }
166             else
167             {
168                 _window.GetDefaultLayer().Add(_webView);
169                 FocusManager.Instance.SetCurrentFocusView(_webView);
170
171                 TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(false);
172                 EventHandler<WebViewPageLoadEventArgs> onLoadFinished = (s, e) =>
173                 {
174                     Log.Info(tag, "onLoadFinished is called.");
175                     tcs.TrySetResult(true);
176                 };
177                 Log.Info(tag, "onLoadFinished is called.");
178                 _webView.PageLoadFinished += onLoadFinished;
179
180                 EventHandler<WebViewContextMenuShownEventArgs> onContextMenuShown = (s, e) =>
181                 {
182                     Log.Info(tag, "onContextMenuShown is called.");
183                     if (_menu == null)
184                     {
185                         _menu = new Button();
186                         _menu.Text = "Click here to trigger menu hide event.";
187                         _menu.Feedback = false;
188                         _menu.PointSize = 20.0f;
189                         _menu.BackgroundColor = Color.Cyan;
190                         _menu.Size = new Size(Window.Instance.Size.Width * 0.1f, Window.Instance.Size.Height * 0.05f);
191                         _menu.Clicked += OnMenuClicked;
192                         _window.GetDefaultLayer().Add(_menu);
193                     }
194                     _contextMenu = e.ContextMenu;
195                 };
196                 _webView.ContextMenuShown += onContextMenuShown;
197
198                 _webView.ContextMenuHidden += OnContextMenuHidden;
199                 Log.Info(tag, "onLoadFinished is called.");
200                 _webView.LoadUrl(urlForContextMenu);
201
202                 Log.Info(tag, "onLoadFinished is called.");
203                 var result = await tcs.Task;
204                 Assert.IsTrue(result, "PageLoadFinished event should be invoked");
205
206                 Log.Info(tag, "onLoadFinished is called.");
207
208                 // Waits for user confirmation.
209                 await ManualTest.WaitForConfirm();
210                 _webView.PageLoadFinished -= onLoadFinished;
211                 _webView.ContextMenuShown -= OnContextMenuShown;
212                 _webView.ContextMenuHidden -= OnContextMenuHidden;
213                 _window.GetDefaultLayer().Remove(_webView);
214             }
215         }
216
217         [Test]
218         [Category("P1")]
219         [Description("Test: Handle event. Try to sroll to edge to trigger the event.")]
220         [Property("SPEC", "Tizen.NUI.BaseComponents.WebView.ScrollEdgeReached E")]
221         [Property("SPEC_URL", "-")]
222         [Property("CRITERIA", "EVL")]
223         [Property("AUTHOR", "guowei.wang@samsung.com")]
224         [Precondition(1, "In case of Emulator, only use PC keyboard or remote controller of emulator itself.")]
225         [Precondition(2, "If test on TV, prepare mouse and connect to TV.")]
226         [Step(1, "Click to runt TC.")]
227         [Step(2, "Scroll testing page to edge.")]
228         [Step(3, "If there is no exception, test will be automatically passed.")]
229         [Postcondition(1, "NA")]
230         public async Task ScrollEdgeReached_CB()
231         {
232             if (ManualTest.IsWearable())
233             {
234                 _wearTestPage.ExecuteTC(_label);
235                 await ManualTest.WaitForConfirm();
236                 _wearTestPage.ClearTestCase(_label);
237             }
238             else
239             {
240                 _window.GetDefaultLayer().Add(_webView);
241                 FocusManager.Instance.SetCurrentFocusView(_webView);
242
243                 TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(false);
244                 EventHandler<WebViewPageLoadEventArgs> onLoadFinished = (s, e) =>
245                 {
246                     Log.Info(tag, "onLoadFinished is called.");
247                     tcs.TrySetResult(true);
248                 };
249                 Log.Info(tag, "onLoadFinished is called.");
250                 _webView.PageLoadFinished += onLoadFinished;
251
252                 EventHandler<WebViewScrollEdgeReachedEventArgs> onScrollEdgeReached = (s, e) =>
253                 {
254                     Log.Info(tag, "TUnit WebView.ScrollEdgeReached!!!!!!!!!!!!!!!!!!!!!");
255                     ManualTest.Confirm();
256                 };
257                 _webView.ScrollEdgeReached += onScrollEdgeReached;
258                 Log.Info(tag, "onLoadFinished is called.");
259                 _webView.LoadUrl(urlForScrollEdge);
260
261                 Log.Info(tag, "onLoadFinished is called.");
262                 var result = await tcs.Task;
263                 Assert.IsTrue(result, "PageLoadFinished event should be invoked");
264
265                 Log.Info(tag, "onLoadFinished is called.");
266
267                 // Waits for user confirmation.
268                 await ManualTest.WaitForConfirm();
269                 _webView.PageLoadFinished -= onLoadFinished;
270                 _webView.ScrollEdgeReached -= onScrollEdgeReached;
271                 _window.GetDefaultLayer().Remove(_webView);
272             }
273         }
274
275         [Test]
276         [Category("P1")]
277         [Description("Test: Handle event. Try to sroll to edge to trigger the event.")]
278         [Property("SPEC", "Tizen.NUI.BaseComponents.WebView.NewWindowCreated E")]
279         [Property("SPEC_URL", "-")]
280         [Property("CRITERIA", "EVL")]
281         [Property("AUTHOR", "guowei.wang@samsung.com")]
282         [Precondition(1, "In case of Emulator, only use PC keyboard or remote controller of emulator itself.")]
283         [Precondition(2, "If test on TV, prepare mouse and connect to TV.")]
284         [Step(1, "Click to runt TC.")]
285         [Step(2, "Click button to open new window.")]
286         [Step(3, "If there is no exception, test will be automatically passed.")]
287         [Postcondition(1, "NA")]
288         public async Task NewWindowCreated_CB()
289         {
290             if (ManualTest.IsWearable())
291             {
292                 _wearTestPage.ExecuteTC(_label);
293                 await ManualTest.WaitForConfirm();
294                 _wearTestPage.ClearTestCase(_label);
295             }
296             else
297             {
298                 _window.GetDefaultLayer().Add(_webView);
299                 FocusManager.Instance.SetCurrentFocusView(_webView);
300
301                 TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(false);
302                 EventHandler<WebViewPageLoadEventArgs> onLoadFinished = (s, e) =>
303                 {
304                     Log.Info(tag, "onLoadFinished is called.");
305                     tcs.TrySetResult(true);
306                 };
307                 Log.Info(tag, "onLoadFinished is called.");
308                 _webView.PageLoadFinished += onLoadFinished;
309
310                 EventHandlerWithReturnType<object, EventArgs, BaseComponents.WebView> onNewWindowCreated = (s, e) =>
311                 {
312                     Log.Info(tag, "TUnit WebView.NewWindowCreated!!!!!!!!!!!!!!!!!!!!!");
313                     ManualTest.Confirm();
314                     return null;
315                 };
316                 _webView.NewWindowCreated += onNewWindowCreated;
317                 Log.Info(tag, "onLoadFinished is called.");
318                 _webView.LoadUrl(urlForOpenWindow);
319
320                 Log.Info(tag, "onLoadFinished is called.");
321                 var result = await tcs.Task;
322                 Assert.IsTrue(result, "PageLoadFinished event should be invoked");
323
324                 Log.Info(tag, "onLoadFinished is called.");
325
326                 // Waits for user confirmation.
327                 await ManualTest.WaitForConfirm();
328                 _webView.PageLoadFinished -= onLoadFinished;
329                 _webView.NewWindowCreated -= onNewWindowCreated;
330                 _window.GetDefaultLayer().Remove(_webView);
331             }
332         }
333
334         //TODO... FormRepostPolicyDecided event is very hard to be triggered. And this event is used rarely.
335         //[Test]
336         //[Category("P1")]
337         //[Description("Test: Handle event. Try to sroll to edge to trigger the event.")]
338         //[Property("SPEC", "Tizen.NUI.BaseComponents.WebView.FormRepostPolicyDecided E")]
339         //[Property("SPEC_URL", "-")]
340         //[Property("CRITERIA", "EVL")]
341         //[Property("AUTHOR", "guowei.wang@samsung.com")]
342         //[Precondition(1, "In case of Emulator, only use PC keyboard or remote controller of emulator itself.")]
343         //[Precondition(2, "If test on TV, prepare mouse and connect to TV.")]
344         //[Step(1, "Click to runt TC.")]
345         //[Step(2, "Click submit button.")]
346         //[Step(3, "Click submit button again to simulate form repost.")]
347         //[Step(4, "If there is no exception, test will be automatically passed.")]
348         //[Postcondition(1, "NA")]
349         //public async Task FormRepostPolicyDecided_CB()
350         //{
351         //    if (ManualTest.IsWearable())
352         //    {
353         //        _wearTestPage.ExecuteTC(_label);
354         //        await ManualTest.WaitForConfirm();
355         //        _wearTestPage.ClearTestCase(_label);
356         //    }
357         //    else
358         //    {
359         //        _window.GetDefaultLayer().Add(_webView);
360         //        FocusManager.Instance.SetCurrentFocusView(_webView);
361
362         //        TaskCompletionSource<bool> tcs1 = new TaskCompletionSource<bool>(false);
363         //        EventHandler<WebViewPageLoadEventArgs> onLoadFinished = async (s, e) =>
364         //        {
365         //            if (_webView.Url.Contains("index.html"))
366         //            {
367         //                tcs1.TrySetResult(true);
368         //            }
369         //        };
370         //        Log.Info(tag, "onLoadFinished is called.");
371         //        _webView.PageLoadFinished += onLoadFinished;
372
373         //        EventHandler<WebViewFormRepostPolicyDecidedEventArgs> onFormRepostPolicyDecided = (s, e) =>
374         //        {
375         //            Log.Info(tag, "TUnit WebView.FormRepostPolicyDecided!!!!!!!!!!!!!!!!!!!!!");
376         //            ManualTest.Confirm();
377         //        };
378         //        _webView.FormRepostPolicyDecided += onFormRepostPolicyDecided;
379         //        EventHandlerWithReturnType<object, View.TouchEventArgs, bool> onTouched = (s, e) =>
380         //        {
381         //            // Load index.html.
382         //            Log.Info(tag, "TUnit WebView.TouchEvent !!!!!!!!!!!!!!!!!!!!!");
383         //            _webView.LoadUrl(url);
384         //            return false;
385         //        };
386         //        _webView.TouchEvent += onTouched;
387         //        Log.Info(tag, "onLoadFinished is called.");
388         //        _webView.LoadUrl(urlForFormSubmit);
389
390         //        Log.Info(tag, "onLoadFinished is called.");
391         //        var result = await tcs1.Task;
392         //        Assert.IsTrue(result, "PageLoadFinished event should be invoked");
393
394         //        if (_webView.CanGoBack())
395         //        {
396         //            Log.Info(tag, $"GoBack is called. url: {_webView.Url}");
397         //            _webView.GoBack();
398         //        }
399
400         //        _webView.PageLoadFinished -= onLoadFinished;
401
402         //        // Waits for user confirmation.
403         //        await ManualTest.WaitForConfirm();
404         //        _webView.FormRepostPolicyDecided -= onFormRepostPolicyDecided;
405         //        _window.GetDefaultLayer().Remove(_webView);
406         //    }
407         //}
408     }
409 }