# pylint: disable=C0103,C0111

import json
import unittest
import mock

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

import tests.mocks as mocks

from bumblebee.config import Config
from bumblebee.input import LEFT_MOUSE
from bumblebee.modules.caffeine import Module

class TestCaffeineModule(unittest.TestCase):
    def setUp(self):
        mocks.setup_test(self, Module)

        self.xset_active = "  timeout:  0    cycle:  123"
        self.xset_inactive = "  timeout:  600    cycle:  123"

    def tearDown(self):
        mocks.teardown_test(self)

    def test_text(self):
        self.assertEquals(self.module.caffeine(self.anyWidget), "")

    def test_active(self):
        self.popen.mock.communicate.return_value = (self.xset_active, None)
        self.assertTrue(not "deactivated" in self.module.state(self.anyWidget))
        self.assertTrue("activated" in self.module.state(self.anyWidget))

    def test_inactive(self):
        self.popen.mock.communicate.return_value = (self.xset_inactive, None)
        self.assertTrue("deactivated" in self.module.state(self.anyWidget))
        self.popen.mock.communicate.return_value = ("no text", None)
        self.assertTrue("deactivated" in self.module.state(self.anyWidget))

    def test_toggle(self):
        self.popen.mock.communicate.return_value = (self.xset_active, None)
        mocks.mouseEvent(stdin=self.stdin, button=LEFT_MOUSE, inp=self.input, module=self.module)
        self.popen.assert_call("xset s default")
        self.popen.assert_call("notify-send \"Out of coffee\"")
        
        self.popen.mock.communicate.return_value = (self.xset_inactive, None)
        mocks.mouseEvent(stdin=self.stdin, button=LEFT_MOUSE, inp=self.input, module=self.module)
        self.popen.assert_call("xset s off")
        self.popen.assert_call("notify-send \"Consuming caffeine\"")

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

@@ -35,194 +35,68 @@
 ort 
-json%0Aimport unittest%0Aimport mock%0A%0Atry:%0A    from StringIO import StringIO%0Aexcept ImportError:%0A    from io import StringIO%0A%0Aimport tests.mocks as mocks%0A%0Afrom bumblebee.config import Config
+unittest%0Afrom mock import patch%0Aimport tests.mocks as mocks%0A
 %0Afro
@@ -292,297 +292,311 @@
     
-    self.xset_active = %22  timeout:  0    cycle:  123%22%0A        self.xset_inactive = %22  timeout:  600    cycle:  123%22%0A%0A    def tearDown(self):%0A        mocks.teardown_test(self)%0A%0A    def test_text(self):%0A        self.assertEquals(self.module.caffeine(self.anyWidget), %22%22)%0A%0A    def test_active
+def tearDown(self):%0A        mocks.teardown_test(self)%0A%0A    def test_check_requirements(self):%0A        with patch('bumblebee.util.which', side_effect=%5B'', 'xprop', 'xdg-screensaver'%5D):%0A            self.assertTrue(%5B'xdotool'%5D == self.module._check_requirements())%0A%0A    def test_get_i3bar_xid_returns_digit
 (sel
@@ -651,32 +651,25 @@
 alue = (
-self.xset_active
+%228388614%22
 , None)%0A
@@ -696,160 +696,294 @@
 rue(
-not %22deactivated%22 in self.module.state(self.anyWidget))%0A        self.assertTrue(%22activated%22 in self.module.state(self.anyWidget))%0A%0A    def test_inactive
+self.module._get_i3bar_xid().isdigit())%0A%0A    def test_get_i3bar_xid_returns_error_string(self):%0A        self.popen.mock.communicate.return_value = (%22Some error message%22, None)%0A        self.assertTrue(self.module._get_i3bar_xid() is None)%0A%0A    def test_get_i3bar_xid_returns_empty_string
 (sel
@@ -1038,34 +1038,18 @@
 alue = (
-self.xset_inactive
+%22%22
 , None)%0A
@@ -1076,607 +1076,850 @@
 rue(
-%22deactivated%22 in self.module.state(self.anyWidget))%0A        self.popen.mock.communicate.return_value = (%22no text%22, None)%0A        self.assertTrue(%22deactivated%22 in self.module.state(self.anyWidget))%0A%0A    def test_toggle(self):%0A        self.popen.mock.communicate.return_value = (self.xset_active, None)%0A        mocks.mouseEvent(stdin=self.stdin, button=LEFT_MOUSE, inp=self.input, module=self.module)%0A        self.popen.assert_call(%22xset s default%22)%0A        self.popen.assert_call(%22notify-send %5C%22Out of coffee%5C%22%22)%0A        %0A        self.popen.mock.communicate.return_value = (self.xset_inactive, None)%0A
+self.module._get_i3bar_xid() is None)%0A%0A    def test_suspend_screensaver_success(self):%0A        with patch.object(self.module, '_get_i3bar_xid', return_value=8388614):%0A            mocks.mouseEvent(stdin=self.stdin, button=LEFT_MOUSE, inp=self.input, module=self.module)%0A            self.assertTrue(self.module._suspend_screensaver() is True)%0A%0A    def test_suspend_screensaver_fail(self):%0A        with patch.object(self.module, '_get_i3bar_xid', return_value=None):%0A            self.module._active = False%0A            mocks.mouseEvent(stdin=self.stdin, button=LEFT_MOUSE, inp=self.input, module=self.module)%0A            self.assertTrue(self.module._suspend_screensaver() is False)%0A%0A    def test_resume_screensaver(self):%0A        with patch.object(self.module, '_check_requirements', return_value=%5B%5D):%0A            self.module._active = True%0A    
     
@@ -2024,112 +2024,69 @@
     
+    
 self.
-popen.assert_call(%22xset s off%22)%0A        self.popen.assert_call(%22notify-send %5C%22Consuming caffeine%5C%22%22
+assertTrue(self.module._resume_screensaver() is True
 )%0A%0A#

Add test cases