public AppConfigFileBasedConfiguration()
        {
            AutomationControllerName = ConfigurationManager.AppSettings[EmuStepsAutomationControllerKeyName];
            ControllerInitialisationString = ConfigurationManager.AppSettings[EmuStepsControllerInitialisationKeyName];
            if (string.IsNullOrEmpty(AutomationControllerName))
                AutomationControllerName = "wp";
            if (string.IsNullOrEmpty(ControllerInitialisationString))
                ControllerInitialisationString = string.Empty;

            AutomationIdentification automationIdentification;
            if (Enum.TryParse(ConfigurationManager.AppSettings[EmuStepsAutomationIdentificationKeyName], true, out automationIdentification))
                AutomationIdentification = automationIdentification;
            else
                AutomationIdentification = AutomationIdentification.TryEverything;

            ApplicationDefinition = new ApplicationDefinition();
            foreach (var key in ConfigurationManager.AppSettings.AllKeys)
            {
                if (key.StartsWith(EmuStepsApplicationPrefix) && key.Length > EmuStepsApplicationPrefix.Length)
                {
                    ApplicationDefinition.Fields[key.Substring(EmuStepsApplicationPrefix.Length)] =
                        ConfigurationManager.AppSettings[key];
                }
            }
        }
        /// <summary>
        /// The install.
        /// </summary>
        /// <param name="applicationDefinition">
        /// The application definition.
        /// </param>
        /// <returns>
        /// The <see cref="InstallationResult"/>.
        /// </returns>
        public override InstallationResult Install(ApplicationDefinition applicationDefinition)
        {
            InstallationResult result = base.Install(applicationDefinition);

            RemoteIsolatedStorageFile store = GetIsoStorage(applicationDefinition);

            if (store.FileExists(BddhostFilePath))
            {
                store.DeleteFile(BddhostFilePath);
            }

            string hostName = Dns.GetHostEntry("127.0.0.1").HostName + ":" + this.Port;

            File.WriteAllLines(BddhostFilePath, new[] {hostName});

            store.SendFile(BddhostFilePath, BddhostFilePath, true);

            return result;
        }
 protected ParsedApplicationDefinition(ApplicationDefinition applicationDefinition)
     : base(applicationDefinition.Fields)
 {
 }
 public UninstallationResult ForceUninstall(ApplicationDefinition applicationDefinition)
 {
     Stop(applicationDefinition);
     return Uninstall(applicationDefinition);
 }
 public abstract void RestoreIsolatedStorage(ApplicationDefinition applicationDefinition, string isolatedStorage);
 public abstract StartResult Start(ApplicationDefinition applicationDefinition);
 public abstract UninstallationResult Uninstall(ApplicationDefinition applicationDefinition);
 public WindowsPhoneApplicationDefinition(ApplicationDefinition applicationDefinition)
     : base(applicationDefinition)
 {
 }
 public override string GetIsolatedStorage(ApplicationDefinition applicationDefinition)
 {
     throw new NotImplementedException();
 }
        public override StopResult Stop(ApplicationDefinition applicationDefinition)
        {
            var windowsApplicationDefinition = ToWindowsPhoneApplicationDefinition(applicationDefinition);

            InvokeTrace("ensuring application is stopped...");

            if (!Device.IsApplicationInstalled(windowsApplicationDefinition.ProductGuid))
            {
                InvokeTrace("application is not installed");
                return StopResult.NotInstalled;
            }

            var app = SafeGetApplication(windowsApplicationDefinition.ProductGuid);
            if (app == null)
            {
                InvokeTrace("failed to get application");
                return StopResult.NotInstalled; // really this is an error case - but just return NotInstalled for now
            }

#warning Can we kill this IsRunning block - it's commented out and really dead now?
            /*
             IsRunning is not supported on WP7
            if (!app.IsRunning())
            {
                InvokeTrace("application is not running");
                return StopResult.NotRunning;
            }
             */

            InvokeTrace("stopping application...");
            try
            {
                app.TerminateRunningInstances();
            }
            catch (Exception ex)
            {
#warning Nested pokemon exception handling - hard to read and understand
                try
                {
                    InvokeTrace("CurrentDeviceID =" + Device.Id);
                    // An exception here can't be recovered but we can leave things in a better state for the next test if we kill the emulator
                    var processes = Process.GetProcessesByName("Xde");
                    foreach (var process in processes)
                    {
                        InvokeTrace("ProcessId = " + process.Id);
                        process.Kill();
                    }
                }
                catch (Exception)
                {
                    // We can but try, but if we can't let's ignore it
                    InvokeTrace("failed to close emulator");
                }

                throw ex;
            }

            InvokeTrace("application stopped");
            return StopResult.Success;
        }
        public override UninstallationResult Uninstall(ApplicationDefinition applicationDefinition)
        {
            var windowsApplicationDefinition = ToWindowsPhoneApplicationDefinition(applicationDefinition);

            if (!Device.IsApplicationInstalled(windowsApplicationDefinition.ProductGuid))
                return UninstallationResult.NotInstalled;

            InvokeTrace("uninstalling xap from device...");
            var app = SafeGetApplication(windowsApplicationDefinition.ProductGuid);
            app.Uninstall();
            InvokeTrace("xap uninstalled from  device");

            return UninstallationResult.Success;
        }
        public override InstallationResult Install(ApplicationDefinition applicationDefinition)
        {
            var windowsApplicationDefinition = ToWindowsPhoneApplicationDefinition(applicationDefinition);

            if (IsInstalled(windowsApplicationDefinition.ProductGuid))
                return InstallationResult.AlreadyInstalled;

            InvokeTrace("installing xap to device...");

            if (windowsApplicationDefinition.ProductGuid == Guid.Empty)
                throw new ArgumentException("Empty productId");

            if (!File.Exists(windowsApplicationDefinition.ApplicationPackagePath))
                throw new FileNotFoundException("File not found - " + windowsApplicationDefinition.ApplicationPackagePath);

            if (!File.Exists(windowsApplicationDefinition.ApplicationIconPath))
                throw new FileNotFoundException("File not found - " + windowsApplicationDefinition.ApplicationIconPath);

            Device.InstallApplication(
                windowsApplicationDefinition.ProductGuid,
                windowsApplicationDefinition.InstanceGuid,
                windowsApplicationDefinition.ApplicationName,
                windowsApplicationDefinition.ApplicationIconPath,
                windowsApplicationDefinition.ApplicationPackagePath);

            InvokeTrace("xap installed");
            return InstallationResult.Success;
        }
 private WindowsPhoneApplicationDefinition ToWindowsPhoneApplicationDefinition(ApplicationDefinition applicationDefinition)
 {
     return new WindowsPhoneApplicationDefinition(applicationDefinition);
 }
        public override StartResult Start(ApplicationDefinition applicationDefinition)
        {
            var windowsApplicationDefinition = ToWindowsPhoneApplicationDefinition(applicationDefinition);

            InvokeTrace("launching app..." + windowsApplicationDefinition.ProductGuid);
            var app = SafeGetApplication(windowsApplicationDefinition.ProductGuid);
            if (app == null)
            {
                InvokeTrace("not installed");
                return StartResult.NotInstalled;
            }

            /*
             app.IsRunning is not supported for WP7
            if (app.IsRunning())
            {
                InvokeTrace("already running");
                return StartResult.AlreadyRunning;
            }
             */
            try
            {
                app.TerminateRunningInstances();
            }
            catch (Exception ex)
            {
                InvokeTrace("could not close instace {0}",ex);
            }
            
            app.Launch();
            InvokeTrace("app launched");
            return StartResult.Success;
        }
 public override StopResult Stop(ApplicationDefinition applicationDefinition)
 {
     // see - main answer in - http://stackoverflow.com/questions/2720164/android-process-killer
     InvokeTrace("Stop command ignored for Android");
     return StopResult.Success;
 }
        public override StartResult Start(ApplicationDefinition applicationDefinition)
        {
            var adbDefinition = new AdbApplicationDefinition(applicationDefinition);
            /*
            ExecuteAdb("-s {0} shell am start -a {1} -n {2}/{3}",
                            _configuration.RunningEmulatorAdbName,
                            adbDefinition.Action,
                            adbDefinition.PackageName,
                            adbDefinition.ActivityClassName);
             */

            // TODO - check return here?
            ExecuteAdb(DoNotWait,
                       "-s {0} shell am instrument -w {1}/android.test.InstrumentationTestRunner",
                       _configuration.RunningEmulatorAdbName,
                       adbDefinition.StubPackageName);

            InvokeTrace("Not currently checking the start response!");

            return StartResult.Success;
        }
        public override string GetIsolatedStorage(ApplicationDefinition applicationDefinition)
        {
            var store = GetIsoStorage(applicationDefinition);

            string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\IsolatedStore\Temp\" + Guid.NewGuid().ToString(); 
            ReceiveDirectory(store, desktopPath);

            return desktopPath;
        }
 public override void RestoreIsolatedStorage(ApplicationDefinition applicationDefinition, string isolatedStorage)
 {
     throw new NotImplementedException();
 }
        protected RemoteIsolatedStorageFile GetIsoStorage(ApplicationDefinition applicationDefinition)
        {
            var windowsApplicationDefinition = ToWindowsPhoneApplicationDefinition(applicationDefinition);

            var app = SafeGetApplication(windowsApplicationDefinition.ProductGuid);

            var store = app.GetIsolatedStore();
            return store;
        }
        public override void RestoreIsolatedStorage(ApplicationDefinition applicationDefinition, string isolatedStorage)
        {
            var store = GetIsoStorage(applicationDefinition);

            if (Directory.Exists(isolatedStorage))
            {
                Console.WriteLine("Restoring isolated storage from: {0}", isolatedStorage);

                SendDirectory(store, isolatedStorage);
            }
            else
            {
                Console.WriteLine("Isolated storage folder \"{0}\" does not exist.", isolatedStorage);
            }
        }
        public override StartResult Start(ApplicationDefinition applicationDefinition)
        {
            var windowsApplicationDefinition = ToWindowsPhoneApplicationDefinition(applicationDefinition);

            InvokeTrace("launching app...");
            var app = SafeGetApplication(windowsApplicationDefinition.ProductGuid);
            if (app == null)
            {
                InvokeTrace("not installed");
                return StartResult.NotInstalled;
            }

            /*
             app.IsRunning is not supported for WP7
            if (app.IsRunning())
            {
                InvokeTrace("already running");
                return StartResult.AlreadyRunning;
            }
             */

            app.Launch();
            InvokeTrace("app launched");
            return StartResult.Success;
        }
 public abstract StopResult Stop(ApplicationDefinition applicationDefinition);
 public AdbApplicationDefinition(ApplicationDefinition applicationDefinition)
     : base(applicationDefinition)
 {
 }
 public abstract string GetIsolatedStorage(ApplicationDefinition applicationDefinition);
        public override InstallationResult Install(ApplicationDefinition applicationDefinition)
        {
            var adbDefinition = new AdbApplicationDefinition(applicationDefinition);

            // first install the stub
            var installStubResult = Install(adbDefinition.StubPackagePath);
            if (installStubResult != InstallationResult.Success)
                InvokeTrace("TODO!");

            var installMainResult = Install(adbDefinition.PackagePath);
            return installMainResult;
        }
 public InstallationResult ForceInstall(ApplicationDefinition applicationDefinition)
 {
     ForceUninstall(applicationDefinition);
     return Install(applicationDefinition);
 }
        public override UninstallationResult Uninstall(ApplicationDefinition applicationDefinition)
        {
            var adbDefinition = new AdbApplicationDefinition(applicationDefinition);

            // first uninstall the stub package
            Uninstall(adbDefinition.StubPackageName);

            // then uninstall the app package
            return Uninstall(adbDefinition.PackageName);
        }
 public StartResult ForceStart(ApplicationDefinition applicationDefinition)
 {
     Stop(applicationDefinition);
     return Start(applicationDefinition);
 }
        public override StopResult Stop(ApplicationDefinition applicationDefinition)
        {
            var windowsApplicationDefinition = ToWindowsPhoneApplicationDefinition(applicationDefinition);

            InvokeTrace("ensuring application is stopped...");

            if (!Device.IsApplicationInstalled(windowsApplicationDefinition.ProductGuid))
            {
                InvokeTrace("application is not installed");
                return StopResult.NotInstalled;
            }

            var app = SafeGetApplication(windowsApplicationDefinition.ProductGuid);
            if (app == null)
            {
                InvokeTrace("failed to get application");
                return StopResult.NotInstalled; // really this is an error case - but just return NotInstalled for now
            }

            /*
             IsRunning is not supported on WP7
            if (!app.IsRunning())
            {
                InvokeTrace("application is not running");
                return StopResult.NotRunning;
            }
             */

            InvokeTrace("stopping application...");
            app.TerminateRunningInstances();
            InvokeTrace("application stopped");
            return StopResult.Success;
        }