<s> import unittest <EOL> import mock <EOL> from pyface . qt import QtCore , QtGui <EOL> from pyface . qt . QtTest import QTest <EOL> from pyface . ui . qt4 . code_editor . code_widget import CodeWidget , AdvancedCodeWidget <EOL> class TestCodeWidget ( unittest . TestCase ) : <EOL> @ classmethod <EOL> def setupClass ( cls ) : <EOL> cls . qapp = QtGui . QApplication . instance ( ) or QtGui . QApplication ( [ ] ) <EOL> def tearDown ( self ) : <EOL> self . qapp . processEvents ( ) <EOL> def test_readonly_editor ( self ) : <EOL> cw = CodeWidget ( None ) <EOL> text = '<STR_LIT>' <EOL> cw . setPlainText ( text ) <EOL> def check ( typed , expected ) : <EOL> cursor = cw . textCursor ( ) <EOL> cursor . setPosition ( <NUM_LIT:0> ) <EOL> cw . setTextCursor ( cursor ) <EOL> QTest . keyClicks ( cw , typed ) <EOL> self . assertEqual ( cw . toPlainText ( ) , expected ) <EOL> cw . setReadOnly ( True ) <EOL> check ( '<STR_LIT>' , text ) <EOL> cw . setReadOnly ( False ) <EOL> check ( '<STR_LIT>' , '<STR_LIT>' + text ) <EOL> def test_readonly_replace_widget ( self ) : <EOL> acw = AdvancedCodeWidget ( None ) <EOL> text = '<STR_LIT>' <EOL> acw . code . setPlainText ( text ) <EOL> acw . show ( ) <EOL> FindKey = QtGui . QKeySequence ( '<STR_LIT>' ) <EOL> ReplaceKey = QtGui . QKeySequence ( '<STR_LIT>' ) <EOL> patcher_find = mock . patch ( '<STR_LIT>' , FindKey ) <EOL> patcher_replace = mock . patch ( '<STR_LIT>' , <EOL> ReplaceKey ) <EOL> patcher_find . start ( ) <EOL> patcher_replace . start ( ) <EOL> self . addCleanup ( patcher_find . stop ) <EOL> self . addCleanup ( patcher_replace . stop ) <EOL> def click_key_seq ( widget , key_seq ) : <EOL> if not isinstance ( key_seq , QtGui . QKeySequence ) : <EOL> key_seq = QtGui . QKeySequence ( key_seq ) <EOL> try : <EOL> first_key = key_seq [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return False <EOL> key = QtCore . Qt . Key ( first_key & ~ QtCore . Qt . KeyboardModifierMask ) <EOL> modifier = QtCore . Qt . KeyboardModifier ( <EOL> first_key & QtCore . Qt . KeyboardModifierMask ) <EOL> QTest . keyClick ( widget , key , modifier ) <EOL> return True <EOL> acw . code . setReadOnly ( True ) <EOL> if click_key_seq ( acw , FindKey ) : <EOL> self . assertTrue ( acw . find . isVisible ( ) ) <EOL> acw . find . hide ( ) <EOL> acw . code . setReadOnly ( False ) <EOL> if click_key_seq ( acw , FindKey ) : <EOL> self . assertTrue ( acw . find . isVisible ( ) ) <EOL> acw . find . hide ( ) <EOL> acw . code . setReadOnly ( True ) <EOL> if click_key_seq ( acw , ReplaceKey ) : <EOL> self . assertFalse ( acw . replace . isVisible ( ) ) <EOL> acw . code . setReadOnly ( False ) <EOL> if click_key_seq ( acw , ReplaceKey ) : <EOL> self . assertTrue ( acw . replace . isVisible ( ) ) <EOL> acw . replace . hide ( ) <EOL> self . assertFalse ( acw . replace . isVisible ( ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
