Skip to content

Commit

Permalink
Fix issue where an originally-collapsed WebView2 can't be interacted …
Browse files Browse the repository at this point in the history
…with (#6761)

* Align WebView2Tests.cs with WinUI 3
* Add HiddenThenVisibleTest and ParentHiddentThenVisibleTest
* Add Panel content to WebView2
   * Resize child panel via arrange pass
* Add IAutomationPeerOverrides GetBoundingRectangleCore()
* Don't set Bounds on CoreWebView2 Controller when nothing has changed
* Replace hardcoded test page names

Co-authored-by: Kristen Schau <krschau@users.noreply.github.com>
  • Loading branch information
krschau and krschau authored Mar 3, 2022
1 parent 05a0b06 commit 397647e
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 38 deletions.
69 changes: 61 additions & 8 deletions dev/WebView2/InteractionTests/WebView2Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ public void MultipleWebviews_FocusTest()
Log.Comment("Focus on tabstop1");
x1.SetFocus();
Wait.ForIdle();
Verify.IsTrue(x1.HasKeyboardFocus);
Verify.IsTrue(x1.HasKeyboardFocus, "TabStopButton1 has keyboard focus");

Log.Comment("Tab tabstop1 -> MyWebView2");
KeyboardHelper.PressKey(Key.Tab);
Expand Down Expand Up @@ -1167,13 +1167,11 @@ public void MouseCaptureTest()
PointerInput.Press(PointerButtons.Primary);
PointerInput.Move(outsidePoint);
PointerInput.Release(PointerButtons.Primary);
Wait.ForIdle();

// Move mouse back across text
// If the WebView did not correctly handle captured mouse input then the text will be deselected
// due to the WebView still thinking that the mouse's left-button is pressed.
PointerInput.Move(startPoint);
Wait.ForIdle();

using (var pasteTestWaiter = new ValueChangedEventWaiter(CopyPasteTextBox2, "MouseCaptureResult"))
{
Expand Down Expand Up @@ -1338,7 +1336,6 @@ public void ResizeTest()

[TestMethod]
[TestProperty("Ignore", "True")] // Task 31708332: WebView2 Touch Tests still failing on Helix
[TestProperty("Ignore", "True")] //Task 31704068: Unreliable tests: WebView2 BasicTapTouchTest, BasicFlingTouchTest, BasicLongPressTouchTest
[TestProperty("TestSuite", "B")]
public void BasicTapTouchTest()
{
Expand Down Expand Up @@ -1366,7 +1363,6 @@ public void BasicTapTouchTest()

[TestMethod]
[TestProperty("Ignore", "True")] // Task 31708332: WebView2 Touch Tests still failing on Helix
[TestProperty("Ignore", "True")] //Task 31704068: Unreliable tests: WebView2 BasicTapTouchTest, BasicFlingTouchTest, BasicLongPressTouchTest
[TestProperty("TestSuite", "B")]
public void BasicFlingTouchTest()
{
Expand Down Expand Up @@ -1553,7 +1549,6 @@ public void ReparentElementTest()
CompleteTestAndWaitForResult("ReparentElementTest");

// Ensure that the moved WebView is still visible
Log.Comment("ReparentElementTest Test App Result ignored due to RTB issues with the test. Verifying from the test runner.");
WebView2Temporary.WebView2RenderingVerifier.VerifyInstances("ReparentElementTest");
}
}
Expand Down Expand Up @@ -2013,7 +2008,7 @@ public void CloseThenDPIChangeTest()

[TestMethod] // Test fails because .NET UWP doesn't support Object -> VARIANT marshalling.
[TestProperty("TestSuite", "D")]
[TestProperty("Ignore", "True")] // 32510465
[TestProperty("Ignore", "True")]
public void AddHostObjectToScriptTest()
{
if (!PlatformConfiguration.IsOsVersionGreaterThanOrEqual(OSVersion.Redstone5))
Expand Down Expand Up @@ -2149,10 +2144,68 @@ public void HtmlDropdownTest()
}
}

[TestMethod]
[TestProperty("TestSuite", "D")]
public void HiddenThenVisibleTest()
{
using (var setup = new WebView2TestSetupHelper(new[] { "WebView2 Tests", "navigateToBasicWebView2" }))
{
ChooseTest("HiddenThenVisibleTest");
CompleteTestAndWaitForResult("HiddenThenVisibleTest");

// Clear the cache so we can find the new webview
ElementCache.Clear();
var webview = FindElement.ById("MyWebView2");
Verify.IsTrue(webview.IsOffscreen == false);

// Click in the webview to ensure we can interact with it
Rectangle bounds = webview.BoundingRectangle;
Log.Comment("Bounds = X:{0}, Y:{1}, Width:{2}, Height:{3}", bounds.X, bounds.Y, bounds.Width, bounds.Height);
var point = new Point(bounds.X + 20, bounds.Y + 20);
Log.Comment("Move mouse to ({0}, {1})", bounds.X + 20, bounds.Y + 20);
PointerInput.Move(point);

PointerInput.Press(PointerButtons.Primary);
PointerInput.Release(PointerButtons.Primary);
Wait.ForIdle();

WaitForWebMessageResult("HiddenThenVisibleTest");
}
}

[TestMethod]
[TestProperty("TestSuite", "D")]
public void ParentHiddenThenVisibleTest()
{
using (var setup = new WebView2TestSetupHelper(new[] { "WebView2 Tests", "navigateToBasicWebView2" }))
{
ChooseTest("ParentHiddenThenVisibleTest");
CompleteTestAndWaitForResult("ParentHiddenThenVisibleTest");

// Clear the cache so we can find the new webview
ElementCache.Clear();
var webview = FindElement.ById("MyWebView2");
Verify.IsTrue(webview.IsOffscreen == false);

// Click in the webview to ensure we can interact with it
Rectangle bounds = webview.BoundingRectangle;
Log.Comment("Bounds = X:{0}, Y:{1}, Width:{2}, Height:{3}", bounds.X, bounds.Y, bounds.Width, bounds.Height);
var point = new Point(bounds.X + 20, bounds.Y + 20);
Log.Comment("Move mouse to ({0}, {1})", bounds.X + 20, bounds.Y + 20);
PointerInput.Move(point);

PointerInput.Press(PointerButtons.Primary);
PointerInput.Release(PointerButtons.Primary);
Wait.ForIdle();

WaitForWebMessageResult("ParentHiddenThenVisibleTest");
}
}

private static void BeginSubTest(string testName, string testDescription)
{
Log.Comment(Environment.NewLine + testName + ": " + testDescription);

Log.Comment("Resetting event and exception counts...");
Button resetCounts_Button = new Button(FindElement.ById("ResetCounts_Button"));
resetCounts_Button.Invoke();
Expand Down
4 changes: 3 additions & 1 deletion dev/WebView2/TestUI/WebView2BasicPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<ComboBoxItem AutomationProperties.Name="Focus_MouseActivateTest">Focus_MouseActivateTest</ComboBoxItem>
<ComboBoxItem AutomationProperties.Name="Focus_ReverseTabTest">Focus_ReverseTabTest</ComboBoxItem>
<ComboBoxItem AutomationProperties.Name="GoBackAndForwardTest">GoBackAndForwardTest</ComboBoxItem>
<ComboBoxItem AutomationProperties.Name="HiddenThenVisibleTest">HiddenThenVisibleTest</ComboBoxItem>
<ComboBoxItem AutomationProperties.Name="HostNameToFolderMappingTest">HostNameToFolderMappingTest</ComboBoxItem>
<ComboBoxItem AutomationProperties.Name="HtmlDropdownTest">HtmlDropdownTest</ComboBoxItem>
<ComboBoxItem AutomationProperties.Name="MouseCaptureTest">MouseCaptureTest</ComboBoxItem>
Expand All @@ -63,6 +64,7 @@
<ComboBoxItem AutomationProperties.Name="NavigationStartingTest">NavigationStartingTest</ComboBoxItem>
<ComboBoxItem AutomationProperties.Name="NonAsciiUriTest">NonAsciiUriTest</ComboBoxItem>
<ComboBoxItem AutomationProperties.Name="OffTreeWebViewInputTest">OffTreeWebViewInputTest</ComboBoxItem>
<ComboBoxItem AutomationProperties.Name="ParentHiddenThenVisibleTest">ParentHiddenThenVisibleTest</ComboBoxItem>
<ComboBoxItem AutomationProperties.Name="ParentVisibilityHiddenTest">ParentVisibilityHiddenTest</ComboBoxItem>
<ComboBoxItem AutomationProperties.Name="ParentVisibilityTurnedOnTest">ParentVisibilityTurnedOnTest</ComboBoxItem>
<ComboBoxItem AutomationProperties.Name="PointerReleaseWithoutPressTest">PointerReleaseWithoutPressTest</ComboBoxItem>
Expand Down Expand Up @@ -175,7 +177,7 @@
</ScrollViewer>
</StackPanel>
<StackPanel Orientation="Vertical">
<TextBlock Text="RTB:" FontWeight="Bold" FontSize="11" Margin="0,2,0,2"/>
<TextBlock Text="Focus:" FontWeight="Bold" FontSize="11" Margin="0,2,0,2"/>
<Border Width="400" Height="320" BorderBrush="Blue" BorderThickness="1" HorizontalAlignment="Left" Visibility="Visible">
<ScrollViewer x:Name="focusScrollViewer" VerticalScrollBarVisibility="Visible">
<TextBlock x:Name="focusLog" ></TextBlock>
Expand Down
124 changes: 111 additions & 13 deletions dev/WebView2/TestUI/WebView2BasicPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ enum TestList
NonAsciiUriTest,
OffTreeWebViewInputTest,
HtmlDropdownTest,
HiddenThenVisibleTest,
ParentHiddenThenVisibleTest
};

// Map of TestList entry to its webpage (index in TestPageNames[])
Expand Down Expand Up @@ -260,6 +262,8 @@ enum TestList
{ TestList.NonAsciiUriTest, 7 },
{ TestList.OffTreeWebViewInputTest, 1 },
{ TestList.HtmlDropdownTest, 5 },
{ TestList.HiddenThenVisibleTest, 1 },
{ TestList.ParentHiddenThenVisibleTest, 1 },
};

