Esempio n. 1
0
        /// <summary>
        /// Enumerates files in current directory and finds tests.
        /// </summary>
        /// <returns>List of tests loaded.</returns>
        public List <TestInfo> LoadTests()
        {
            _testInfos     = new List <TestInfo>();
            _onvifProfiles = new List <IProfileDefinition>();

            _controls      = new Dictionary <Guid, SettingsTabPage>();
            _settingsTypes = new List <Type>();

            Dictionary <Type, Type> controls = new Dictionary <Type, Type>();


            string location = Assembly.GetExecutingAssembly().Location;
            string path     = Path.GetDirectoryName(location);

            foreach (string file in Directory.GetFiles(path, "*.dll"))
            {
                try
                {
                    System.Reflection.Assembly assembly = Assembly.LoadFile(file);
                    if (assembly.GetCustomAttributes(
                            typeof(TestAssemblyAttribute),
                            false).Length > 0)
                    {
                        // Test assembly

                        foreach (Type t in assembly.GetTypes())
                        {
                            object[] attrs = t.GetCustomAttributes(typeof(TestClassAttribute), true);
                            if (attrs.Length > 0)
                            {
                                LoadTests(t);
                            }

                            attrs = t.GetCustomAttributes(typeof(ProfileDefinitionAttribute), true);
                            if (attrs.Length > 0)
                            {
                                LoadProfile(t);
                            }

                            attrs = t.GetCustomAttributes(typeof(SettingsControlAttribute), true);
                            if (attrs.Length > 0)
                            {
                                SettingsControlAttribute attr = (SettingsControlAttribute)attrs[0];
                                if (!controls.ContainsKey(attr.ParametersType))
                                {
                                    controls.Add(attr.ParametersType, t);
                                }
                            }
                        }
                    }
                }
                catch (Exception exc)
                {
                }
            }


            foreach (Type type in _settingsTypes)
            {
                Type ctrlType = controls[type];

                object          ctrl = Activator.CreateInstance(ctrlType);
                SettingsTabPage ctl  = (SettingsTabPage)ctrl;
                ctl.Dock = DockStyle.Fill;

                _controls.Add(type.GUID, (SettingsTabPage)ctrl);
            }

            if (SettingPagesLoaded != null)
            {
                SettingPagesLoaded(_controls.Values.ToList());
            }

            View.DisplayTests(_testInfos);

            View.DisplayProfiles(_onvifProfiles.OrderBy(P => P.Name));

            if (TestsLoaded != null)
            {
                TestsLoaded(_testInfos);
            }
            return(_testInfos);
        }
        /// <summary>
        /// Enumerates files in current directory and finds tests.
        /// </summary>
        /// <returns>List of tests loaded.</returns>
        public List <TestInfo> LoadTests()
        {
            // initialize lists
            _testInfos     = new List <TestInfo>();
            _onvifProfiles = new List <IProfileDefinition>();
            _controls      = new Dictionary <Guid, SettingsTabPage>();
            _settingsTypes = new List <Type>();

            Dictionary <Type, Type> controls = new Dictionary <Type, Type>();

            string location = Assembly.GetExecutingAssembly().Location;
            string path     = Path.GetDirectoryName(location);

            // enumerate files
            foreach (string file in Directory.GetFiles(path, "*.dll"))
            {
                try
                {
                    System.Reflection.Assembly assembly = Assembly.LoadFile(file);

                    // check if this is a test assembly
                    if (assembly.GetCustomAttributes(
                            typeof(TestAssemblyAttribute),
                            false).Length > 0)
                    {
                        // Test assembly

                        // enumerate types
                        foreach (Type t in assembly.GetTypes())
                        {
                            // Load tests, if this is a test class
                            object[] attrs = t.GetCustomAttributes(typeof(TestClassAttribute), true);
                            if (attrs.Length > 0)
                            {
                                LoadTests(t);
                            }

                            // Load profiles, if this is sa profile
                            attrs = t.GetCustomAttributes(typeof(ProfileDefinitionAttribute), true);
                            if (attrs.Length > 0)
                            {
                                LoadProfile(t);
                            }

                            // Load settings controls, if this is a settings control
                            attrs = t.GetCustomAttributes(typeof(SettingsControlAttribute), true);
                            if (attrs.Length > 0)
                            {
                                SettingsControlAttribute attr = (SettingsControlAttribute)attrs[0];
                                // use only one control for each type.
                                if (!controls.ContainsKey(attr.ParametersType))
                                {
                                    controls.Add(attr.ParametersType, t);
                                }
                            }
                        }
                    }
                }
                catch (Exception exc)
                {
                }
            }

            // create only necessary settings controls
            foreach (Type type in _settingsTypes)
            {
                Type ctrlType = controls[type];

                object          ctrl = Activator.CreateInstance(ctrlType);
                SettingsTabPage ctl  = (SettingsTabPage)ctrl;
                ctl.Dock = DockStyle.Fill;

                _controls.Add(type.GUID, (SettingsTabPage)ctrl);
            }

            // notify that settings control are loaded
            if (SettingPagesLoaded != null)
            {
                SettingPagesLoaded(_controls.Values.ToList());
            }

            // Display tests
            if (View.TestTreeView != null)
            {
                View.TestTreeView.DisplayTests(_testInfos);
            }
            // Display profiles
            if (View.ProfilesView != null)
            {
                View.ProfilesView.DisplayProfiles(_onvifProfiles.OrderBy(P => P.GetProfileName()));
            }

            return(_testInfos);
        }