HistoryTests.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEditor.Collaboration;
  7. using UnityEngine.TestTools;
  8. using NUnit.Framework;
  9. namespace UnityEditor.Collaboration.Tests
  10. {
  11. [TestFixture]
  12. internal class HistoryTests
  13. {
  14. private TestHistoryWindow _window;
  15. private TestRevisionsService _service;
  16. private CollabHistoryPresenter _presenter;
  17. [SetUp]
  18. public void SetUp()
  19. {
  20. _window = new TestHistoryWindow();
  21. _service = new TestRevisionsService();
  22. _presenter = new CollabHistoryPresenter(_window, new CollabHistoryItemFactory(), _service);
  23. }
  24. [TearDown]
  25. public void TearDown()
  26. {
  27. }
  28. [Test]
  29. public void CollabHistoryPresenter_OnUpdatePage__PropagatesRevisionResult()
  30. {
  31. _service.result = new RevisionsResult()
  32. {
  33. Revisions = new List<Revision>()
  34. {
  35. new Revision(authorName: "authorName", comment: "comment", revisionID: "revisionID"),
  36. }
  37. };
  38. _presenter.OnUpdatePage(0);
  39. var item = _window.items.First();
  40. Assert.AreEqual("revisionID", item.id);
  41. Assert.AreEqual("authorName", item.authorName);
  42. Assert.AreEqual("comment", item.comment);
  43. }
  44. [Test]
  45. public void CollabHistoryPresenter_OnUpdatePage__RevisionNumberingIsInOrder()
  46. {
  47. _service.result = new RevisionsResult()
  48. {
  49. RevisionsInRepo = 4,
  50. Revisions = new List<Revision>()
  51. {
  52. new Revision(revisionID: "0"),
  53. new Revision(revisionID: "1"),
  54. new Revision(revisionID: "2"),
  55. new Revision(revisionID: "3"),
  56. }
  57. };
  58. _presenter.OnUpdatePage(0);
  59. var items = _window.items.ToArray();
  60. Assert.AreEqual(4, items[0].index);
  61. Assert.AreEqual(3, items[1].index);
  62. Assert.AreEqual(2, items[2].index);
  63. Assert.AreEqual(1, items[3].index);
  64. }
  65. [Test]
  66. public void CollabHistoryPresenter_OnUpdatePage__RevisionNumberingChangesForMorePages()
  67. {
  68. _service.result = new RevisionsResult()
  69. {
  70. RevisionsInRepo = 12,
  71. Revisions = new List<Revision>()
  72. {
  73. new Revision(revisionID: "0"),
  74. new Revision(revisionID: "1"),
  75. new Revision(revisionID: "2"),
  76. new Revision(revisionID: "3"),
  77. new Revision(revisionID: "4"),
  78. }
  79. };
  80. _presenter.OnUpdatePage(1);
  81. var items = _window.items.ToArray();
  82. Assert.AreEqual(12, items[0].index);
  83. Assert.AreEqual(11, items[1].index);
  84. Assert.AreEqual(10, items[2].index);
  85. Assert.AreEqual(9, items[3].index);
  86. Assert.AreEqual(8, items[4].index);
  87. }
  88. [Test]
  89. public void CollabHistoryPresenter_OnUpdatePage__ObtainedIsCalculated()
  90. {
  91. _service.result = new RevisionsResult()
  92. {
  93. Revisions = new List<Revision>()
  94. {
  95. new Revision(isObtained: false),
  96. new Revision(isObtained: true),
  97. }
  98. };
  99. _presenter.OnUpdatePage(0);
  100. var items = _window.items.ToArray();
  101. Assert.IsFalse(items[0].obtained);
  102. Assert.IsTrue(items[1].obtained);
  103. }
  104. [Test]
  105. public void CollabHistoryPresenter_OnUpdatePage__CurrentIsCalculated()
  106. {
  107. _service.result = new RevisionsResult()
  108. {
  109. Revisions = new List<Revision>()
  110. {
  111. new Revision(revisionID: "1"),
  112. new Revision(revisionID: "2"),
  113. new Revision(revisionID: "3"),
  114. }
  115. };
  116. _service.tipRevision = "2";
  117. _presenter.OnUpdatePage(0);
  118. var items = _window.items.ToArray();
  119. Assert.AreEqual(false, items[0].current);
  120. Assert.AreEqual(true, items[1].current);
  121. Assert.AreEqual(false, items[2].current);
  122. }
  123. [Test]
  124. public void CollabHistoryPresenter_OnUpdatePage__InProgressIsCalculated()
  125. {
  126. _service.result = new RevisionsResult()
  127. {
  128. Revisions = new List<Revision>()
  129. {
  130. new Revision(revisionID: "1"),
  131. new Revision(revisionID: "2"),
  132. new Revision(revisionID: "3"),
  133. }
  134. };
  135. _window.inProgressRevision = "2";
  136. _presenter.OnUpdatePage(0);
  137. var items = _window.items.ToArray();
  138. Assert.IsFalse(items[0].inProgress);
  139. Assert.IsTrue(items[1].inProgress);
  140. Assert.IsFalse(items[2].inProgress);
  141. }
  142. [Test]
  143. public void CollabHistoryPresenter_OnUpdatePage__EnabledIsCalculated()
  144. {
  145. _service.result = new RevisionsResult()
  146. {
  147. Revisions = new List<Revision>()
  148. {
  149. new Revision(revisionID: "0"),
  150. }
  151. };
  152. _window.revisionActionsEnabled = true;
  153. _presenter.OnUpdatePage(0);
  154. var item = _window.items.First();
  155. Assert.AreEqual(true, item.enabled);
  156. }
  157. [Test]
  158. public void CollabHistoryPresenter_OnUpdatePage__DisabledIsCalculated()
  159. {
  160. _service.result = new RevisionsResult()
  161. {
  162. Revisions = new List<Revision>()
  163. {
  164. new Revision(revisionID: "0"),
  165. }
  166. };
  167. _window.revisionActionsEnabled = false;
  168. _presenter.OnUpdatePage(0);
  169. var item = _window.items.First();
  170. Assert.AreEqual(false, item.enabled);
  171. }
  172. [Test]
  173. public void CollabHistoryPresenter_OnUpdatePage__BuildStateHasNoneWhenNotTip()
  174. {
  175. _service.result = new RevisionsResult()
  176. {
  177. Revisions = new List<Revision>()
  178. {
  179. new Revision(revisionID: "1"),
  180. }
  181. };
  182. _service.tipRevision = "0";
  183. _presenter.BuildServiceEnabled = false;
  184. _presenter.OnUpdatePage(0);
  185. var item = _window.items.First();
  186. Assert.AreEqual(BuildState.None, item.buildState);
  187. }
  188. [Test]
  189. public void CollabHistoryPresenter_OnUpdatePage__BuildStateTipHasNoneWhenEnabled()
  190. {
  191. _service.result = new RevisionsResult()
  192. {
  193. Revisions = new List<Revision>()
  194. {
  195. new Revision(revisionID: "0"),
  196. }
  197. };
  198. _service.tipRevision = "0";
  199. _presenter.BuildServiceEnabled = true;
  200. _presenter.OnUpdatePage(0);
  201. var item = _window.items.First();
  202. Assert.AreEqual(BuildState.None, item.buildState);
  203. }
  204. [Test]
  205. public void CollabHistoryPresenter_OnUpdatePage__BuildStateHasConfigureWhenTip()
  206. {
  207. _service.result = new RevisionsResult()
  208. {
  209. Revisions = new List<Revision>()
  210. {
  211. new Revision(revisionID: "0"),
  212. }
  213. };
  214. _service.tipRevision = "0";
  215. _presenter.BuildServiceEnabled = false;
  216. _presenter.OnUpdatePage(0);
  217. var item = _window.items.First();
  218. Assert.AreEqual(BuildState.Configure, item.buildState);
  219. }
  220. [Test]
  221. public void CollabHistoryPresenter_OnUpdatePage__BuildStateHasConfigureWhenZeroBuildStatus()
  222. {
  223. _service.result = new RevisionsResult()
  224. {
  225. Revisions = new List<Revision>()
  226. {
  227. new Revision(revisionID: "0"),
  228. }
  229. };
  230. _service.tipRevision = "0";
  231. _presenter.BuildServiceEnabled = false;
  232. _presenter.OnUpdatePage(0);
  233. var item = _window.items.First();
  234. Assert.AreEqual(BuildState.Configure, item.buildState);
  235. }
  236. [Test]
  237. public void CollabHistoryPresenter_OnUpdatePage__BuildStateHasNoneWhenZeroBuildStatuses()
  238. {
  239. _service.result = new RevisionsResult()
  240. {
  241. Revisions = new List<Revision>()
  242. {
  243. new Revision(revisionID: "0"),
  244. }
  245. };
  246. _service.tipRevision = "0";
  247. _presenter.BuildServiceEnabled = true;
  248. _presenter.OnUpdatePage(0);
  249. var item = _window.items.First();
  250. Assert.AreEqual(BuildState.None, item.buildState);
  251. }
  252. [Test]
  253. public void CollabHistoryPresenter_OnUpdatePage__BuildStateHasSuccessWhenCompleteAndSucceeded()
  254. {
  255. _service.result = new RevisionsResult()
  256. {
  257. Revisions = new List<Revision>()
  258. {
  259. new Revision
  260. (
  261. revisionID: "0",
  262. buildStatuses: new CloudBuildStatus[1]
  263. {
  264. new CloudBuildStatus(complete: true, success: true),
  265. }
  266. ),
  267. }
  268. };
  269. _service.tipRevision = "0";
  270. _presenter.BuildServiceEnabled = true;
  271. _presenter.OnUpdatePage(0);
  272. var item = _window.items.First();
  273. Assert.AreEqual(BuildState.Success, item.buildState);
  274. }
  275. [Test]
  276. public void CollabHistoryPresenter_OnUpdatePage__BuildStateHasInProgress()
  277. {
  278. _service.result = new RevisionsResult()
  279. {
  280. Revisions = new List<Revision>()
  281. {
  282. new Revision
  283. (
  284. revisionID: "0",
  285. buildStatuses: new CloudBuildStatus[1]
  286. {
  287. new CloudBuildStatus(complete: false),
  288. }
  289. ),
  290. }
  291. };
  292. _service.tipRevision = "0";
  293. _presenter.BuildServiceEnabled = true;
  294. _presenter.OnUpdatePage(0);
  295. var item = _window.items.First();
  296. Assert.AreEqual(BuildState.InProgress, item.buildState);
  297. }
  298. [Test]
  299. public void CollabHistoryPresenter_OnUpdatePage__BuildStateHasFailure()
  300. {
  301. _service.result = new RevisionsResult()
  302. {
  303. Revisions = new List<Revision>()
  304. {
  305. new Revision
  306. (
  307. revisionID: "0",
  308. buildStatuses: new CloudBuildStatus[1]
  309. {
  310. new CloudBuildStatus(complete: true, success: false),
  311. }
  312. ),
  313. }
  314. };
  315. _service.tipRevision = "0";
  316. _presenter.BuildServiceEnabled = true;
  317. _presenter.OnUpdatePage(0);
  318. var item = _window.items.First();
  319. Assert.AreEqual(BuildState.Failed, item.buildState);
  320. }
  321. [Test]
  322. public void CollabHistoryPresenter_OnUpdatePage__BuildStateHasFailureWhenAnyBuildsFail()
  323. {
  324. _service.result = new RevisionsResult()
  325. {
  326. Revisions = new List<Revision>()
  327. {
  328. new Revision
  329. (
  330. revisionID: "0",
  331. buildStatuses: new CloudBuildStatus[3]
  332. {
  333. new CloudBuildStatus(complete: true, success: false),
  334. new CloudBuildStatus(complete: true, success: false),
  335. new CloudBuildStatus(complete: true, success: true),
  336. }
  337. ),
  338. }
  339. };
  340. _service.tipRevision = "0";
  341. _presenter.BuildServiceEnabled = true;
  342. _presenter.OnUpdatePage(0);
  343. var item = _window.items.First();
  344. Assert.AreEqual(BuildState.Failed, item.buildState);
  345. }
  346. [Test]
  347. public void CollabHistoryPresenter_OnUpdatePage__ChangesPropagateThrough()
  348. {
  349. _service.result = new RevisionsResult()
  350. {
  351. Revisions = new List<Revision>()
  352. {
  353. new Revision(revisionID: "0", entries: GenerateChangeActions(3)),
  354. }
  355. };
  356. _presenter.OnUpdatePage(0);
  357. var item = _window.items.First();
  358. var changes = item.changes.ToList();
  359. Assert.AreEqual("Path0", changes[0].path);
  360. Assert.AreEqual("Path1", changes[1].path);
  361. Assert.AreEqual("Path2", changes[2].path);
  362. }
  363. [Test]
  364. public void CollabHistoryPresenter_OnUpdatePage__ChangesTotalIsCalculated()
  365. {
  366. _service.result = new RevisionsResult()
  367. {
  368. Revisions = new List<Revision>()
  369. {
  370. new Revision(revisionID: "0", entries: GenerateChangeActions(3)),
  371. }
  372. };
  373. _presenter.OnUpdatePage(0);
  374. var item = _window.items.First();
  375. Assert.AreEqual(3, item.changes.Count);
  376. }
  377. [Test]
  378. public void CollabHistoryPresenter_OnUpdatePage__ChangesTruncatedIsCalculated()
  379. {
  380. for (var i = 0; i < 20; i++)
  381. {
  382. _service.result = new RevisionsResult()
  383. {
  384. Revisions = new List<Revision>()
  385. {
  386. new Revision(revisionID: "0", entries: GenerateChangeActions(i)),
  387. }
  388. };
  389. _presenter.OnUpdatePage(0);
  390. var item = _window.items.First();
  391. Assert.AreEqual(i > 10, item.changesTruncated);
  392. }
  393. }
  394. [Test]
  395. public void CollabHistoryPresenter_OnUpdatePage__OnlyKeeps10ChangeActions()
  396. {
  397. _service.result = new RevisionsResult()
  398. {
  399. Revisions = new List<Revision>()
  400. {
  401. new Revision(authorName: "Test", author: "test", entries: GenerateChangeActions(12)),
  402. }
  403. };
  404. _presenter.OnUpdatePage(1);
  405. var item = _window.items.First();
  406. Assert.AreEqual(10, item.changes.Count);
  407. Assert.AreEqual(12, item.changesTotal);
  408. Assert.AreEqual(true, item.changesTruncated);
  409. }
  410. [Test]
  411. public void CollabHistoryPresenter_OnUpdatePage__DeduplicatesMetaFiles()
  412. {
  413. _service.result = new RevisionsResult()
  414. {
  415. Revisions = new List<Revision>()
  416. {
  417. new Revision
  418. (
  419. authorName: "Test",
  420. author: "test",
  421. revisionID: "",
  422. entries: new ChangeAction[2]
  423. {
  424. new ChangeAction(path: "Path1", action: "Action1"),
  425. new ChangeAction(path: "Path1.meta", action: "Action1"),
  426. }
  427. ),
  428. }
  429. };
  430. _presenter.OnUpdatePage(1);
  431. var item = _window.items.First();
  432. Assert.AreEqual(1, item.changes.Count);
  433. Assert.AreEqual(1, item.changesTotal);
  434. Assert.AreEqual("Path1", item.changes.First().path);
  435. }
  436. [Test]
  437. public void CollabHistoryPresenter_OnUpdatePage__FolderMetaFilesAreCounted()
  438. {
  439. _service.result = new RevisionsResult()
  440. {
  441. Revisions = new List<Revision>()
  442. {
  443. new Revision
  444. (
  445. authorName: "Test",
  446. author: "test",
  447. entries: new ChangeAction[1]
  448. {
  449. new ChangeAction(path: "Folder1.meta", action: "Action1"),
  450. }
  451. ),
  452. }
  453. };
  454. _presenter.OnUpdatePage(1);
  455. var item = _window.items.First();
  456. Assert.AreEqual(1, item.changes.Count);
  457. Assert.AreEqual(1, item.changesTotal);
  458. Assert.AreEqual("Folder1", item.changes.First().path);
  459. }
  460. private static ChangeAction[] GenerateChangeActions(int count)
  461. {
  462. var entries = new ChangeAction[count];
  463. for (var i = 0; i < count; i++)
  464. entries[i] = new ChangeAction(path: "Path" + i, action: "Action" + i);
  465. return entries;
  466. }
  467. }
  468. internal class TestRevisionsService : IRevisionsService
  469. {
  470. public RevisionsResult result;
  471. public event RevisionsDelegate FetchRevisionsCallback;
  472. public string tipRevision { get; set; }
  473. public string currentUser { get; set; }
  474. public void GetRevisions(int offset, int count)
  475. {
  476. if(FetchRevisionsCallback != null)
  477. {
  478. FetchRevisionsCallback(result);
  479. }
  480. }
  481. }
  482. internal class TestHistoryWindow : ICollabHistoryWindow
  483. {
  484. public IEnumerable<RevisionData> items;
  485. public bool revisionActionsEnabled { get; set; }
  486. public int itemsPerPage { get; set; }
  487. public string errMessage { get; set; }
  488. public string inProgressRevision { get; set; }
  489. public PageChangeAction OnPageChangeAction { get; set; }
  490. public RevisionAction OnGoBackAction { get; set; }
  491. public RevisionAction OnUpdateAction { get; set; }
  492. public RevisionAction OnRestoreAction { get; set; }
  493. public ShowBuildAction OnShowBuildAction { get; set; }
  494. public Action OnShowServicesAction { get; set; }
  495. public void UpdateState(HistoryState state, bool force)
  496. {
  497. }
  498. public void UpdateRevisions(IEnumerable<RevisionData> items, string tip, int totalRevisions, int currPage)
  499. {
  500. this.items = items;
  501. }
  502. }
  503. }