# Path: tests/setup.py
# class TestPygletGUI(unittest.TestCase):
#     """
#     Default configuration of testing unit.
#     """
#     def setUp(self):
#         # define a theme
#         self.theme = pyglet_gui.theme.ThemeFromPath(os.path.join(sys.path[0], '../theme'))
# 
#         self.window = pyglet.window.Window(640, 480, resizable=True, vsync=True)
#         self.batch = pyglet.graphics.Batch()
# 
#         @self.window.event
#         def on_draw():
#             self.window.clear()
#             self.batch.draw()
# 
#     def tearDown(self):
#         self.window.close()
# 
# Path: pyglet_gui/manager.py
# class Manager(ViewerManager, ControllerManager):
#     def __init__(self,
#                  content,
#                  theme,
#                  window=None,
#                  batch=None,
#                  group=None,
#                  is_movable=True,
#                  anchor=ANCHOR_CENTER,
#                  offset=(0, 0)):
#         ControllerManager.__init__(self)
#         ViewerManager.__init__(self, content, theme, window, batch, group, anchor, offset)
# 
#         self.is_movable = is_movable
#         self._is_dragging = False
# 
#     def hit_test(self, x, y):
#         return self.is_inside(x, y)
# 
#     def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
#         if not ControllerManager.on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
#             if self.is_movable and self._is_dragging:
#                 x, y = self._offset
#                 self._offset = (int(x + dx), int(y + dy))
#                 self.set_position(*self.get_position())
#                 return True
# 
#     def on_mouse_motion(self, x, y, dx, dy):
#         ControllerManager.on_mouse_motion(self, x, y, dx, dy)
#         if self.hit_test(x, y):
#             if not self._root_group.is_on_top():
#                 self.pop_to_top()
#             return True
# 
#     def on_mouse_press(self, x, y, button, modifiers):
#         retval = ControllerManager.on_mouse_press(self, x, y, button, modifiers)
#         if self.hit_test(x, y):
#             if not retval:
#                 self._is_dragging = True
#                 retval = True
#         return retval
# 
#     def on_mouse_release(self, x, y, button, modifiers):
#         self._is_dragging = False
#         return ControllerManager.on_mouse_release(self, x, y, button, modifiers)
# 
#     def on_resize(self, width, height):
#         """
#         Update our knowledge of the window's width and height.
#         """
#         if self.screen.width != width or self.screen.height != height:
#             self.screen.width, self.screen.height = width, height
#             self.set_position(*self.get_position())
# 
#     def delete(self):
#         ViewerManager.delete(self)
#         ControllerManager.delete(self)
# 
# Path: pyglet_gui/sliders.py
# class HorizontalSlider(Slider):
#     def __init__(self, value=0.0, min_value=0.0, max_value=1.0, steps=None,
#                  width=100, on_set=None):
#         Slider.__init__(self, value=value,
#                         min_value=min_value,
#                         max_value=max_value,
#                         steps=steps,
#                         on_set=on_set)
# 
#         self.min_width = width
# 
#     def layout(self):
#         left, right, top, bottom = self._padding
#         self._bar.update(self.x + left, self.y + bottom,
#                          self.width - left - right,
#                          self.height - top - bottom)
# 
#         x, y, width, height = self._bar.get_content_region()
# 
#         # knob is positioned with an (x,y) offset
#         # since its graphics are on its bottom-left corner.
#         offset_x, offset_y = self._offset
#         self._knob.update(x + int(width * self._knob_pos()) + offset_x,
#                           y + offset_y,
#                           self._knob.width, self._knob.height)
# 
#         if self.steps is not None:
#             step = float(width) / self.steps
#             offset_x, offset_y = self._step_offset
#             for n in range(0, self.steps + 1):
#                 self._markers[n].update(int(x + step * n) + offset_x,
#                                         y + offset_y,
#                                         self._markers[n].width,
#                                         self._markers[n].height)
# 
#     def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
#         bar_x, bar_y, bar_width, bar_height = self._bar.get_content_region()
#         self.set_knob_pos(float(x - bar_x) / bar_width)
#         return True
# 
#     def compute_size(self):
#         width, height = self._bar.get_needed_size(self.min_width, 0)
#         left, right, top, bottom = self._padding
# 
#         return width + left + right, height + top + bottom


from .setup import TestPygletGUI
from pyglet_gui.manager import Manager
from pyglet_gui.sliders import HorizontalSlider



class TestSlider(TestPygletGUI):

    def setUp(self):
        TestPygletGUI.setUp(self)
        self.slider = HorizontalSlider(min_value=0, max_value=10)
        self.manager = Manager(self.slider, window=self.window, batch=self.batch, theme=self.theme)

    def test_set_value(self):
        self.slider.set_knob_pos(0.5)
        self.assertEqual(self.slider.value, 5)

    def test_mouse_slide(self):

        # push the slider to the minimum
        self.slider.on_mouse_press(0, 0, None, None)
        self.assertEqual(self.slider.value, 0)

        # push the slider to the maximum
        self.slider.on_mouse_press(10000, 0, None, None)

