Fix code for building with corefx
[platform/core/csapi/download.git] / tct-download-tizen-tests / src / Testcase / TSRequest.cs
1 using System;
2 using System.Threading.Tasks;
3 using System.Threading;
4 using Tizen.Content.Download;
5 using System.Collections.Generic;
6 using TestFramework;
7 using System.Linq;
8 using System.Reflection;
9 using System.Collections;
10 using System.Runtime.CompilerServices;
11 using System.Diagnostics;
12 using Tizen;
13 using Tizen.Applications;
14 using Tizen.UI;
15 using System.Collections.ObjectModel;
16 using System.Collections.Specialized;
17 using System.Runtime.InteropServices;
18
19 namespace TizenTest.Download {
20             
21     [TestFixture]
22     [Description("Tizen.Content.Download.Request Tests.")]
23     public static class TSRequest {
24         public static Tizen.Content.Download.Request req;
25         public static string url;
26         public static string destinationPath;
27         public static string fileName;
28         public static Tizen.Content.Download.NetworkType type;
29         public static IDictionary<string, string> httpHeaders;
30         public static bool bFlag;
31         public static Request request;
32         public static Request testRequest;
33         public static async Task waitFlag()
34         {
35             int count = 0;
36             while (true)
37             {
38                 await Task.Delay(1000);
39                 count++;
40                 if (bFlag)
41                 {
42                          Tizen.Log.Info("Tizen.Content.Download" , "bFlag is true");break;
43                 }
44                 if (count == 30)
45                 {Tizen.Log.Info("Tizen.Content.Download" , "count is 8"); break;
46                 }
47             }
48         }
49
50         [SetUp]
51         public static void Init() {
52             LogUtils.write(LogUtils.DEBUG, LogUtils.TAG , "Preconditions for each Test");
53             url = "http://static.campaign.naver.com/0/hangeul/2011/img/img_family.gif";
54             destinationPath = "/opt/usr/";
55             fileName = "TestCaseDownload";
56             type = NetworkType.All;
57             httpHeaders = new Dictionary<string, string>();
58             httpHeaders.Add("Pragma", "no-cache");
59             bFlag = false;
60         }
61
62         [TearDown]
63         public static void Destroy() {
64             LogUtils.write(LogUtils.DEBUG, LogUtils.TAG , "Postconditions for each Test");
65             req = null;
66             url = null;
67             destinationPath = null;
68             fileName = null;
69             httpHeaders = null;
70         }
71
72         static void OnProgressChanged(object sender, ProgressChangedEventArgs e)
73         {
74                 Tizen.Log.Info("Tizen.Content.Download" , "OnProgressChanged");
75                         bFlag = true;
76         }
77
78         static void OnStateChanged(object sender, StateChangedEventArgs e)
79         {
80                         Tizen.Log.Info("Tizen.Content.Download" , "OnStateChanged"+e.State);  
81             switch (e.State)
82             {
83                 case DownloadState.None:
84                                             {
85                                                 
86                                             }
87                                             break;
88                 case DownloadState.Ready:
89                                             {
90                                                 
91                                             }
92                                             break;
93                 case DownloadState.Queued:
94                                             {
95                                                 
96                                             }
97                                             break;
98                 case DownloadState.Downloading:
99                                             {
100                                                 
101                                             }
102                                             break;
103                 case DownloadState.Paused:
104                                             {
105                                                 
106                                             }
107                                             break;
108                 case DownloadState.Completed:
109                                             {
110                                                 bFlag = true;
111                                             }
112                                             break;
113                 case DownloadState.Canceled:
114                                             {
115                                               
116                                             }
117                                             break;
118             }
119         }
120
121                 [Test]
122                 [Category("P1")]
123                 [Description("Test : Request constructor with url as parameter - Object should not be null after initialization")]
124                 [Property("SPEC", "Tizen.Content.Download.Request C")]
125                 [Property("SPEC_URL", "-")]
126                 [Property("CRITERIA", "CONSTR")]
127                 [Property("AUTHOR", "Yuvaraj S, yuvaraj.s@samsung.com")]
128                 public static void Request_INIT1() {
129                         /* TEST CODE */
130                         req = new Tizen.Content.Download.Request(url);
131                         Assert.IsNotNull(req, "Object should be not null  after init");
132                 }
133
134                 [Test]
135                 [Category("P1")]
136                 [Description("Test : Request constructor with url, destination path, file name and network type as parameters - Object should not be null after initialization")]
137                 [Property("SPEC", "Tizen.Content.Download.Request C")]
138                 [Property("SPEC_URL", "-")]
139                 [Property("CRITERIA", "CONSTR")]
140                 [Property("AUTHOR", "Yuvaraj S, yuvaraj.s@samsung.com")]
141                 public static void Request_INIT2()
142                 {
143                         /* TEST CODE */
144                         req = new Tizen.Content.Download.Request(url, destinationPath, fileName, type);
145                         Assert.IsNotNull(req, "Object should be not null  after init");
146                 }
147
148                 [Test]
149                 [Category("P1")]
150                 [Description("Test : Request constructor with url, destination path, file name, network type and header as parameters - Object should not be null after initialization")]
151                 [Property("SPEC", "Tizen.Content.Download.Request C")]
152                 [Property("SPEC_URL", "-")]
153                 [Property("CRITERIA", "CONSTR")]
154                 [Property("AUTHOR", "Yuvaraj S, yuvaraj.s@samsung.com")]
155                 public static void Request_INIT3()
156                 {
157                         /* TEST CODE */
158                         req = new Tizen.Content.Download.Request(url, destinationPath, fileName, type, httpHeaders);
159                         Assert.IsNotNull(req, "Object should be not null  after init");
160                 }
161
162                 [Test]
163                 [Category("P1")]
164                 [Description("Test : StateChanged callback")]
165                 [Property("SPEC", "Tizen.Content.Download.StateChanged E")]
166                 [Property("SPEC_URL", "-")]
167                 [Property("CRITERIA", "EVL")]
168                 [Property("AUTHOR", "Yuvaraj S, yuvaraj.s@samsung.com")]
169                 public static async Task StateChanged_CB()
170                 {
171                         /* 
172              * PRECONDITION
173              * 1. Wifi or Data Network must be connected
174              */
175                         string testUrl = "http://static.campaign.naver.com/0/hangeul/2011/img/img_family.gif";
176                         Request testRequest = new Tizen.Content.Download.Request(testUrl);
177                         testRequest.StateChanged += OnStateChanged;
178                         testRequest.Start();
179
180                         await waitFlag();
181
182                         Assert.True(bFlag, "StateChanged event should occur");
183                         Tizen.Log.Info("Tizen.Content.Download" , "TC Completed");
184                 }
185
186                 [Test]
187                 [Category("P1")]
188                 [Description("Test : ProgressChanged callback")]
189                 [Property("SPEC", "Tizen.Content.Download.ProgressChanged E")]
190                 [Property("SPEC_URL", "-")]
191                 [Property("CRITERIA", "EVL")]
192                 [Property("AUTHOR", "Yuvaraj S, yuvaraj.s@samsung.com")]
193                 public static async Task ProgressChanged_CB()
194                 {
195                         /* 
196              * PRECONDITION
197              * 1. Wifi or Data Network must be connected
198              */
199                         string testUrl = "http://static.campaign.naver.com/0/hangeul/2011/img/img_family.gif";
200                         Request testRequest = new Tizen.Content.Download.Request(testUrl);
201                         testRequest.ProgressChanged += OnProgressChanged;
202                         testRequest.Start();
203
204                         await waitFlag();
205
206                         Assert.True(bFlag, "ProgressChanged event should occur");
207                         Tizen.Log.Info("Tizen.Content.Download" , "TC Completed");
208                 }
209
210         [Test]
211         [Category("P1")]
212         [Description("Test : Request's Get Properties")]
213         [Property("SPEC", " Tizen.Content.Download.Request M")]
214         [Property("SPEC_URL", "-")]
215         [Property("CRITERIA", "MR")]
216         [Property("AUTHOR", "Yuvaraj S, yuvaraj.s@samsung.com")]
217         public static async Task Request_PROPERTIES_GET()
218         {
219             /* 
220              * PRECONDITION
221              * 1. Wifi or Data Network must be connected
222              */
223             string testUrl = "http://static.campaign.naver.com/0/hangeul/2011/img/img_family.gif";
224             testRequest = new Tizen.Content.Download.Request(testUrl);
225             Assert.IsNotNull(testRequest, "Object should not be null after initializing");
226
227                         testRequest.StateChanged += OnStateChanged;
228                     testRequest.Start();
229
230                         await waitFlag();
231                     
232                         Assert.AreEqual(testUrl, testRequest.Url, "Propety \"URL\": the set value and the get value should be equal");
233             Assert.True(bFlag, "DownloadState should be Completed");
234             Assert.IsNotNull((string)testRequest.DownloadedPath, "DownloadedPath is null");
235             Assert.IsNotNull((string)testRequest.MimeType, "MimeType is null");
236             Assert.IsNotNull((DownloadState)testRequest.State, "State is null");
237             Assert.IsNotNull((string)testRequest.ContentName, "ContentName is null");
238             Assert.IsNotNull((ulong)testRequest.ContentSize, "ContentSize is null");
239             Assert.IsNotNull((int)testRequest.HttpStatus, "HttpStatus is null");
240             Assert.IsNotNull((string)testRequest.ETagValue, "ETagValue is null");
241             Assert.IsNotNull((string)testRequest.TempFilePath, "TempFilePath is null");
242                     Tizen.Log.Info("Tizen.Content.Download" , "TC Completed");
243         }
244
245                 [Test]
246                 [Category("P1")]
247                 [Description("Test : Request's Set/Get Properties")]
248                 [Property("SPEC", " Tizen.Content.Download.Request A")]
249                 [Property("SPEC_URL", "-")]
250                 [Property("CRITERIA", "MR")]
251                 [Property("AUTHOR", "Yuvaraj S, yuvaraj.s@samsung.com")]
252                 public static void Request_PROPERTIES_SET_GET()
253                 {
254                         Request testRequest;
255                         string testUrl = "http://static.campaign.naver.com/0/hangeul/2011/img/img_family.gif";
256                         NetworkType testType = NetworkType.All;
257                         string testDestinationPath = "/home/owner/content/";
258                         string testFileName = "SampleDownloadFile";
259                         bool testAutoDownload = true;
260                         IDictionary<string, string> testHttpHeaders = new Dictionary<string,string>();
261                         testHttpHeaders.Add("Pragma","no-cache");
262
263
264                         testRequest = new Tizen.Content.Download.Request(testUrl);
265                         Assert.IsNotNull(testRequest, "Object should not be null after initializing");
266
267                         testRequest.Url = testUrl;
268                         Assert.AreEqual(testUrl, testRequest.Url, "Propety \"Url\": the set value and the get value should be equal");
269
270                         testRequest.AllowedNetworkType = testType;
271                         Assert.IsTrue(testType == testRequest.AllowedNetworkType, "Propety \"AllowedNetworkType\": the set value and the get value should be equal");            
272
273                         testRequest.DestinationPath = testDestinationPath;
274                         Assert.IsTrue(testDestinationPath == testRequest.DestinationPath, "Propety \"DestinationPath\": the set value and the get value should be equal");
275
276                         testRequest.FileName = testFileName;
277                         Assert.AreEqual(testFileName, testRequest.FileName, "Propety \"FileName\": the set value and the get value should be equal");
278
279                         testRequest.AutoDownload = testAutoDownload;
280                         Assert.IsTrue(testAutoDownload == testRequest.AutoDownload, "Propety \"AutoDownload\": the set value and the get value should be equal");
281
282                         Tizen.Log.Info("Tizen.Content.Download" , "TC Completed");
283                 }
284     }
285 }
286
287