Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/Reporting/CS/DevExpress.DemoReports/Tests/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/Reporting/CS/DevExpress.DemoReports/Tests/Tests.cs

#if DEBUGTEST
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Text;
using DevExpress.Utils;
using DevExpress.XtraPrinting;
using DevExpress.XtraPrinting.Tests;
using DevExpress.XtraReports.UI;
using NUnit.Framework;

namespace XtraReportsDemos.Tests {
    public static class TestHelper {
        public static void SetupDataDirectory(string dir) {
            string[] fileNames = new string[] { "biolife.txt", "CarsDB.mdb", "countriesDB.mdb", "Contacts.mdb", "nwind.mdb", "StyleSheetLavender.repss" };
            foreach(string fileName in fileNames) {
                string filePath = Path.Combine(dir, fileName);
                if(!File.Exists(filePath)) {
                    byte[] bytes = ResourceStreamHelper.GetBytes("DevExpress.DemoReports.Tests." + fileName, Assembly.GetExecutingAssembly());
                    File.WriteAllBytes(filePath, bytes);
                }
            }
        }
    }
    [TestFixture]
    public class XMLSerialization {
        [SetUp]
        public void Setup() {
            string dir = DevExpress.XtraPrinting.Tests.FileHelper.GetTempPath("Data");
            TestHelper.SetupDataDirectory(dir);
            ConnectionHelper.ApplyDataDirectory(dir);
        }
        [Test]
        public void SerializeDeserializeAllReports() {
            List<Type> skipReportTypes = new List<Type>() { 
                typeof(XtraReportsDemos.Subreports.DetailReport),
                typeof(XtraReportsDemos.SideBySideReports.EmployeeComparisonReport),
                typeof(XtraReportsDemos.ReportMerging.MergedReport),
                typeof(XtraReportsDemos.PivotGrid.Report),
                typeof(XtraReportsDemos.PivotGridAndChart.Report),
                typeof(XtraReportsDemos.HugeAmountRecords.Report),
                typeof(XtraReportsDemos.MasterDetailReport.Report),
                typeof(XtraReportsDemos.Charts.Report),
                typeof(XtraReportsDemos.TreeView.Report),
                typeof(XtraReportsDemos.HugeAmountRecords.ReportWeb),
                typeof(XtraReportsDemos.NorthwindTraders.CatalogReport)
            };

            Type[] types = Assembly.GetExecutingAssembly().GetTypes();
            StringBuilder failedReportTypes = new StringBuilder();
            foreach (Type type in types) {
                if (typeof(XtraReport).IsAssignableFrom(type)) {
                    if (!skipReportTypes.Contains(type) && XMLSerializationIsInvalid(type))
                        failedReportTypes.AppendLine(type.FullName + ",");
                } 
            }
            if(failedReportTypes.Length > 0)
                NUnit.Framework.Assert.Fail("Incorrect XML serialization, types = " + failedReportTypes.ToString());

        }
        bool XMLSerializationIsInvalid(Type type) {
            using (XtraReport report = (XtraReport)Activator.CreateInstance(type)) {
                Stream pdfMemoryStream1 = new MemoryStream();
                try {
                    NativeFormatOptions nativeFormatOptions = new NativeFormatOptions();
                    nativeFormatOptions.Compressed = false;
                    report.CreateDocument();
                    report.PrintingSystem.SaveDocument(pdfMemoryStream1, nativeFormatOptions); 
                    using (XtraReport r = (XtraReport)Activator.CreateInstance(type)) {
                        Stream memoryStream1 = new MemoryStream(); 
                        try {
                            SerializeDeserializeReport(report, r, memoryStream1);
                        } finally {
                            memoryStream1.Close();
                        }
                        Stream pdfMemoryStream2 = new MemoryStream();
                        try {
                            r.CreateDocument();
                            r.PrintingSystem.SaveDocument(pdfMemoryStream2, nativeFormatOptions);
                            return !EtalonComparer.CompareStreams(pdfMemoryStream1, pdfMemoryStream2);
                        } finally {
                            pdfMemoryStream2.Close();
                        }
                    }
                } finally {
                    pdfMemoryStream1.Close();
                }
            }
        }
        void SerializeDeserializeReport(XtraReport sourceReport, XtraReport destinationReport, Stream stream) {
            sourceReport.SaveLayoutToXml(stream);
            destinationReport.LoadLayoutFromXml(stream);
        }
    }
}
#endif