def setUp(self): self.widgetName = "foo" self.widget = MockWidget(self.widgetName) self.config = Config() self.anyWidgetName = "random-widget-name" self.noSuchModule = "this-module-does-not-exist" self.moduleWithoutWidgets = Module(engine=None, widgets=None) self.moduleWithOneWidget = Module(engine=None, widgets=self.widget, config={"config": self.config}) self.moduleWithMultipleWidgets = Module( engine=None, widgets=[self.widget, self.widget, self.widget]) self.anyConfigName = "cfg" self.anotherConfigName = "cfg2" self.anyModule = Module(engine=None, widgets=self.widget, config={ "name": self.anyConfigName, "config": self.config }) self.anotherModule = Module(engine=None, widgets=self.widget, config={ "name": self.anotherConfigName, "config": self.config }) self.anyKey = "some-parameter" self.anyValue = "value" self.anotherValue = "another-value" self.emptyKey = "i-do-not-exist" self.config.set("{}.{}".format(self.anyConfigName, self.anyKey), self.anyValue) self.config.set("{}.{}".format(self.anotherConfigName, self.anyKey), self.anotherValue)
def setUp(self): self._stdout = mock.patch("sys.stdout", new_callable=StringIO) self._exists = mock.patch("bumblebee.modules.battery.os.path.exists") self._open = mock.patch("bumblebee.modules.battery.open", create=True) self.stdout = self._stdout.start() self.exists = self._exists.start() self.open = self._open.start() self.file = mock.Mock() self.file.__enter__ = lambda x: self.file self.file.__exit__ = lambda x, a, b, c: "" self.file.read.return_value = "120" self.open.return_value = self.file self.exists.return_value = True self.engine = mock.Mock() self.config = Config() self.config.set("battery.showremaining", "false") self.module = Module(engine=self.engine, config={"config":self.config}) self.config.set("battery.critical", "20") self.config.set("battery.warning", "25") self.criticalValue = "19" self.warningValue = "21" self.normalValue = "26" self.chargedValue = "96" self.module.widgets()[0]
def test_unknow(self): config = Config() config.set("http_status.target", "invalid target") self.module = Module(engine=mock.Mock(), config={"config":config}) self.assertTrue("warning" in self.module.state(self.module.widgets()[0])) self.assertEqual(self.module.getStatus(), "UNK") self.assertEqual(self.module.getOutput(), "UNK != 200")
class TestConfig(unittest.TestCase): def setUp(self): self._stdout = mock.patch("bumblebee.config.sys.stdout", new_callable=StringIO) self._stderr = mock.patch("bumblebee.config.sys.stderr", new_callable=StringIO) self.stdout = self._stdout.start() self.stderr = self._stderr.start() self.defaultConfig = Config() self.someSimpleModules = ["foo", "bar", "baz"] self.someAliasModules = ["foo:a", "bar:b", "baz:c"] self.someTheme = "some-theme" def tearDown(self): self._stdout.stop() self._stderr.stop() def test_no_modules_by_default(self): self.assertEquals(self.defaultConfig.modules(), []) def test_simple_modules(self): cfg = Config(["-m"] + self.someSimpleModules) self.assertEquals(cfg.modules(), [{ "name": x, "module": x } for x in self.someSimpleModules]) def test_alias_modules(self): cfg = Config(["-m"] + self.someAliasModules) self.assertEquals(cfg.modules(), [{ "module": x.split(":")[0], "name": x.split(":")[1], } for x in self.someAliasModules]) def test_parameters(self): cfg = Config(["-m", "module", "-p", "module.key=value"]) self.assertEquals(cfg.get("module.key"), "value") def test_theme(self): cfg = Config(["-t", self.someTheme]) self.assertEquals(cfg.theme(), self.someTheme) def test_notheme(self): self.assertEquals(self.defaultConfig.theme(), "default") def test_list_themes(self): with self.assertRaises(SystemExit): cfg = Config(["-l", "themes"]) result = self.stdout.getvalue() for theme in themes(): self.assertTrue(theme in result) def test_invalid_list(self): with self.assertRaises(SystemExit): cfg = Config(["-l", "invalid"]) self.assertTrue("invalid choice" in "".join(self.stderr.getvalue()))
def test_status_success(self): config = Config() config.set("http_status.target", "http://example.org") self.module = Module(engine=mock.Mock(), config={"config":config}) self.assertTrue(not "warning" in self.module.state(self.module.widgets()[0])) self.assertTrue(not "critical" in self.module.state(self.module.widgets()[0])) self.assertEqual(self.module.getStatus(), "200") self.assertEqual(self.module.getOutput(), "200")
def _initialize_pca(self): log.debug("_initialize_pca()") self._initialize_loaders() self._config = Config() self._config.load_or_create() # Schedule further initialization. wx.CallLater(1, self._startup_system_participants)
class TestModule(unittest.TestCase): def setUp(self): self.widget = MockWidget("foo") self.config = Config() self.moduleWithoutWidgets = Module(engine=None, widgets=None) self.moduleWithOneWidget = Module(engine=None, widgets=self.widget) self.moduleWithMultipleWidgets = Module( engine=None, widgets=[self.widget, self.widget, self.widget]) self.anyConfigName = "cfg" self.anotherConfigName = "cfg2" self.anyModule = Module(engine=None, widgets=self.widget, config={ "name": self.anyConfigName, "config": self.config }) self.anotherModule = Module(engine=None, widgets=self.widget, config={ "name": self.anotherConfigName, "config": self.config }) self.anyKey = "some-parameter" self.anyValue = "value" self.anotherValue = "another-value" self.emptyKey = "i-do-not-exist" self.config.set("{}.{}".format(self.anyConfigName, self.anyKey), self.anyValue) self.config.set("{}.{}".format(self.anotherConfigName, self.anyKey), self.anotherValue) def test_empty_widgets(self): self.assertEquals(self.moduleWithoutWidgets.widgets(), []) def test_single_widget(self): self.assertEquals(self.moduleWithOneWidget.widgets(), [self.widget]) def test_multiple_widgets(self): for widget in self.moduleWithMultipleWidgets.widgets(): self.assertEquals(widget, self.widget) def test_parameters(self): self.assertEquals(self.anyModule.parameter(self.anyKey), self.anyValue) self.assertEquals(self.anotherModule.parameter(self.anyKey), self.anotherValue) def test_default_parameters(self): self.assertEquals(self.anyModule.parameter(self.emptyKey), None) self.assertEquals( self.anyModule.parameter(self.emptyKey, self.anyValue), self.anyValue)
def setUp(self): self._stdout = mock.patch("bumblebee.config.sys.stdout", new_callable=StringIO) self._stderr = mock.patch("bumblebee.config.sys.stderr", new_callable=StringIO) self.stdout = self._stdout.start() self.stderr = self._stderr.start() self.defaultConfig = Config() self.someSimpleModules = ["foo", "bar", "baz"] self.someAliasModules = ["foo:a", "bar:b", "baz:c"] self.someTheme = "some-theme"
def test_custom_cmd(self): testmodules = [ { "name": "test", "button": "test.left-click", "action": "echo" }, { "name": "test:alias", "button": "alias.right-click", "action": "echo2" }, ] cmd = "-m" for test in testmodules: cmd += " " + test["name"] cmd += " -p" for test in testmodules: cmd += " " + test["button"] + "=" + test["action"] cfg = Config(shlex.split(cmd)) inp = MockInput() engine = Engine(config=cfg, output=MockOutput(), inp=inp) i = 0 for module in engine.modules(): callback = inp.get_callback(module.id) self.assertTrue(callback is not None) self.assertEquals(callback["command"], testmodules[i]["action"]) if "left" in testmodules[i]["button"]: self.assertTrue(callback["button"], bumblebee.input.LEFT_MOUSE) if "right" in testmodules[i]["button"]: self.assertTrue(callback["button"], bumblebee.input.RIGHT_MOUSE) i += 1
def setUp(self): config = Config() self.module = bumblebee.modules.hddtemp.Module( engine=mock.Mock(), config={"config": config}) self.data_line = "|/dev/sda|TOSHIBA DT01ACA100 �|35|C||/dev/sdb|TOSHIBA DT01ACA100 �|37|C|" self.expected_parts = [ "/dev/sda", "TOSHIBA DT01ACA100 �", "35", "C", "", "/dev/sdb", "TOSHIBA DT01ACA100 �", "37", "C", ""] self.expected_per_disk = [ ["/dev/sda", "TOSHIBA DT01ACA100 �", "35", "C", ""], ["/dev/sdb", "TOSHIBA DT01ACA100 �", "37", "C", ""]] self.device_record = self.expected_per_disk[0] self.expected_name_and_temp = ("sda", "35") self.expected_hddtemp = "sda+35°C"
def setUp(self): self.widgetName = "foo" self.widget = MockWidget(self.widgetName) self.config = Config() self.anyWidgetName = "random-widget-name" self.noSuchModule = "this-module-does-not-exist" self.moduleWithoutWidgets = Module(engine=None, widgets=None) self.moduleWithOneWidget = Module(engine=None, widgets=self.widget, config={"config": self.config}) self.moduleWithMultipleWidgets = Module(engine=None, widgets=[self.widget, self.widget, self.widget] ) self.anyConfigName = "cfg" self.anotherConfigName = "cfg2" self.anyModule = Module(engine=None, widgets=self.widget, config={ "name": self.anyConfigName, "config": self.config }) self.anotherModule = Module(engine=None, widgets=self.widget, config={ "name": self.anotherConfigName, "config": self.config }) self.anyKey = "some-parameter" self.anyValue = "value" self.anotherValue = "another-value" self.emptyKey = "i-do-not-exist" self.config.set("{}.{}".format(self.anyConfigName, self.anyKey), self.anyValue) self.config.set("{}.{}".format(self.anotherConfigName, self.anyKey), self.anotherValue)
def _initialize_pca(self): log.debug("_initialize_pca()") self._initialize_loaders() self._config = Config() self._config.load_or_create() # Schedule further initialization. wx.CallLater(1, self._startup_system_participants)
class TestModule(unittest.TestCase): def setUp(self): self.widget = MockWidget("foo") self.config = Config() self.moduleWithoutWidgets = Module(engine=None, widgets=None) self.moduleWithOneWidget = Module(engine=None, widgets=self.widget) self.moduleWithMultipleWidgets = Module(engine=None, widgets=[self.widget, self.widget, self.widget] ) self.anyConfigName = "cfg" self.anotherConfigName = "cfg2" self.anyModule = Module(engine=None, widgets=self.widget, config={ "name": self.anyConfigName, "config": self.config }) self.anotherModule = Module(engine=None, widgets=self.widget, config={ "name": self.anotherConfigName, "config": self.config }) self.anyKey = "some-parameter" self.anyValue = "value" self.anotherValue = "another-value" self.emptyKey = "i-do-not-exist" self.config.set("{}.{}".format(self.anyConfigName, self.anyKey), self.anyValue) self.config.set("{}.{}".format(self.anotherConfigName, self.anyKey), self.anotherValue) def test_empty_widgets(self): self.assertEquals(self.moduleWithoutWidgets.widgets(), []) def test_single_widget(self): self.assertEquals(self.moduleWithOneWidget.widgets(), [self.widget]) def test_multiple_widgets(self): for widget in self.moduleWithMultipleWidgets.widgets(): self.assertEquals(widget, self.widget) def test_parameters(self): self.assertEquals(self.anyModule.parameter(self.anyKey), self.anyValue) self.assertEquals(self.anotherModule.parameter(self.anyKey), self.anotherValue) def test_default_parameters(self): self.assertEquals(self.anyModule.parameter(self.emptyKey), None) self.assertEquals(self.anyModule.parameter(self.emptyKey, self.anyValue), self.anyValue)
def setUp(self): self._stdout = mock.patch("bumblebee.config.sys.stdout", new_callable=StringIO) self._stderr = mock.patch("bumblebee.config.sys.stderr", new_callable=StringIO) self.stdout = self._stdout.start() self.stderr = self._stderr.start() self.defaultConfig = Config() self.someSimpleModules = ["foo", "bar", "baz"] self.someAliasModules = ["foo:a", "bar:b", "baz:c"] self.someTheme = "some-theme"
def setUp(self): self.engine = Engine(config=Config(), output=MockOutput(), inp=MockInput()) self.testModule = "test" self.testAlias = "test-alias" self.singleWidgetModule = [{"module": self.testModule, "name": "a"}] self.singleWidgetAlias = [{"module": self.testAlias, "name": "a" }] self.invalidModule = "no-such-module" self.testModuleSpec = "bumblebee.modules.{}".format(self.testModule) self.testModules = [ {"module": "test", "name": "a"}, {"module": "test", "name": "b"}, ]
def test_label(self): config = Config() config.set("http_status.label", "example") config.set("http_status.target", "http://example.org") self.module = Module(engine=mock.Mock(), config={"config":config}) self.assertEqual(self.module.getOutput(), "example: 200")
def setUp(self, mock_output): mock_output.return_value = MockCommunicate() engine = MockEngine() config = Config() self.objects = {} for mod in all_modules(): cls = importlib.import_module("bumblebee.modules.{}".format( mod["name"])) self.objects[mod["name"]] = getattr(cls, "Module")(engine, { "config": config }) for widget in self.objects[mod["name"]].widgets(): self.assertEquals(widget.get("variable", None), None)
def setup_test(test, Module): test._stdin, test._select, test.stdin, test.select = epoll_mock("bumblebee.input") test.popen = MockPopen() test.config = Config() test.input = I3BarInput() test.engine = mock.Mock() test.engine.input = test.input test.input.need_event = True test.module = Module(engine=test.engine, config={ "config": test.config }) for widget in test.module.widgets(): widget.link_module(test.module) test.anyWidget = widget
def setUp(self): engine = mock.Mock() engine.input = mock.Mock() config = Config() self.objects = {} self.popen = mocks.MockPopen() self.popen.mock.communicate.return_value = (str.encode("1"), "error") self.popen.mock.returncode = 0 self._platform = mock.patch("bumblebee.modules.kernel.platform") self.platform = self._platform.start() self.platform.release.return_value = "unknown linux v1" for mod in all_modules(): name = "bumblebee.modules.{}".format(mod["name"]) cls = importlib.import_module(name) self.objects[mod["name"]] = getattr(cls, "Module")(engine, { "config": config }) for widget in self.objects[mod["name"]].widgets(): self.assertEquals(widget.get("variable", None), None)
class TestConfig(unittest.TestCase): def setUp(self): self.defaultConfig = Config() self.someSimpleModules = ["foo", "bar", "baz"] self.someAliasModules = ["foo:a", "bar:b", "baz:c"] def test_no_modules_by_default(self): self.assertEquals(self.defaultConfig.modules(), []) def test_simple_modules(self): cfg = Config(["-m"] + self.someSimpleModules) self.assertEquals(cfg.modules(), [{ "name": x, "module": x } for x in self.someSimpleModules]) def test_alias_modules(self): cfg = Config(["-m"] + self.someAliasModules) self.assertEquals(cfg.modules(), [{ "module": x.split(":")[0], "name": x.split(":")[1], } for x in self.someAliasModules]) @mock.patch("sys.stdout", new_callable=StringIO) @mock.patch("sys.exit") def test_list_themes(self, exit, stdout): cfg = Config(["-l", "themes"]) result = stdout.getvalue() for theme in themes(): self.assertTrue(theme in result) @mock.patch("sys.stdout", new_callable=StringIO) @mock.patch("sys.exit") def test_list_modules(self, exit, stdout): cfg = Config(["-l", "modules"]) result = stdout.getvalue() for module in all_modules(): self.assertTrue(module["name"] in result)
class TestConfig(unittest.TestCase): def setUp(self): self.defaultConfig = Config() self.someSimpleModules = ["foo", "bar", "baz"] self.someAliasModules = ["foo:a", "bar:b", "baz:c"] def test_no_modules_by_default(self): self.assertEquals(self.defaultConfig.modules(), []) def test_simple_modules(self): cfg = Config(["-m"] + self.someSimpleModules) self.assertEquals(cfg.modules(), [{ "name": x, "module": x } for x in self.someSimpleModules]) def test_alias_modules(self): cfg = Config(["-m"] + self.someAliasModules) self.assertEquals(cfg.modules(), [{ "module": x.split(":")[0], "name": x.split(":")[1], } for x in self.someAliasModules]) @mock.patch("sys.stdout", new_callable=StringIO) @mock.patch("sys.exit") def test_list_themes(self, exit, stdout): cfg = Config(["-l", "themes"]) result = stdout.getvalue() for theme in themes(): self.assertTrue(theme in result) @mock.patch("sys.stdout", new_callable=StringIO) @mock.patch("sys.exit") def test_list_modules(self, exit, stdout): cfg = Config(["-l", "modules"]) result = stdout.getvalue() for module in all_modules(): self.assertTrue(module["name"] in result)
def setUp(self): self.defaultConfig = Config() self.someSimpleModules = ["foo", "bar", "baz"] self.someAliasModules = ["foo:a", "bar:b", "baz:c"]
def test_list_themes(self, exit, stdout): cfg = Config(["-l", "themes"]) result = stdout.getvalue() for theme in themes(): self.assertTrue(theme in result)
class BumblebeeApp(wx.App): """ Main Bumblebee application class. This is a `wx.App` derived class which drives the rest of the Bumblebee application. This class also manages the connection between Bumblebee and the speech recognition engine. See `_connect_to_sr_engine` for implementation details. """ def __init__(self): wx.App.__init__(self) self._config = Config() #----------------------------------------------------------------------- # Overridden wx.App methods. def OnInit(self): log.debug("OnInit()") self._setup_logging() # Create main GUI frame. self._main_frame = MainFrame(None, -1, "Bumblebee") # Schedule further initialization. wx.CallLater(1, self._initialize_pca) # Return true to indicate that initialization was successful. return True def OnExit(self): self._shutdown_system_participants() #----------------------------------------------------------------------- # Internal methods. def _setup_logging(self): log.debug("_setup_logging()") log_levels = { "compound.parse": logging.INFO, "engine": logging.INFO, "grammar.begin": logging.INFO, "grammar.decode": logging.INFO, "dictation.formatter": logging.INFO, "context.match": logging.INFO, "grammar.load": logging.INFO, "action.exec": logging.INFO, } for name, level in log_levels.items(): this_log = logging.getLogger(name) this_log.setLevel(level) def _initialize_pca(self): log.debug("_initialize_pca()") self._initialize_loaders() self._config = Config() self._config.load_or_create() # Schedule further initialization. wx.CallLater(1, self._startup_system_participants) def _startup_system_participants(self): for participant in ExtensionPoint(ISystemParticipant): participant.startup() def _shutdown_system_participants(self): for participant in ExtensionPoint(ISystemParticipant): participant.shutdown() def _initialize_loaders(self): log.debug("_initialize_loaders()") # Import loaders so that they are registered in the PCA. import bumblebee.command.legacy_loader # Setup infrastructure for periodic updating of loaders. loaders = ExtensionPoint(ICommandSetLoader) def on_timer(event): reloaded = self._config.reload_if_modified() if reloaded: for participant in ExtensionPoint(ISystemParticipant): participant.config_changed() for loader in loaders: loader.update() self.Bind(wx.EVT_TIMER, on_timer) # Timer object must be bound to self, because otherwise it would # be garbage collected and therefore destroyed without ever # firing. self._timer = wx.Timer(self) self._timer.Start(500, oneShot=False)
def __init__(self): wx.App.__init__(self) self._config = Config()
def test_theme(self): cfg = Config(["-t", self.someTheme]) self.assertEquals(cfg.theme(), self.someTheme)
def test_simple_modules(self): cfg = Config(["-m"] + self.someSimpleModules) self.assertEquals(cfg.modules(), [{ "name": x, "module": x } for x in self.someSimpleModules])
def test_alias_modules(self): cfg = Config(["-m"] + self.someAliasModules) self.assertEquals(cfg.modules(), [{ "module": x.split(":")[0], "name": x.split(":")[1], } for x in self.someAliasModules])
def setUp(self): self.defaultConfig = Config() self.someSimpleModules = ["foo", "bar", "baz"] self.someAliasModules = ["foo:a", "bar:b", "baz:c"]
def test_alias_modules(self): cfg = Config(["-m"] + self.someAliasModules) self.assertEquals(cfg.modules(), [{ "module": x.split(":")[0], "name": x.split(":")[1], } for x in self.someAliasModules])
def test_simple_modules(self): cfg = Config(["-m"] + self.someSimpleModules) self.assertEquals(cfg.modules(), [{ "name": x, "module": x } for x in self.someSimpleModules])
class TestConfig(unittest.TestCase): def setUp(self): self._stdout = mock.patch("bumblebee.config.sys.stdout", new_callable=StringIO) self._stderr = mock.patch("bumblebee.config.sys.stderr", new_callable=StringIO) self.stdout = self._stdout.start() self.stderr = self._stderr.start() self.defaultConfig = Config() self.someSimpleModules = ["foo", "bar", "baz"] self.someAliasModules = ["foo:a", "bar:b", "baz:c"] self.someTheme = "some-theme" def tearDown(self): self._stdout.stop() self._stderr.stop() def test_no_modules_by_default(self): self.assertEquals(self.defaultConfig.modules(), []) def test_simple_modules(self): cfg = Config(["-m"] + self.someSimpleModules) self.assertEquals(cfg.modules(), [{ "name": x, "module": x } for x in self.someSimpleModules]) def test_alias_modules(self): cfg = Config(["-m"] + self.someAliasModules) self.assertEquals(cfg.modules(), [{ "module": x.split(":")[0], "name": x.split(":")[1], } for x in self.someAliasModules]) def test_parameters(self): cfg = Config(["-m", "module", "-p", "module.key=value"]) self.assertEquals(cfg.get("module.key"), "value") def test_theme(self): cfg = Config(["-t", self.someTheme]) self.assertEquals(cfg.theme(), self.someTheme) def test_notheme(self): self.assertEquals(self.defaultConfig.theme(), "default") def test_list_themes(self): with self.assertRaises(SystemExit): cfg = Config(["-l", "themes"]) result = self.stdout.getvalue() for theme in themes(): self.assertTrue(theme in result) def test_list_modules(self): with self.assertRaises(SystemExit): cfg = Config(["-l", "modules"]) result = self.stdout.getvalue() for module in all_modules(): self.assertTrue(module["name"] in result) def test_invalid_list(self): with self.assertRaises(SystemExit): cfg = Config(["-l", "invalid"]) self.assertTrue("invalid choice" in "".join(self.stderr.getvalue()))
class BumblebeeApp(wx.App): """ Main Bumblebee application class. This is a `wx.App` derived class which drives the rest of the Bumblebee application. This class also manages the connection between Bumblebee and the speech recognition engine. See `_connect_to_sr_engine` for implementation details. """ def __init__(self): wx.App.__init__(self) self._config = Config() #----------------------------------------------------------------------- # Overridden wx.App methods. def OnInit(self): log.debug("OnInit()") self._setup_logging() # Create main GUI frame. self._main_frame = MainFrame(None, -1, "Bumblebee") # Schedule further initialization. wx.CallLater(1, self._initialize_pca) # Return true to indicate that initialization was successful. return True def OnExit(self): self._shutdown_system_participants() #----------------------------------------------------------------------- # Internal methods. def _setup_logging(self): log.debug("_setup_logging()") log_levels = { "compound.parse": logging.INFO, "engine": logging.INFO, "grammar.begin": logging.INFO, "grammar.decode": logging.INFO, "dictation.formatter": logging.INFO, "context.match": logging.INFO, "grammar.load": logging.INFO, "action.exec": logging.INFO, } for name, level in log_levels.items(): this_log = logging.getLogger(name) this_log.setLevel(level) def _initialize_pca(self): log.debug("_initialize_pca()") self._initialize_loaders() self._config = Config() self._config.load_or_create() # Schedule further initialization. wx.CallLater(1, self._startup_system_participants) def _startup_system_participants(self): for participant in ExtensionPoint(ISystemParticipant): participant.startup() def _shutdown_system_participants(self): for participant in ExtensionPoint(ISystemParticipant): participant.shutdown() def _initialize_loaders(self): log.debug("_initialize_loaders()") # Import loaders so that they are registered in the PCA. import bumblebee.command.legacy_loader # Setup infrastructure for periodic updating of loaders. loaders = ExtensionPoint(ICommandSetLoader) def on_timer(event): reloaded = self._config.reload_if_modified() if reloaded: for participant in ExtensionPoint(ISystemParticipant): participant.config_changed() for loader in loaders: loader.update() self.Bind(wx.EVT_TIMER, on_timer) # Timer object must be bound to self, because otherwise it would # be garbage collected and therefore destroyed without ever # firing. self._timer = wx.Timer(self) self._timer.Start(500, oneShot=False)
def test_parameters(self): cfg = Config(["-m", "module", "-p", "module.key=value"]) self.assertEquals(cfg.get("module.key"), "value")
def test_list_modules(self, exit, stdout): cfg = Config(["-l", "modules"]) result = stdout.getvalue() for module in all_modules(): self.assertTrue(module["name"] in result)
def test_parameters(self): cfg = Config(["-m", "module", "-p", "module.key=value"]) self.assertEquals(cfg.get("module.key"), "value")
class TestModule(unittest.TestCase): def setUp(self): self.widgetName = "foo" self.widget = MockWidget(self.widgetName) self.config = Config() self.anyWidgetName = "random-widget-name" self.noSuchModule = "this-module-does-not-exist" self.moduleWithoutWidgets = Module(engine=None, widgets=None) self.moduleWithOneWidget = Module(engine=None, widgets=self.widget, config={"config": self.config}) self.moduleWithMultipleWidgets = Module(engine=None, widgets=[self.widget, self.widget, self.widget] ) self.anyConfigName = "cfg" self.anotherConfigName = "cfg2" self.anyModule = Module(engine=None, widgets=self.widget, config={ "name": self.anyConfigName, "config": self.config }) self.anotherModule = Module(engine=None, widgets=self.widget, config={ "name": self.anotherConfigName, "config": self.config }) self.anyKey = "some-parameter" self.anyValue = "value" self.anotherValue = "another-value" self.emptyKey = "i-do-not-exist" self.config.set("{}.{}".format(self.anyConfigName, self.anyKey), self.anyValue) self.config.set("{}.{}".format(self.anotherConfigName, self.anyKey), self.anotherValue) def test_empty_widgets(self): self.assertEquals(self.moduleWithoutWidgets.widgets(), []) def test_single_widget(self): self.assertEquals(self.moduleWithOneWidget.widgets(), [self.widget]) def test_multiple_widgets(self): for widget in self.moduleWithMultipleWidgets.widgets(): self.assertEquals(widget, self.widget) def test_retrieve_widget_by_name(self): widget = MockWidget(self.anyWidgetName) widget.name = self.anyWidgetName module = Module(engine=None, widgets=[self.widget, widget, self.widget]) retrievedWidget = module.widget(self.anyWidgetName) self.assertEquals(retrievedWidget, widget) def test_retrieve_widget_by_id(self): widget = MockWidget(self.anyWidgetName) widget.id = self.anyWidgetName module = Module(engine=None, widgets=[self.widget, widget, self.widget]) retrievedWidget = module.widget_by_id(self.anyWidgetName) self.assertEquals(retrievedWidget, widget) def test_retrieve_missing_widget(self): module = self.moduleWithMultipleWidgets widget = module.widget(self.noSuchModule) self.assertEquals(widget, None) widget = module.widget_by_id(self.noSuchModule) self.assertEquals(widget, None) def test_threshold(self): module = self.moduleWithOneWidget module.name = self.widgetName self.config.set("{}.critical".format(self.widgetName), 10.0) self.config.set("{}.warning".format(self.widgetName), 8.0) self.assertEquals("critical", module.threshold_state(10.1, 0, 0)) self.assertEquals("warning", module.threshold_state(10.0, 0, 0)) self.assertEquals(None, module.threshold_state(8.0, 0, 0)) self.assertEquals(None, module.threshold_state(7.9, 0, 0)) def test_parameters(self): self.assertEquals(self.anyModule.parameter(self.anyKey), self.anyValue) self.assertEquals(self.anotherModule.parameter(self.anyKey), self.anotherValue) def test_default_parameters(self): self.assertEquals(self.anyModule.parameter(self.emptyKey), None) self.assertEquals(self.anyModule.parameter(self.emptyKey, self.anyValue), self.anyValue)
class TestBatteryModule(unittest.TestCase): def setUp(self): self._stdout = mock.patch("sys.stdout", new_callable=StringIO) self._exists = mock.patch("bumblebee.modules.battery.os.path.exists") self._open = mock.patch("bumblebee.modules.battery.open", create=True) self.stdout = self._stdout.start() self.exists = self._exists.start() self.open = self._open.start() self.file = mock.Mock() self.file.__enter__ = lambda x: self.file self.file.__exit__ = lambda x, a, b, c: "" self.file.read.return_value = "120" self.open.return_value = self.file self.exists.return_value = True self.engine = mock.Mock() self.config = Config() self.config.set("battery.showremaining", "false") self.module = Module(engine=self.engine, config={"config":self.config}) self.config.set("battery.critical", "20") self.config.set("battery.warning", "25") self.criticalValue = "19" self.warningValue = "21" self.normalValue = "26" self.chargedValue = "96" self.module.widgets()[0] def tearDown(self): self._stdout.stop() self._exists.stop() self._open.stop() def test_format(self): for widget in self.module.widgets(): self.assertEquals(len(widget.full_text()), len("100%")) def test_critical(self): self.file.read.return_value = self.criticalValue self.module.update_all() self.assertTrue("critical" in self.module.state(self.module.widgets()[0])) def test_warning(self): self.file.read.return_value = self.warningValue self.module.update_all() self.assertTrue("warning" in self.module.state(self.module.widgets()[0])) def test_normal(self): self.file.read.return_value = self.normalValue self.module.update_all() self.assertTrue(not "warning" in self.module.state(self.module.widgets()[0])) self.assertTrue(not "critical" in self.module.state(self.module.widgets()[0])) def test_overload(self): self.file.read.return_value = "120" self.module.update_all() self.assertTrue(not "warning" in self.module.state(self.module.widgets()[0])) self.assertTrue(not "critical" in self.module.state(self.module.widgets()[0])) self.assertEquals(self.module.capacity(self.module.widgets()[0]), "100%") def test_ac(self): self.exists.return_value = False self.file.read.return_value = "120" self.module.update_all() self.assertEquals(self.module.capacity(self.module.widgets()[0]), "ac") self.assertTrue("AC" in self.module.state(self.module.widgets()[0])) def test_error(self): self.file.read.side_effect = IOError("failed to read") self.module.update_all() self.assertEquals(self.module.capacity(self.module.widgets()[0]), "n/a") self.assertTrue("critical" in self.module.state(self.module.widgets()[0])) self.assertTrue("unknown" in self.module.state(self.module.widgets()[0])) def test_charging(self): self.file.read.return_value = self.chargedValue self.module.update_all() self.assertTrue("charged" in self.module.state(self.module.widgets()[0])) self.file.read.return_value = self.normalValue self.module.update_all() self.assertTrue("charging" in self.module.state(self.module.widgets()[0])) def test_discharging(self): for limit in [ 10, 25, 50, 80, 100 ]: value = limit - 1 self.file.read.return_value = str(value) self.module.update_all() self.file.read.return_value = "Discharging" self.assertTrue("discharging-{}".format(limit) in self.module.state(self.module.widgets()[0]))
def test_theme(self): cfg = Config(["-t", self.someTheme]) self.assertEquals(cfg.theme(), self.someTheme)
def __init__(self): wx.App.__init__(self) self._config = Config()
class TestModule(unittest.TestCase): def setUp(self): self.widgetName = "foo" self.widget = MockWidget(self.widgetName) self.config = Config() self.anyWidgetName = "random-widget-name" self.noSuchModule = "this-module-does-not-exist" self.moduleWithoutWidgets = Module(engine=None, widgets=None) self.moduleWithOneWidget = Module(engine=None, widgets=self.widget, config={"config": self.config}) self.moduleWithMultipleWidgets = Module( engine=None, widgets=[self.widget, self.widget, self.widget]) self.anyConfigName = "cfg" self.anotherConfigName = "cfg2" self.anyModule = Module(engine=None, widgets=self.widget, config={ "name": self.anyConfigName, "config": self.config }) self.anotherModule = Module(engine=None, widgets=self.widget, config={ "name": self.anotherConfigName, "config": self.config }) self.anyKey = "some-parameter" self.anyValue = "value" self.anotherValue = "another-value" self.emptyKey = "i-do-not-exist" self.config.set("{}.{}".format(self.anyConfigName, self.anyKey), self.anyValue) self.config.set("{}.{}".format(self.anotherConfigName, self.anyKey), self.anotherValue) def test_empty_widgets(self): self.assertEquals(self.moduleWithoutWidgets.widgets(), []) def test_single_widget(self): self.assertEquals(self.moduleWithOneWidget.widgets(), [self.widget]) def test_multiple_widgets(self): for widget in self.moduleWithMultipleWidgets.widgets(): self.assertEquals(widget, self.widget) def test_retrieve_widget_by_name(self): widget = MockWidget(self.anyWidgetName) widget.name = self.anyWidgetName module = Module(engine=None, widgets=[self.widget, widget, self.widget]) retrievedWidget = module.widget(self.anyWidgetName) self.assertEquals(retrievedWidget, widget) def test_retrieve_widget_by_id(self): widget = MockWidget(self.anyWidgetName) widget.id = self.anyWidgetName module = Module(engine=None, widgets=[self.widget, widget, self.widget]) retrievedWidget = module.widget_by_id(self.anyWidgetName) self.assertEquals(retrievedWidget, widget) def test_retrieve_missing_widget(self): module = self.moduleWithMultipleWidgets widget = module.widget(self.noSuchModule) self.assertEquals(widget, None) widget = module.widget_by_id(self.noSuchModule) self.assertEquals(widget, None) def test_threshold(self): module = self.moduleWithOneWidget module.name = self.widgetName self.config.set("{}.critical".format(self.widgetName), 10.0) self.config.set("{}.warning".format(self.widgetName), 8.0) self.assertEquals("critical", module.threshold_state(10.1, 0, 0)) self.assertEquals("warning", module.threshold_state(10.0, 0, 0)) self.assertEquals(None, module.threshold_state(8.0, 0, 0)) self.assertEquals(None, module.threshold_state(7.9, 0, 0)) def test_parameters(self): self.assertEquals(self.anyModule.parameter(self.anyKey), self.anyValue) self.assertEquals(self.anotherModule.parameter(self.anyKey), self.anotherValue) def test_default_parameters(self): self.assertEquals(self.anyModule.parameter(self.emptyKey), None) self.assertEquals( self.anyModule.parameter(self.emptyKey, self.anyValue), self.anyValue)
def test_list_themes(self): with self.assertRaises(SystemExit): cfg = Config(["-l", "themes"]) result = self.stdout.getvalue() for theme in themes(): self.assertTrue(theme in result)
def test_invalid_list(self): with self.assertRaises(SystemExit): cfg = Config(["-l", "invalid"]) self.assertTrue("invalid choice" in "".join(self.stderr.getvalue()))
def test_list_modules(self): with self.assertRaises(SystemExit): cfg = Config(["-l", "modules"]) result = self.stdout.getvalue() for module in all_modules(): self.assertTrue(module["name"] in result)