VisualFSharpPowerTools /tests/TestUtilities/VsIdeHostAdapter.cs

Language C# Lines 1242
MD5 Hash 3a660e262f0a7ce2b23970379fd641ee Estimated Cost $21,542 (why?)
Repository https://github.com/jeroldhaas/VisualFSharpPowerTools.git View Raw File View Project SPDX
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the Apache License, Version 2.0, please send an email to 
 * vspython@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Apache License, Version 2.0.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * ***************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Serialization.Formatters;
using System.Security.Principal;
using System.Text.RegularExpressions;
using System.Xml;
using EnvDTE;
using Microsoft.TC.TestHostAdapters;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.TestTools.Common;
using Microsoft.VisualStudio.TestTools.Common.Xml;
using Microsoft.VisualStudio.TestTools.Execution;
using Microsoft.VisualStudio.TestTools.TestAdapter;
using Microsoft.Win32;
using IBindCtx = Microsoft.VisualStudio.OLE.Interop.IBindCtx;
using IEnumMoniker = Microsoft.VisualStudio.OLE.Interop.IEnumMoniker;
using IMessageFilter = Microsoft.VisualStudio.OLE.Interop.IMessageFilter;
using IMoniker = Microsoft.VisualStudio.OLE.Interop.IMoniker;
using IRunningObjectTable = Microsoft.VisualStudio.OLE.Interop.IRunningObjectTable;
using Process = System.Diagnostics.Process;

namespace TestUtilities
{
    /// <summary>
    /// Vs Ide Host Adapter: Agent side.
    /// This wraps ITestAdapter and looks like ITestAdapter for the Agent.
    /// Internally it delegates to original test adapter hosted by Visual Studio IDE.
    /// </summary>
    public class VsIdeHostAdapter : ITestAdapter
    {
        public const string DynamicHostAdapterName = "TC Dynamic";
        public const string VsAddinName = "TcVsIdeTestHost";

        private IRunContext _runContext;
        private TestRunConfiguration _runConfig;
        private string _workingDir;
        private ITestAdapter _hostSide;
        private object _hostSideLock = new object();
        private IVsIdeTestHostAddin _testHostAddin;
        private IChannel _clientChannel;
        private IChannel _serverChannel;
        private VisualStudioIde _vsIde;
        private string _vsRegistryHive;    // This is like 10.0 or 10.0Exp.
        private RetryMessageFilter _comMessageFilter;
        private static readonly TimeSpan _ideStartupTimeout = TimeSpan.FromMinutes(2);
        private static readonly TimeSpan _addinWaitTimeout = TimeSpan.FromMinutes(1);
        private static readonly TimeSpan _debuggerTimeout = TimeSpan.FromMinutes(3);
        private static readonly TimeSpan _baseSleepDuration = TimeSpan.FromMilliseconds(250);
        private static readonly TimeSpan _baseSleepDoubleDuration = TimeSpan.FromMilliseconds(500);

        /// <summary>
        /// Constructor. Called by Agent via Activator.CreateInstance and should not have any parameters.
        /// </summary>
        public VsIdeHostAdapter()
        {
        }

        /// <summary>
        /// The Host Side of Vs Ide Host Adapter.
        /// </summary>
        private ITestAdapter HostSide
        {
            get
            {
                return _hostSide;
            }
        }

        /// <summary>
        /// ITestAdapter method: called to initialize run context for this adapter.
        /// </summary>
        /// <param name="runContext">The run context to be used for this run</param>
        void ITestAdapter.Initialize(IRunContext runContext)
        {
            Contract.Assert(runContext != null);

            _runContext = runContext;
            _runConfig = _runContext.RunConfig.TestRun.RunConfiguration;
            _workingDir = _runContext.RunContextVariables.GetStringValue("TestDeploymentDir");

            Contract.Assert(_runConfig != null);
            Contract.Assert(!string.IsNullOrEmpty(_workingDir));

            SetupChannels();

            // Install COM message filter to retry COM calls when VS IDE is busy, e.g. when getting the addin from VS IDE.
            // This prevents RPC_E_CALL_REJECTED error when VS IDE is busy.
            _comMessageFilter = new RetryMessageFilter();

            InitHostSide();
        }

        /// <summary>
        /// IBaseAdapter method: called to execute a test.
        /// </summary>
        /// <param name="testElement">The test object to run</param>
        /// <param name="testContext">The Test conext for this test invocation</param>
        void IBaseAdapter.Run(ITestElement testElement, ITestContext testContext)
        {
            if (testElement.TestCategories.Contains(new TestCategoryItem("RestartVS"))) {
                CleanupHostSide();
                InitHostSide();
            }

            _hostSide.Run(testElement, testContext);
        }

        /// <summary>
        /// IBaseAdapter method: called when the test run is complete.
        /// </summary>
        void IBaseAdapter.Cleanup()
        {
            try
            {
                CleanupHostSide();

                // Uninstall COM message filter.
                _comMessageFilter.Dispose();
            }
            finally
            {
                CleanupChannels();
            }
        }

