/* Copyright (c) Citrix Systems, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, * with or without modification, are permitted provided * that the following conditions are met: * * * Redistributions of source code must retain the above * copyright notice, this list of conditions and the * following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the * following disclaimer in the documentation and/or other * materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ using System; using System.IO; using System.Reflection; using System.Windows.Forms; using NUnit.Framework; using XenAdmin; using XenAdmin.Controls; using XenAdmin.Controls.DataGridViewEx; using XenAdmin.Controls.MainWindowControls; using XenAdmin.Controls.XenSearch; namespace XenAdminTests { internal static class TestUtils { /// /// Gets recursively a private field contained in a class of the specified instance. /// /// The instance of the class. /// The name of the private field. /// The value of the private field from the class of the specified instance. public static TField GetFieldDeep(object o, string name) { Util.ThrowIfStringParameterNullOrEmpty(name, "name"); string[] parts = name.Split(new[] { '.' }, 2); object obj = null; try { Type type = o.GetType(); FieldInfo info = GetBaseTypeField(type, parts[0]); obj = info.GetValue(o); } catch (Exception e) { Assert.Fail($"Field {parts[0]} of {o.GetType().Name} throws {e.GetType().Name}: {e.Message}."); } if (parts.Length == 1) return (TField)obj; return GetFieldDeep(obj, parts[1]); } /// Thrown if type==null private static FieldInfo GetBaseTypeField(Type type, string fieldName) { FieldInfo info = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); if (info == null) { Type baseType = type.BaseType; return GetBaseTypeField(baseType, fieldName); } return info; } public static TextBox GetTextBox(object o, string name) { return GetFieldDeep(o, name); } public static SearchTextBox GetSearchTextBox(object o, string name) { return GetFieldDeep(o, name); } public static ComboBox GetComboBox(object o, string name) { return GetFieldDeep(o, name); } public static CheckBox GetCheckBox(object o, string name) { return GetFieldDeep(o, name); } public static Button GetButton(object o, string name) { return GetFieldDeep