Пример #1
0
        internal HarnessExecutionResult Run(BaseTest test, XmlLog log)
        {
            m_initialTime = new DateTime();
            string file           = string.Empty;
            string csprojFilePath = string.Empty;

            m_logFileSaved = false;
            m_debugText    = new StringBuilder();
            Thread desktopThread             = null;
            NamedPipeServerStream pipeStream = null;

            try
            {
                // Set the file paths.
                file = BuildFilePath(test, ref m_xslPath);
                Console.WriteLine("\nTest: " + test.Name);
                string pathForFile = file.Substring(0, file.LastIndexOf(@"\"));
                if (!pathForFile.EndsWith(@"\"))
                {
                    pathForFile = string.Format(@"{0}\", pathForFile);
                }
                string slnFileName = test.Name.Substring(test.Name.LastIndexOf(@"\") + 1);

                try
                {
                    // Get reference list and build the test
                    ArrayList referenceList = GetProjectReferences(file, ref csprojFilePath);
                    if (m_currentAppType != TestType.DeviceDesktop)
                    {
                        BuildTest(csprojFilePath, m_currentAppType);
                    }
                    else
                    {
                        pipeStream = new
                                     NamedPipeServerStream("MFHarnessPipe", PipeDirection.InOut, 1,
                                                           PipeTransmissionMode.Message, PipeOptions.WriteThrough);
                    }

                    // Set log file name.
                    SetLogFileName(test.Name);
                    test.LogFile = m_outputXmlFileName;

                    if (!(test is ProfilerTest))
                    {
                        m_log = log;
                        m_log.StartLog(m_outputXmlFileName, m_xslPath);
                    }

                    // Get build and exe path.
                    string buildPath = GetBuildPath(file);
                    test.ExeLocation = buildPath + m_assemblyName + ".exe";

                    // If this is a profiler test, run the test and return.
                    if (test is ProfilerTest)
                    {
                        return(RunProfilerTest(file, test.ExeLocation, buildPath, referenceList));
                    }

                    string testName = m_logFileName.Replace(".xml", string.Empty);
                    StartCodeCoverage(testName);
                    m_startTime = DateTime.Now;

                    switch (m_currentAppType)
                    {
                    case TestType.Device:
                        try
                        {
                            RunDeviceTest(buildPath, test.ExeLocation, referenceList);
                        }
                        catch (Exception ex)
                        {
                            Close();
                            return(HarnessResult(ex));
                        }
                        break;

                    case TestType.DeviceDesktop:
                        // Desktop apps under
                        //              DevBox: %spoclient%\Test\Platform\Tests\Desktop\Applications\
                        //              TestBox: %programfiles%\Microsoft .NET Micro Framework\<version>\Tests\Desktop\
                        desktopThread = StartDesktopApplication(pathForFile, ref csprojFilePath);
                        pipeStream.WaitForConnection();

                        // Get references and build the device test.
                        GetProjectReferences(file, ref csprojFilePath);
                        BuildTest(csprojFilePath, TestType.Device);
                        RunDeviceTest(buildPath, test.ExeLocation, referenceList);
                        break;

                    case TestType.Desktop:
                        RunDesktopTest(test.ExeLocation);
                        break;
                    }

                    m_endTime = DateTime.Now;
                    StopCodeCoverage(testName);
                }
                catch (Exception ex)
                {
                    m_endTime = DateTime.Now;
                    if (null != m_log)
                    {
                        m_log.WriteElementString("Test_Exception", ex.Message + ex.StackTrace);
                    }
                    Utils.WriteToEventLog("Exception: " + ex.ToString());
                    Close();
                    if (ex is ApplicationException && ex.Message.ToLower().Contains("build failure"))
                    {
                        return(HarnessExecutionResult.Abort);
                    }
                    else
                    {
                        return(HarnessExecutionResult.Unavailable);
                    }
                }
                finally
                {
                    if (m_currentAppType == TestType.DeviceDesktop)
                    {
                        StopDesktopApplication(pipeStream, desktopThread);
                    }

                    Close();

                    if (null != m_log)
                    {
                        switch (m_currentAppType)
                        {
                        case TestType.DeviceDesktop:
                            SaveLogFile(test.Location, TestType.DeviceDesktop);
                            break;

                        default:
                            SaveLogFile(test.Location);
                            break;
                        }
                    }

                    // Change test results back to unknown for the next test.
                    TestResults = Result.NotKnown;
                }

                if (!string.IsNullOrEmpty(file))
                {
                    if (!string.Equals(m_transport.ToLower(), "emulator"))
                    {
                        m_log.SynchronizeLogTime(file, m_initialTime);
                    }
                }
                else
                {
                    return(HarnessExecutionResult.Unavailable);
                }
            }
            catch (Exception ex)
            {
                if (ex is FileNotFoundException)
                {
                    throw ex;
                }

                Utils.WriteToEventLog(string.Format("Exception in Harness: {0}", ex.ToString()));
                return(HarnessExecutionResult.Abort);
            }

            return(HarnessExecutionResult.Success);
        }