1# Copyright (c) 2021 elParaguayo 2# 3# Permission is hereby granted, free of charge, to any person obtaining a copy 4# of this software and associated documentation files (the "Software"), to deal 5# in the Software without restriction, including without limitation the rights 6# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7# copies of the Software, and to permit persons to whom the Software is 8# furnished to do so, subject to the following conditions: 9# 10# The above copyright notice and this permission notice shall be included in 11# all copies or substantial portions of the Software. 12# 13# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19# SOFTWARE. 20 21# Widget specific tests 22 23import sys 24from importlib import reload 25from types import ModuleType 26 27from libqtile.bar import Bar 28from libqtile.widget import gmail_checker 29 30 31def no_op(*args, **kwargs): 32 pass 33 34 35class FakeIMAP(ModuleType): 36 class IMAP4_SSL: # noqa: N801 37 def __init__(self, *args, **kwargs): 38 pass 39 40 def login(self, username, password): 41 self.username = username 42 self.password = password 43 44 def status(self, path, *args, **kwargs): 45 if not (self.username and self.password): 46 return False, None 47 48 return ( 49 "OK", 50 ['("{}" (MESSAGES 10 UNSEEN 2)'.format(path).encode()] 51 ) 52 53 54def test_gmail_checker_valid_response(fake_qtile, monkeypatch, fake_window): 55 monkeypatch.setitem(sys.modules, "imaplib", FakeIMAP("imaplib")) 56 reload(gmail_checker) 57 58 gmc = gmail_checker.GmailChecker(username="qtile", password="test") 59 fakebar = Bar([gmc], 24) 60 fakebar.window = fake_window 61 fakebar.width = 10 62 fakebar.height = 10 63 fakebar.draw = no_op 64 gmc._configure(fake_qtile, fakebar) 65 text = gmc.poll() 66 assert text == "inbox[10],unseen[2]" 67 68 69def test_gmail_checker_invalid_response(fake_qtile, monkeypatch, fake_window): 70 monkeypatch.setitem(sys.modules, "imaplib", FakeIMAP("imaplib")) 71 reload(gmail_checker) 72 73 gmc = gmail_checker.GmailChecker() 74 fakebar = Bar([gmc], 24) 75 fakebar.window = fake_window 76 fakebar.width = 10 77 fakebar.height = 10 78 fakebar.draw = no_op 79 gmc._configure(fake_qtile, fakebar) 80 text = gmc.poll() 81 assert text == "UNKNOWN ERROR" 82 83 84# This test is only required because the widget is written 85# inefficiently. display_fmt should use keys instead of indices. 86def test_gmail_checker_only_unseen(fake_qtile, monkeypatch, fake_window): 87 monkeypatch.setitem(sys.modules, "imaplib", FakeIMAP("imaplib")) 88 reload(gmail_checker) 89 90 gmc = gmail_checker.GmailChecker( 91 display_fmt="unseen[{0}]", 92 status_only_unseen=True, 93 username="qtile", 94 password="test" 95 ) 96 fakebar = Bar([gmc], 24) 97 fakebar.window = fake_window 98 fakebar.width = 10 99 fakebar.height = 10 100 fakebar.draw = no_op 101 gmc._configure(fake_qtile, fakebar) 102 text = gmc.poll() 103 assert text == "unseen[2]" 104