public void BasicCoreObjectCreationAndDestructionTest()

in dev/WebView2/InteractionTests/WebView2Tests.cs [2253:2638]


        public void BasicCoreObjectCreationAndDestructionTest()
        {
            if (!PlatformConfiguration.IsOsVersionGreaterThanOrEqual(OSVersion.Redstone5))
            {
                Log.Warning("CoreWebView2 doesn't work RS2-RS4 yet");
                return;
            }

            using (var setup = new WebView2TestSetupHelper(new[] { "WebView2 Tests", "navigateToCoreObjectsWebView2" }))
            {
                ChooseTest("BasicCoreObjectCreationAndDestructionTest", false /* waitForLoadCompleted */);
                Wait.ForIdle();

                Button resetCounts_Button = new Button(FindElement.ById("ResetCounts_Button"));
                resetCounts_Button.Invoke();
                Wait.ForIdle();

                var coreWebView2InitializedCount = new Edit(FindElement.ById("CoreWebView2InitializedCount"));
                var ensureCoreWebView2CompletionCount = new Edit(FindElement.ById("EnsureCoreWebView2CompletionCount")); 
                var navigationCompletedCount = new Edit(FindElement.ById("NavigationCompletedCount"));
                var loadedCount = new Edit(FindElement.ById("LoadedCount"));
                var unloadedCount = new Edit(FindElement.ById("UnloadedCount"));

                BeginSubTest("Preload Test #1", "CreateOffline -> SetSource -> Wait(NavCompleted) -> EnterTree -> Wait(Loaded)");
                Button create_OfflineElement_Button = new Button(FindElement.ById("Create_OfflineElement_Button"));
                Button setSource_OfflineElement_Button = new Button(FindElement.ById("SetSource_OfflineElement_Button"));
                Button add_OfflineElement_Button = new Button(FindElement.ById("Add_OfflineElement_Button"));

                // Create offline element and set source
                Log.Comment("Create offline element and set source...");
                using (var navigationCompletedWaiter = new ValueChangedEventWaiter(navigationCompletedCount, "1"))
                {
                    create_OfflineElement_Button.Invoke();
                    setSource_OfflineElement_Button.Invoke();
                    navigationCompletedWaiter.Wait();
                }

                ValidateEventCounts(
                    1,      // coreWebView2InitializedCountExpected
                    0,      // ensureCoreWebView2CompletionCountExpected
                    1,      // navigationStartingCountExpected
                    1,      // navigationCompletedCountExpected
                    0,      // loadedCountExpected
                    0,      // unloadedCountExpected
                    0,      // coreProcessFailedCountExpected
                    "Created offline w/ source (not live yet)"
                );

                Log.Comment("Add to tree...");
                using (var loadedWaiter = new ValueChangedEventWaiter(loadedCount, "1"))
                {
                    add_OfflineElement_Button.Invoke();
                    loadedWaiter.Wait();
                }

                ValidateEventCounts(
                    1,      // coreWebView2InitializedCountExpected
                    0,      // ensureCoreWebView2CompletionCountExpected
                    1,      // navigationStartingCountExpected
                    1,      // navigationCompletedCountExpected
                    1,      // loadedCountExpected
                    0,      // unloadedCountExpected
                    0,      // coreProcessFailedCountExpected
                    "Added to tree"
                );
                EndSubTest("Preload Test #1");

                BeginSubTest("Preload Test #2", "CreateOffline -> EnsureCWV2() -> Wait(EnsureCWV2_Completion) -> EnterTree -> Wait(Loaded) -> SetSource");
                Button ensureCoreWebView2_OfflineElement_Button = new Button(FindElement.ById("EnsureCoreWebView2_OfflineElement_Button"));

                Log.Comment("Create offline element and EnsureCWV2()...");
                using (var ensureCoreWebView2CompletionWaiter = new ValueChangedEventWaiter(ensureCoreWebView2CompletionCount, "1"))
                {
                    create_OfflineElement_Button.Invoke();
                    ensureCoreWebView2_OfflineElement_Button.Invoke();
                    ensureCoreWebView2CompletionWaiter.Wait();
                }

                ValidateEventCounts(
                    1,      // coreWebView2InitializedCountExpected
                    1,      // ensureCoreWebView2CompletionCountExpected
                    0,      // navigationStartingCountExpected
                    0,      // navigationCompletedCountExpected
                    0,      // loadedCountExpected
                    0,      // unloadedCountExpected
                    0,      // coreProcessFailedCountExpected
                    "Created offline without source (not live yet)"
                );

                Log.Comment("Add to tree...");
                using (var loadedWaiter = new ValueChangedEventWaiter(loadedCount, "1"))
                {
                    add_OfflineElement_Button.Invoke();
                    loadedWaiter.Wait();
                }

                ValidateEventCounts(
                    1,      // coreWebView2InitializedCountExpected
                    1,      // ensureCoreWebView2CompletionCountExpected
                    0,      // navigationStartingCountExpected
                    0,      // navigationCompletedCountExpected
                    1,      // loadedCountExpected
                    0,      // unloadedCountExpected
                    0,      // coreProcessFailedCountExpected
                    "Added to tree"
                );

                Log.Comment("Set source...");
                using (var navigationCompletedWaiter = new ValueChangedEventWaiter(navigationCompletedCount, "1"))
                {
                    setSource_OfflineElement_Button.Invoke();
                    navigationCompletedWaiter.Wait();
                }

                ValidateEventCounts(
                    1,      // coreWebView2InitializedCountExpected
                    1,      // ensureCoreWebView2CompletionCountExpected
                    1,      // navigationStartingCountExpected
                    1,      // navigationCompletedCountExpected
                    1,      // loadedCountExpected
                    0,      // unloadedCountExpected
                    0,      // coreProcessFailedCountExpected
                    "Set source"
                );
                EndSubTest("Preload Test #2");

                BeginSubTest("Live Element Test #1", "CreateLive -> Set Source -> Wait(NavCompleted/Loaded)");
                Button create_LiveElement_Button = new Button(FindElement.ById("Create_LiveElement_Button"));
                Button setSource_LiveElement_Button = new Button(FindElement.ById("SetSource_LiveElement_Button"));

                // Notice logic in the waiters here to handle race between NavCompleted and Loaded
                Log.Comment("Create live element and set source...");
                using (var navigationCompletedWaiter = new ValueChangedEventWaiter(navigationCompletedCount, "1"))
                {
                    create_LiveElement_Button.Invoke();
                    setSource_LiveElement_Button.Invoke();
                    navigationCompletedWaiter.Wait();
                }

                if (loadedCount.Value != "1")
                {
                    using (var loadedWaiter = new ValueChangedEventWaiter(loadedCount, "1"))
                    {
                        loadedWaiter.Wait();
                    }
                }

                ValidateEventCounts(
                    1,      // coreWebView2InitializedCountExpected
                    0,      // ensureCoreWebView2CompletionCountExpected
                    1,      // navigationStartingCountExpected
                    1,      // navigationCompletedCountExpected
                    1,      // loadedCountExpected
                    0,      // unloadedCountExpected
                    0,      // coreProcessFailedCountExpected
                    "Create live with source"
                );
                EndSubTest("Live Element Test #1");

                BeginSubTest("Live Element Test #2", "CreateLive -> EnsureCWV2() -> Wait(EnsureCWV2_Completion/Loaded) -> SetSource");
                Button ensureCoreWebView2_LiveElement_Button = new Button(FindElement.ById("EnsureCoreWebView2_LiveElement_Button"));

                Log.Comment("Create live element and EnsureCWV2()...");
                using (var ensureCoreWebView2CompletionWaiter = new ValueChangedEventWaiter(ensureCoreWebView2CompletionCount, "1"))
                {
                    create_LiveElement_Button.Invoke();
                    ensureCoreWebView2_LiveElement_Button.Invoke();
                    ensureCoreWebView2CompletionWaiter.Wait();
                }

                if (loadedCount.Value != "1")
                {
                    using (var loadedWaiter = new ValueChangedEventWaiter(loadedCount, "1"))
                    {
                        loadedWaiter.Wait();
                    }
                }
                ValidateEventCounts(
                    1,      // coreWebView2InitializedCountExpected
                    1,      // ensureCoreWebView2CompletionCountExpected
                    0,      // navigationStartingCountExpected
                    0,      // navigationCompletedCountExpected
                    1,      // loadedCountExpected
                    0,      // unloadedCountExpected
                    0,      // coreProcessFailedCountExpected
                    "Create live without source"
                );

                Log.Comment("Set source...");
                using (var navigationCompletedWaiter = new ValueChangedEventWaiter(navigationCompletedCount, "1"))
                {
                    setSource_LiveElement_Button.Invoke();
                    navigationCompletedWaiter.Wait();
                }

                ValidateEventCounts(
                    1,      // coreWebView2InitializedCountExpected
                    1,      // ensureCoreWebView2CompletionCountExpected
                    1,      // navigationStartingCountExpected
                    1,      // navigationCompletedCountExpected
                    1,      // loadedCountExpected
                    0,      // unloadedCountExpected
                    0,      // coreProcessFailedCountExpected
                    "Source set"
                );
                EndSubTest("Live Element Test #2");

                BeginSubTest("Markup Test #1", "Create_XamlReaderLoad -> EnterTree-> EnsureCWV2() -> Wait(EnsureCWV2_Completion/Loaded) -> SetSource");
                Button create_MarkupElement_Blank_Button = new Button(FindElement.ById("Create_MarkupElement_Blank_Button"));
                Button ensureCoreWebView2_MarkupElement_Button = new Button(FindElement.ById("EnsureCoreWebView2_MarkupElement_Button"));
                Button setSource_MarkupElement_Button = new Button(FindElement.ById("SetSource_MarkupElement_Button"));

                Log.Comment("Load from Markup and add to tree");
                using (var loadedWwaiter = new ValueChangedEventWaiter(loadedCount, "1"))
                {
                    create_MarkupElement_Blank_Button.Invoke();
                    loadedWwaiter.Wait();
                }
                ValidateEventCounts(
                    0,      // coreWebView2InitializedCountExpected
                    0,      // ensureCoreWebView2CompletionCountExpected
                    0,      // navigationStartingCountExpected
                    0,      // navigationCompletedCountExpected
                    1,      // loadedCountExpected
                    0,      // unloadedCountExpected
                    0,      // coreProcessFailedCountExpected
                    "Loaded from markup and added to tree"
                );

                Log.Comment("Call EnsureCWV2()");
                using (var ensureCoreWebView2CompletionWaiter = new ValueChangedEventWaiter(ensureCoreWebView2CompletionCount, "1"))
                {
                    ensureCoreWebView2_MarkupElement_Button.Invoke();
                    ensureCoreWebView2CompletionWaiter.Wait();
                }
                ValidateEventCounts(
                    1,      // coreWebView2InitializedCountExpected
                    1,      // ensureCoreWebView2CompletionCountExpected
                    0,      // navigationStartingCountExpected
                    0,      // navigationCompletedCountExpected
                    1,      // loadedCountExpected
                    0,      // unloadedCountExpected
                    0,      // coreProcessFailedCountExpected
                    "Loaded from markup and added to tree"
                );

                Log.Comment("Set Source");
                using (var navigationCompletedWaiter = new ValueChangedEventWaiter(navigationCompletedCount, "1"))
                {
                    setSource_MarkupElement_Button.Invoke();
                    navigationCompletedWaiter.Wait();
                }

                ValidateEventCounts(
                    1,      // coreWebView2InitializedCountExpected
                    1,      // ensureCoreWebView2CompletionCountExpected
                    1,      // navigationStartingCountExpected
                    1,      // navigationCompletedCountExpected
                    1,      // loadedCountExpected
                    0,      // unloadedCountExpected
                    0,      // coreProcessFailedCountExpected
                    "Source set"
                );
                EndSubTest("Markup Test #1");

                // MarkupTest #2: XamlReader.Load() WV2 with Source Set
                BeginSubTest("Markup Test #2", "Create_XamlReaderLoad (with Source set)");
                Button create_MarkupElement_Source_Button = new Button(FindElement.ById("Create_MarkupElement_Source_Button"));

                Log.Comment("Create Live element w/ Source");
                using (var navigationCompletedWaiter = new ValueChangedEventWaiter(navigationCompletedCount, "1"))
                {
                    create_MarkupElement_Source_Button.Invoke();
                    navigationCompletedWaiter.Wait();
                }

                if (loadedCount.Value != "1")
                {
                    using (var loadedWaiter = new ValueChangedEventWaiter(loadedCount, "1"))
                    {
                        loadedWaiter.Wait();
                    }
                }

                ValidateEventCounts(
                    1,      // coreWebView2InitializedCountExpected
                    0,      // ensureCoreWebView2CompletionCountExpected
                    1,      // navigationStartingCountExpected
                    1,      // navigationCompletedCountExpected
                    1,      // loadedCountExpected
                    0,      // unloadedCountExpected
                    0,      // coreProcessFailedCountExpected
                    "Loaded from markup w/ Source"
                );
                EndSubTest("Markup Test #2");

                Log.Comment("Close() API Tests: Force close the 3 WV2s we created above");
                // Close() API Test: Force close the 3 WV2' we created above
                //   Part A: Close WV2 element used earlier for "Offline (pre-load) Creation" test
                BeginSubTest("Close Test #1", "Close WV2 element used earlier for 'Offline (pre-load) Creation' test");
                Button releaseReference_OfflineElement_Button = new Button(FindElement.ById("ReleaseReference_OfflineElement_Button"));
                Button close_OfflineElement_Button = new Button(FindElement.ById("Close_OfflineElement_Button"));
                Button remove_OfflineElement_Button = new Button(FindElement.ById("Remove_OfflineElement_Button"));

                Log.Comment("Remove from tree, then Close");
                using (var unloadedWaiter = new ValueChangedEventWaiter(unloadedCount, "1"))
                {
                    remove_OfflineElement_Button.Invoke();
                    close_OfflineElement_Button.Invoke();
                    unloadedWaiter.Wait();
                }
                ValidateEventCounts(
                    0,      // coreWebView2InitializedCountExpected
                    0,      // ensureCoreWebView2CompletionCountExpected
                    0,      // navigationStartingCountExpected
                    0,      // navigationCompletedCountExpected
                    0,      // loadedCountExpected
                    1,      // unloadedCountExpected
                    0,      // coreProcessFailedCountExpected
                    "Remove, Close"
                );

                Log.Comment("Try to set Source -> expect RO_E_CLOSED");
                setSource_OfflineElement_Button.Invoke();
                ValidateExceptionCounts(
                    1       // closedExceptionCountExpected
                );
                EndSubTest("Close Test #1");

                BeginSubTest("Close Test #2", "Close WV2 element used earlier for 'Live (in-tree) Creation' test");
                Button remove_LiveElement_Button = new Button(FindElement.ById("Remove_LiveElement_Button"));
                Button close_LiveElement_Button = new Button(FindElement.ById("Close_LiveElement_Button"));

                // Test just Close(). No events to wait on here.
                Log.Comment("Close");
                close_LiveElement_Button.Invoke();

                ValidateEventCounts(
                    0,      // coreWebView2InitializedCountExpected
                    0,      // ensureCoreWebView2CompletionCountExpected
                    0,      // navigationStartingCountExpected
                    0,      // navigationCompletedCountExpected
                    0,      // loadedCountExpected
                    0,      // unloadedCountExpected
                    0,      // coreProcessFailedCountExpected
                    "Close"
                );

                Log.Comment("Try EnsureCWV2 -> expect RO_E_CLOSED");
                ensureCoreWebView2_LiveElement_Button.Invoke();
                ValidateExceptionCounts(
                    1       // closedExceptionCountExpected
                );
                EndSubTest("Close Test #2");

                BeginSubTest("Close Test #3", "Close WV2 element used earlier for 'Markup-based Creation' test");
                Button remove_MarkupElement_Button = new Button(FindElement.ById("Remove_MarkupElement_Button"));
                Button close_MarkupElement_Button = new Button(FindElement.ById("Close_MarkupElement_Button"));

                Log.Comment("Close, then Remove from tree");
                using (var unloadedWaiter = new ValueChangedEventWaiter(unloadedCount, "1"))
                {
                    close_MarkupElement_Button.Invoke();
                    remove_MarkupElement_Button.Invoke();
                    unloadedWaiter.Wait();
                }

                ValidateEventCounts(
                    0,      // coreWebView2InitializedCountExpected
                    0,      // ensureCoreWebView2CompletionCountExpected
                    0,      // navigationStartingCountExpected
                    0,      // navigationCompletedCountExpected
                    0,      // loadedCountExpected
                    1,      // unloadedCountExpected
                    0,      // coreProcessFailedCountExpected
                    "Close, Remove"
                );

                Log.Comment("Try EnsureCWV2 -> expect RO_E_CLOSED");
                ensureCoreWebView2_MarkupElement_Button.Invoke();
                ValidateExceptionCounts(
                    1       // closedExceptionCountExpected
                );
                EndSubTest("Close Test #3");
            }
        }