FileHistoryTest.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using PicView.Core.FileHandling;
  2. using PicView.Core.Navigation;
  3. namespace PicView.Tests;
  4. public class FileHistoryTest
  5. {
  6. [Fact]
  7. public void TestFileHistory()
  8. {
  9. var list = new List<string>();
  10. var history = new FileHistory();
  11. Assert.NotNull(history);
  12. // Check adding
  13. for (var i = 0; i <= FileHistory.MaxCount; i++)
  14. {
  15. AddRandomFiles(history, list);
  16. }
  17. Assert.Equal(FileHistory.MaxCount, history.GetCount());
  18. AddRandomFiles(history, list);
  19. Assert.Equal(FileHistory.MaxCount, history.GetCount());
  20. // Check removing
  21. history.Remove(history.GetLastFile());
  22. Assert.Equal(FileHistory.MaxCount - 1, history.GetCount());
  23. // Check renaming
  24. var lastFile = history.GetLastFile();
  25. var newFile = Path.GetFileNameWithoutExtension(lastFile);
  26. newFile = Path.GetRandomFileName();
  27. history.Rename(lastFile, newFile);
  28. Assert.Equal(newFile, history.GetLastFile());
  29. // Check removing
  30. history.Remove(newFile);
  31. Assert.False(history.Contains(newFile));
  32. // Check getting iterations
  33. var entry = history.GetEntryAt(1);
  34. Assert.NotNull(entry);
  35. var nextEntry = history.GetNextEntry(looping: true, 2, list);
  36. Assert.NotNull(nextEntry);
  37. Assert.True(File.Exists(nextEntry));
  38. var prevEntry = history.GetNextEntry(looping: false, 2, list);
  39. Assert.NotNull(prevEntry);
  40. Assert.True(File.Exists(prevEntry));
  41. foreach (var t in list)
  42. {
  43. FileDeletionHelper.DeleteFileWithErrorMsg(t, false);
  44. Assert.False(File.Exists(t));
  45. history.Remove(t);
  46. }
  47. Assert.Equal(0, history.GetCount());
  48. }
  49. private static void AddRandomFiles(FileHistory history, List<string> list)
  50. {
  51. var imageFileExtensionArray = new[] { ".jpg", ".png", ".bmp", ".gif", ".tiff", ".webp" };
  52. var path = Path.GetTempPath() + Path.GetRandomFileName();
  53. var directory = ArchiveHelper.CreateTempDirectory(path);
  54. Assert.True(directory);
  55. var randomExtension = imageFileExtensionArray[new Random().Next(0, imageFileExtensionArray.Length)];
  56. var randomFileNameWithExtension = path + randomExtension;
  57. var fullPath = Path.Combine(ArchiveHelper.TempFilePath, randomFileNameWithExtension);
  58. using var fs = File.Create(fullPath);
  59. Assert.True(File.Exists(fullPath));
  60. history.Add(randomFileNameWithExtension);
  61. list.Add(randomFileNameWithExtension);
  62. }
  63. }