TestBase.cs
上传用户:hbhltzc
上传日期:2022-06-04
资源大小:1925k
文件大小:6k
源码类别:

xml/soap/webservice

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.Reflection;
  7. using System.Threading;
  8. using System.Xml;
  9. using Accessibility;
  10. using System.Runtime.InteropServices;
  11. using System.Diagnostics;
  12. using System.Drawing;
  13. using System.IO;
  14. namespace UnitTests {
  15.     public class TestBase {
  16.         public class NodeInfo {
  17.             XmlNodeType nt;
  18.             string name;
  19.             string value;
  20.             public NodeInfo(XmlReader r) {
  21.                 this.nt = r.NodeType;
  22.                 this.name = r.Name;
  23.                 this.value = Normalize(r.Value);
  24.             }
  25.             public bool Equals(NodeInfo other) {
  26.                 return this.nt == other.nt && this.name == other.name &&
  27.                     this.value == other.value;
  28.             }
  29.             string Normalize(string value) {
  30.                 // So text indented different still compares as the same.
  31.                 if (string.IsNullOrEmpty(value)) return null;
  32.                 StringBuilder sb = new StringBuilder();
  33.                 bool wasnewline = true; // collapse leading spaces
  34.                 for (int i = 0, n = value.Length; i < n; i++) {
  35.                     char ch = value[i];
  36.                     if (ch == 'r'){
  37.                         if (i + 1 < n && value[i + 1] == 'n') {
  38.                             i++;
  39.                         }
  40.                         sb.Append('n');
  41.                         wasnewline = true;
  42.                     } else if (ch == 'n'){
  43.                         sb.Append(ch);
  44.                         wasnewline = true;
  45.                     } else if (Char.IsWhiteSpace(ch)) {
  46.                         if (!wasnewline) sb.Append(' ');
  47.                     } else {
  48.                         sb.Append(ch);
  49.                         wasnewline = false;
  50.                     }
  51.                 }
  52.                 return sb.ToString();
  53.             }
  54.         }
  55.         Window main;
  56.         public void Sleep(int ms) {
  57.             Thread.Sleep(ms);
  58.         }
  59.         public Window LaunchApp(string exeFileName, string args) {
  60.             ProcessStartInfo info = new ProcessStartInfo();
  61.             info.FileName = exeFileName;
  62.             info.Arguments = args;
  63.             Process p = new Process();
  64.             p.StartInfo = info;
  65.             if (!p.Start()) {
  66.                 string msg = "Error launching " + exeFileName;
  67.                 MessageBox.Show("Error Creating Process", msg, 
  68.                     MessageBoxButtons.OK, MessageBoxIcon.Error);
  69.                 throw new Exception(msg);
  70.             }
  71.             if (p.HasExited) {
  72.                 throw new Exception(string.Format("Failed to launch '{0'}, exit code {1}", exeFileName, p.ExitCode.ToString()));
  73.             }
  74.             Window w = new Window(p);
  75.             w.TestBase = this;
  76.             if (this.main == null) {
  77.                 this.main = w;
  78.             }
  79.             return w;
  80.         }
  81.         public void CloseApp() {
  82.             if (this.main != null) {
  83.                 this.main.Dispose();
  84.                 this.main = null;
  85.             }
  86.         }
  87.         public Window TestForm {
  88.             get { return main; }
  89.         }
  90.         
  91.         public static Point Center(Rectangle bounds) {
  92.             return new Point(bounds.Left + (bounds.Width / 2),
  93.                 bounds.Top + (bounds.Height / 2));
  94.         }
  95.         public void DeleteFile(string fname) {
  96.             if (File.Exists(fname))
  97.                 File.Delete(fname);
  98.         }
  99.         public void CompareResults(List<NodeInfo> nodes, string outFile) {
  100.             int pos = 0;
  101.             XmlReader reader = XmlReader.Create(outFile);
  102.             IXmlLineInfo li = (IXmlLineInfo)reader;
  103.             using (reader) {
  104.                 while (reader.Read()) {
  105.                     if (reader.NodeType == XmlNodeType.Whitespace ||
  106.                         reader.NodeType == XmlNodeType.SignificantWhitespace ||
  107.                         reader.NodeType == XmlNodeType.XmlDeclaration)
  108.                         continue;
  109.                     NodeInfo node = new NodeInfo(reader);
  110.                     if (pos >= nodes.Count) {
  111.                         throw new ApplicationException("Found too many nodes");
  112.                     }
  113.                     NodeInfo other = nodes[pos++];
  114.                     if (!node.Equals(other)) {
  115.                         throw new ApplicationException(
  116.                             string.Format("Mismatching nodes at line {0},{1}",
  117.                             li.LineNumber, li.LinePosition));
  118.                     }
  119.                 }
  120.             }
  121.         }
  122.         public List<NodeInfo> ReadNodes(string fileName) {
  123.             XmlReader reader = XmlReader.Create(fileName);
  124.             return ReadNodes(reader);
  125.         }
  126.         public List<NodeInfo> ReadNodes(XmlReader reader) {        
  127.             List<NodeInfo> nodes = new List<NodeInfo>();
  128.             using (reader) {
  129.                 while (reader.Read()) {
  130.                     if (reader.NodeType == XmlNodeType.Whitespace ||
  131.                         reader.NodeType == XmlNodeType.SignificantWhitespace ||
  132.                         reader.NodeType == XmlNodeType.XmlDeclaration)
  133.                         continue;
  134.                     nodes.Add(new NodeInfo(reader));
  135.                 }
  136.             }
  137.             return nodes;
  138.         }
  139.         public virtual void CheckClipboard(string expected) {
  140.             if (!Clipboard.ContainsText()) {
  141.                 throw new ApplicationException("clipboard does not contain any text!");
  142.             }
  143.             string text = Clipboard.GetText();
  144.             if (text != expected) {
  145.                 throw new ApplicationException("clipboard does not match expected cut node");
  146.             }
  147.         }
  148.         public Window LaunchIE(string args) {
  149.             string ie = Environment.GetEnvironmentVariable("ProgramFiles") + "\Internet Explorer\iexplore.exe";
  150.             return LaunchApp(ie, args);
  151.         }
  152.     }
  153.     
  154. }