using Avalonia; using Avalonia.Headless; using Avalonia.Controls; using TovarV2; using Avalonia.Media.Imaging; using System.IO; using System.Linq; namespace TestTovarV2 { [TestFixture] public class AddTovarTests { private AddTovar _addTovar; [OneTimeSetUp] public void OneTimeSetup() { AppBuilder.Configure() .UseHeadless(new AvaloniaHeadlessPlatformOptions()) .SetupWithoutStarting(); } [SetUp] public void Setup() { ListPr.ListProd.Clear(); ListPr.b = 0; _addTovar = new AddTovar(); _addTovar.Show(); } [TearDown] public void TearDown() { _addTovar.Close(); ListPr.ListProd.Clear(); } [Test] public void AddTovarOkClick_ShouldAddProduct_WhenFieldsIsValid() { _addTovar.GetNameTov.Text = "New Product"; _addTovar.GetPriceTov.Text = "100"; _addTovar.GetQuantityTov.Text = "10"; _addTovar.bitmapToBind = new Bitmap(Path.Combine("Assets", "test_image.png")); _addTovar.AddTovarOk_Click(null, null); Assert.That(ListPr.ListProd.Count, Is.EqualTo(1)); Assert.That(ListPr.ListProd[0].nameProd, Is.EqualTo("New Product")); Assert.That(ListPr.ListProd[0].priceProd, Is.EqualTo(100)); Assert.That(ListPr.ListProd[0].quantityProd, Is.EqualTo(10)); Assert.That(ListPr.ListProd[0].bitmapProd, Is.Not.Null); Assert.That(ProductEdit.IsOpen, Is.True); } [Test] public void AddTovarOkClick_ShouldShowError_WhenProductNameExists() { ListPr.ListProd.Add(new Product { nameProd = "Existing Product" }); _addTovar.GetNameTov.Text = "Existing Product"; _addTovar.AddTovarOk_Click(null, null); Assert.That(ListPr.ListProd.Count, Is.EqualTo(1)); Assert.That(_addTovar.GetErrorMsg.Text, Is.EqualTo("Товар с таким именем уже имеется в каталоге")); } [Test] public void AddTovarOkClick_ShouldAddProduct_WhenImageNotSelected() { _addTovar.GetNameTov.Text = "Product Without Image"; _addTovar.GetPriceTov.Text = "200"; _addTovar.GetQuantityTov.Text = "20"; _addTovar.bitmapToBind = null; _addTovar.AddTovarOk_Click(null, null); Assert.That(ListPr.ListProd[0].nameProd, Is.EqualTo("Product Without Image")); Assert.That(ListPr.ListProd[0].bitmapProd, Is.Null); } [Test] public void AddTovarOkClick_ShouldHandleInvalidNumberFormat_WhenFieldsIsInvalid() { _addTovar.GetNameTov.Text = "Invalid Input Product"; _addTovar.GetPriceTov.Text = "invalid"; _addTovar.GetQuantityTov.Text = "invalid"; Assert.DoesNotThrow(() => _addTovar.AddTovarOk_Click(null, null)); } [Test] public void AddTovarImgClick_ShouldUpdateImage_WhenFileSelected() { var testBitmap = new Bitmap(Path.Combine("Assets", "test_image.png")); _addTovar.bitmapToBind = testBitmap; _addTovar.AddTovarImg_Click(null, null); Assert.That(_addTovar.GetImagePath.Source, Is.EqualTo(testBitmap)); } [Test] public void AddTovarOkClick_ShouldIncrementIdCounter_WhenFieldsIsValid() { var initialCounter = ListPr.b; _addTovar.GetNameTov.Text = "Product 1"; _addTovar.GetPriceTov.Text = "100"; _addTovar.GetQuantityTov.Text = "10"; _addTovar.AddTovarOk_Click(null, null); _addTovar.GetNameTov.Text = "Product 2"; _addTovar.AddTovarOk_Click(null, null); Assert.That(ListPr.ListProd[0].Id, Is.EqualTo(initialCounter)); Assert.That(ListPr.ListProd[1].Id, Is.EqualTo(initialCounter + 1)); } [Test] public void AddTovarOkClick_ShouldHandleEmptyFields_WhenFieldsIsEmpty() { _addTovar.GetNameTov.Text = ""; _addTovar.GetPriceTov.Text = ""; _addTovar.GetQuantityTov.Text = ""; Assert.DoesNotThrow(() => _addTovar.AddTovarOk_Click(null, null)); } [Test] public void AddTovarOkClick_ShouldCloseWindow_WhenSuccessful() { _addTovar.GetNameTov.Text = "Test Close"; _addTovar.GetPriceTov.Text = "100"; _addTovar.GetQuantityTov.Text = "10"; _addTovar.AddTovarOk_Click(null, null); Assert.That(AddTovar.IsOpen, Is.False); } [Test] public void AddTovarOkClick_ShouldOpenProductEdit_WhenSuccessful() { _addTovar.GetNameTov.Text = "Test Open Edit"; _addTovar.GetPriceTov.Text = "100"; _addTovar.GetQuantityTov.Text = "10"; _addTovar.AddTovarOk_Click(null, null); Assert.That(ProductEdit.IsOpen, Is.True); } } }