Snap for 6001391 from 052ca13a6e862007edc59f706841332850c8d8f1 to qt-aml-networking-release

Change-Id: I60b20f880cbd29c6cd47895b78f985ce65517c85
diff --git a/.gitignore b/.gitignore
index 8b3433a..55f5a57 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,3 +11,7 @@
 # Exceptions
 !aidegen/test_data/*.iml
 !aidegen/test_data/.idea
+
+# Because atest plugin needs use the directory, which contains "build"
+# folder, as fake date to test. Add the path to gitignore exception.
+!asuite_plugin/src/test/resources/root/build
diff --git a/aidegen/lib/sdk_config.py b/aidegen/lib/sdk_config.py
index f865e02..713bc37 100644
--- a/aidegen/lib/sdk_config.py
+++ b/aidegen/lib/sdk_config.py
@@ -90,6 +90,7 @@
     _NAME_JDK18 = 'JDK18'
     _TYPE_ANDROID_SDK = 'Android SDK'
     _INPUT_QUERY_TIMES = 3
+    _USER_HOME = '$USER_HOME$'
     _ANDROID_SDK_VERSION = 'Android API {API_LEVEL} Platform'
     _API_FOLDER_RE = re.compile(r'platforms/android-(?P<api_level>[\d]+)')
     _API_LEVEL_RE = re.compile(r'android-(?P<api_level>[\d]+)')
@@ -224,6 +225,9 @@
             jdk_name = self._get_first_element_value(jdk, self._TAG_NAME)
             jdk_type = self._get_first_element_value(jdk, self._TAG_TYPE)
             home_path = self._get_first_element_value(jdk, self._TAG_HOMEPATH)
+            if home_path.startswith(self._USER_HOME):
+                home_path = home_path.replace(self._USER_HOME,
+                                              os.path.expanduser('~'))
             platform_api = self._get_max_platform_api(home_path)
             sdk_api = self._get_api_from_xml(jdk, self._TAG_ADDITIONAL,
                                              self._ATTRIBUTE_SDK)
diff --git a/asuite_plugin/build.gradle b/asuite_plugin/build.gradle
index ef4cc62..7cd08cd 100644
--- a/asuite_plugin/build.gradle
+++ b/asuite_plugin/build.gradle
@@ -15,6 +15,7 @@
     }
     test {
         java.srcDirs = ['src/test']
+        resources.srcDirs = ['src/test/resources']
     }
 }
 
diff --git a/asuite_plugin/src/java/com/android/atest/AtestUtils.java b/asuite_plugin/src/java/com/android/atest/AtestUtils.java
index 4da3344..d35c52f 100644
--- a/asuite_plugin/src/java/com/android/atest/AtestUtils.java
+++ b/asuite_plugin/src/java/com/android/atest/AtestUtils.java
@@ -15,6 +15,7 @@
  */
 package com.android.atest;
 
+import java.io.File;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 
@@ -29,4 +30,26 @@
     public static boolean hasTestMapping(String path) {
         return Files.exists(Paths.get(path, Constants.TEST_MAPPING_FILE_NAME));
     }
+
+    /**
+     * Gets the Android root path by project path.
+     *
+     * @param projectPath the base path of user's project.
+     * @return the Android root path or null.
+     */
+    public static String getAndroidRoot(String projectPath) {
+        File currentFolder = new File(projectPath);
+        File parentFolder = currentFolder.getParentFile();
+        File checkFolder = new File(currentFolder, Constants.BUILD_ENVIRONMENT);
+        while (parentFolder != null) {
+            if (checkFolder.exists()) {
+                return currentFolder.getPath();
+            } else {
+                currentFolder = parentFolder;
+                parentFolder = currentFolder.getParentFile();
+                checkFolder = new File(currentFolder, Constants.BUILD_ENVIRONMENT);
+            }
+        }
+        return null;
+    }
 }
diff --git a/asuite_plugin/src/java/com/android/atest/Constants.java b/asuite_plugin/src/java/com/android/atest/Constants.java
index cd42c97..48720b3 100644
--- a/asuite_plugin/src/java/com/android/atest/Constants.java
+++ b/asuite_plugin/src/java/com/android/atest/Constants.java
@@ -21,4 +21,5 @@
     public static final String ATEST_NAME = "Atest";
     public static final String TEST_MAPPING_FILE_NAME = "TEST_MAPPING";
     public static final String ATEST_TOOL_WINDOW = "AtestToolWindow";
+    public static final String BUILD_ENVIRONMENT = "build/envsetup.sh";
 }
diff --git a/asuite_plugin/src/test/resources/root/a/b/TEST_MAPPING b/asuite_plugin/src/test/resources/root/a/b/TEST_MAPPING
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/asuite_plugin/src/test/resources/root/a/b/TEST_MAPPING
@@ -0,0 +1 @@
+{}
diff --git a/asuite_plugin/src/test/resources/root/a/b/c/dummy b/asuite_plugin/src/test/resources/root/a/b/c/dummy
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/asuite_plugin/src/test/resources/root/a/b/c/dummy
diff --git a/asuite_plugin/src/test/resources/root/build/envsetup.sh b/asuite_plugin/src/test/resources/root/build/envsetup.sh
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/asuite_plugin/src/test/resources/root/build/envsetup.sh
diff --git a/asuite_plugin/src/test/unittests/AtestUtilsTest.java b/asuite_plugin/src/test/unittests/AtestUtilsTest.java
new file mode 100644
index 0000000..4cd6487
--- /dev/null
+++ b/asuite_plugin/src/test/unittests/AtestUtilsTest.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package unittests;
+
+import com.android.atest.AtestUtils;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class AtestUtilsTest {
+
+    /** Tests GetAndroidRoot. */
+    @Test
+    public void testGetAndroidRoot() {
+        Assert.assertEquals(
+                AtestUtils.getAndroidRoot("src/test/resources/root/a/b/c"),
+                "src/test/resources/root");
+        Assert.assertNull(AtestUtils.getAndroidRoot("src/test"));
+    }
+
+    /** Tests hasTestMapping. */
+    @Test
+    public void testhasTestMapping() {
+        Assert.assertTrue(AtestUtils.hasTestMapping("src/test/resources/root/a/b"));
+        Assert.assertFalse(AtestUtils.hasTestMapping("src/test/resources/root/a/b/c"));
+    }
+}
diff --git a/asuite_plugin/src/test/unittests/CommandRunnerTest.java b/asuite_plugin/src/test/unittests/CommandRunnerTest.java
index 5cff23e..6920acf 100644
--- a/asuite_plugin/src/test/unittests/CommandRunnerTest.java
+++ b/asuite_plugin/src/test/unittests/CommandRunnerTest.java
@@ -26,7 +26,7 @@
 
 public class CommandRunnerTest {
 
-    /** Test CommandRunner by ls command. */
+    /** Tests CommandRunner by ls command. */
     @Test
     public void testCommandRunnerByLs() throws NoSuchFieldException, IllegalAccessException {
         ArrayList<String> command = new ArrayList<String>();
@@ -39,7 +39,7 @@
         Assert.assertEquals(commandLine.getCommandLineString(), "ls");
     }
 
-    /** Test CommandRunner by lunch target and test target. */
+    /** Tests CommandRunner by lunch target and test target. */
     @Test
     public void testCommandRunnerByTarget() throws NoSuchFieldException, IllegalAccessException {
         CommandRunner lsCommand = new CommandRunner("a", "b", "/", null);