mirror of
https://github.com/cyanfish/naps2.git
synced 2024-11-13 06:27:11 +03:00
Add more TwainScan tests
This commit is contained in:
parent
42f9e892f1
commit
a59019eb87
@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using NAPS2.Images.Storage;
|
||||
|
||||
namespace NAPS2.Sdk.Tests
|
||||
{
|
||||
@ -9,8 +10,9 @@ namespace NAPS2.Sdk.Tests
|
||||
{
|
||||
public FileSystemTests()
|
||||
{
|
||||
FolderPath = $"temp_{Path.GetRandomFileName()}";
|
||||
FolderPath = $"naps2_test_temp_{Path.GetRandomFileName()}";
|
||||
Folder = Directory.CreateDirectory(FolderPath);
|
||||
FileStorageManager.Current = new FileStorageManager(FolderPath);
|
||||
}
|
||||
|
||||
public string FolderPath { get; }
|
||||
|
@ -20,8 +20,8 @@ namespace NAPS2.Sdk.Tests.Util
|
||||
var obj = reader.ReadPemObject();
|
||||
Assert.Equal("CERTIFICATE", obj.Type);
|
||||
var certObj = new X509Certificate(obj.Content);
|
||||
Assert.Equal("CN=naps2", certObj.Issuer);
|
||||
Assert.Equal("CN=naps2", certObj.Subject);
|
||||
Assert.Equal("CN=localhost", certObj.Issuer);
|
||||
Assert.Equal("CN=localhost", certObj.Subject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Grpc.Core;
|
||||
@ -19,7 +18,7 @@ using Xunit;
|
||||
|
||||
namespace NAPS2.Sdk.Tests.Worker
|
||||
{
|
||||
public class WorkerChannelTests
|
||||
public class WorkerChannelTests : FileSystemTests
|
||||
{
|
||||
private Channel Start(ITwainWrapper twainWrapper = null, ThumbnailRenderer thumbnailRenderer = null, IMapiWrapper mapiWrapper = null, ServerCredentials serverCreds = null, ChannelCredentials clientCreds = null)
|
||||
{
|
||||
@ -54,17 +53,9 @@ namespace NAPS2.Sdk.Tests.Worker
|
||||
{
|
||||
using (var channel = Start())
|
||||
{
|
||||
var fsm = FileStorageManager.Current;
|
||||
try
|
||||
{
|
||||
channel.Client.Init(@"C:\Somewhere");
|
||||
Assert.IsType<RecoveryStorageManager>(FileStorageManager.Current);
|
||||
Assert.StartsWith(@"C:\Somewhere", ((RecoveryStorageManager)FileStorageManager.Current).RecoveryFolderPath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
FileStorageManager.Current = fsm;
|
||||
}
|
||||
channel.Client.Init(@"C:\Somewhere");
|
||||
Assert.IsType<RecoveryStorageManager>(FileStorageManager.Current);
|
||||
Assert.StartsWith(@"C:\Somewhere", ((RecoveryStorageManager)FileStorageManager.Current).RecoveryFolderPath);
|
||||
}
|
||||
}
|
||||
|
||||
@ -96,35 +87,72 @@ namespace NAPS2.Sdk.Tests.Worker
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TwainScan()
|
||||
public async Task TwainScanWithMemoryStorage()
|
||||
{
|
||||
// TODO: More tests, verifying correct serialization and using RecoveryStorageManager
|
||||
// TODO: Also check correct error handling
|
||||
StorageManager.ConfigureBackingStorage<FileStorage>();
|
||||
StorageManager.ConfigureBackingStorage<GdiImage>();
|
||||
try
|
||||
{
|
||||
var twainWrapper = new TwainWrapperMockScanner
|
||||
{
|
||||
Images = new List<ScannedImage>
|
||||
{
|
||||
CreateScannedImage(),
|
||||
CreateScannedImage()
|
||||
}
|
||||
};
|
||||
|
||||
using (var channel = Start(twainWrapper))
|
||||
{
|
||||
var receivedImages = new List<ScannedImage>();
|
||||
await channel.Client.TwainScan(new ScanDevice("test_id", "test_name"), new ScanProfile(), new ScanParams(), IntPtr.Zero,
|
||||
CancellationToken.None,
|
||||
(img, path) => { receivedImages.Add(img); });
|
||||
|
||||
Assert.Equal(2, receivedImages.Count);
|
||||
}
|
||||
await TwainScanInternalTest();
|
||||
}
|
||||
finally
|
||||
{
|
||||
StorageManager.ConfigureBackingStorage<GdiImage>();
|
||||
StorageManager.ConfigureBackingStorage<IStorage>();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TwainScanWithFileStorage()
|
||||
{
|
||||
StorageManager.ConfigureBackingStorage<FileStorage>();
|
||||
try
|
||||
{
|
||||
await TwainScanInternalTest();
|
||||
}
|
||||
finally
|
||||
{
|
||||
StorageManager.ConfigureBackingStorage<IStorage>();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TwainScanWithRecovery()
|
||||
{
|
||||
StorageManager.ConfigureBackingStorage<FileStorage>();
|
||||
var rsm = new RecoveryStorageManager(FolderPath);
|
||||
StorageManager.ImageMetadataFactory = rsm;
|
||||
FileStorageManager.Current = rsm;
|
||||
try
|
||||
{
|
||||
await TwainScanInternalTest();
|
||||
}
|
||||
finally
|
||||
{
|
||||
// TODO: Set all static things in a base class constructor so we don't have any fragility here
|
||||
StorageManager.ConfigureBackingStorage<IStorage>();
|
||||
StorageManager.ImageMetadataFactory = new StubImageMetadataFactory();
|
||||
rsm.ForceReleaseLock();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task TwainScanInternalTest()
|
||||
{
|
||||
var twainWrapper = new TwainWrapperMockScanner
|
||||
{
|
||||
Images = new List<ScannedImage>
|
||||
{
|
||||
CreateScannedImage(),
|
||||
CreateScannedImage()
|
||||
}
|
||||
};
|
||||
|
||||
using (var channel = Start(twainWrapper))
|
||||
{
|
||||
var receivedImages = new List<ScannedImage>();
|
||||
await channel.Client.TwainScan(new ScanDevice("test_id", "test_name"), new ScanProfile(), new ScanParams(), IntPtr.Zero,
|
||||
CancellationToken.None,
|
||||
(img, path) => { receivedImages.Add(img); });
|
||||
|
||||
Assert.Equal(2, receivedImages.Count);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -15,6 +15,17 @@ namespace NAPS2.Images.Storage
|
||||
set => _current = value ?? throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
public virtual string NextFilePath() => Path.Combine(Paths.Temp, Path.GetRandomFileName());
|
||||
public FileStorageManager() : this(Paths.Temp)
|
||||
{
|
||||
}
|
||||
|
||||
public FileStorageManager(string folderPath)
|
||||
{
|
||||
FolderPath = folderPath;
|
||||
}
|
||||
|
||||
protected string FolderPath { get; }
|
||||
|
||||
public virtual string NextFilePath() => Path.Combine(FolderPath, Path.GetRandomFileName());
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ namespace NAPS2.Images.Storage
|
||||
this.rsm = rsm;
|
||||
// TODO: Maybe not a constructor param?
|
||||
this.indexImage = indexImage;
|
||||
rsm.Index.Images.Add(indexImage);
|
||||
}
|
||||
|
||||
public List<Transform> TransformList
|
||||
|
@ -20,13 +20,12 @@ namespace NAPS2.Images.Storage
|
||||
private Stream folderLock;
|
||||
private ConfigManager<RecoveryIndex> indexConfigManager;
|
||||
|
||||
public RecoveryStorageManager(string recoveryFolderPath, bool skipCreate = false)
|
||||
public RecoveryStorageManager(string recoveryFolderPath, bool skipCreate = false) : base(recoveryFolderPath)
|
||||
{
|
||||
RecoveryFolderPath = recoveryFolderPath;
|
||||
folderCreated = skipCreate;
|
||||
}
|
||||
|
||||
public string RecoveryFolderPath { get; }
|
||||
public string RecoveryFolderPath => FolderPath;
|
||||
|
||||
public bool DisableRecoveryCleanup { get; set; }
|
||||
|
||||
@ -61,10 +60,18 @@ namespace NAPS2.Images.Storage
|
||||
|
||||
public void Commit()
|
||||
{
|
||||
// TODO: Clean up when all contents are removed
|
||||
|
||||
EnsureFolderCreated();
|
||||
indexConfigManager.Save();
|
||||
if (indexConfigManager.Config.Images.Count == 0)
|
||||
{
|
||||
// Clean up
|
||||
ForceReleaseLock();
|
||||
Directory.Delete(RecoveryFolderPath, true);
|
||||
folderCreated = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
indexConfigManager.Save();
|
||||
}
|
||||
}
|
||||
|
||||
public IImageMetadata CreateMetadata(IStorage storage)
|
||||
@ -79,5 +86,13 @@ namespace NAPS2.Images.Storage
|
||||
TransformList = new List<Transform>()
|
||||
});
|
||||
}
|
||||
|
||||
public void ForceReleaseLock()
|
||||
{
|
||||
folderLock?.Close();
|
||||
folderLockFile?.Delete();
|
||||
folderLock = null;
|
||||
folderLockFile = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user