Atest: new --list-module/-L argument for *ts.

This CL responds the users who'd like to list testable modules
by suite name on atest. Now atest provides a "--list-modules"
argument which behaves the same as running *ts-tradefed list modules.

Refactor below methods from module_finder to module_info.py.
* is_testable_module
* has_test_config
* get_robolectric_test_name
* is_robolectric_test
* is_auto_gen_test_config

Add new methods in module-info.py.
* get_testable_modules

This could also be benefit for tab_completion.

Bug: b/122353423

Test: atest --list-modules cts
      atest --list-modules vts
      Make sure the results be consistent with *ts-tradefed.

Change-Id: I76a60e1142cd5e3d6d38d37bebb8d57c45ffc5e2
diff --git a/atest/module_info_unittest.py b/atest/module_info_unittest.py
index f247b4d..b6537c8 100755
--- a/atest/module_info_unittest.py
+++ b/atest/module_info_unittest.py
@@ -34,6 +34,29 @@
 PATH_TO_MULT_MODULES_WITH_MULTI_ARCH = 'shared/path/to/be/used2'
 TESTABLE_MODULES_WITH_SHARED_PATH = ['multiarch1', 'multiarch2', 'multiarch3', 'multiarch3_32']
 
+ROBO_MOD_PATH = ['/shared/robo/path']
+NON_RUN_ROBO_MOD_NAME = 'robo_mod'
+RUN_ROBO_MOD_NAME = 'run_robo_mod'
+NON_RUN_ROBO_MOD = {constants.MODULE_NAME: NON_RUN_ROBO_MOD_NAME,
+                    constants.MODULE_PATH: ROBO_MOD_PATH,
+                    constants.MODULE_CLASS: ['random_class']}
+RUN_ROBO_MOD = {constants.MODULE_NAME: RUN_ROBO_MOD_NAME,
+                constants.MODULE_PATH: ROBO_MOD_PATH,
+                constants.MODULE_CLASS: [constants.MODULE_CLASS_ROBOLECTRIC]}
+MOD_PATH_INFO_DICT = {ROBO_MOD_PATH[0]: [RUN_ROBO_MOD, NON_RUN_ROBO_MOD]}
+MOD_NAME_INFO_DICT = {
+    RUN_ROBO_MOD_NAME: RUN_ROBO_MOD,
+    NON_RUN_ROBO_MOD_NAME: NON_RUN_ROBO_MOD}
+MOD_NAME1 = 'mod1'
+MOD_NAME2 = 'mod2'
+MOD_NAME3 = 'mod3'
+MOD_NAME4 = 'mod4'
+MOD_INFO_DICT = {}
+MODULE_INFO = {constants.MODULE_NAME: 'random_name',
+               constants.MODULE_PATH: 'a/b/c/path',
+               constants.MODULE_CLASS: ['random_class']}
+NAME_TO_MODULE_INFO = {'random_name' : MODULE_INFO}
+
 #pylint: disable=protected-access
 class ModuleInfoUnittests(unittest.TestCase):
     """Unit tests for module_info.py"""
@@ -130,6 +153,123 @@
         TESTABLE_MODULES_WITH_SHARED_PATH.sort()
         self.assertEqual(module_list, TESTABLE_MODULES_WITH_SHARED_PATH)
 
