test_app.py
Go to the documentation of this file.
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 rospkg
00041 import rosunit
00042 
00043 class AppTest(unittest.TestCase):
00044   
00045   
00046     def test_Interface(self):
00047         from app_manager.app import Interface
00048         s1 = {'/chatter': 'std_msgs/String'}
00049         p1 = {'/chatter': 'std_msgs/String'}
00050         i1 = Interface(s1, p1)
00051         self.assertEquals(i1, i1)
00052         self.assertEquals(s1, i1.subscribed_topics)
00053         self.assertEquals(s1, i1.published_topics)
00054         
00055         i2 = Interface({'/chatter': 'std_msgs/String'}, {'/chatter': 'std_msgs/String'})
00056         self.assertEquals(i1, i2)
00057         i3 = Interface({'/chatter2': 'std_msgs/String'}, {'/chatter': 'std_msgs/String'})
00058         self.assertNotEquals(i1, i3)
00059         i4 = Interface({'/chatter': 'std_msgs/String'}, {'/chatter2': 'std_msgs/String'})
00060         self.assertNotEquals(i1, i4)
00061         
00062     def test_Client(self):
00063         from app_manager.app import Client
00064         c1 = Client(client_type="android",
00065                     manager_data={"manager1": "dataA"},
00066                     app_data={"app1": "dataB"})
00067         self.assertEquals("android", c1.client_type)
00068         self.assertEquals({"manager1": "dataA"}, c1.manager_data)
00069         self.assertEquals({"app1": "dataB"}, c1.app_data)
00070         self.assertEquals(c1, c1)
00071         c2 = Client(client_type="android",
00072                     manager_data={"manager1": "dataA"},
00073                     app_data={"app1": "dataB"})
00074         self.assertEquals(c1, c2)
00075         noteq = [
00076             Client("androidB", {"manager1": "dataA"},
00077                    {"app1": "dataB"}),
00078             Client("android", {"manager2": "dataA"},
00079                    {"app1": "dataB"}),
00080             Client("android", {"manager1": "dataA"},
00081                    {"app2": "dataB"}),
00082             ]
00083         for c3 in noteq:
00084             self.assertNotEquals(c1, c3)
00085             
00086     def test_AppDefinition(self):
00087         # name, display_name, description, platform, launch, interface, clients
00088         from app_manager.app import AppDefinition, Client
00089         ad1 = AppDefinition('foo', 'Foo', 'Is Foo', 'android', 'foo.launch', 'foo.interface', [Client("android", {}, {})])
00090         self.assertEquals(ad1, ad1)
00091         ad2 = AppDefinition('foo', 'Foo', 'Is Foo', 'android', 'foo.launch', 'foo.interface', [Client("android", {}, {})])
00092         self.assertEquals(ad1, ad2)
00093         ad3 = AppDefinition('foo', 'Foo', 'Is Foo', 'android', 'foo.launch', 'foo.interface', [Client("android", {}, {})], None)
00094         self.assertEquals(ad1, ad3)
00095 
00096         ad1b = AppDefinition('foo', 'Foo', 'Is Foo', 'android', 'foo.launch', 'foo.interface', [Client("android", {}, {})], 'app_manager/icon.png')
00097         self.assertEquals(ad1, ad1)
00098         ad2b = AppDefinition('foo', 'Foo', 'Is Foo', 'android', 'foo.launch', 'foo.interface', [Client("android", {}, {})], 'app_manager/icon.png')
00099         self.assertEquals(ad1b, ad2b)
00100         nad2b = AppDefinition('foo', 'Foo', 'Is Foo', 'android', 'foo.launch', 'foo.interface', [Client("android", {}, {})], 'app_manager/icon2.png')
00101         self.assertNotEquals(ad1, nad2b)
00102         
00103         noteq = [
00104             AppDefinition('bar', 'Foo', 'Is Foo', 'android', 'foo.launch', 'foo.interface', [Client("android", {}, {})]),
00105             AppDefinition('foo', 'Bar', 'Is Foo', 'android', 'foo.launch', 'foo.interface', [Client("android", {}, {})]),
00106             AppDefinition('foo', 'Foo', 'Is Bar', 'android', 'foo.launch', 'foo.interface', [Client("android", {}, {})]),
00107             AppDefinition('foo', 'Foo', 'Is Foo', 'ios', 'foo.launch', 'foo.interface', [Client("android", {}, {})]),
00108             AppDefinition('foo', 'Foo', 'Is Foo', 'android', 'bar.launch', 'foo.interface', [Client("android", {}, {})]),
00109             AppDefinition('foo', 'Foo', 'Is Foo', 'android', 'foo.launch', 'bar.interface', [Client("android", {}, {})]),
00110             AppDefinition('foo', 'Foo', 'Is Foo', 'android', 'foo.launch', 'foo.interface', [Client("ios", {}, {})]),
00111             ]
00112         for nad in noteq:
00113             self.assertNotEquals(ad1, nad)
00114         
00115     def test_find_resource(self):
00116         from app_manager.app import find_resource
00117         rospack = rospkg.RosPack()
00118         path = rospack.get_path(PKG)
00119         test_dir = os.path.join(path, 'test')
00120 
00121         e = os.path.join(test_dir, 'empty.interface')
00122         self.assertEquals(e, find_resource('%s/empty.interface'%(PKG)))
00123 
00124         e = os.path.join(test_dir, 'applist1', 'apps1.installed')
00125         self.assertEquals(e, find_resource('%s/apps1.installed'%(PKG)))
00126 
00127         try:
00128             find_resource('empty.interface')
00129             self.fail("should have thrown ValueError: no package name")
00130         except ValueError:
00131             pass
00132         try:
00133             find_resource('app_manager')
00134             self.fail("should have thrown ValueError: no resource name")
00135         except ValueError:
00136             pass
00137         
00138     def test_load_AppDefinition_by_name(self):
00139         rospack = rospkg.RosPack()
00140         rl_dir = rospack.get_path('roslaunch')
00141         from app_manager import NotFoundException, InternalAppException
00142         from app_manager.app import load_AppDefinition_by_name, Interface
00143         try:
00144             load_AppDefinition_by_name(None)
00145             self.fail("should fail")
00146         except ValueError: pass
00147         try:
00148             load_AppDefinition_by_name("fake_pkg/appA")
00149             self.fail("should fail")
00150         except NotFoundException: pass
00151         
00152         ad = load_AppDefinition_by_name('app_manager/appA')
00153         self.assertEquals("app_manager/appA", ad.name)
00154         self.assertEquals("Android Joystick", ad.display_name)
00155         self.assertEquals("Control the TurtleBot with an Android device", ad.description)
00156         self.assertEquals("turtlebot", ad.platform)
00157         self.assertEquals(os.path.join(rl_dir, 'example-min.launch'), ad.launch)
00158         self.assert_(isinstance(ad.interface, Interface))
00159         self.assertEquals({}, ad.interface.subscribed_topics)
00160         self.assertEquals({}, ad.interface.published_topics)
00161         self.assertEquals([], ad.clients)
00162 
00163         #monkey patch in for coverage
00164         import errno
00165         def fake_load_enoent(*args):
00166             raise IOError(errno.ENOENT, "fnf")
00167         def fake_load_gen(*args):
00168             raise IOError()
00169         import app_manager.app
00170         load_actual = app_manager.app.load_AppDefinition_from_file
00171         try:
00172             app_manager.app.load_AppDefinition_from_file = fake_load_enoent
00173             try:
00174                 load_AppDefinition_by_name('app_manager/appA')
00175                 self.fail("should have raised")
00176             except NotFoundException: pass
00177 
00178             app_manager.app.load_AppDefinition_from_file = fake_load_gen
00179             try:
00180                 load_AppDefinition_by_name('app_manager/appA')
00181                 self.fail("should have raised")
00182             except InternalAppException: pass
00183         finally:
00184             app_manager.app.load_AppDefinition_from_file = load_actual
00185         
00186     def test_load_AppDefinition_from_file(self):
00187         rospack = rospkg.RosPack()
00188         rl_dir = rospack.get_path('roslaunch')
00189         path = rospack.get_path(PKG)
00190         test_dir = os.path.join(path, 'test')
00191 
00192         from app_manager.app import load_AppDefinition_from_file, Interface
00193         ad = load_AppDefinition_from_file(os.path.join(test_dir, 'appA.app'), 'foo/AppA')
00194         self.assertEquals("foo/AppA", ad.name)
00195         self.assertEquals("Android Joystick", ad.display_name)
00196         self.assertEquals("Control the TurtleBot with an Android device", ad.description)
00197         self.assertEquals("turtlebot", ad.platform)
00198         self.assertEquals(os.path.join(rl_dir, 'example-min.launch'), ad.launch)
00199         self.assert_(isinstance(ad.interface, Interface))
00200         self.assertEquals({}, ad.interface.subscribed_topics)
00201         self.assertEquals({}, ad.interface.published_topics)
00202         self.assertEquals([], ad.clients)
00203         #self.assertEquals('app_manager/empty.interface', ad.interface)
00204         
00205     def test_load_Interface_from_file(self):
00206         from app_manager.app import load_Interface_from_file
00207         rospack = rospkg.RosPack()
00208         path = rospack.get_path(PKG)
00209         test_dir = os.path.join(path, 'test')
00210 
00211         empty = load_Interface_from_file(os.path.join(test_dir, 'empty.interface'))
00212         self.assertEquals({}, empty.subscribed_topics)
00213         self.assertEquals({}, empty.published_topics)
00214 
00215         test1 = load_Interface_from_file(os.path.join(test_dir, 'test1.interface'))
00216         self.assertEquals({'/camera/rgb/image_color/compressed': 'sensor_msgs/CompressedImage'}, test1.published_topics)
00217         self.assertEquals({'/turtlebot_node/cmd_vel': 'geometry_msgs/Twist'}, test1.subscribed_topics)
00218       
00219       
00220 if __name__ == '__main__':
00221   rosunit.unitrun(PKG, 'test_app', AppTest, coverage_packages=['app_manager.app'])
00222 


app_manager
Author(s): Jeremy Leibs, Ken Conley
autogenerated on Thu Sep 3 2015 10:15:41