readonly string[] TestPageNames =
Expand Down Expand Up @@ -537,7 +541,7 @@ private void TestNameComboBox_SelectionChanged(object sender, SelectionChangedEv
{
AddWebViewControl("MyWebView2B");
AddWebViewControl("MyWebView2C");
WebView2Common.LoadWebPage(MyWebView2, TestPageNames[0]);
WebView2Common.LoadWebPage(MyWebView2, TestPageNames[TestInfoDictionary[test]]);
}
break;
case TestList.MultipleWebviews_FocusTest:
Expand Down Expand Up @@ -565,9 +569,7 @@ private void TestNameComboBox_SelectionChanged(object sender, SelectionChangedEv
case TestList.SourceBeforeLoadTest:
{
// Remove existing WebView, we're going to replace it
RemoveWebViewEventHandlers(MyWebView2);
Border parentBorder = MyWebView2.Parent as Border;
parentBorder.Child = null;
Border parentBorder = RemoveExistingWebViewControl(MyWebView2);

Uri uri = WebView2Common.GetTestPageUri("SimplePageWithButton.html");
var newWebView2 = new WebView2() {
Expand Down Expand Up @@ -612,9 +614,7 @@ private void TestNameComboBox_SelectionChanged(object sender, SelectionChangedEv
case TestList.WindowlessPopupTest:
{
// Remove existing WebView so it doesn't get confused with the one in the popup
RemoveWebViewEventHandlers(MyWebView2);
Border parentBorder = MyWebView2.Parent as Border;
parentBorder.Child = null;
_ = RemoveExistingWebViewControl(MyWebView2);

// Create popup contents: a new webview
var webviewInPopup = new WebView2() {
Expand Down Expand Up @@ -662,9 +662,7 @@ private void TestNameComboBox_SelectionChanged(object sender, SelectionChangedEv
case TestList.CursorUpdateTest:
{
// Remove existing WebView, we're going to replace it
RemoveWebViewEventHandlers(MyWebView2);
Border parentBorder = MyWebView2.Parent as Border;
parentBorder.Child = null;
Border parentBorder = RemoveExistingWebViewControl(MyWebView2);

// Insert webview with public ProtectedCursor
var newWebView2 = new WebView2WithCursor() {
Expand All @@ -691,12 +689,69 @@ private void TestNameComboBox_SelectionChanged(object sender, SelectionChangedEv
case TestList.OffTreeWebViewInputTest:
{
// Remove existing webview
RemoveWebViewEventHandlers(MyWebView2);
Border parentBorder = MyWebView2.Parent as Border;
parentBorder.Child = null;
Border parentBorder = RemoveExistingWebViewControl(MyWebView2);
parentBorder.BorderBrush = new SolidColorBrush(Colors.Pink);
}
break;
case TestList.HiddenThenVisibleTest:
{
// Remove existing WebView, we're going to replace it with a hidden one
Border parentBorder = RemoveExistingWebViewControl(MyWebView2);
var parentStackPanel = parentBorder.Parent as StackPanel;
parentStackPanel.Children.Remove(parentBorder);

// Create a new border that gets its size from the webview
var newBorder = new Border() {
BorderBrush = new SolidColorBrush(Colors.Red),
BorderThickness = new Thickness(5)
};

// Add a new, collapsed WebView2 into the tree
var uri = WebView2Common.GetTestPageUri(TestPageNames[TestInfoDictionary[test]]);
var newWebView2 = new WebView2() {
Name = "MyWebView2",
Margin = new Thickness(8, 8, 8, 8),
Width = 670,
Height = 370,
Source = uri,
Visibility = Visibility.Collapsed,
};
AutomationProperties.SetName(newWebView2, "MyWebView2");
newBorder.Child = newWebView2;
parentStackPanel.Children.Add(newBorder);
AddWebViewEventHandlers(newWebView2);
}
break;
case TestList.ParentHiddenThenVisibleTest:
{
// Remove existing WebView, we're going to replace it with a hidden one
Border parentBorder = RemoveExistingWebViewControl(MyWebView2);
var parentStackPanel = parentBorder.Parent as StackPanel;
parentStackPanel.Children.Remove(parentBorder);

// Create a new, collapsed border that gets its size from the webview
var newBorder = new Border() {
BorderBrush = new SolidColorBrush(Colors.Red),
BorderThickness = new Thickness(5),
Visibility = Visibility.Collapsed
};

// Add a new WebView2 into the tree
var uri = WebView2Common.GetTestPageUri(TestPageNames[TestInfoDictionary[test]]);
var newWebView2 = new WebView2() {
Name = "MyWebView2",
Margin = new Thickness(8, 8, 8, 8),
Width = 670,
Height = 370,
Source = uri,
Visibility = Visibility.Visible,
};
AutomationProperties.SetName(newWebView2, "MyWebView2");
newBorder.Child = newWebView2;
parentStackPanel.Children.Add(newBorder);
AddWebViewEventHandlers(newWebView2);
}
break;
default:
WebView2Common.LoadWebPage(MyWebView2, TestPageNames[TestInfoDictionary[test]]);
break;
Expand Down Expand Up @@ -903,6 +958,14 @@ void MoveWebViewControl(string webviewName, WebView2 webView2)
WebView2Collection.Children.Add(stackPanel);
}

Border RemoveExistingWebViewControl(WebView2 webview)
{
RemoveWebViewEventHandlers(webview);
Border parentBorder = webview.Parent as Border;
parentBorder.Child = null;
return parentBorder;
}

void AddWebViewEventHandlers(WebView2 webview)
{
webview.NavigationStarting += OnNavigationStarting;
Expand Down Expand Up @@ -1119,6 +1182,11 @@ public void VerifyWebMessageBasedTest(string stringResult, string jsonResult)
expectedStringResult = "Left mouse button clicked.";
break;

case TestList.HiddenThenVisibleTest:
case TestList.ParentHiddenThenVisibleTest:
expectedStringResult = "Left mouse button clicked.";
break;

default:
{
logger.LogError("Unexpected test type in VerifyWebMessageBasedTest(), got message: " + stringResult);
Expand Down Expand Up @@ -1807,6 +1875,36 @@ async public void CompleteCurrentTest(object sender, RoutedEventArgs args)
selectedTest, selctedOption));;
}
break;
case TestList.HiddenThenVisibleTest:
{
logger.Verify(MyWebView2.Visibility == Visibility.Collapsed,
string.Format("Test {0}: Incorrect setup, Expected MyWebView2.Visibility to be Collapsed, was {1}",
selectedTest, MyWebView2.Visibility));

// Make WebView2 visible
MyWebView2.Visibility = Visibility.Visible;

logger.Verify(MyWebView2.IsHitTestVisible,
string.Format("Test {0}: Failed, Expected MyWebView2.IsHitTestVisible to be true, was {1}",
selectedTest, MyWebView2.IsHitTestVisible));
}
break;
case TestList.ParentHiddenThenVisibleTest:
{
var parentBorder = MyWebView2.Parent as Border;

logger.Verify(parentBorder.Visibility == Visibility.Collapsed,
string.Format("Test {0}: Incorrect setup, Expected MyWebView2.Visibility to be Collapsed, was {1}",
selectedTest, parentBorder.Visibility));

// Make WebView2's parent Border visible
parentBorder.Visibility = Visibility.Visible;

logger.Verify(MyWebView2.IsHitTestVisible,
string.Format("Test {0}: Failed, Expected MyWebView2.IsHitTestVisible to be true, was {1}",
selectedTest, MyWebView2.IsHitTestVisible));
}
break;

default:
break;
Expand Down
Loading

0 comments on commit 397647e

Please sign in to comment.