        /// <summary>
        /// IBaseAdapter method: called when the user stops the test run.
        /// </summary>
        void IBaseAdapter.StopTestRun()
        {
            HostSide.StopTestRun();
        }

        /// <summary>
        /// IBaseAdapter method: called when the test run is aborted.
        /// </summary>
        void IBaseAdapter.AbortTestRun()
        {
            HostSide.AbortTestRun();
        }

        /// <summary>
        /// IBaseAdapter method: called when the user pauses the test run.
        /// </summary>
        void IBaseAdapter.PauseTestRun()
        {
            HostSide.PauseTestRun();
        }

        /// <summary>
        /// IBaseAdapter method: called when the user resumes a paused test run.
        /// </summary>
        void IBaseAdapter.ResumeTestRun()
        {
            HostSide.ResumeTestRun();
        }

        /// <summary>
        /// ITestAdapter method: called when a message is sent from the UI or the controller.
        /// </summary>
        /// <param name="obj">The message object</param>
        void ITestAdapter.ReceiveMessage(object obj)
        {
            HostSide.ReceiveMessage(obj);
        }

        /// <summary>
        /// ITestAdapter method: called just before the test run finishes and
        /// gives the adapter a chance to do any clean-up.
        /// </summary>
        /// <param name="runContext">The run context for this run</param>
        void ITestAdapter.PreTestRunFinished(IRunContext runContext)
        {
            HostSide.PreTestRunFinished(runContext);
        }

        /// <summary>
        /// Creates VS IDE and HostSide inside it.
        /// This can be called multiple times in the run if specified to restart VS between tests.
        /// </summary>
        private void InitHostSide()
        {
            Contract.Assert(_runContext != null);
            try
            {
                _vsRegistryHive = GetRegistryHive();

                lock (_hostSideLock)
                {
                    // Get the "host side" of the host adapter.
                    CreateHostSide();

                    // Call Initialize for the host side.
                    ((ITestAdapter)HostSide).Initialize(_runContext);

                    // If test run was started under debugger, attach debugger.
                    CheckAttachDebugger();
                }
            }
            catch (Exception ex)
            {
                // Report the error to the agent.
                SendResult(string.Format(CultureInfo.InvariantCulture, "VsIdeHostAdapter: Failed to initialize the host side: {0}", ex.ToString()), TestOutcome.Error, true);
                throw;
            }
        }


        /// <summary>
        /// Determine which registry hive to use for Visual Studio:
        ///     If using RunConfig, get it from RunConfig
        ///     Else get it from environment.
        /// </summary>
        /// <returns></returns>
        private string GetRegistryHive()
        {
            // Note that Run Config Data can be null, e.g. when executing using HostType attribute.
            TestRunConfiguration runConfig = _runContext.RunConfig.TestRun.RunConfiguration;
            string configKey;
            if (TryGetRegistryHiveFromConfig(runConfig, out configKey))
            {
                return configKey;
            }

            if (System.Environment.GetEnvironmentVariable("RUN_NO_EXP") != null)
                return VSUtility.Version;

            // Default to the experimental hive for the development evnironment.
            return VSUtility.Version + "Exp";
        }

        /// <summary>
        /// Starts new Visual Studio process and obtains host side from it.
        /// </summary>
        private void CreateHostSide()
        {
            Contract.Assert(!string.IsNullOrEmpty(_workingDir));
            Contract.Assert(_hostSide == null);
            Contract.Assert(_vsIde == null);

            // Start devenv.
            _vsIde = new VisualStudioIde(new VsIdeStartupInfo(_vsRegistryHive, _workingDir));
            _vsIde.ErrorHandler += HostProcessErrorHandler;

            Stopwatch timer = Stopwatch.StartNew();
            do
            {
                try
                {
                    _vsIde.Dte.MainWindow.Visible = true;    // This could be in TestRunConfig options for this host type.
                    break;
                }
                catch (Exception)
                {
                }
                System.Threading.Thread.Sleep(_baseSleepDuration);
            } while (timer.Elapsed < _ideStartupTimeout);

            _hostSide = GetHostSideFromAddin();
        }