+    def test_is_suite_in_compatibility_suites(self):
+        """Test is_suite_in_compatibility_suites."""
+        mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH)
+        info = {'compatibility_suites': []}
+        self.assertFalse(mod_info.is_suite_in_compatibility_suites("cts", info))
+        info2 = {'compatibility_suites': ["cts"]}
+        self.assertTrue(mod_info.is_suite_in_compatibility_suites("cts", info2))
+        self.assertFalse(mod_info.is_suite_in_compatibility_suites("vts", info2))
+        info3 = {'compatibility_suites': ["cts", "vts"]}
+        self.assertTrue(mod_info.is_suite_in_compatibility_suites("cts", info3))
+        self.assertTrue(mod_info.is_suite_in_compatibility_suites("vts", info3))
+        self.assertFalse(mod_info.is_suite_in_compatibility_suites("ats", info3))
+
+    @mock.patch.object(module_info.ModuleInfo, 'is_testable_module')
+    @mock.patch.object(module_info.ModuleInfo, 'is_suite_in_compatibility_suites')
+    def test_get_testable_modules(self, mock_is_suite_exist, mock_is_testable):
+        """Test get_testable_modules."""
+        mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH)
+        mock_is_testable.return_value = False
+        self.assertEqual(mod_info.get_testable_modules(), set())
+        mod_info.name_to_module_info = NAME_TO_MODULE_INFO
+        mock_is_testable.return_value = True
+        mock_is_suite_exist.return_value = True
+        self.assertEqual(1, len(mod_info.get_testable_modules('test_suite')))
+        mock_is_suite_exist.return_value = False
+        self.assertEqual(0, len(mod_info.get_testable_modules('test_suite')))
+        self.assertEqual(1, len(mod_info.get_testable_modules()))
+
+    @mock.patch.object(module_info.ModuleInfo, 'has_test_config')
+    @mock.patch.object(module_info.ModuleInfo, 'is_robolectric_test')
+    def test_is_testable_module(self, mock_is_robo_test, mock_has_test_config):
+        """Test is_testable_module."""
+        mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH)
+        mock_is_robo_test.return_value = False
+        mock_has_test_config.return_value = True
+        installed_module_info = {constants.MODULE_INSTALLED:
+                                 uc.DEFAULT_INSTALL_PATH}
+        non_installed_module_info = {constants.MODULE_NAME: 'rand_name'}
+        # Empty mod_info or a non-installed module.
+        self.assertFalse(mod_info.is_testable_module(non_installed_module_info))
+        self.assertFalse(mod_info.is_testable_module({}))
+        # Testable Module or is a robo module for non-installed module.
+        self.assertTrue(mod_info.is_testable_module(installed_module_info))
+        mock_has_test_config.return_value = False
+        self.assertFalse(mod_info.is_testable_module(installed_module_info))
+        mock_is_robo_test.return_value = True
+        self.assertTrue(mod_info.is_testable_module(non_installed_module_info))
+
+    @mock.patch.object(module_info.ModuleInfo, 'is_auto_gen_test_config')
+    def test_has_test_config(self, mock_is_auto_gen):
+        """Test has_test_config."""
+        mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH)
+        info = {constants.MODULE_PATH:[uc.TEST_DATA_DIR]}
+        mock_is_auto_gen.return_value = True
+        # Validate we see the config when it's auto-generated.
+        self.assertTrue(mod_info.has_test_config(info))
+        self.assertTrue(mod_info.has_test_config({}))
+        # Validate when actual config exists and there's no auto-generated config.
+        mock_is_auto_gen.return_value = False
+        self.assertTrue(mod_info.has_test_config(info))
+        self.assertFalse(mod_info.has_test_config({}))
+        # Validate the case mod_info MODULE_TEST_CONFIG be set
+        info2 = {constants.MODULE_PATH:[uc.TEST_CONFIG_DATA_DIR],
+                 constants.MODULE_TEST_CONFIG:[os.path.join(uc.TEST_CONFIG_DATA_DIR, "a.xml")]}
+        self.assertTrue(mod_info.has_test_config(info2))
+
+    @mock.patch.object(module_info.ModuleInfo, 'get_module_names')
+    def test_get_robolectric_test_name(self, mock_get_module_names):
+        """Test get_robolectric_test_name."""
+        # Happy path testing, make sure we get the run robo target.
+        mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH)
+        mod_info.name_to_module_info = MOD_NAME_INFO_DICT
+        mod_info.path_to_module_info = MOD_PATH_INFO_DICT
+        mock_get_module_names.return_value = [RUN_ROBO_MOD_NAME, NON_RUN_ROBO_MOD_NAME]
+        self.assertEqual(mod_info.get_robolectric_test_name(
+            NON_RUN_ROBO_MOD_NAME), RUN_ROBO_MOD_NAME)
+        # Let's also make sure we don't return anything when we're not supposed
+        # to.
+        mock_get_module_names.return_value = [NON_RUN_ROBO_MOD_NAME]
+        self.assertEqual(mod_info.get_robolectric_test_name(
+            NON_RUN_ROBO_MOD_NAME), None)
+
+    @mock.patch.object(module_info.ModuleInfo, 'get_module_info')
+    @mock.patch.object(module_info.ModuleInfo, 'get_module_names')
+    def test_is_robolectric_test(self, mock_get_module_names, mock_get_module_info):
+        """Test is_robolectric_test."""
+        # Happy path testing, make sure we get the run robo target.
+        mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH)
+        mod_info.name_to_module_info = MOD_NAME_INFO_DICT
+        mod_info.path_to_module_info = MOD_PATH_INFO_DICT
+        mock_get_module_names.return_value = [RUN_ROBO_MOD_NAME, NON_RUN_ROBO_MOD_NAME]
+        mock_get_module_info.return_value = RUN_ROBO_MOD
+        # Test on a run robo module.
+        self.assertTrue(mod_info.is_robolectric_test(RUN_ROBO_MOD_NAME))
+        # Test on a non-run robo module but shares with a run robo module.
+        self.assertTrue(mod_info.is_robolectric_test(NON_RUN_ROBO_MOD_NAME))
+        # Make sure we don't find robo tests where they don't exist.
+        mock_get_module_info.return_value = None
+        self.assertFalse(mod_info.is_robolectric_test('rand_mod'))
+
+    @mock.patch.object(module_info.ModuleInfo, 'is_module')
+    def test_is_auto_gen_test_config(self, mock_is_module):
+        """Test is_auto_gen_test_config correctly detects the module."""
+        mod_info = module_info.ModuleInfo(module_file=JSON_FILE_PATH)
+        mock_is_module.return_value = True
+        is_auto_test_config = {'auto_test_config': [True]}
+        is_not_auto_test_config = {'auto_test_config': [False]}
+        is_not_auto_test_config_again = {'auto_test_config': []}
+        MOD_INFO_DICT[MOD_NAME1] = is_auto_test_config
+        MOD_INFO_DICT[MOD_NAME2] = is_not_auto_test_config
+        MOD_INFO_DICT[MOD_NAME3] = is_not_auto_test_config_again
+        MOD_INFO_DICT[MOD_NAME4] = {}
+        mod_info.name_to_module_info = MOD_INFO_DICT
+        self.assertTrue(mod_info.is_auto_gen_test_config(MOD_NAME1))
+        self.assertFalse(mod_info.is_auto_gen_test_config(MOD_NAME2))
+        self.assertFalse(mod_info.is_auto_gen_test_config(MOD_NAME3))
+        self.assertFalse(mod_info.is_auto_gen_test_config(MOD_NAME4))
 
 if __name__ == '__main__':
     unittest.main()