# Path: tests/setup.py
# class TestPygletGUI(unittest.TestCase):
#     """
#     Default configuration of testing unit.
#     """
#     def setUp(self):
#         # define a theme
#         self.theme = pyglet_gui.theme.ThemeFromPath(os.path.join(sys.path[0], '../theme'))
# 
#         self.window = pyglet.window.Window(640, 480, resizable=True, vsync=True)
#         self.batch = pyglet.graphics.Batch()
# 
#         @self.window.event
#         def on_draw():
#             self.window.clear()
#             self.batch.draw()
# 
#     def tearDown(self):
#         self.window.close()
# 
# Path: pyglet_gui/manager.py
# class Manager(ViewerManager, ControllerManager):
#     def __init__(self,
#                  content,
#                  theme,
#                  window=None,
#                  batch=None,
#                  group=None,
#                  is_movable=True,
#                  anchor=ANCHOR_CENTER,
#                  offset=(0, 0)):
#         ControllerManager.__init__(self)
#         ViewerManager.__init__(self, content, theme, window, batch, group, anchor, offset)
# 
#         self.is_movable = is_movable
#         self._is_dragging = False
# 
#     def hit_test(self, x, y):
#         return self.is_inside(x, y)
# 
#     def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
#         if not ControllerManager.on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
#             if self.is_movable and self._is_dragging:
#                 x, y = self._offset
#                 self._offset = (int(x + dx), int(y + dy))
#                 self.set_position(*self.get_position())
#                 return True
# 
#     def on_mouse_motion(self, x, y, dx, dy):
#         ControllerManager.on_mouse_motion(self, x, y, dx, dy)
#         if self.hit_test(x, y):
#             if not self._root_group.is_on_top():
#                 self.pop_to_top()
#             return True
# 
#     def on_mouse_press(self, x, y, button, modifiers):
#         retval = ControllerManager.on_mouse_press(self, x, y, button, modifiers)
#         if self.hit_test(x, y):
#             if not retval:
#                 self._is_dragging = True
#                 retval = True
#         return retval
# 
#     def on_mouse_release(self, x, y, button, modifiers):
#         self._is_dragging = False
#         return ControllerManager.on_mouse_release(self, x, y, button, modifiers)
# 
#     def on_resize(self, width, height):
#         """
#         Update our knowledge of the window's width and height.
#         """
#         if self.screen.width != width or self.screen.height != height:
#             self.screen.width, self.screen.height = width, height
#             self.set_position(*self.get_position())
# 
#     def delete(self):
#         ViewerManager.delete(self)
#         ControllerManager.delete(self)
# 
# Path: pyglet_gui/sliders.py
# class HorizontalSlider(Slider):
#     def __init__(self, value=0.0, min_value=0.0, max_value=1.0, steps=None,
#                  width=100, on_set=None):
#         Slider.__init__(self, value=value,
#                         min_value=min_value,
#                         max_value=max_value,
#                         steps=steps,
#                         on_set=on_set)
# 
#         self.min_width = width
# 
#     def layout(self):
#         left, right, top, bottom = self._padding
#         self._bar.update(self.x + left, self.y + bottom,
#                          self.width - left - right,
#                          self.height - top - bottom)
# 
#         x, y, width, height = self._bar.get_content_region()
# 
#         # knob is positioned with an (x,y) offset
#         # since its graphics are on its bottom-left corner.
#         offset_x, offset_y = self._offset
#         self._knob.update(x + int(width * self._knob_pos()) + offset_x,
#                           y + offset_y,
#                           self._knob.width, self._knob.height)
# 
#         if self.steps is not None:
#             step = float(width) / self.steps
#             offset_x, offset_y = self._step_offset
#             for n in range(0, self.steps + 1):
#                 self._markers[n].update(int(x + step * n) + offset_x,
#                                         y + offset_y,
#                                         self._markers[n].width,
#                                         self._markers[n].height)
# 
#     def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
#         bar_x, bar_y, bar_width, bar_height = self._bar.get_content_region()
#         self.set_knob_pos(float(x - bar_x) / bar_width)
#         return True
# 
#     def compute_size(self):
#         width, height = self._bar.get_needed_size(self.min_width, 0)
#         left, right, top, bottom = self._padding
# 
#         return width + left + right, height + top + bottom


# Path: tests/test_slider.py
from .setup import TestPygletGUI
from pyglet_gui.manager import Manager
from pyglet_gui.sliders import HorizontalSlider




class TestSlider(TestPygletGUI):

    def setUp(self):
        TestPygletGUI.setUp(self)
        self.slider = HorizontalSlider(min_value=0, max_value=10)
        self.manager = Manager(self.slider, window=self.window, batch=self.batch, theme=self.theme)

    def test_set_value(self):
        self.slider.set_knob_pos(0.5)
        self.assertEqual(self.slider.value, 5)

    def test_mouse_slide(self):

        # push the slider to the minimum
        self.slider.on_mouse_press(0, 0, None, None)
        self.assertEqual(self.slider.value, 0)

        # push the slider to the maximum
        self.slider.on_mouse_press(10000, 0, None, None)

        self.assertEqual(self.slider.value, 10)