        /// <summary>
        /// Obtain host side from the addin.
        /// </summary>
        /// <returns></returns>
        private ITestAdapter GetHostSideFromAddin()
        {
            // Find the Addin.
            // Note: After VS starts addin needs some time to load, so we try a few times.
            AddIn addinLookingFor = null;

            Stopwatch timer = Stopwatch.StartNew();
            try
            {
                do
                {
                    try
                    {
                        // There is no index-by-name API, so we have to check all addins.
                        foreach (AddIn addin in _vsIde.Dte.AddIns)
                        {
                            if (addin.Name.StartsWith(VsAddinName))
                            {
                                addinLookingFor = addin;
                                break;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // Catch all exceptions to prevent intermittent failures such as COMException (0x8001010A)
                        // while VS has not started yet. Just retry again until we timeout.
                    }

                    if (addinLookingFor != null)
                    {
                        break;
                    }

                    System.Threading.Thread.Sleep(_baseSleepDuration);

                } while (timer.Elapsed < _addinWaitTimeout);
            }
            finally
            {
                timer.Stop();
            }

            if (addinLookingFor == null)
            {
                throw new VsIdeTestHostException("Timed out getting Vs Ide Test Host Add-in from Visual Studio. Please make sure that the Add-in is installed and started when VS starts (use Tools->Add-in Manager).");
            }

            _testHostAddin = (IVsIdeTestHostAddin)addinLookingFor.Object;
            ITestAdapter hostSide = _testHostAddin.GetHostSide();
            Contract.Assert(hostSide != null);

            return hostSide;
        }

        /// <summary>
        /// Check if we need to attach debugger and attach it.
        /// </summary>
        private void CheckAttachDebugger()
        {
            Contract.Assert(_runConfig != null);

            if (_runConfig.IsExecutedUnderDebugger)
            {
                Contract.Assert(_vsIde != null);

                DebugTargetInfo debugInfo = new DebugTargetInfo();
                debugInfo.ProcessId = _vsIde.Process.Id;
                ExecutionUtilities.DebugTarget(_runContext.ResultSink, _runContext.RunConfig.TestRun.Id, debugInfo, _debuggerTimeout);
            }
        }

        /// <summary>
        /// Clean up the host side.
        /// We do the following steps. Each step is important and must be done whenever previous step throws or not.
        /// - Call HostSide.Cleanup.
        /// - Ask Runner IDE to detach debugger (if attached).
        /// - Dispose m_vsIde.
        /// This should not throw.
        /// </summary>
        private void CleanupHostSide()
        {
            lock (_hostSideLock)
            {
                Contract.Assert(HostSide != null);

                if (HostSide != null)
                {
                    try
                    {
                        HostSide.Cleanup();
                    }
                    catch (Exception ex)    // We don't know what this can throw in advance.
                    {
                        SendResult(string.Format(CultureInfo.InvariantCulture, "Warning: VsIdeHostAdapter failed to call ITestAdapter.Cleanup: {0}", ex), TestOutcome.Warning);
                    }
                }

                try
                {
                    Contract.Assert(_vsIde != null);
                    _vsIde.Dispose();
                }
                catch (InvalidComObjectException)
                {
                    // This exception is always thrown when quitting VS. Nothing
                    // is gained by reporting it for every run.
                }
                catch (Exception ex)
                {
                    SendResult(string.Format(CultureInfo.InvariantCulture, "Warning: VsIdeHostAdapter: error shutting down VS IDE: {0}", ex), TestOutcome.Warning);
                }

                _vsIde = null;
                _hostSide = null;  // Note: host side lifetime is controlled by the addin.
            }
        }

        /// <summary>
        /// Set up remoting communication channels.
        /// </summary>
        private void SetupChannels()
        {
            string channelPrefix = "EqtVsIdeHostAdapter_" + Guid.NewGuid().ToString();

            // Server channel is required for callbacks from client side.
            // Actually it is not required when running from vstesthost as vstesthost already sets up the channels
            // but since we have /noisolation (~ no vstesthost) mode we need to create this channel. 
            BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
            serverProvider.TypeFilterLevel = TypeFilterLevel.Full;  // Enable remoting objects as arguments.
            Hashtable props = new Hashtable();
            string serverChannelName = channelPrefix + "_ServerChannel";
            props["name"] = serverChannelName;
            props["portName"] = serverChannelName;           // Must be different from client's port.
            // Default IpcChannel security is: allow for all users who can authorize on this machine.
            props["authorizedGroup"] = WindowsIdentity.GetCurrent().Name;
            _serverChannel = new IpcServerChannel(props, serverProvider);
            ChannelServices.RegisterChannel(_serverChannel, false);

            _clientChannel = new IpcClientChannel(channelPrefix + "_ClientChannel", new BinaryClientFormatterSinkProvider());
            ChannelServices.RegisterChannel(_clientChannel, false);
        }

        /// <summary>
        /// Clean up remoting communication channels.
        /// </summary>
        private void CleanupChannels()
        {
            if (_clientChannel != null)
            {
                ChannelServices.UnregisterChannel(_clientChannel);
                _clientChannel = null;
            }
            if (_serverChannel != null)
            {
                ChannelServices.UnregisterChannel(_serverChannel);
                _serverChannel = null;
            }
        }

        /// <summary>
        /// Error handler for the Visual Studio process exited event.
        /// </summary>
        /// <param name="errorMessage">The error message.</param>
        /// <param name="outcome">The outcome for the test.</param>
        /// <param name="abortTestRun">Whether test run needs to be aborted.</param>
        private void HostProcessErrorHandler(string errorMessage, TestOutcome outcome, bool abortTestRun)
        {
            Contract.Assert(_runContext != null);
            SendResult(errorMessage, outcome, abortTestRun);
        }

        /// <summary>
        /// Helper method to send message to the Agent.
        /// </summary>
        /// <param name="messageText">The text for the message.</param>
        /// <param name="outcome">The outcome for the test.</param>
        private void SendResult(string messageText, TestOutcome outcome)
        {
            SendResult(messageText, outcome, false);
        }

        /// <summary>
        /// Sends run level message to the result sink.
        /// </summary>
        /// <param name="message">Text for the message.</param>
        /// <param name="outcome">Outcome for the message. Affects test run outcome.</param>
        /// <param name="abortTestRun">If true, we use TMK.Panic, otherwise TMK.TextMessage</param>
        private void SendResult(string messageText, TestOutcome outcome, bool abortTestRun)
        {
            Contract.Assert(!abortTestRun || outcome == TestOutcome.Error,
                "SendResult: When abortTestRun = true, Outcome should be Error.");

            TestRunTextResultMessage message = new TestRunTextResultMessage(
                Environment.MachineName,
                _runContext.RunConfig.TestRun.Id,
                messageText,
                TestMessageKind.TextMessage);
            message.Outcome = outcome;

            _runContext.ResultSink.AddResult(message);

            if (abortTestRun)
            {
                _runContext.ResultSink.AddResult(new RunStateEvent(_runContext.RunConfig.TestRun.Id,
                                                                RunState.Aborting,
                                                                Environment.MachineName));
            }
        }

        private bool TryGetRegistryHiveFromConfig(TestRunConfiguration runConfig, out string hive)
        {
            const string VSHiveElement = "VSHive";
            hive = null;

            if (runConfig == null)
            {
                return false;
            }
            string configFileName = runConfig.Storage;
            if (string.IsNullOrEmpty(configFileName))
            {
                return false;
            }
            if (!File.Exists(configFileName))
            {
                // This will happen in the case with no file where a default file is created.
                if (!configFileName.StartsWith("default", StringComparison.OrdinalIgnoreCase))
                {
                    SendResult(string.Format(CultureInfo.InvariantCulture, "VsIdeHostAdapter: Unable to find config file: {0}", configFileName), TestOutcome.Warning, false);
                }
                return false;
            }

            try
            {
                using (var configXml = new XmlTextReader(configFileName))
                {
                    while (configXml.Read())
                    {
                        if (configXml.NodeType == XmlNodeType.Element && configXml.Name == VSHiveElement)
                        {
                            configXml.Read();
                            if (configXml.NodeType == XmlNodeType.Text)
                            {
                                hive = configXml.Value;
                                return true;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // Report the error to the agent.
                SendResult(string.Format(CultureInfo.InvariantCulture, "VsIdeHostAdapter: Error reading config file: {0}", ex.ToString()), TestOutcome.Warning, false);
            }
            return false;
        }

        /// <summary>
        /// The information to start Visual Studio.
        /// </summary>
        internal class VsIdeStartupInfo
        {
            private string _registryHive;
            private string _workingDirectory;

            internal VsIdeStartupInfo(string registryHive, string workingDirectory)
            {
                // Note: registryHive can be null when using the attribute. That's OK, VsIde will figure out.
                Contract.Assert(!string.IsNullOrEmpty(workingDirectory));

                _registryHive = registryHive;
                _workingDirectory = workingDirectory;
            }

            /// <summary>
            /// Hive name under Microsoft.VisualStudio, like 10.0Exp.
            /// </summary>
            internal string RegistryHive
            {
                get { return _registryHive; }
                set { _registryHive = value; }
            }

            /// <summary>
            /// Working directory for devenv.exe process.
            /// </summary>
            internal string WorkingDirectory
            {
                get { return _workingDirectory; }
            }
        }

        /// <summary>
        /// This wraps Visual Studio DTE (automation object).
        /// </summary>
        internal class VisualStudioIde : IDisposable
        {

            /// <summary>
            /// Used for error reporting.
            /// </summary>
            /// <param name="errorMessage">Error message.</param>
            /// <param name="outcome">The outcome for the test due to this error.</param>
            /// <param name="abortTestRun">Whether the error causes test run to abort.</param>
            internal delegate void VsIdeHostErrorHandler(string errorMessage, TestOutcome outcome, bool abortTestRun);

            private const string BaseProgId = "VisualStudio.DTE";
            // HRESULTs for COM errors.
            private const int CallRejectedByCalleeErrorCode = -2147418111;

            /// <summary>
            /// Time to wait for the VS process to exit after resetting it's profile. 
            /// </summary>
            private static readonly TimeSpan _ideFirstRunTimeout = TimeSpan.FromMinutes(2);

            /// <summary>
            /// How long to wait for IDE to appear in ROT. 
            /// </summary>
            private static readonly TimeSpan _ideStartupTimeout = TimeSpan.FromMinutes(2);

            /// <summary>
            /// How long to wait before killing devenv.exe after Dispose() is called. During this time VS can e.g. save buffers to disk.
            /// </summary>
            private static readonly TimeSpan _ideExitTimeout = TimeSpan.FromSeconds(5);

            /// <summary>
            /// Timeout to wait while VS rejects calls.
            /// </summary>
            private static readonly TimeSpan _rejectedCallTimeout = TimeSpan.FromSeconds(30);


            private DTE _dte;
            private Process _process;
            private object _cleanupLock = new object();

            /// <summary>
            /// Constructor. Starts new instance of VS IDE.
            /// </summary>
            public VisualStudioIde(VsIdeStartupInfo info)
            {
                Contract.Assert(info != null);

                if (string.IsNullOrEmpty(info.RegistryHive))
                {
                    info.RegistryHive = VsRegistry.GetDefaultVersion();
                    if (string.IsNullOrEmpty(info.RegistryHive))
                    {
                        throw new VsIdeTestHostException(string.Format(CultureInfo.InvariantCulture, "Cannot find installation of Visual Studio in '{0}' registry hive.", info.RegistryHive));
                    }
                }

                StartNewInstance(info);
            }

            /// <summary>
            /// Finalizer.
            /// </summary>
            ~VisualStudioIde()
            {
                Dispose(false);
            }

            public DTE Dte
            {
                get { return _dte; }
            }

            public Process Process
            {
                get { return _process; }
            }

            public event VsIdeHostErrorHandler ErrorHandler;

            private static void ResetVSSettings(string vsPath, string hiveSuffix)
            {
                Process process = new Process();
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.FileName = vsPath;
                process.StartInfo.Arguments = "/resetsettings \"General.vssettings\" /Command \"File.Exit\"";
                if (!string.IsNullOrEmpty(hiveSuffix))
                {
                    process.StartInfo.Arguments += " /RootSuffix " + hiveSuffix;
                }

                // Launch VS for the "first time"
                if (!process.Start())
                {
                    throw new VsIdeTestHostException("Failed to start Visual Studio process.");
                }

                // Wait for the process to exit
                if (!process.WaitForExit((int)_ideFirstRunTimeout.TotalMilliseconds))
                {
                    // Kill the process and raise an exception 
                    process.Kill();

                    string message = string.Format(
                        CultureInfo.InvariantCulture,
                        "First run Visual Studio process, used for resetting settings did not exit after {0} seconds.",
                        _ideFirstRunTimeout.TotalSeconds);

                    throw new TimeoutException(message);
                }
            }

            /// <summary>
            /// Create a Visual Studio process.
            /// </summary>
            /// <param name="info">Startup information.</param>
            private void StartNewInstance(VsIdeStartupInfo startupInfo)
            {
                Contract.Assert(startupInfo != null);
                Contract.Assert(_process == null, "VisualStudioIde.StartNewInstance: _process should be null!");

                Process process = new Process();
                process.StartInfo.UseShellExecute = false;
                if (startupInfo.WorkingDirectory != null)
                {
                    process.StartInfo.WorkingDirectory = startupInfo.WorkingDirectory;
                }

                // Note that this needs to be partial (not $-terminated) as we partially match/replace.
                Regex versionRegex = new Regex(@"^[0-9]+\.[0-9]+");

                string hiveVersion = versionRegex.Match(startupInfo.RegistryHive).Value;
                string hiveSuffix = versionRegex.Replace(startupInfo.RegistryHive, string.Empty);

                if (!string.IsNullOrEmpty(hiveSuffix)) {
                    process.StartInfo.Arguments = "/RootSuffix " + hiveSuffix + " /Log";
                } else {
                    process.StartInfo.Arguments = "/Log";
                }

                process.StartInfo.FileName = VsRegistry.GetVsLocation(hiveVersion);
                Contract.Assert(!string.IsNullOrEmpty(process.StartInfo.FileName));

                // Prevent the settings popup on startup
                if (!VsRegistry.UserSettingsArchiveExists(startupInfo.RegistryHive))
                {
                    ResetVSSettings(process.StartInfo.FileName, hiveSuffix);
                    if (!VsRegistry.UserSettingsArchiveExists(startupInfo.RegistryHive))
                    {
                        throw new VsIdeTestHostException("Unable to reset VS settings.");
                    }
                }

                process.Exited += new EventHandler(ProcessExited);
                process.EnableRaisingEvents = true;

                if (!process.Start())
                {
                    throw new VsIdeTestHostException("Failed to start Visual Studio process.");
                }

                _process = process;

                string progId = string.Format(CultureInfo.InvariantCulture, "{0}.{1}", VisualStudioIde.BaseProgId, hiveVersion);
                _dte = GetDteFromRot(progId, _process.Id);
                if (_dte == null)
                {
                    throw new VsIdeTestHostException("Failed to get the DTE object from Visual Studio process. Please make sure that the Add-in is registered in Tools->Add-in Manager to load in when Visual Studio starts.");
                }
            }

            /// <summary>
            /// Obtains Visual Studio automation object from Running Object Table.
            /// </summary>
            /// <param name="progId">DTE's prog id.</param>
            /// <param name="processId">Visual Studio process id to obtain the automation object for.</param>
            /// <returns>Visual Studio automation object.</returns>
            public static DTE GetDteFromRot(string progId, int processId)
            {
                Contract.Assert(!string.IsNullOrEmpty(progId));

                EnvDTE.DTE dte;
                string moniker = string.Format(CultureInfo.InvariantCulture, "!{0}:{1}", progId, processId);

                // It takes some time after process started to register in ROT.
                Stopwatch sw = Stopwatch.StartNew();
                do
                {
                    dte = GetDteFromRot(moniker);
                    if (dte != null)
                    {
                        break;
                    }
                    System.Threading.Thread.Sleep(_baseSleepDoubleDuration);
                } while (sw.Elapsed < _ideStartupTimeout);

                if (dte == null)
                {
                    throw new VsIdeTestHostException("Timed out getting VS.DTE from COM Running Object Table.");
                }
                return dte;
            }

            /// <summary>
            /// Obtains Visual Studio automation object from Running Object Table.
            /// </summary>
            /// <param name="monikerName">The moniker to use as a filter when looking in Running Object Table.</param>
            /// <returns></returns>
            private static DTE GetDteFromRot(string monikerName)
            {
                Contract.Assert(!string.IsNullOrEmpty(monikerName));

                IRunningObjectTable rot;
                IEnumMoniker monikerEnumerator;
                object dte = null;
                try
                {
                    NativeMethods.GetRunningObjectTable(0, out rot);
                    rot.EnumRunning(out monikerEnumerator);
                    monikerEnumerator.Reset();

                    uint fetched = 0;
                    IMoniker[] moniker = new IMoniker[1];
                    while (monikerEnumerator.Next(1, moniker, out fetched) == 0)
                    {
                        IBindCtx bindingContext;
                        NativeMethods.CreateBindCtx(0, out bindingContext);

                        string name;
                        moniker[0].GetDisplayName(bindingContext, null, out name);
                        if (name == monikerName)
                        {
                            object returnObject;
                            rot.GetObject(moniker[0], out returnObject);
                            dte = (object)returnObject;
                            break;
                        }
                    }
                }
                catch
                {
                    return null;
                }

                return (DTE)dte;
            }

            /// <summary>
            /// Called when Visual Studio process exits.
            /// </summary>
            /// <param name="sender">The sender of the event.</param>
            /// <param name="args">Event arguments.</param>
            private void ProcessExited(object sender, EventArgs args)
            {
                lock (_cleanupLock)
                {
                    _process.EnableRaisingEvents = false;
                    _process.Exited -= new EventHandler(ProcessExited);

                    if (ErrorHandler != null)
                    {
                        ErrorHandler("Visual Studio process exited unexpectedly.", TestOutcome.Error, true);
                    }
                }
            }

            /// <summary>
            /// Implements Idisposable.Dispose.
            /// </summary>
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }

            /// <summary>
            /// The other part of the .NET Dispose pattern.
            /// </summary>
            /// <param name="disposingNotFinalizing">Whether this is explicit dispose, not auto-finalization of the object.</param>
            private void Dispose(bool explicitDispose)
            {
                if (!explicitDispose)
                {
                    // When called from finalizer, just clean up. Don't lock. Don't throw.
                    KillProcess();
                }
                else
                {
                    lock (_cleanupLock)
                    {
                        if (_process.EnableRaisingEvents)
                        {
                            _process.EnableRaisingEvents = false;
                            _process.Exited -= new EventHandler(ProcessExited);
                        }

                        try
                        {
                            if (_dte != null)
                            {
                                // Visual Studio sometimes rejects the call to Quit() so we need to retry it.
                                Stopwatch sw = Stopwatch.StartNew();
                                bool timedOut = true;
                                do
                                {
                                    try 
                                    {
                                        _dte.Quit();
                                        timedOut = false;
                                        break;
                                    } 
                                    catch (COMException ex) 
                                    {
                                        if (ex.ErrorCode == CallRejectedByCalleeErrorCode) 
                                        {
                                            System.Threading.Thread.Sleep(_baseSleepDoubleDuration);
                                        } 
                                        else 
                                        {
                                            throw;
                                        }
                                    } 
                                    catch (Exception) 
                                    {
                                        throw;
                                    }
                                } while (sw.Elapsed < _rejectedCallTimeout);

                                if (timedOut)
                                {
                                    throw new VsIdeTestHostException("Warning: timed out calling Dte.Quit: all the calls were rejected. Visual Studio process will be killed.");
                                }
                            }
                        }
                        finally
                        {
                            KillProcess();
                        }
                    }
                }
            }

            /// <summary>
            /// Waits for Visual Studio process to exit and if it does not in s_ideExitTimeout time, kills it.
            /// </summary>
            private void KillProcess()
            {
                if (_process != null)
                {
                    // wait for the specified time for the IDE to exit.  
                    // If it hasn't, kill the process so we can proceed to the next test.
                    Stopwatch sw = Stopwatch.StartNew();
                    while (!_process.HasExited && (sw.Elapsed < _ideExitTimeout))
                    {
                        System.Threading.Thread.Sleep(_baseSleepDuration);
                    }

                    if (!_process.HasExited)
                    {
                        _process.Kill();
                    }

                    _process = null;
                }
            }
        }

        /// <summary>
        /// COM message filter class to prevent RPC_E_CALL_REJECTED error while DTE is busy.
        /// The filter is used by COM to handle incoming/outgoing messages while waiting for response from a synchonous call.
        /// </summary>
        /// <seealso cref="http://msdn.microsoft.com/library/en-us/com/html/e12d48c0-5033-47a8-bdcd-e94c49857248.asp"/>
        [ComVisible(true)]
        internal class RetryMessageFilter : IMessageFilter, IDisposable
        {
            private const uint RetryCall = 99;
            private const uint CancelCall = unchecked((uint)-1);   // For COM this must be -1 but IMessageFilter.RetryRejectedCall returns uint.

            private IMessageFilter _oldFilter;

            /// <summary>
            /// Constructor.
            /// </summary>
            public RetryMessageFilter()
            {
                // Register the filter.
                NativeMethods.CoRegisterMessageFilter(this, out _oldFilter);
            }

            /// <summary>
            /// FInalizer.
            /// </summary>
            ~RetryMessageFilter()
            {
                Dispose();
            }

            /// <summary>
            /// Implements IDisposable.Dispose.
            /// </summary>
            public void Dispose()
            {
                // Unregister the filter.
                IMessageFilter ourFilter;
                NativeMethods.CoRegisterMessageFilter(_oldFilter, out ourFilter);
                GC.SuppressFinalize(this);
            }

            /// <summary>
            /// Provides an ability to filter or reject incoming calls (or callbacks) to an object or a process. 
            /// Called by COM prior to each method invocation originating outside the current process. 
            /// </summary>
            public uint HandleInComingCall(uint dwCallType, IntPtr htaskCaller, uint dwTickCount, INTERFACEINFO[] lpInterfaceInfo)
            {
                // Let current process try process the call.
                return (uint)SERVERCALL.SERVERCALL_ISHANDLED;
            }

            /// <summary>
            /// An ability to choose to retry or cancel the outgoing call or switch to the task specified by threadIdCallee.
            /// Called by COM immediately after receiving SERVERCALL_RETRYLATER or SERVERCALL_REJECTED
            /// from the IMessageFilter::HandleIncomingCall method on the callee's IMessageFilter interface.
            /// </summary>
            /// <returns>
            /// -1: The call should be canceled. COM then returns RPC_E_CALL_REJECTED from the original method call. 
            /// 0..99: The call is to be retried immediately. 
            /// 100 and above: COM will wait for this many milliseconds and then retry the call.
            /// </returns>
            public uint RetryRejectedCall(IntPtr htaskCallee, uint dwTickCount, uint dwRejectType)
            {
                if (dwRejectType == (uint)SERVERCALL.SERVERCALL_RETRYLATER)
                {
                    // The server called by this process is busy. Ask COM to retry the outgoing call.
                    return RetryCall;
                }
                else
                {
                    // Ask COM to cancel the call and return RPC_E_CALL_REJECTED from the original method call. 
                    return CancelCall;
                }
            }

            /// <summary>
            /// Called by COM when a Windows message appears in a COM application's message queue 
            /// while the application is waiting for a reply to an outgoing remote call. 
            /// </summary>
            /// <returns>
            /// Tell COM whether: to process the message without interrupting the call, 
            /// to continue waiting, or to cancel the operation. 
            /// </returns>
            public uint MessagePending(IntPtr htaskCallee, uint dwTickCount, uint dwPendingType)
            {
                // Continue waiting for the reply, and do not dispatch the message unless it is a task-switching or window-activation message. 
                // A subsequent message will trigger another call to IMessageFilter::MessagePending. 
                return (uint)PENDINGMSG.PENDINGMSG_WAITDEFPROCESS;
            }

        }

        /// <summary>
        /// The data for this host type to extend Run Config with.
        /// - Registry Hive, like 10.0Exp.
        /// - Session id for debugging.
        /// </summary>
        [Serializable]
        internal class RunConfigData : IHostSpecificRunConfigurationData, IXmlTestStore, IXmlTestStoreCustom
        {
            private const string RegistryHiveAttributeName = "registryHive";
            private const string XmlNamespaceUri = "http://microsoft.com/schemas/TC/TCTestHostAdapters";
            private const string XmlElementName = "VsIdeTestHostRunConfig";

            /// <summary>
            /// The registry hive of VS to use for the VS instance to start.
            /// This field is persisted in the .TestRunConfig file.
            /// </summary>
            private string _registryHive;

            /// <summary>
            /// Constructor.
            /// </summary>
            /// <param name="registryHive">The registry hive to use settings from for new Visual Studio instance.</param>
            internal RunConfigData(string registryHive)
            {
                _registryHive = registryHive;  // null is OK. null means get latest version.
            }

            /// <summary>
            /// The description of this host to use in Run Config dialog.
            /// </summary>
            public string RunConfigurationInformation
            {
                get 
                {
                    return "VsIdeHostAdapter Test Host Configuration Data"; 
                }
            }

            /// <summary>
            /// Implements ICloneable.Clone.
            /// </summary>
            public object Clone()
            {
                return new RunConfigData(_registryHive);
            }

            /// <summary>
            /// The registry hive to use settings from for new Visual Studio instance.
            /// </summary>
            internal string RegistryHive
            {
                get { return _registryHive; }
                set { _registryHive = value; }
            }

            public void Load(XmlElement element, XmlTestStoreParameters parameters)
            {
                this.RegistryHive = element.GetAttribute(RegistryHiveAttributeName);
            }

            public void Save(XmlElement element, XmlTestStoreParameters parameters)
            {
                element.SetAttribute(RegistryHiveAttributeName, this.RegistryHive);
            }


            public string ElementName
            {
                get { return XmlElementName; }
            }

            public string NamespaceUri
            {
                get { return XmlNamespaceUri; }
            }

        }
        /// <summary>
        /// Helper class for VS registry.
        /// Used by both Host Adapter and UI side.
        /// </summary>
        internal static class VsRegistry
        {
            private const string ProcessName = "devenv.exe";
            internal const string VSRegistryRoot = @"SOFTWARE\Microsoft\VisualStudio";

            /// <summary>
            /// Obtains all installed Visual Studio versions.
            /// </summary>
            internal static List<string> GetVersions()
            {
                List<string> versions = new List<string>();
                GetVersionsHelper(versions);
                return versions;
            }

            /// <summary>
            /// Returns max version without suffix.
            /// </summary>
            internal static string GetDefaultVersion()
            {
                return VSUtility.Version;
            }

            internal static bool UserSettingsArchiveExists(string registryHive)
            {
                // This must be a key that does not get set if you start up, hit the no settings prompt and select 
                // "Exit Visual Studio", but does get set if you select a default
#if DEV12_OR_LATER
                const string SettingsMarkerKey = @"General\\ToolsOptions";
#else
                const string SettingsMarkerKey = @"StartPage";
#endif

                string versionKeyName = VSRegistryRoot + @"\" + registryHive;
                using (RegistryKey hive = Registry.CurrentUser.OpenSubKey(versionKeyName))
                {
                    if (hive == null)
                    {
                        return false;
                    }
                    using (RegistryKey settingsMarker = hive.OpenSubKey(SettingsMarkerKey))
                    {
                        return settingsMarker != null;
                    }
                }
            }


            /// <summary>
            /// Returns location of devenv.exe on disk.
            /// </summary>
            /// <param name="registryHive">The registry hive (version) of Visual Studio to get location for.</param>
            internal static string GetVsLocation(string registryHive)
            {
                Contract.Assert(!string.IsNullOrEmpty(registryHive));

                string versionKeyName = VSRegistryRoot + @"\" + registryHive;

                string installDir = null; 
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(versionKeyName))
                {
                    if (key != null)
                    {
                        object value = key.GetValue("InstallDir");
                        installDir = value as String;
                    }
                }
                if (string.IsNullOrEmpty(installDir))
                {
                    throw new VsIdeTestHostException(string.Format(CultureInfo.InvariantCulture, "Cannot find installation of Visual Studio in '{0}' registry hive.", registryHive));
                }

                return Path.Combine(installDir, ProcessName);
            }

            /// <summary>
            /// Obtains installed Visual Studio versions and default version.
            /// </summary>
            /// <param name="versions">If null, this is ignored.</param>
            /// <returns>Returns default version = max version without suffix.</returns>
            private static string GetVersionsHelper(List<string> versions)
            {
                // Default is the latest version without suffix, like 10.0.
                string defaultVersion = null;
                Regex versionNoSuffixRegex = new Regex(@"^[0-9]+\.[0-9]+$");

                // Note that the version does not have to be numeric only: can be 10.0Exp.
                using (RegistryKey vsKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio"))
                {
                    foreach (string versionKeyName in vsKey.GetSubKeyNames())
                    {
                        // If there's no InstallDir subkey we skip this key.
                        using (RegistryKey versionKey = vsKey.OpenSubKey(versionKeyName))
                        {
                            if (versionKey.GetValue("InstallDir") == null)
                            {
                                continue;
                            }
                            if (versions != null)
                            {
                                versions.Add(versionKeyName);
                            }
                        }

                        if (versionNoSuffixRegex.Match(versionKeyName).Success &&
                            string.Compare(versionKeyName, defaultVersion, StringComparison.OrdinalIgnoreCase) > 0) // null has the smallest value.
                        {
                            defaultVersion = versionKeyName;
                        }
                    }
                }

                return defaultVersion;
            }
        }
    }
}
Back to Top