$search
00001 #!/usr/bin/env python 00002 # Software License Agreement (BSD License) 00003 # 00004 # Copyright (c) 2011, Willow Garage, Inc. 00005 # All rights reserved. 00006 # 00007 # Redistribution and use in source and binary forms, with or without 00008 # modification, are permitted provided that the following conditions 00009 # are met: 00010 # 00011 # * Redistributions of source code must retain the above copyright 00012 # notice, this list of conditions and the following disclaimer. 00013 # * Redistributions in binary form must reproduce the above 00014 # copyright notice, this list of conditions and the following 00015 # disclaimer in the documentation and/or other materials provided 00016 # with the distribution. 00017 # * Neither the name of Willow Garage, Inc. nor the names of its 00018 # contributors may be used to endorse or promote products derived 00019 # from this software without specific prior written permission. 00020 # 00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 # POSSIBILITY OF SUCH DAMAGE. 00033 PKG = 'app_manager' 00034 import roslib; roslib.load_manifest(PKG) 00035 00036 import os 00037 import sys 00038 import unittest 00039 00040 import rosunit 00041 00042 def touch(filename): 00043 os.utime(filename, None) 00044 00045 class AppListTest(unittest.TestCase): 00046 00047 def test_AppList(self): 00048 import app_manager 00049 path = roslib.packages.get_pkg_dir(PKG) 00050 test_dir = os.path.join(path, 'test') 00051 00052 app_list = app_manager.AppList([os.path.join(test_dir, 'applist0')]) 00053 self.assertEquals([], app_list.get_app_list()) 00054 00055 filename = os.path.join(test_dir, 'applist1') 00056 app_list = app_manager.AppList([filename]) 00057 al = app_list.get_app_list() 00058 self.assertEquals([], app_list.invalid_installed_files) 00059 #self.assertEquals(1, len(al), al.invalid_installed_files) 00060 self.assertEquals('Android Joystick', al[0].display_name ) 00061 00062 #Had to be commented out, see app_list.py 00063 #mtime = app_list._applist_directory_mtime 00064 #app_list.update() 00065 #self.assertEquals(mtime, app_list._applist_directory_mtime) 00066 #touch(filename) 00067 #app_list.update() 00068 #self.assertNotEquals(mtime, app_list._applist_directory_mtime) 00069 00070 filename = os.path.join(test_dir, 'applistbad') 00071 app_list = app_manager.AppList([filename]) 00072 al = app_list.get_app_list() 00073 self.assertEquals([], al) 00074 self.assertEquals(2, len(app_list.invalid_installed_files)) 00075 00076 def test_get_default_applist_directory(self): 00077 import app_manager.app_list 00078 self.assertEquals('/etc/robot/apps', app_manager.app_list.get_default_applist_directory()) 00079 00080 def test_InstalledFile(self): 00081 from app_manager import InvalidAppException 00082 from app_manager.app import find_resource 00083 from app_manager.app_list import InstalledFile 00084 00085 filename = find_resource('app_manager/apps1.installed') 00086 inf = InstalledFile(filename) 00087 self.failIf(inf._file_mtime is None) 00088 self.assertEquals(filename, inf.filename) 00089 self.assertEquals(1, len(inf.available_apps)) 00090 self.assertEquals('Android Joystick', inf.available_apps[0].display_name) 00091 00092 #Had to be commented out, see app_list.py 00093 #mtime = inf._file_mtime 00094 #inf.update() 00095 #self.assertEquals(mtime, inf._file_mtime) 00096 #touch(filename) 00097 #inf.update() 00098 #self.assertNotEquals(mtime, inf._file_mtime) 00099 00100 for bad in ['app_manager/bad.installed', 'app_manager/bad2.installed']: 00101 filename = find_resource(bad) 00102 try: 00103 inf = InstalledFile(filename) 00104 self.fail("should have thrown") 00105 except InvalidAppException: pass 00106 00107 def test_dict_to_KeyValue(self): 00108 from app_manager.msg import KeyValue 00109 from app_manager.app_list import dict_to_KeyValue 00110 00111 v = dict_to_KeyValue({}) 00112 self.assertEquals([], v) 00113 00114 v = dict_to_KeyValue({'a': 'b'}) 00115 self.assertEquals([KeyValue('a', 'b')], v) 00116 00117 v = dict_to_KeyValue({'a': 'b', 'c': 'd'}) 00118 for ve in [KeyValue('a', 'b'), KeyValue('c', 'd')]: 00119 self.assert_(ve in v) 00120 00121 # make sure that types convert 00122 v = dict_to_KeyValue({'a': 1}) 00123 self.assertEquals([KeyValue('a', '1')], v) 00124 00125 def test_AppDefinition_to_App(self): 00126 from app_manager.msg import App, ClientApp, KeyValue 00127 from app_manager.app import AppDefinition, Client 00128 from app_manager.app_list import AppDefinition_to_App, dict_to_KeyValue 00129 00130 ad = AppDefinition(name="appname", display_name="An App", 00131 description="Does something", platform="fakebot", 00132 launch="file.launch", interface="file.interface", clients=[]) 00133 a = AppDefinition_to_App(ad) 00134 self.assertEquals(a.name, 'appname') 00135 self.assertEquals(a.display_name, 'An App') 00136 self.assertEquals([], a.client_apps) 00137 00138 client1 = Client('android', 00139 {'manager1': 'data1'}, 00140 {'app1': 'data1'}) 00141 ca = ClientApp('android', [KeyValue('manager1', 'data1')], [KeyValue('app1', 'data1')]) 00142 ad = AppDefinition(name="appname", display_name="An App", 00143 description="Does something", platform="fakebot", 00144 launch="file.launch", interface="file.interface", clients=[client1]) 00145 a = AppDefinition_to_App(ad) 00146 self.assertEquals([ca], a.client_apps) 00147 00148 client1 = Client('android', 00149 {'manager1': 'data1', 'manager2': 'data2'}, 00150 {'app1': 'data1', 'app2': 'data2'}) 00151 ca = ClientApp('android', dict_to_KeyValue(client1.manager_data), dict_to_KeyValue(client1.app_data)) 00152 ad = AppDefinition(name="appname", display_name="An App", 00153 description="Does something", platform="fakebot", 00154 launch="file.launch", interface="file.interface", clients=[client1]) 00155 a = AppDefinition_to_App(ad) 00156 self.assertEquals([ca], a.client_apps) 00157 00158 client2 = Client('web', {}, 00159 {'app2': 'data2', 'app2b': 'data2b'}) 00160 ca2 = ClientApp('web', [], dict_to_KeyValue(client2.app_data)) 00161 ad = AppDefinition(name="appname", display_name="An App", 00162 description="Does something", platform="fakebot", 00163 launch="file.launch", interface="file.interface", clients=[client1, client2]) 00164 a = AppDefinition_to_App(ad) 00165 self.assertEquals([ca, ca2], a.client_apps) 00166 00167 00168 if __name__ == '__main__': 00169 rosunit.unitrun(PKG, 'test_app_list', AppListTest, coverage_packages=['app_manager.app_list']) 00170