OSDN Git Service

na-get-lib,all-getについてアップデート処理を新方式(NaGetUpdate2)に変更。
[applistation/AppliStation.git] / test-na-get-lib / TaskTest.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Threading;
4 using NUnit.Framework;
5
6 using NaGet.Tasks;
7
8 namespace test_na_get_lib
9 {
10         [TestFixture]
11         public class TaskTest
12         {
13                 [Test]
14                 public void Running()
15                 {
16                         ATaskForTest task = new ATaskForTest();
17                         task.isRunning = true;
18                         Assert.AreEqual(true, task.Running);
19                         task.isRunning = false;
20                         Assert.AreEqual(false, task.Running);
21                 }
22                 
23                 [Test]
24                 public void Done()
25                 {
26                         ATaskForTest task = new ATaskForTest();
27                         task.isDone = true;
28                         Assert.AreEqual(true, task.Done);
29                         task.isDone = false;
30                         Assert.AreEqual(false, task.Done);
31                 }
32                 
33                 [Test]
34                 public void Cancelable()
35                 {
36                         ATaskForTest task = new ATaskForTest();
37                         Assert.AreEqual(false, task.Cancelable);
38                 }
39                 
40                 [Test]
41                 public void Cancel()
42                 {
43                         ATaskForTest task = new ATaskForTest();
44                         Assert.AreEqual(false, task.Cancel());
45                 }
46                 
47                 [Test]
48                 public void Run()
49                 {
50                         List<TaskEventArgs> eventlist = new List<TaskEventArgs>();
51                         ATaskForTest task = new ATaskForTest();
52                         task.TaskEventRaised += delegate (object sender, TaskEventArgs e) {
53                                 Assert.AreEqual(task, sender);
54                                 eventlist.Add(e);
55                         };
56
57                         bool threadChecked = false;
58                         (new Thread(
59                                 delegate() {
60                                         Thread.Sleep(10);
61                                         Assert.AreEqual(true, task.isRunning);
62                                         threadChecked = true;
63                                 })
64                         ).Start();
65                         task.Run();
66                         Assert.AreEqual(true, threadChecked); // スレッド内のパス確認
67                         Assert.AreEqual(false, task.isRunning);
68                         Assert.AreEqual(true, task.isDone);
69                         
70                         Assert.AreEqual(TaskEventType.STARTED,          eventlist[0].Type);
71                         Assert.AreEqual(TaskEventType.PING,             eventlist[1].Type);
72                         Assert.AreEqual(TaskEventType.COMPLETED,        eventlist[2].Type);
73                         Assert.AreEqual("STARTED",              eventlist[0].TaskMessage);
74                         Assert.AreEqual("PING",         eventlist[1].TaskMessage);
75                         Assert.AreEqual("COMPLETED",    eventlist[2].TaskMessage);
76                 }
77                 
78                 #region テスト用派生クラス
79                 
80                 private class ATaskForTest : Task
81                 {
82                         internal bool isRunning = false;
83                         
84                         internal bool isDone = false;
85                         
86                         public override bool Running {
87                                 get {
88                                         return isRunning;
89                                 }
90                         }
91                         
92                         public override bool Done {
93                                 get {
94                                         return isDone;
95                                 }
96                         }
97                         
98                         public override void Run()
99                         {
100                                 isRunning = true;
101                                 RaiseTaskSetEvent(TaskEventType.STARTED, TaskEventType.STARTED.ToString(), 0);
102                                 
103                                 Thread.Sleep(10);
104                                 RaiseTaskSetEvent(TaskEventType.PING, TaskEventType.PING.ToString(), 50);
105                                 Thread.Sleep(10);
106                                 
107                                 RaiseTaskSetEvent(TaskEventType.COMPLETED, TaskEventType.COMPLETED.ToString(), 100);
108                                 isRunning = false;
109                                 isDone = true;
110                         }
111                         
112                 }
113                 
114                 #endregion
115                 
116         }
117 }