330b62d975c241bb0ba2c884b512c8f8a00d217a
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Tests / Tizen.NUI.Devel.Tests / testcase / internal / WebView / TSWebHttpAuthHandler.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/WebHttpAuthHandler")]
14     public class InternalWebHttpAuthHandlerTest
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 MyWebHttpAuthHandler : WebHttpAuthHandler
22         {
23             public MyWebHttpAuthHandler(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("WebHttpAuthHandler constructor.")]
47         [Property("SPEC", "Tizen.NUI.WebHttpAuthHandler.WebHttpAuthHandler C")]
48         [Property("SPEC_URL", "-")]
49         [Property("CRITERIA", "CONSTR")]
50         [Property("COVPARAM", "")]
51         [Property("AUTHOR", "guowei.wang@samsung.com")]
52         public void WebHttpAuthHandlerConstructor()
53         {
54             tlog.Debug(tag, $"WebHttpAuthHandlerConstructor START");
55
56             using (Tizen.NUI.BaseComponents.WebView webview = new Tizen.NUI.BaseComponents.WebView("Shanghai", "Asia/Shanghai"))
57             {
58                 var testingTarget = new WebHttpAuthHandler(webview.SwigCPtr.Handle, false);
59                 Assert.IsNotNull(testingTarget, "null handle");
60                 Assert.IsInstanceOf<WebHttpAuthHandler>(testingTarget, "Should return WebHttpAuthHandler instance.");
61
62                 testingTarget.Dispose();
63             }
64
65             tlog.Debug(tag, $"WebHttpAuthHandlerConstructor END (OK)");
66         }
67
68         [Test]
69         [Category("P1")]
70         [Description("WebHttpAuthHandler Realm.")]
71         [Property("SPEC", "Tizen.NUI.WebHttpAuthHandler.Realm A")]
72         [Property("SPEC_URL", "-")]
73         [Property("CRITERIA", "PRO")]
74         [Property("COVPARAM", "")]
75         [Property("AUTHOR", "guowei.wang@samsung.com")]
76         public async Task WebHttpAuthHandlerRealm()
77         {
78             tlog.Debug(tag, $"WebHttpAuthHandlerRealm START");
79
80             var testingTarget = new Tizen.NUI.BaseComponents.WebView(runtimeArgs)
81             {
82                 Size = new Size(500, 200),
83                 UserAgent = USER_AGENT
84             };
85             Assert.IsNotNull(testingTarget, "null handle");
86             Assert.IsInstanceOf<Tizen.NUI.BaseComponents.WebView>(testingTarget, "Should return Tizen.NUI.BaseComponents.WebView instance.");
87
88             testingTarget.HttpAuthRequested += OnHttpAuthRequested;
89             NUIApplication.GetDefaultWindow().Add(testingTarget);
90
91             testingTarget.LoadUrl("http://www.163.com");
92             await Task.Delay(10000);
93
94             testingTarget.ClearCache();
95             testingTarget.ClearCookies();
96             NUIApplication.GetDefaultWindow().Remove(testingTarget);
97
98             testingTarget.Dispose();
99             tlog.Debug(tag, $"WebHttpAuthHandlerRealm END (OK)");
100         }
101
102         private void OnHttpAuthRequested(object sender, WebViewHttpAuthRequestedEventArgs e)
103         {
104             tlog.Info(tag, $"HttpAuthRequested, Url: {e.HttpAuthHandler.Realm}");
105             e.HttpAuthHandler.CancelCredential();
106             e.HttpAuthHandler.UseCredential("tizen", "samsung");
107             e.HttpAuthHandler.Suspend();
108         }
109     }   
110 }