/// <summary> /// Private helper function (formerly in SaveSnapshotZip) to make it easier to call with different inputs /// </summary> private static void SaveSnapshotFromElement(int?focusedElementId, A11yFileMode mode, Contexts.ElementContext ec, Package package, A11yElement root) { var json = JsonConvert.SerializeObject(root, Formatting.Indented); using (MemoryStream mStrm = new MemoryStream(Encoding.UTF8.GetBytes(json))) { AddStream(package, mStrm, elementFileName); } if (ec.DataContext.Screenshot != null) { using (MemoryStream mStrm = new MemoryStream()) { ec.DataContext.Screenshot.Save(mStrm, System.Drawing.Imaging.ImageFormat.Png); mStrm.Seek(0, SeekOrigin.Begin); AddStream(package, mStrm, screenshotFileName); } } var meta = new SnapshotMetaInfo(mode, RuleRunner.RuleVersion, focusedElementId, ec.DataContext.ScreenshotElementId); var jsonMeta = JsonConvert.SerializeObject(meta, Formatting.Indented); using (MemoryStream mStrm = new MemoryStream(Encoding.UTF8.GetBytes(jsonMeta))) { AddStream(package, mStrm, metadataFileName); } }
/// <summary> /// Consctructor /// </summary> /// <param name="el">The element that was selected when the file was saved</param> /// <param name="bmp">Actual screenshot</param> /// <param name="synthesizedBmp">Synthesized ("yellow box") bitmap</param> /// <param name="settings">Metadata about the snapshot</param> public LoadActionParts(A11yElement el, Bitmap bmp, Bitmap synthesizedBmp, SnapshotMetaInfo settings) { this.Element = el; this.Bmp = bmp; this.SynthesizedBmp = synthesizedBmp; this.MetaInfo = settings; }
/// <summary> /// Extract snapshot file /// </summary> /// <param name="path"></param> /// <returns></returns> internal static LoadActionParts LoadSnapshotZip(string path) { using (FileStream str = File.Open(path, FileMode.Open, FileAccess.Read)) using (Package package = ZipPackage.Open(str, FileMode.Open, FileAccess.Read)) { var parts = package.GetParts(); var elementPart = (from p in parts where p.Uri.OriginalString == "/" + SaveAction.elementFileName select p.GetStream()).First(); A11yElement element = A11yElement.FromStream(elementPart); elementPart.Close(); Bitmap bmp; try { var bmpPart = (from p in parts where p.Uri.OriginalString == "/" + SaveAction.screenshotFileName select p.GetStream()).First(); bmp = LoadBmp(bmpPart); bmpPart.Close(); } catch (InvalidOperationException e) // Gets thrown if screenshot doesn't exist in file { e.ReportException(); bmp = null; } var metadataPart = (from p in parts where p.Uri.OriginalString == "/" + SaveAction.metadataFileName select p.GetStream()).First(); SnapshotMetaInfo meta = SnapshotMetaInfo.DeserializeFromStream(metadataPart); metadataPart.Close(); var selectedElement = element.FindDescendant(k => k.UniqueId == meta.ScreenshotElementId); return(new LoadActionParts(element, bmp, selectedElement.SynthesizeBitmapFromElements(), meta)); } }
/// <summary> /// Private helper function (formerly in SaveSnapshotZip) to make it easier to call with different inputs /// </summary> private static void SaveSnapshotFromElement(int?focusedElementId, A11yFileMode mode, Dictionary <SnapshotMetaPropertyName, object> otherProperties, CompletenessMode completenessMode, Contexts.ElementContext ec, Package package, A11yElement root) { var json = JsonConvert.SerializeObject(root, Formatting.Indented); using (MemoryStream mStrm = new MemoryStream(Encoding.UTF8.GetBytes(json))) { AddStream(package, mStrm, elementFileName); } if (completenessMode == CompletenessMode.Full && ec.DataContext.Screenshot != null) { using (MemoryStream mStrm = new MemoryStream()) { ec.DataContext.Screenshot.Save(mStrm, System.Drawing.Imaging.ImageFormat.Png); mStrm.Seek(0, SeekOrigin.Begin); AddStream(package, mStrm, screenshotFileName); } } var meta = new SnapshotMetaInfo(mode, SuiteFactory.GetRuleVersion(), focusedElementId, ec.DataContext.ScreenshotElementId, otherProperties); var jsonMeta = JsonConvert.SerializeObject(meta, Formatting.Indented); using (MemoryStream mStrm = new MemoryStream(Encoding.UTF8.GetBytes(jsonMeta))) { AddStream(package, mStrm, metatdataFileName); } }
private static Dictionary <SnapshotMetaPropertyName, object> GetDeserializedProperties(Dictionary <SnapshotMetaPropertyName, object> inputProperties) { SnapshotMetaInfo info = new SnapshotMetaInfo(A11yFileMode.Contrast, "Test", 0, 0, inputProperties); var text = JsonConvert.SerializeObject(info); using (var testStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(text))) { SnapshotMetaInfo metaInfo = SnapshotMetaInfo.DeserializeFromStream(testStream); return(metaInfo.OtherProperties); } }
/// <summary> /// Extract snapshot file /// </summary> /// <param name="path"></param> /// <returns></returns> internal static LoadActionParts LoadSnapshotZip(string path) { using (FileStream str = File.Open(path, FileMode.Open, FileAccess.Read)) using (Package package = ZipPackage.Open(str, FileMode.Open, FileAccess.Read)) { var parts = package.GetParts(); var elementPart = (from p in parts where p.Uri.OriginalString == "/" + SaveAction.elementFileName select p.GetStream()).First(); A11yElement element = A11yElement.FromStream(elementPart); elementPart.Close(); Bitmap bmp; try { var bmpPart = (from p in parts where p.Uri.OriginalString == "/" + SaveAction.screenshotFileName select p.GetStream()).First(); bmp = LoadBmp(bmpPart); bmpPart.Close(); } catch (InvalidOperationException e) // Gets thrown if screenshot doesn't exist in file { e.ReportException(); bmp = null; } var metadataPart = (from p in parts where p.Uri.OriginalString == "/" + SaveAction.metadataFileName select p.GetStream()).First(); SnapshotMetaInfo meta = SnapshotMetaInfo.DeserializeFromStream(metadataPart); metadataPart.Close(); IReadOnlyDictionary <int, CustomProperty> CustomProperties; try { var customPropertiesPart = (from p in parts where p.Uri.OriginalString == "/" + SaveAction.customPropsFileName select p.GetStream()).First(); CustomProperties = LoadCustomProperties(customPropertiesPart); customPropertiesPart.Close(); } #pragma warning disable CA1031 // Do not catch general exception types: specific handlers placed in conditional below catch (Exception e) #pragma warning restore CA1031 // Do not catch general exception types: specific handlers placed in conditional below { if (!(e is InvalidOperationException)) // An expected exception thrown when file does not exist, such as in old a11ytest files { e.ReportException(); } CustomProperties = null; } var selectedElement = element.FindDescendant(k => k.UniqueId == meta.ScreenshotElementId); return(new LoadActionParts(element, bmp, selectedElement.SynthesizeBitmapFromElements(), meta)); } }
private static Dictionary <SnapshotMetaPropertyName, object> GetDeserializedProperties(Dictionary <SnapshotMetaPropertyName, object> inputProperties) { using (ShimsContext.Create()) { SnapshotMetaInfo info = new SnapshotMetaInfo(A11yFileMode.Contrast, "Test", 0, 0, inputProperties); var text = JsonConvert.SerializeObject(info); ShimStreamReader.AllInstances.ReadToEnd = (_) => { return(text); }; using (var testStream = new MemoryStream()) { SnapshotMetaInfo metaInfo = SnapshotMetaInfo.DeserializeFromStream(testStream); return(metaInfo.OtherProperties); } } }