diff --git a/OpenConsole.sln b/OpenConsole.sln index 722804733..788474d06 100644 --- a/OpenConsole.sln +++ b/OpenConsole.sln @@ -193,6 +193,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WindowsTerminal", "src\casc EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TerminalApp", "src\cascadia\TerminalApp\TerminalApp.vcxproj", "{CA5CAD1A-44BD-4AC7-AC72-F16E576FDD12}" ProjectSection(ProjectDependencies) = postProject + {CA5CAD1A-9A12-429C-B551-8562EC954746} = {CA5CAD1A-9A12-429C-B551-8562EC954746} {CA5CAD1A-C46D-4588-B1C0-40F31AE9100B} = {CA5CAD1A-C46D-4588-B1C0-40F31AE9100B} {CA5CAD1A-44BD-4AC7-AC72-6CA5B3AB89ED} = {CA5CAD1A-44BD-4AC7-AC72-6CA5B3AB89ED} {CA5CAD1A-D7EC-4107-B7C6-79CB77AE2907} = {CA5CAD1A-D7EC-4107-B7C6-79CB77AE2907} @@ -231,6 +232,17 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{89CDCC EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Types.Unit.Tests", "src\types\ut_types\Types.Unit.Tests.vcxproj", "{34DE34D3-1CD6-4EE3-8BD9-A26B5B27EC73}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "UnitTests_TerminalApp", "src\cascadia\ut_app\TerminalApp.UnitTests.vcxproj", "{CA5CAD1A-9333-4D05-B12A-1905CBF112F9}" + ProjectSection(ProjectDependencies) = postProject + {CA5CAD1A-9A12-429C-B551-8562EC954746} = {CA5CAD1A-9A12-429C-B551-8562EC954746} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TerminalAppLib", "src\cascadia\TerminalApp\lib\TerminalAppLib.vcxproj", "{CA5CAD1A-9A12-429C-B551-8562EC954746}" + ProjectSection(ProjectDependencies) = postProject + {CA5CAD1A-44BD-4AC7-AC72-6CA5B3AB89ED} = {CA5CAD1A-44BD-4AC7-AC72-6CA5B3AB89ED} + {CA5CAD1A-D7EC-4107-B7C6-79CB77AE2907} = {CA5CAD1A-D7EC-4107-B7C6-79CB77AE2907} + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution AuditMode|ARM64 = AuditMode|ARM64 @@ -1102,6 +1114,42 @@ Global {34DE34D3-1CD6-4EE3-8BD9-A26B5B27EC73}.Release|x64.Build.0 = Release|x64 {34DE34D3-1CD6-4EE3-8BD9-A26B5B27EC73}.Release|x86.ActiveCfg = Release|Win32 {34DE34D3-1CD6-4EE3-8BD9-A26B5B27EC73}.Release|x86.Build.0 = Release|Win32 + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9}.AuditMode|ARM64.ActiveCfg = AuditMode|ARM64 + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9}.AuditMode|ARM64.Build.0 = AuditMode|ARM64 + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9}.AuditMode|x64.ActiveCfg = AuditMode|x64 + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9}.AuditMode|x64.Build.0 = AuditMode|x64 + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9}.AuditMode|x86.ActiveCfg = AuditMode|Win32 + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9}.AuditMode|x86.Build.0 = AuditMode|Win32 + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9}.Debug|ARM64.Build.0 = Debug|ARM64 + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9}.Debug|x64.ActiveCfg = Debug|x64 + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9}.Debug|x64.Build.0 = Debug|x64 + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9}.Debug|x86.ActiveCfg = Debug|Win32 + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9}.Debug|x86.Build.0 = Debug|Win32 + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9}.Release|ARM64.ActiveCfg = Release|ARM64 + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9}.Release|ARM64.Build.0 = Release|ARM64 + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9}.Release|x64.ActiveCfg = Release|x64 + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9}.Release|x64.Build.0 = Release|x64 + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9}.Release|x86.ActiveCfg = Release|Win32 + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9}.Release|x86.Build.0 = Release|Win32 + {CA5CAD1A-9A12-429C-B551-8562EC954746}.AuditMode|ARM64.ActiveCfg = Release|ARM64 + {CA5CAD1A-9A12-429C-B551-8562EC954746}.AuditMode|ARM64.Build.0 = Release|ARM64 + {CA5CAD1A-9A12-429C-B551-8562EC954746}.AuditMode|x64.ActiveCfg = Release|x64 + {CA5CAD1A-9A12-429C-B551-8562EC954746}.AuditMode|x64.Build.0 = Release|x64 + {CA5CAD1A-9A12-429C-B551-8562EC954746}.AuditMode|x86.ActiveCfg = Release|Win32 + {CA5CAD1A-9A12-429C-B551-8562EC954746}.AuditMode|x86.Build.0 = Release|Win32 + {CA5CAD1A-9A12-429C-B551-8562EC954746}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {CA5CAD1A-9A12-429C-B551-8562EC954746}.Debug|ARM64.Build.0 = Debug|ARM64 + {CA5CAD1A-9A12-429C-B551-8562EC954746}.Debug|x64.ActiveCfg = Debug|x64 + {CA5CAD1A-9A12-429C-B551-8562EC954746}.Debug|x64.Build.0 = Debug|x64 + {CA5CAD1A-9A12-429C-B551-8562EC954746}.Debug|x86.ActiveCfg = Debug|Win32 + {CA5CAD1A-9A12-429C-B551-8562EC954746}.Debug|x86.Build.0 = Debug|Win32 + {CA5CAD1A-9A12-429C-B551-8562EC954746}.Release|ARM64.ActiveCfg = Release|ARM64 + {CA5CAD1A-9A12-429C-B551-8562EC954746}.Release|ARM64.Build.0 = Release|ARM64 + {CA5CAD1A-9A12-429C-B551-8562EC954746}.Release|x64.ActiveCfg = Release|x64 + {CA5CAD1A-9A12-429C-B551-8562EC954746}.Release|x64.Build.0 = Release|x64 + {CA5CAD1A-9A12-429C-B551-8562EC954746}.Release|x86.ActiveCfg = Release|Win32 + {CA5CAD1A-9A12-429C-B551-8562EC954746}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1160,6 +1208,8 @@ Global {05500DEF-2294-41E3-AF9A-24E580B82836} = {89CDCC5C-9F53-4054-97A4-639D99F169CD} {1E4A062E-293B-4817-B20D-BF16B979E350} = {89CDCC5C-9F53-4054-97A4-639D99F169CD} {34DE34D3-1CD6-4EE3-8BD9-A26B5B27EC73} = {89CDCC5C-9F53-4054-97A4-639D99F169CD} + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9} = {59840756-302F-44DF-AA47-441A9D673202} + {CA5CAD1A-9A12-429C-B551-8562EC954746} = {59840756-302F-44DF-AA47-441A9D673202} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3140B1B7-C8EE-43D1-A772-D82A7061A271} diff --git a/doc/cascadia/Unittesting-CppWinRT-Xaml.md b/doc/cascadia/Unittesting-CppWinRT-Xaml.md new file mode 100644 index 000000000..9fd3378df --- /dev/null +++ b/doc/cascadia/Unittesting-CppWinRT-Xaml.md @@ -0,0 +1,527 @@ +# Getting TAEF unittests to work with a C++/WinRT XAML Islands application + +* __Author__: Mike Griese @zadjii-msft +* __Created on__: 2019-06-06 + + +So you've built a Win32 application that uses XAML Islands to display it's UI +with C++/WinRT. How do you go about adding unittests to this application? I'm +going to cover the steps that I took to get the Windows Terminal updated to be +able to test not only our C++/WinRT components, but also pure c++ classes that +were used in the application, and components that used XAML UI elements. + +## Prerequisites + +Make sure you're using at least the 2.0.190605.7 version of the CppWinRT nuget +package. Prior to this version, there are some bugs with C++/WinRT's detection +of static lib dependencies. You might be able to get your build working with +Visual Studio on earlier versions, but not straight from MsBuild. + +Also, if you're going to be running your tests in a CI build of some sort, make +sure that your tests are running on a machine running at least Windows 18362. If +your CI isn't running that version, then this doesn't matter at all. + +Furthermore, you may need an updated TAEF package as well. Our CI uses the TAEF +VsTest adapter to allow ADO to run TAEF tests in CI. However, there's a bug in +the tests adapter that prevents it from running tests in a UAP context. The +`10.38.190605002` TAEF is the most recent release at the time of writing, +however, that doesn't have the fix necessary. Fortunately, the TAEF team was +kind enough to prototype a fix for us, which is the version +`10.38.190610001-uapadmin`, which we're using in this repo until an official +release with the fix is available. + +## Move the C++/WinRT implementation to a static lib + +By default, most (newly authored) C++/WinRT components are authored as a dll +that can be used to activate your types. However, you might have other classes +in that binary that you want to be able to test, which aren't winrt types. If +the implementation is sitting in a DLL, it'll be hard to write a TAEF unittest +dll that can call the pure c++ types you've defined. + +The first thing you're going to need to do is move the implementation of your +winrt component from a dll to a static lib. Once you have the static lib, we'll +be able to link it into the dll you were previously producing, as well as being +able to link it into the dll we'll be using to test the types. Once this is +complete, your dll project will exist as little more than some extra packaging +for your new lib, as all your code will be built by the lib. + +To aid in this description, I'll be referring to the projects that we changed. +The dll project we changed to a lib was the `TerminalApp` project. From it, we +created a new `TerminalAppLib` project, and changed `TerminalApp` to create a +dll by linking the lib `TerminalAppLib` produced. + +### Create the static lib project + +We'll start by creating a new static lib project. The easiest way to do this is +by copying your existing dll `vcxproj` file into a new file. Make sure to change +the `ProjectGuid` and to add the new project to your `.sln` file. Then, change +the `ConfigurationType` to `StaticLibrary`. This Lib should be responsible for +building all of your headers, `.cpp` files, `.idl`s for your winrt types, and +any `.xaml` files you might have. + +You'll likely need to place this new file into a separate directory from the +existing dll project, as C++/WinRT uses the project directory as the root of the +intermediate build tree. Each directory should only have one `.vcxproj` file in +it. For the Terminal project, we created a subdirectory `lib/` underneath +`TerminalApp/`, and updated the `Include` paths to properly point at the +original files. You could alternatively put all the source in one directory, and +have separate `dll/` and `lib/` subdirectories from the source that are solely +responsible for building their binary. + +At this point, you might face some difficulty including the right wimnd +references, especially from other C++/WinRT dependencies for this project that +exist in your solution. I don't know why, but I had a fair amount of difficulty +using a `ProjectReference` from a C++/WinRT StaticLibrary to another C++/WinRT +project in my solution. If you're referring to any other projects, you'll need +to set up a reference to their built `.winmd`'s manually. + +As an example, here's how we've added a reference to the `TerminalSettings` +project from our `TerminalAppLib` project: + +```xml + + + + + $(SolutionDir)$(Platform)\$(Configuration)\TerminalSettings\Microsoft.Terminal.Settings.winmd + true + false + false + + +``` + +The `HintPath` may be different depending on your project structure - verify +locally the right path to the `.winmd` file you're looking for. + +Notably, you'll also need to put a `pch.h` and `pch.cpp` in the new lib's +directory, and use them instead of the `pch.h` used by the dll. C++/WinRT will be +very angry with you if you try to use a `pch.h` in another directory. Since +we're putting all the code into the static lib project, take your existing +`pch.h` and move it to the lib project's directory and create an empty `pch.h` +in the dll project's directory. + +### Update the dll project + +Now that we havea lib that builds all your code, we can go ahead and tear out +most of the dead code from the old dll project. Remove all the source files from +the dll's `.vcxproj` file, save for the `pch.h` and `pch.cpp` files. You _may_ +need to leave the headers for any C++/WinRT types you've authored in this project +- I'm not totally sure it's necessary. + +Now, to link the static lib we've created. For whatever reason, adding a +`ProjectReference` to the static lib doesn't work. So, we'll need to manually +link the lib from the lib project. You can do that by adding the lib's output +dir to your `AdditionalLibraryDirectories`, and adding the lib to your +`AdditionalDependencies`, like so: + +```xml + + + + $(SolutionDir)\$(Platform)\$(Configuration)\TerminalAppLib;%(AdditionalLibraryDirectories) + + TerminalAppLib.lib;%(AdditionalDependencies) + + +``` + +We are NOT adding a reference to the static lib project's .winmd here. As of the +2.0.190605.7 CppWinRT nuget package, this is enough for MsBuild and Visual +Studio to be able to determine that the static lib's `.winmd` should be included +in this package. + +At this point, you might have some mdmerge errors, which complain about +duplicate types in one of your dependencies. This might especially happen if one +of your dependencies (ex `A.dll`) is also a dependency for one of your _other_ +dependencies (ex `B.dll`). In this example, your final output project `C.dll` +depends on both `A.dll` and `B.dll`, and `B.dll` _also_ depends on `A.dll`. If +you're seeing this, I recommend adding `Private=false` and +`CopyLocalSatelliteAssemblies=false` to your dependent dlls. In this example, +add similar code to `B.dll`: + +```xml + + false + false + +``` + +where `TerminalSettings` is your `A.dll`, which is included by both `B` and `C`. + +We additionally had an `.exe` project that was including our `TerminalApp` +project, and all its `.xbf` and `.pri` files. If you have a similar project +aggregating all your resources, you might need to update the paths to point to +the new static lib project. + +At this point, you should be able to rebuild your solution, and everything +should be working just the same as before. + + +## Add TAEF Tests + +Now that you have a static library project, you can start building your unittest +dll. Start by creating a new directory for your unittest code, and creating a +`.vcxproj` for a TAEF unittest dll. For the Terminal solution, we use the TAEF +nuget package `Taef.Redist.Wlk`. + +### Referencing your C++/WinRT static lib + +This step is the easiest. Add a `ProjectReference` to your static lib project, +and your lib will be linked into your unittest dll. + +```xml + +``` + +Congratulations, you can now instantiate the pure c++ types you've authored in +your static lib. But what if you want to test your C++/WinRT types too? + +### Using your C++/WinRT types + +To be able to instantiate your C++/WinRT types in a TAEF unittest, you'll need +to rely on a new feature to Windows in version 1903 which enables unpackaged +activation of WinRT types. To do this, we'll need to author a SxS manifest that +lists each of our types, and include it in the dll, and also activate it +manually from TAEF. + +#### Creating the manifest + +First, you need to create a manifest file that lists each dll your test depends +upon, and each of the types in that dll. For example, here's an excerpt from the +Terminal's manifest: + +```xml + + + + + + + + + + + + +``` + +Here we have two dlls that we depend upon, `TerminalSettings.dll` and +`TerminalApp.dll`. `TerminalSettings` implements two types, +`Microsoft.Terminal.Settings.KeyChord` and +`Microsoft.Terminal.Settings.TerminalSettings`. + +#### Linking the manifest to the test dll + +Now that we have a manifest file, we need to embed it in your unittest dll. This +is done with the following properties in your `vcxproj` file: + +```xml + + true + true + + + + +``` +where `TerminalApp.Unit.Tests.manifest` is the name of your manifest file. + +Additionally, you'll need to binplace the manifest _adjacent to your test +binary_, so TAEF can find it at runtime. I've done this in the following way, +though I'm sure there's a better way: + +```xml + + + + + (xcopy /Y "$(OpenConsoleDir)src\cascadia\ut_app\TerminalApp.Unit.Tests.manifest" "$(OutDir)\TerminalApp.Unit.Tests.manifest*" ) + + + +``` + +#### Copying your dependencies + +Additionally, any dlls that implement any types your test is dependent upon will +also need to be in the output directory for the test. Manually copy those DLLs +to the tests' output directory too. The updated `PostBuildEvent` looks like +this: + +```xml + + + + echo OutDir=$(OutDir) + (xcopy /Y "$(SolutionDir)src\cascadia\ut_app\TerminalApp.Unit.Tests.manifest" "$(OutDir)\TerminalApp.Unit.Tests.manifest*" ) + + (xcopy /Y "$(SolutionDir)$(Platform)\$(Configuration)\TerminalConnection\TerminalConnection.dll" "$(OutDir)\TerminalConnection.dll*" ) + (xcopy /Y "$(SolutionDir)$(Platform)\$(Configuration)\TerminalSettings\TerminalSettings.dll" "$(OutDir)\TerminalSettings.dll*" ) + (xcopy /Y "$(SolutionDir)$(Platform)\$(Configuration)\TerminalControl\TerminalControl.dll" "$(OutDir)\TerminalControl.dll*" ) + + + +``` + +Again, verify the correct paths to your dependant C++/WinRT dlls, as they may be +different than the above + +#### Activating the manifest from TAEF + +Now that the manifest lives adjacent to your test dll, and all your dependent +dlls are also adjacent to the unittest dll, there's only one thing left to do. +TAEF will not use your dll's manifest by default, so you'll need to add a +property to your test class/method to tell TAEF to do so. You can do this with +the following: + +```c++ +class SettingsTests +{ + // Use a custom manifest to ensure that we can activate winrt types from + // our test. This property will tell taef to manually use this as the + // sxs manifest during this test class. It includes all the C++/WinRT + // types we've defined, so if your test is crashing for an unknown + // reason, make sure it's included in that file. + BEGIN_TEST_CLASS(SettingsTests) + TEST_CLASS_PROPERTY(L"ActivationContext", L"TerminalApp.Unit.Tests.manifest") + END_TEST_CLASS() + + // Other Test code here +} +``` + +Now, if you try to add any test methods that instantiate WinRT types you've +authored, they'll work. That is of course, so long as they don't use XAML. If +you want to use any XAML types, then you'll have to keep reading. + +### Using Xaml Types (with XAML Islands) + +To be able to instatiate XAML types in your unittest, we'll need to make use of +the [XAML Hosting +API](https://docs.microsoft.com/en-us/windows/apps/desktop/modernize/using-the-xaml-hosting-api) +(Xaml Islands). This enables you to use XAML APIs from a Win32 context. + + +#### Adding XAML Hosting code + +First and foremost, you'll need to add the following to your test's `precomp.h`: + +```c++ +#include +#include +#include +#include +``` + +If you hit a compile warning that refers to `GetCurrentTime`, you'll probably +also need the following, after you've `#include`'d `Windows.h`: + +```c++ +#ifdef GetCurrentTime +#undef GetCurrentTime +#endif +``` + +Then, somewhere in your test code, you'll need to start up Xaml Islands. I've done this in my `TEST_CLASS_SETUP`, so that I only create it once, and re-use it for each method. + +```c++ + +class TabTests +{ + TEST_CLASS_SETUP(ClassSetup) + { + winrt::init_apartment(winrt::apartment_type::single_threaded); + // Initialize the Xaml Hosting Manager + _manager = winrt::Windows::UI::Xaml::Hosting::WindowsXamlManager::InitializeForCurrentThread(); + _source = winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource{}; + + return true; + } + +private: + winrt::Windows::UI::Xaml::Hosting::WindowsXamlManager _manager{ nullptr }; + winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource _source{ nullptr }; +``` + +#### Authoring your test's `AppxManifest.xml` + +This alone however is not enough to get XAML Islands to work. There was a fairly +substantial change to the XAML Hosting API around Windows build 18295, so it +explicitly requires that you have your executable's manifest set +`maxversiontested` to higher than that version. However, because TAEF's `te.exe` +is not so manifested, we can't just use our SxS manifest from before to set that +version. Instead, you'll need to make TAEF run your test binary in a packaged +content, with our own appxmanifest. + +To do this, we'll need to author an `Appxmanifest.xml` to use with the test, and +associate that manifest with the test. + +Here's the AppxManifest we're using: + +```xml + + + + + + TerminalApp.Unit.Tests.Package Host Process + Microsoft Corp. + taef.png + TAEF Packaged Cwa FullTrust Application Host Process + + + + + + + + + + + + + + + + + + + + + + + + + + + + TerminalSettings.dll + + + + + + + +``` + +Change the `Identity.Name` and `Properties.DisplayName` to be more appropriate +for your test, as well as other properties if you feel the need. TAEF will +deploy the test package and remove it from your machine during testing, so it +doesn't terribly matter what these values are. + +MAKE SURE that `MaxVersionTested` is higher than `10.0.18295.0`. If it isn't, +XAML islands will still prevent you from activating it. + +UNDER NO CIRCUMSTANCE should you change the `` +line. This is how TAEF activates the TAEF host for your test binary. You might +get a warning about `TE.ProcessHost.exe` being deprecated in favor of +`TE.ProcessHost.UAP.exe`, but I haven't had success with the UAP version. + +Lower in the file, you'll see the `Extensions` block. In here you'll put each of +the winrt dependencies that your test needs, much like we did for the previous +manifest. Note that the syntax is _not_ exactly the same as the SxS manifest. + +#### Copy the AppxManifest to your `$(OutDir)` + +Again, we'll need to copy this appxmanifest adjacent to the test binary so we +can load it from the test. We'll do this similar to how we did the SxS manifest +before. The complete `PostBuildEvent` now looks like this: + +```xml + + + + (xcopy /Y "$(SolutionDir)src\cascadia\ut_app\TerminalApp.Unit.Tests.manifest" "$(OutDir)\TerminalApp.Unit.Tests.manifest*" ) + + (xcopy /Y "$(SolutionDir)src\cascadia\ut_app\TerminalApp.Unit.Tests.AppxManifest.xml" "$(OutDir)\TerminalApp.Unit.Tests.AppxManifest.xml*" ) + + (xcopy /Y "$(SolutionDir)$(Platform)\$(Configuration)\TerminalConnection\TerminalConnection.dll" "$(OutDir)\TerminalConnection.dll*" ) + (xcopy /Y "$(SolutionDir)$(Platform)\$(Configuration)\TerminalSettings\TerminalSettings.dll" "$(OutDir)\TerminalSettings.dll*" ) + (xcopy /Y "$(SolutionDir)$(Platform)\$(Configuration)\TerminalControl\TerminalControl.dll" "$(OutDir)\TerminalControl.dll*" ) + + + +``` + +The new line here is the line referencing +`TerminalApp.Unit.Tests.AppxManifest.xml`. You can only have one +`PostBuildEvent` per project, so don't go re-defining it for each additional +step - MsBuild will only use the last one. Again, this is probably not the best +way of copying these files over, but it works. + +#### Use the AppxManifest in the test code + +Now that we have the AppxManifest being binplaced next to our test, we can +finally reference it in the test. Instead of using the `ActivationContext` from +before, we'll use two new properties to tell TAEF to run this test as a package, +and to use our manifest as the AppxManifest for the package. + +```c++ + BEGIN_TEST_CLASS(TabTests) + TEST_CLASS_PROPERTY(L"RunAs", L"UAP") + TEST_CLASS_PROPERTY(L"UAP:AppXManifest", L"TerminalApp.Unit.Tests.AppxManifest.xml") + END_TEST_CLASS() +``` + +The complete Xaml Hosting test now looks like this: + +```c++ + class TabTests + { + BEGIN_TEST_CLASS(TabTests) + TEST_CLASS_PROPERTY(L"RunAs", L"UAP") + TEST_CLASS_PROPERTY(L"UAP:AppXManifest", L"TerminalApp.Unit.Tests.AppxManifest.xml") + END_TEST_CLASS() + + TEST_METHOD(TryCreateXamlObjects); + + TEST_CLASS_SETUP(ClassSetup) + { + winrt::init_apartment(winrt::apartment_type::single_threaded); + // Initialize the Xaml Hosting Manager + _manager = winrt::Windows::UI::Xaml::Hosting::WindowsXamlManager::InitializeForCurrentThread(); + _source = winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource{}; + + return true; + } + + private: + winrt::Windows::UI::Xaml::Hosting::WindowsXamlManager _manager{ nullptr }; + winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource _source{ nullptr }; + }; + void TabTests::TryCreateXamlObjects(){ ... } +``` + +Congratulations, you can now use XAML types from your unittest. + +### Using types from `Microsoft.UI.Xaml` + +Let's say you're extra crazy and you're using the `Microsoft.UI.Xaml` nuget +package. If you've followed all the steps above exactly, you're probably already +fine! You've already put the types in your appxmanifest (there are a lot of +them). You should be able to call the `Microsoft.UI.Xaml` types without any +problems. + +This is because of a few key lines we already put in the appxmanifest: + +```xml + + + + + +``` + +Without these `PackageDependency` entries for the VCLibs, Microsoft.UI.Xaml.dll +will not be able to load. diff --git a/src/cascadia/TerminalApp/AppKeyBindingsSerialization.h b/src/cascadia/TerminalApp/AppKeyBindingsSerialization.h index 4b4cc778f..2f1c479c3 100644 --- a/src/cascadia/TerminalApp/AppKeyBindingsSerialization.h +++ b/src/cascadia/TerminalApp/AppKeyBindingsSerialization.h @@ -2,7 +2,7 @@ // Licensed under the MIT license. // // Module Name: -// - Profile.hpp +// - AppKeyBindingsSerialization.h // // Abstract: // - A couple helper functions for serializing/deserializing an AppKeyBindings diff --git a/src/cascadia/TerminalApp/ColorScheme.cpp b/src/cascadia/TerminalApp/ColorScheme.cpp index 30832d036..cc7ffd3cc 100644 --- a/src/cascadia/TerminalApp/ColorScheme.cpp +++ b/src/cascadia/TerminalApp/ColorScheme.cpp @@ -10,8 +10,6 @@ using namespace TerminalApp; using namespace ::Microsoft::Console; using namespace winrt::Microsoft::Terminal::Settings; using namespace winrt::Microsoft::Terminal::TerminalControl; -using namespace winrt::TerminalApp; -using namespace winrt::Windows::Data::Json; static constexpr std::string_view NameKey{ "name" }; static constexpr std::string_view TableKey{ "colors" }; diff --git a/src/cascadia/TerminalApp/ColorScheme.h b/src/cascadia/TerminalApp/ColorScheme.h index 49d6616e0..fd3595a20 100644 --- a/src/cascadia/TerminalApp/ColorScheme.h +++ b/src/cascadia/TerminalApp/ColorScheme.h @@ -17,7 +17,6 @@ Author(s): #pragma once #include #include -#include #include "../../inc/conattrs.hpp" namespace TerminalApp diff --git a/src/cascadia/TerminalApp/Profile.cpp b/src/cascadia/TerminalApp/Profile.cpp index 4c4f9f638..ae0b780b3 100644 --- a/src/cascadia/TerminalApp/Profile.cpp +++ b/src/cascadia/TerminalApp/Profile.cpp @@ -9,8 +9,6 @@ using namespace TerminalApp; using namespace winrt::Microsoft::Terminal::Settings; -using namespace winrt::TerminalApp; -using namespace winrt::Windows::Data::Json; using namespace ::Microsoft::Console; static constexpr std::string_view NameKey{ "name" }; diff --git a/src/cascadia/TerminalApp/TerminalApp.vcxproj b/src/cascadia/TerminalApp/TerminalApp.vcxproj index c292673de..2a38ed959 100644 --- a/src/cascadia/TerminalApp/TerminalApp.vcxproj +++ b/src/cascadia/TerminalApp/TerminalApp.vcxproj @@ -1,7 +1,8 @@ - + + @@ -15,106 +16,32 @@ {CA5CAD1A-44BD-4AC7-AC72-F16E576FDD12} TerminalApp TerminalApp - - false - - - Designer - + - - + - - - TerminalPage.xaml - Code - - - MinMaxCloseControl.xaml - - - - - - - - - - + - - AppKeyBindings.idl - - - App.xaml - + + + + - - TerminalPage.xaml - Code - - - MinMaxCloseControl.xaml - - - TerminalPage.xaml - Code - - - MinMaxCloseControl.xaml - Code - - - - - - - - - - - + Create - - AppKeyBindings.idl - - - App.xaml - - - - - NotUsing - - - App.xaml - - + @@ -131,19 +58,31 @@ {18D09A24-8240-42D6-8CB6-236EEE820263} - - {CA5CAD1A-44BD-4AC7-AC72-6CA5B3AB89ED} + + + + + + + + + + true + true - - - - - Designer - - - Designer - + + + + + <_BinRoot Condition="'$(Platform)' != 'Win32'">$(OpenConsoleDir)$(Platform)\$(Configuration)\ + <_BinRoot Condition="'$(Platform)' == 'Win32'">$(OpenConsoleDir)$(Configuration)\ + + - - <_GenerateProjectPriFileDependsOn>OpenConsolePlaceAppXbfAtRootOfResourceTree;$(_GenerateProjectPriFileDependsOn) - - - - <_RelocatedAppXamlData Include="@(PackagingOutputs)" Condition="'%(Filename)' == 'App' and ('%(Extension)' == '.xaml' or '%(Extension)' == '.xbf')" /> - - - %(Filename)%(Extension) - - - + + diff --git a/src/cascadia/TerminalApp/lib/TerminalAppLib.vcxproj b/src/cascadia/TerminalApp/lib/TerminalAppLib.vcxproj new file mode 100644 index 000000000..acffbbbf1 --- /dev/null +++ b/src/cascadia/TerminalApp/lib/TerminalAppLib.vcxproj @@ -0,0 +1,280 @@ + + + + + + + + StaticLibrary + Console + true + + false + + + + + + + + Designer + + + + + + Designer + + + Designer + + + + + + + + ../MinMaxCloseControl.xaml + + + ../TerminalPage.xaml + Code + + + + + + + + + + + + + + ../App.xaml + + + + + + + ../MinMaxCloseControl.xaml + + + ../TerminalPage.xaml + Code + + + + + + + + + + + + + Create + + + ../AppKeyBindings.idl + + + ../App.xaml + + + + + NotUsing + + + + + + + + + ../App.xaml + + + + ../MinMaxCloseControl.xaml + Code + + + ../TerminalPage.xaml + Code + + + + + + + + + + + + + + + + + + + <_BinRoot Condition="'$(Platform)' != 'Win32'">$(OpenConsoleDir)$(Platform)\$(Configuration)\ + <_BinRoot Condition="'$(Platform)' == 'Win32'">$(OpenConsoleDir)$(Configuration)\ + + + + + Warning + + + + + + + $(_BinRoot)TerminalSettings\Microsoft.Terminal.Settings.winmd + true + false + false + + + + $(_BinRoot)TerminalConnection\Microsoft.Terminal.TerminalConnection.winmd + true + false + false + + + + $(_BinRoot)TerminalControl\Microsoft.Terminal.TerminalControl.winmd + true + false + false + + + + + + + + pch.h + ..;$(OpenConsoleDir)\dep\jsoncpp\json;%(AdditionalIncludeDirectories); + + 4702;%(DisableSpecificWarnings) + + + WindowsApp.lib;shell32.lib;%(AdditionalDependencies) + + + + + + {CA5CAD1A-9A12-429C-B551-8562EC954746} + Win32Proj + TerminalApp + TerminalAppLib + TerminalAppLib + 10.0.17763.0 + + + + + true + + + + + + + + x86 + $(Platform) + <_MUXRoot>$(OpenConsoleDir)\packages\Microsoft.UI.Xaml.2.2.190611001-prerelease\ + <_MUXAppRoot>$(OpenConsoleDir)\packages\Microsoft.Toolkit.Win32.UI.XamlApplication.6.0.0-preview6.2\ + + + + + Microsoft.UI.Xaml.dll + true + false + false + + + + + + + Microsoft.Toolkit.Win32.UI.XamlHost.dll + true + false + false + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + + + + <_GenerateProjectPriFileDependsOn>OpenConsolePlaceAppXbfAtRootOfResourceTree;$(_GenerateProjectPriFileDependsOn) + + + + <_RelocatedAppXamlData Include="@(PackagingOutputs)" Condition="'%(Filename)' == 'App' and ('%(Extension)' == '.xaml' or '%(Extension)' == '.xbf')" /> + + + %(Filename)%(Extension) + + + + diff --git a/src/cascadia/TerminalApp/lib/pch.cpp b/src/cascadia/TerminalApp/lib/pch.cpp new file mode 100644 index 000000000..3c27d44d5 --- /dev/null +++ b/src/cascadia/TerminalApp/lib/pch.cpp @@ -0,0 +1,4 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +#include "pch.h" diff --git a/src/cascadia/TerminalApp/lib/pch.h b/src/cascadia/TerminalApp/lib/pch.h new file mode 100644 index 000000000..ebf961fa7 --- /dev/null +++ b/src/cascadia/TerminalApp/lib/pch.h @@ -0,0 +1,56 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. +// +// pch.h +// Header for platform projection include files +// + +#pragma once + +#define WIN32_LEAN_AND_MEAN + +#include +// This is inexplicable, but for whatever reason, cppwinrt conflicts with the +// SDK definition of this function, so the only fix is to undef it. +// from WinBase.h +// Windows::UI::Xaml::Media::Animation::IStoryboard::GetCurrentTime +#ifdef GetCurrentTime +#undef GetCurrentTime +#endif + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "winrt/Windows.UI.Xaml.Markup.h" +#include "winrt/Windows.UI.Xaml.Documents.h" + +#include + +#include + +#include + +// Including TraceLogging essentials for the binary +#include +#include +TRACELOGGING_DECLARE_PROVIDER(g_hTerminalWin32Provider); +#include +#include + +// JsonCpp +#include + +#include +#include diff --git a/src/cascadia/TerminalApp/packages.config b/src/cascadia/TerminalApp/packages.config index 903f790d7..fe0c6e184 100644 --- a/src/cascadia/TerminalApp/packages.config +++ b/src/cascadia/TerminalApp/packages.config @@ -2,5 +2,5 @@ - + diff --git a/src/cascadia/TerminalApp/pch.h b/src/cascadia/TerminalApp/pch.h index 70bbcec2d..465827b00 100644 --- a/src/cascadia/TerminalApp/pch.h +++ b/src/cascadia/TerminalApp/pch.h @@ -7,51 +7,6 @@ #pragma once -#define WIN32_LEAN_AND_MEAN - -#include -// This is inexplicable, but for whatever reason, cppwinrt conflicts with the -// SDK definition of this function, so the only fix is to undef it. -// from WinBase.h -// Windows::UI::Xaml::Media::Animation::IStoryboard::GetCurrentTime -#ifdef GetCurrentTime -#undef GetCurrentTime -#endif - -#include - -#include - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include - -// Including TraceLogging essentials for the binary -#include -#include -TRACELOGGING_DECLARE_PROVIDER(g_hTerminalWin32Provider); -#include -#include - -// JsonCpp -#include - -#include -#include +// This file can be empty - the pch.h in TerminalApp/lib does the heavy lifting +// of including all the headers we need. As this project is just a dll wrapper, +// we don't actually need anything in here. diff --git a/src/cascadia/TerminalConnection/packages.config b/src/cascadia/TerminalConnection/packages.config index 4f7a6f98a..58e49c05c 100644 --- a/src/cascadia/TerminalConnection/packages.config +++ b/src/cascadia/TerminalConnection/packages.config @@ -1,4 +1,4 @@  - + diff --git a/src/cascadia/TerminalControl/TermControl.cpp b/src/cascadia/TerminalControl/TermControl.cpp index 6a8c3b886..283208f93 100644 --- a/src/cascadia/TerminalControl/TermControl.cpp +++ b/src/cascadia/TerminalControl/TermControl.cpp @@ -665,9 +665,11 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation return; } - const auto modifiers = args.KeyModifiers(); - const auto altEnabled = WI_IsFlagSet(modifiers, VirtualKeyModifiers::Menu); - const auto shiftEnabled = WI_IsFlagSet(modifiers, VirtualKeyModifiers::Shift); + const auto modifiers = static_cast(args.KeyModifiers()); + // static_cast to a uint32_t because we can't use the WI_IsFlagSet + // macro directly with a VirtualKeyModifiers + const auto altEnabled = WI_IsFlagSet(modifiers, static_cast(VirtualKeyModifiers::Menu)); + const auto shiftEnabled = WI_IsFlagSet(modifiers, static_cast(VirtualKeyModifiers::Shift)); if (point.Properties().IsLeftButtonPressed()) { @@ -818,9 +820,11 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation { auto delta = args.GetCurrentPoint(_root).Properties().MouseWheelDelta(); // Get the state of the Ctrl & Shift keys - const auto modifiers = args.KeyModifiers(); - const auto ctrlPressed = WI_IsFlagSet(modifiers, VirtualKeyModifiers::Control); - const auto shiftPressed = WI_IsFlagSet(modifiers, VirtualKeyModifiers::Shift); + // static_cast to a uint32_t because we can't use the WI_IsFlagSet macro + // directly with a VirtualKeyModifiers + const auto modifiers = static_cast(args.KeyModifiers()); + const auto ctrlPressed = WI_IsFlagSet(modifiers, static_cast(VirtualKeyModifiers::Control)); + const auto shiftPressed = WI_IsFlagSet(modifiers, static_cast(VirtualKeyModifiers::Shift)); if (ctrlPressed && shiftPressed) { diff --git a/src/cascadia/TerminalControl/TerminalControl.vcxproj b/src/cascadia/TerminalControl/TerminalControl.vcxproj index 60b8538b2..1fd40b429 100644 --- a/src/cascadia/TerminalControl/TerminalControl.vcxproj +++ b/src/cascadia/TerminalControl/TerminalControl.vcxproj @@ -44,9 +44,15 @@ - + + false + false + - + + false + false + diff --git a/src/cascadia/TerminalControl/packages.config b/src/cascadia/TerminalControl/packages.config index 4f7a6f98a..58e49c05c 100644 --- a/src/cascadia/TerminalControl/packages.config +++ b/src/cascadia/TerminalControl/packages.config @@ -1,4 +1,4 @@  - + diff --git a/src/cascadia/TerminalCore/packages.config b/src/cascadia/TerminalCore/packages.config index 4f7a6f98a..58e49c05c 100644 --- a/src/cascadia/TerminalCore/packages.config +++ b/src/cascadia/TerminalCore/packages.config @@ -1,4 +1,4 @@  - + diff --git a/src/cascadia/TerminalSettings/packages.config b/src/cascadia/TerminalSettings/packages.config index 4f7a6f98a..58e49c05c 100644 --- a/src/cascadia/TerminalSettings/packages.config +++ b/src/cascadia/TerminalSettings/packages.config @@ -1,4 +1,4 @@  - + diff --git a/src/cascadia/WindowsTerminal/WindowsTerminal.manifest b/src/cascadia/WindowsTerminal/WindowsTerminal.manifest index 29d253c8d..4c2159f37 100644 --- a/src/cascadia/WindowsTerminal/WindowsTerminal.manifest +++ b/src/cascadia/WindowsTerminal/WindowsTerminal.manifest @@ -1,5 +1,10 @@ + + + @@ -22,7 +27,7 @@ Whenever you add new cppwinrt classes, make sure to add them here. If you need help with the syntax, the generated AppxManifest.xml should have the classes and files all listed in it. - + TODO MSFT#21300206: Switch to automatically generating these. --> diff --git a/src/cascadia/WindowsTerminal/WindowsTerminal.vcxproj b/src/cascadia/WindowsTerminal/WindowsTerminal.vcxproj index 3c7e2e75a..c5d619d4f 100644 --- a/src/cascadia/WindowsTerminal/WindowsTerminal.vcxproj +++ b/src/cascadia/WindowsTerminal/WindowsTerminal.vcxproj @@ -1,136 +1,158 @@ - - - - - - Application - Windows - false - Windows Store - - - - 15.0 - {CA5CAD1A-1754-4A9D-93D7-857A9D17CB1B} - Win32Proj - WindowsTerminal - WindowsTerminal - WindowsTerminal - - - 10.0.18362.0 - - - - true - - - - - $(OpenConsoleDir)\src\inc;$(OpenConsoleDir)\dep;$(OpenConsoleDir)\dep\Console;$(OpenConsoleDir)\dep\Win32K;$(OpenConsoleDir)\dep\gsl\include;%(AdditionalIncludeDirectories); - - - - "$(OpenConsoleDir)src\cascadia\TerminalCore\lib\Generated Files";%(AdditionalIncludeDirectories); - - - gdi32.lib;dwmapi.lib;Shcore.lib;%(AdditionalDependencies) - - - - true - true - - - - - - - - - - - - - Create - - - - - - - - - - - - - - - - - - - - true - PreserveNewest - - - - true - PreserveNewest - - - - true - PreserveNewest - - - - - - - - - - - - - - - - WindowsLocalDebugger - - - - true - + + + + + + + + Application + Windows + false + Windows Store + + + + 15.0 + {CA5CAD1A-1754-4A9D-93D7-857A9D17CB1B} + Win32Proj + WindowsTerminal + WindowsTerminal + WindowsTerminal + + + 10.0.18362.0 + + + + true + + + + + $(OpenConsoleDir)\src\inc;$(OpenConsoleDir)\dep;$(OpenConsoleDir)\dep\Console;$(OpenConsoleDir)\dep\Win32K;$(OpenConsoleDir)\dep\gsl\include;%(AdditionalIncludeDirectories); + + + + "$(OpenConsoleDir)src\cascadia\TerminalCore\lib\Generated Files";%(AdditionalIncludeDirectories); + + + gdi32.lib;dwmapi.lib;Shcore.lib;%(AdditionalDependencies) + + + + true + true + + + + + + + + + + + + + Create + + + + + + + + + + + + + + + + + + + + true + PreserveNewest + + + + + true + PreserveNewest + + + + + + + + + + + + + + <_BinRoot Condition="'$(Platform)' != 'Win32'">$(OpenConsoleDir)$(Platform)\$(Configuration)\ + <_BinRoot Condition="'$(Platform)' == 'Win32'">$(OpenConsoleDir)$(Configuration)\ + + + + + + $(_BinRoot)\TerminalApp\TerminalApp.winmd + true + false + false + + + + + + + + + + + + WindowsLocalDebugger + + + + true + + - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + - - - - - - - - - - - + + + + + + + + + + + + + diff --git a/src/cascadia/WindowsTerminal/packages.config b/src/cascadia/WindowsTerminal/packages.config index 97e5bcfc0..43e4cebb9 100644 --- a/src/cascadia/WindowsTerminal/packages.config +++ b/src/cascadia/WindowsTerminal/packages.config @@ -1,8 +1,8 @@  - + + - diff --git a/src/cascadia/ut_app/JsonTests.cpp b/src/cascadia/ut_app/JsonTests.cpp new file mode 100644 index 000000000..b21f89c09 --- /dev/null +++ b/src/cascadia/ut_app/JsonTests.cpp @@ -0,0 +1,100 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +#include "precomp.h" + +#include "../TerminalApp/ColorScheme.h" + +using namespace Microsoft::Console; +using namespace TerminalApp; +using namespace WEX::Logging; +using namespace WEX::TestExecution; + +namespace TerminalAppUnitTests +{ + class JsonTests + { + BEGIN_TEST_CLASS(JsonTests) + TEST_CLASS_PROPERTY(L"ActivationContext", L"TerminalApp.Unit.Tests.manifest") + END_TEST_CLASS() + + TEST_METHOD(ParseInvalidJson); + TEST_METHOD(ParseSimpleColorScheme); + + TEST_CLASS_SETUP(ClassSetup) + { + reader = std::unique_ptr(Json::CharReaderBuilder::CharReaderBuilder().newCharReader()); + return true; + } + + Json::Value VerifyParseSucceeded(std::string content); + void VerifyParseFailed(std::string content); + + private: + std::unique_ptr reader; + }; + + Json::Value JsonTests::VerifyParseSucceeded(std::string content) + { + Json::Value root; + std::string errs; + const bool parseResult = reader->parse(content.c_str(), content.c_str() + content.size(), &root, &errs); + VERIFY_IS_TRUE(parseResult, winrt::to_hstring(errs).c_str()); + return root; + } + void JsonTests::VerifyParseFailed(std::string content) + { + Json::Value root; + std::string errs; + const bool parseResult = reader->parse(content.c_str(), content.c_str() + content.size(), &root, &errs); + VERIFY_IS_FALSE(parseResult); + } + + void JsonTests::ParseInvalidJson() + { + const std::string badJson{ "{ foo : bar : baz }" }; + VerifyParseFailed(badJson); + } + + void JsonTests::ParseSimpleColorScheme() + { + const std::string campbellScheme{ "{" + "\"background\" : \"#0C0C0C\"," + "\"black\" : \"#0C0C0C\"," + "\"blue\" : \"#0037DA\"," + "\"brightBlack\" : \"#767676\"," + "\"brightBlue\" : \"#3B78FF\"," + "\"brightCyan\" : \"#61D6D6\"," + "\"brightGreen\" : \"#16C60C\"," + "\"brightPurple\" : \"#B4009E\"," + "\"brightRed\" : \"#E74856\"," + "\"brightWhite\" : \"#F2F2F2\"," + "\"brightYellow\" : \"#F9F1A5\"," + "\"cyan\" : \"#3A96DD\"," + "\"foreground\" : \"#F2F2F2\"," + "\"green\" : \"#13A10E\"," + "\"name\" : \"Campbell\"," + "\"purple\" : \"#881798\"," + "\"red\" : \"#C50F1F\"," + "\"white\" : \"#CCCCCC\"," + "\"yellow\" : \"#C19C00\"" + "}" }; + + const auto schemeObject = VerifyParseSucceeded(campbellScheme); + auto scheme = ColorScheme::FromJson(schemeObject); + VERIFY_ARE_EQUAL(L"Campbell", scheme.GetName()); + VERIFY_ARE_EQUAL(ARGB(0, 0xf2, 0xf2, 0xf2), scheme.GetForeground()); + VERIFY_ARE_EQUAL(ARGB(0, 0x0c, 0x0c, 0x0c), scheme.GetBackground()); + + std::array expectedCampbellTable; + auto campbellSpan = gsl::span(&expectedCampbellTable[0], gsl::narrow(COLOR_TABLE_SIZE)); + Utils::InitializeCampbellColorTable(campbellSpan); + + for (size_t i = 0; i < expectedCampbellTable.size(); i++) + { + const auto& expected = expectedCampbellTable.at(i); + const auto& actual = scheme.GetTable().at(i); + VERIFY_ARE_EQUAL(expected, actual); + } + } +} diff --git a/src/cascadia/ut_app/SettingsTests.cpp b/src/cascadia/ut_app/SettingsTests.cpp new file mode 100644 index 000000000..4f5189ef2 --- /dev/null +++ b/src/cascadia/ut_app/SettingsTests.cpp @@ -0,0 +1,46 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +#include "precomp.h" + +#include "../TerminalApp/ColorScheme.h" + +using namespace Microsoft::Console; +using namespace TerminalApp; +using namespace WEX::Logging; +using namespace WEX::TestExecution; + +namespace TerminalAppUnitTests +{ + // Unfortunately, these tests _WILL NOT_ work in our CI, until we have a lab + // machine available that can run Windows version 18362. Until then, these + // tests will be commented out. GH#1012 should move our CI to that version. + // When that happens, these tests can be re-added. + + // class SettingsTests + // { + // // Use a custom manifest to ensure that we can activate winrt types from + // // our test. This property will tell taef to manually use this as the + // // sxs manifest during this test class. It includes all the cppwinrt + // // types we've defined, so if your test is crashing for an unknown + // // reason, make sure it's included in that file. + // // If you want to do anything XAML-y, you'll need to run yor test in a + // // packaged context. See TabTests.cpp for more details on that. + // BEGIN_TEST_CLASS(SettingsTests) + // TEST_CLASS_PROPERTY(L"ActivationContext", L"TerminalApp.Unit.Tests.manifest") + // END_TEST_CLASS() + + // TEST_METHOD(TryCreateWinRTType); + // }; + + // void SettingsTests::TryCreateWinRTType() + // { + // winrt::Microsoft::Terminal::Settings::TerminalSettings settings{}; + // VERIFY_IS_NOT_NULL(settings); + // auto oldFontSize = settings.FontSize(); + // settings.FontSize(oldFontSize + 5); + // auto newFontSize = settings.FontSize(); + // VERIFY_ARE_NOT_EQUAL(oldFontSize, newFontSize); + // } + +} diff --git a/src/cascadia/ut_app/TabTests.cpp b/src/cascadia/ut_app/TabTests.cpp new file mode 100644 index 000000000..a36e445bb --- /dev/null +++ b/src/cascadia/ut_app/TabTests.cpp @@ -0,0 +1,112 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +#include "precomp.h" + +#include "../TerminalApp/ColorScheme.h" +#include "../TerminalApp/Tab.h" + +using namespace Microsoft::Console; +using namespace TerminalApp; +using namespace WEX::Logging; +using namespace WEX::TestExecution; + +namespace TerminalAppUnitTests +{ + // Unfortunately, these tests _WILL NOT_ work in our CI, until we have a lab + // machine available that can run Windows version 18362. Until then, these + // tests will be commented out. GH#1012 should move our CI to that version. + // When that happens, these tests can be re-added. + + // class TabTests + // { + // // For this set of tests, we need to activate some XAML content. To do + // // that, we need to be able to activate Xaml Islands(XI), using the Xaml + // // Hosting APIs. Because XI looks at the manifest of the exe running, we + // // can't just use the TerminalApp.Unit.Tests.manifest as our + // // ActivationContext. XI is going to inspect `te.exe`s manifest to try + // // and find the maxversiontested property, but te.exe hasn't set that. + // // Instead, this test will run as a UAP application, as a packaged + // // centenial (win32) app. We'll specify our own AppxManifest, so that + // // we'll be able to also load all the dll's for the types we've defined + // // (and want to use here). This does come with a minor caveat, as + // // deploying the appx takes a bit, so use sparingly (though it will + // // deploy once per class when used like this.) + // BEGIN_TEST_CLASS(TabTests) + // TEST_CLASS_PROPERTY(L"RunAs", L"UAP") + // TEST_CLASS_PROPERTY(L"UAP:AppXManifest", L"TerminalApp.Unit.Tests.AppxManifest.xml") + // END_TEST_CLASS() + + // // These four tests act as canary tests. If one of them fails, then they + // // can help you identify if something much lower in the stack has + // // failed. + // TEST_METHOD(TryInitXamlIslands); + // TEST_METHOD(TryCreateLocalWinRTType); + // TEST_METHOD(TryCreateXamlObjects); + // TEST_METHOD(TryCreateTab); + + // TEST_CLASS_SETUP(ClassSetup) + // { + // winrt::init_apartment(winrt::apartment_type::single_threaded); + // // Initialize the Xaml Hosting Manager + // _manager = winrt::Windows::UI::Xaml::Hosting::WindowsXamlManager::InitializeForCurrentThread(); + // _source = winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource{}; + + // return true; + // } + + // private: + // winrt::Windows::UI::Xaml::Hosting::WindowsXamlManager _manager{ nullptr }; + // winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource _source{ nullptr }; + // }; + + // void TabTests::TryInitXamlIslands() + // { + // // Ensures that XAML Islands was initialized correctly + // VERIFY_IS_NOT_NULL(_manager); + // VERIFY_IS_NOT_NULL(_source); + // } + + // void TabTests::TryCreateLocalWinRTType() + // { + // // Verify we can create a WinRT type we authored + // // Just creating it is enough to know that everything is working. + // winrt::Microsoft::Terminal::Settings::TerminalSettings settings{}; + // VERIFY_IS_NOT_NULL(settings); + // auto oldFontSize = settings.FontSize(); + // settings.FontSize(oldFontSize + 5); + // auto newFontSize = settings.FontSize(); + // VERIFY_ARE_NOT_EQUAL(oldFontSize, newFontSize); + // } + + // void TabTests::TryCreateXamlObjects() + // { + // // Verify we can create a some XAML objects + // // Just creating all of them is enough to know that everything is working. + // winrt::Windows::UI::Xaml::Controls::UserControl controlRoot; + // VERIFY_IS_NOT_NULL(controlRoot); + // winrt::Windows::UI::Xaml::Controls::Grid root; + // VERIFY_IS_NOT_NULL(root); + // winrt::Windows::UI::Xaml::Controls::SwapChainPanel swapChainPanel; + // VERIFY_IS_NOT_NULL(swapChainPanel); + // winrt::Windows::UI::Xaml::Controls::Primitives::ScrollBar scrollBar; + // VERIFY_IS_NOT_NULL(scrollBar); + // } + + // void TabTests::TryCreateTab() + // { + // // Just try creating all of: + // // 1. one of our pure c++ types (Profile) + // // 2. one of our c++winrt types (TermControl) + // // 3. one of our types that uses MUX/Xaml (Tab). + // // Just creating all of them is enough to know that everything is working. + // const auto profileGuid{ Utils::CreateGuid() }; + // winrt::Microsoft::Terminal::TerminalControl::TermControl term{}; + // VERIFY_IS_NOT_NULL(term); + + // auto newTab = std::make_shared(profileGuid, term); + + // VERIFY_IS_NOT_NULL(newTab); + // } + +} diff --git a/src/cascadia/ut_app/TerminalApp.Unit.Tests.AppxManifest.xml b/src/cascadia/ut_app/TerminalApp.Unit.Tests.AppxManifest.xml new file mode 100644 index 000000000..4ab002d40 --- /dev/null +++ b/src/cascadia/ut_app/TerminalApp.Unit.Tests.AppxManifest.xml @@ -0,0 +1,196 @@ + + + + + + + + TerminalApp.Unit.Tests.Package Host Process + Microsoft Corp. + taef.png + TAEF Packaged Cwa FullTrust Application Host Process + + + + + + + + + + + + + + + + + + + + + + + + TerminalSettings.dll + + + + + + + TerminalApp.dll + + + + + + + TerminalConnection.dll + + + + + + + TerminalControl.dll + + + + + + Microsoft.UI.Xaml.dll + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/cascadia/ut_app/TerminalApp.Unit.Tests.manifest b/src/cascadia/ut_app/TerminalApp.Unit.Tests.manifest new file mode 100644 index 000000000..4c2159f37 --- /dev/null +++ b/src/cascadia/ut_app/TerminalApp.Unit.Tests.manifest @@ -0,0 +1,141 @@ + + + + + + + + + + + + + + + + + PerMonitorV2 + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/cascadia/ut_app/TerminalApp.UnitTests.vcxproj b/src/cascadia/ut_app/TerminalApp.UnitTests.vcxproj new file mode 100644 index 000000000..d53557135 --- /dev/null +++ b/src/cascadia/ut_app/TerminalApp.UnitTests.vcxproj @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + + + + Create + + + + NotUsing + + + + + + + + + + + + {CA5CAD1A-9333-4D05-B12A-1905CBF112F9} + Win32Proj + TerminalAppUnitTests + UnitTests_TerminalApp + Terminal.App.Unit.Tests + 10.0.18362.0 + 10.0.18362.0 + + + + + + ..;$(OpenConsoleDir)\dep\jsoncpp\json;$(OpenConsoleDir)src\inc;$(OpenConsoleDir)src\inc\test;$(WinRT_IncludePath)\..\cppwinrt\winrt;"$(OpenConsoleDir)\src\cascadia\TerminalApp\lib\Generated Files";%(AdditionalIncludeDirectories) + precomp.h + + + 4702;%(DisableSpecificWarnings) + + + WindowsApp.lib;%(AdditionalDependencies) + + + + + true + true + + + + + + + + + + + + + $(OpenConsoleDir)\bin\$(Platform)\$(Configuration)\$(ProjectName)\ + $(OpenConsoleDir)\obj\$(Platform)\$(Configuration)\$(ProjectName)\ + + + + <_CppWinrtBinRoot>"$(OpenConsoleDir)$(Platform)\$(Configuration)\" + + x86 + $(Platform) + <_MUXBinRoot>"$(OpenConsoleDir)packages\Microsoft.UI.Xaml.2.2.190611001-prerelease\runtimes\win10-$(Native-Platform)\native\" + + + + + + + (xcopy /Y "$(OpenConsoleDir)src\cascadia\WindowsTerminal\WindowsTerminal.manifest" "$(OpenConsoleDir)src\cascadia\ut_app\TerminalApp.Unit.Tests.manifest*" ) + + + + + + + + echo OutDir=$(OutDir) + (xcopy /Y "$(OpenConsoleDir)src\cascadia\ut_app\TerminalApp.Unit.Tests.manifest" "$(OutDir)\TerminalApp.Unit.Tests.manifest*" ) + (xcopy /Y "$(OpenConsoleDir)src\cascadia\ut_app\TerminalApp.Unit.Tests.AppxManifest.xml" "$(OutDir)\TerminalApp.Unit.Tests.AppxManifest.xml*" ) + + (xcopy /Y $(_CppWinrtBinRoot)TerminalConnection\TerminalConnection.dll "$(OutDir)\TerminalConnection.dll*" ) + (xcopy /Y $(_CppWinrtBinRoot)TerminalSettings\TerminalSettings.dll "$(OutDir)\TerminalSettings.dll*" ) + (xcopy /Y $(_CppWinrtBinRoot)TerminalControl\TerminalControl.dll "$(OutDir)\TerminalControl.dll*" ) + + (xcopy /Y $(_MUXBinRoot)Microsoft.UI.Xaml.dll "$(OutDir)\Microsoft.UI.Xaml.dll*" ) + (xcopy /Y $(_MUXBinRoot)Microsoft.UI.Xaml.pri "$(OutDir)\Microsoft.UI.Xaml.pri*" ) + + + + + diff --git a/src/cascadia/ut_app/precomp.cpp b/src/cascadia/ut_app/precomp.cpp new file mode 100644 index 000000000..6a6fa8e5a --- /dev/null +++ b/src/cascadia/ut_app/precomp.cpp @@ -0,0 +1,4 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +#include "precomp.h" \ No newline at end of file diff --git a/src/cascadia/ut_app/precomp.h b/src/cascadia/ut_app/precomp.h new file mode 100644 index 000000000..5c19aff51 --- /dev/null +++ b/src/cascadia/ut_app/precomp.h @@ -0,0 +1,53 @@ +/*++ +Copyright (c) Microsoft Corporation +Licensed under the MIT license. + +Module Name: +- precomp.h + +Abstract: +- Contains external headers to include in the precompile phase of console build process. +- Avoid including internal project headers. Instead include them only in the classes that need them (helps with test project building). + +Author(s): +- Carlos Zamora (cazamor) April 2019 +--*/ + +#pragma once + +// This includes support libraries from the CRT, STL, WIL, and GSL +#include "LibraryIncludes.h" +// This is inexplicable, but for whatever reason, cppwinrt conflicts with the +// SDK definition of this function, so the only fix is to undef it. +// from WinBase.h +// Windows::UI::Xaml::Media::Animation::IStoryboard::GetCurrentTime +#ifdef GetCurrentTime +#undef GetCurrentTime +#endif + +#include +#include +#include "consoletaeftemplates.hpp" + +// Needed just for XamlIslands to work at all: +#include +#include +#include +#include + +// Common includes for most tests: +#include "../../inc/argb.h" +#include "../../inc/conattrs.hpp" +#include "../../types/inc/utils.hpp" +#include "../../inc/DefaultSettings.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include diff --git a/src/common.build.tests.props b/src/common.build.tests.props index 9bd3921a7..4b3e2187c 100644 --- a/src/common.build.tests.props +++ b/src/common.build.tests.props @@ -5,11 +5,11 @@ UNIT_TESTING;%(PreprocessorDefinitions) - + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - + diff --git a/src/cppwinrt.build.post.props b/src/cppwinrt.build.post.props index 0e598d414..38b012cb9 100644 --- a/src/cppwinrt.build.post.props +++ b/src/cppwinrt.build.post.props @@ -64,13 +64,13 @@ - + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - + + diff --git a/src/cppwinrt.build.pre.props b/src/cppwinrt.build.pre.props index 509753567..d34a9ef3f 100644 --- a/src/cppwinrt.build.pre.props +++ b/src/cppwinrt.build.pre.props @@ -3,7 +3,7 @@ - + diff --git a/src/host/ft_uia/Host.Tests.UIA.csproj b/src/host/ft_uia/Host.Tests.UIA.csproj index 3c4bf1d3e..62136016e 100644 --- a/src/host/ft_uia/Host.Tests.UIA.csproj +++ b/src/host/ft_uia/Host.Tests.UIA.csproj @@ -53,7 +53,7 @@ - ..\..\..\packages\Taef.Redist.Wlk.10.30.180808002\lib\net45\TE.Managed.dll + ..\..\..\packages\Taef.Redist.Wlk.10.38.190610001-uapadmin\lib\net45\TE.Managed.dll @@ -64,10 +64,10 @@ ..\..\..\packages\Selenium.Support.3.5.0\lib\net40\WebDriver.Support.dll - ..\..\..\packages\Taef.Redist.Wlk.10.30.180808002\lib\net45\Wex.Common.Managed.dll + ..\..\..\packages\Taef.Redist.Wlk.10.38.190610001-uapadmin\lib\net45\Wex.Common.Managed.dll - ..\..\..\packages\Taef.Redist.Wlk.10.30.180808002\lib\net45\Wex.Logger.Interop.dll + ..\..\..\packages\Taef.Redist.Wlk.10.38.190610001-uapadmin\lib\net45\Wex.Logger.Interop.dll @@ -142,11 +142,11 @@ copy $(SolutionDir)\dep\WinAppDriver\* $(OutDir)\ - + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - + - \ No newline at end of file + diff --git a/src/host/ft_uia/packages.config b/src/host/ft_uia/packages.config index e7a7baf8c..ba53f7a07 100644 --- a/src/host/ft_uia/packages.config +++ b/src/host/ft_uia/packages.config @@ -5,5 +5,5 @@ - - \ No newline at end of file + + diff --git a/src/terminal/parser/ut_parser/packages.config b/src/terminal/parser/ut_parser/packages.config index 24feaf0e2..525e3e670 100644 --- a/src/terminal/parser/ut_parser/packages.config +++ b/src/terminal/parser/ut_parser/packages.config @@ -1,4 +1,4 @@  - + diff --git a/tools/OpenConsole.psm1 b/tools/OpenConsole.psm1 index f01b742ab..7be707963 100644 --- a/tools/OpenConsole.psm1 +++ b/tools/OpenConsole.psm1 @@ -159,7 +159,7 @@ function Invoke-OpenConsoleTests() [switch]$FTOnly, [parameter(Mandatory=$false)] - [ValidateSet('host', 'interactivityWin32', 'terminal', 'adapter', 'feature', 'uia', 'textbuffer', 'types')] + [ValidateSet('host', 'interactivityWin32', 'terminal', 'adapter', 'feature', 'uia', 'textbuffer', 'types', 'terminalCore', 'terminalApp')] [string]$Test, [parameter(Mandatory=$false)] diff --git a/tools/runut.cmd b/tools/runut.cmd index 1f9ade743..419f88b6d 100644 --- a/tools/runut.cmd +++ b/tools/runut.cmd @@ -11,4 +11,5 @@ call %TAEF% ^ %OPENCON%\bin\%PLATFORM%\%_LAST_BUILD_CONF%\ConParser.Unit.Tests.dll ^ %OPENCON%\bin\%PLATFORM%\%_LAST_BUILD_CONF%\ConAdapter.Unit.Tests.dll ^ %OPENCON%\bin\%PLATFORM%\%_LAST_BUILD_CONF%\Types.Unit.Tests.dll ^ + %OPENCON%\bin\%PLATFORM%\%_LAST_BUILD_CONF%\UnitTests_TerminalApp\Terminal.App.Unit.Tests.dll ^ %* diff --git a/tools/tests.xml b/tools/tests.xml index 05a8b4d83..2cf7c3b41 100644 --- a/tools/tests.xml +++ b/tools/tests.xml @@ -3,6 +3,7 @@ +