import sys
import unittest
from subprocess import Popen
from time import sleep

# Check imports
import hpp_idl.hpp.constraints_idl
import hpp_idl.hpp.core_idl
import hpp_idl.hpp.corbaserver

from hpp.corbaserver import Client
from hpp.corbaserver.robot import Robot


class Test(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.omni_names = Popen(['omniNames', '-start'], stdout=sys.stdout)
        sleep(1)
        # In case omniORB.cfg is not present (which is the case by default with robotpkg)
        # we have to explain to the client how to contact the server
        # ref https://github.com/humanoid-path-planner/hpp-corbaserver/issues/90
        cls.server = Popen(["../src/hppcorbaserver"], stdout=sys.stdout, env={'ORBInitRef': 'NameService=corbaname::localhost'})
        sleep(1)
        cls.server.poll()
        if cls.server.returncode is not None:
            print("Process died")

    def setUp(self):
        self.client = Client()

    @classmethod
    def tearDownClass(cls):
        cls.omni_names.kill()
        cls.server.kill()
        # print("stop hppcorbaserver %s" % cls.server.pid)
        cls.omni_names.wait()
        cls.server.wait()

    def test_createRobotFromPython(self):
        self.client.robot.createRobot("test")
        self.assertEqual(self.client.robot.getRobotName(), "test")

        types = ["FreeFlyer", "Planar", "RX", "RY", "RZ", "RUBX", "RUBY", "RUBZ", "PX", "PY", "PZ", "Translation"]

        parent = ""
        for i, type in enumerate(types):
            jn = "joint_{}_{}".format(type, i)
            bn = "body_{}_{}".format(type, i)
            self.client.robot.appendJoint(parent, jn, type, [0, 0, 0, 0, 0, 0, 1])
            self.client.robot.createSphere(bn, 0.001)
            self.client.robot.addObjectToJoint(jn, bn, [0, 0, 1, 0, 0, 0, 1])
            parent = jn

        robot = Robot(client=self.client)

        n = len("JointModel")
        _types = [self.client.robot.getJointType(jn)[n:] for jn in robot.jointNames]
        self.assertListEqual(types, _types)


if __name__ == '__main__':
    unittest.main()

@@ -1,15 +1,4 @@
-import sys%0A
 impo
@@ -12,60 +12,8 @@
 test
-%0Afrom subprocess import Popen%0Afrom time import sleep
 %0A%0A# 
@@ -65,36 +65,8 @@
 idl%0A
-import hpp_idl.hpp.core_idl%0A
 impo
@@ -92,16 +92,43 @@
 aserver%0A
+import hpp_idl.hpp.core_idl
 %0Afrom hp
@@ -195,16 +195,52 @@
 t Robot%0A
+from hpp.utils import ServerManager%0A
 %0A%0Aclass 
@@ -272,652 +272,103 @@
     
-@classmethod%0A    def setUpClass(cls):%0A        cls.omni_names = Popen(%5B'omniNames', '-start'%5D, stdout=sys.stdout)%0A        sleep(1)%0A        # In case omniORB.cfg is not present (which is the case by default with robotpkg)%0A        # we have to explain to the client how to contact the server%0A        # ref https://github.com/humanoid-path-planner/hpp-corbaserver/issues/90%0A        cls.server = Popen(%5B%22../src/hppcorbaserver%22%5D, stdout=sys.stdout, env=%7B'ORBInitRef': 'NameService=corbaname::localhost'%7D)%0A        sleep(1)%0A        cls.server.poll()%0A        if cls.server.returncode is not None:%0A            print(%22Process died%22)%0A%0A    def setUp(self):%0A
+def test_createRobotFromPython(self):%0A        with ServerManager('../src/hppcorbaserver'):%0A    
     
@@ -398,268 +398,12 @@
 t()%0A
-%0A
     
-@classmethod%0A    def tearDownClass(cls):%0A        cls.omni_names.kill()%0A        cls.server.kill()%0A        # print(%22stop hppcorbaserver %25s%22 %25 cls.server.pid)%0A        cls.omni_names.wait()%0A        cls.server.wait()%0A%0A    def test_createRobotFromPython(self):%0A
     
@@ -444,16 +444,20 @@
 %22test%22)%0A
+    
         
@@ -524,16 +524,20 @@
         
+  
+  
 types = 
@@ -636,32 +636,36 @@
 tion%22%5D%0A%0A        
+    
 parent = %22%22%0A    
@@ -668,16 +668,20 @@
         
+ 
+   
 for i, t
@@ -705,16 +705,20 @@
 types):%0A
+    
         
@@ -768,16 +768,20 @@
         
+    
 bn = %22bo
@@ -810,32 +810,36 @@
  i)%0A            
+   
+ 
 self.client.robo
@@ -897,32 +897,36 @@
 1%5D)%0A            
+    
 self.client.robo
@@ -955,32 +955,36 @@
 01)%0A            
+   
+ 
 self.client.robo
@@ -1037,32 +1037,36 @@
 1%5D)%0A            
+    
 parent = jn%0A%0A   
@@ -1070,16 +1070,20 @@
         
+  
+  
 robot = 
@@ -1117,16 +1117,20 @@
         
+    
 n = len(
@@ -1143,16 +1143,20 @@
 Model%22)%0A
+    
         
@@ -1232,16 +1232,20 @@
 tNames%5D%0A
+    
         

use ServerManager