Merge "Add tests for `dumpWindowHierarchy(*)`." into androidx-main
diff --git a/test/uiautomator/integration-tests/testapp/src/androidTest/java/androidx/test/uiautomator/testapp/UiDeviceTest.java b/test/uiautomator/integration-tests/testapp/src/androidTest/java/androidx/test/uiautomator/testapp/UiDeviceTest.java
index 30ee3ee..8562b5cf 100644
--- a/test/uiautomator/integration-tests/testapp/src/androidTest/java/androidx/test/uiautomator/testapp/UiDeviceTest.java
+++ b/test/uiautomator/integration-tests/testapp/src/androidTest/java/androidx/test/uiautomator/testapp/UiDeviceTest.java
@@ -43,6 +43,7 @@
 import org.w3c.dom.Element;
 
 import java.io.File;
+import java.io.FileOutputStream;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -386,23 +387,53 @@
         assertTrue(mDevice.isNaturalOrientation());
     }
 
-    /* TODO(b/235841020): Implement these tests, and the tests for exceptions of each tested method.
+    @Test
+    public void testDumpWindowHierarchy_withString() throws Exception {
+        launchTestActivity(MainActivity.class);
+        File outFile = mTmpDir.newFile();
+        mDevice.dumpWindowHierarchy(outFile.getAbsolutePath());
 
-    public void testDumpWindowHierarchy_withString() {}
-
-    public void testDumpWindowHierarchy_withFile() {} // already added
-
-    public void testDumpWindowHierarchy_withOutputStream() {}
-    */
+        // Verify that a valid XML file was generated and that node attributes are correct.
+        Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(outFile);
+        validateMainActivityXml(xml);
+    }
 
     @Test
-    public void testDumpWindowHierarchy() throws Exception {
+    public void testDumpWindowHierarchy_withFile() throws Exception {
         launchTestActivity(MainActivity.class);
         File outFile = mTmpDir.newFile();
         mDevice.dumpWindowHierarchy(outFile);
 
         // Verify that a valid XML file was generated and that node attributes are correct.
         Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(outFile);
+        validateMainActivityXml(xml);
+    }
+
+    @Test
+    public void testDumpWindowHierarchy_withOutputStream() throws Exception {
+        launchTestActivity(MainActivity.class);
+        File outFile = mTmpDir.newFile();
+        FileOutputStream outStream = new FileOutputStream(outFile);
+        mDevice.dumpWindowHierarchy(outStream);
+
+        Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(outFile);
+        validateMainActivityXml(xml);
+    }
+
+    /* TODO(b/235841020): Implement these tests, and the tests for exceptions of each tested method.
+
+    public void testWaitForWindowUpdate() {}
+
+    public void testTakeScreenshot() {} // already added
+
+    public void testTakeScreenshot_withScaleAndQuality() {} // already added
+
+    public void testGetLauncherPackageName() {}
+
+    public void testExecuteShellCommand() {} // already added
+    */
+
+    private static void validateMainActivityXml(Document xml) throws Exception {
         Element element = (Element) XPathFactory.newInstance().newXPath()
                 .compile("//hierarchy//*/node[@resource-id='" + TEST_APP + ":id/button']")
                 .evaluate(xml, XPathConstants.NODE);
@@ -426,17 +457,4 @@
         assertEquals("true", element.getAttribute("visible-to-user"));
         assertNotNull(element.getAttribute("bounds"));
     }
-
-    /* TODO(b/235841020): Implement these tests, and the tests for exceptions of each tested method.
-
-    public void testWaitForWindowUpdate() {}
-
-    public void testTakeScreenshot() {} // already added
-
-    public void testTakeScreenshot_withScaleAndQuality() {} // already added
-
-    public void testGetLauncherPackageName() {}
-
-    public void testExecuteShellCommand() {} // already added
-    */
 }