def test_find_tf2_exe(self): app = main.TF2RichPresense(self.log, set_process_priority=False) self.assertEqual( app.find_tf2_exe('test_resources\\very real steam'), r'test_resources\very real steam 2\steamapps\common\Team Fortress 2\hl2.exe' ) app.gui.master.destroy()
def test_custom(self): settings.change('wait_time_slow', 1) big_number_file = f'test_{random.randint(10000, 99999)}.temp' with open('custom.py', 'r+') as custom_file: custom_old = custom_file.read() custom_file.seek(0) custom_file.truncate() custom_file.write( "class TF2RPCustom:" "\n\tdef __init__(self, app): pass" "\n\tdef before_loop(self, app): pass" "\n\tdef modify_game_state(self, app): pass" "\n\tdef modify_gui(self, app): pass" "\n\tdef modify_rpc_activity(self, app): pass" f"\n\tdef after_loop(self, app): open('{big_number_file}', 'w').close()" ) custom_file.flush() app = main.TF2RichPresense(self.log, set_process_priority=False) app.loop_body() app.gui.master.destroy() self.assertTrue(os.path.isfile(big_number_file)) time.sleep(0.2) for file in os.listdir(): if file.startswith('test_') and file.endswith('.temp'): os.remove(file) with open('custom.py', 'r+') as custom_file: custom_file.truncate() custom_file.write(custom_old)
def test_steam_config_file(self): app = main.TF2RichPresense(self.log, set_process_priority=False) ref_launch_options = '-novid -noipx -refresh 120 -w 1920 -h 1080 -windowed -noborder -useforcedmparms -noforcemaccel -noforcemspd -dxlevel 95' self.assertEqual( configs.steam_config_file(app, 'test_resources\\', False), ref_launch_options) self.assertEqual( configs.steam_config_file(app, 'test_resources\\', True), None) app.gui.master.destroy()
def test_set_gui_from_game_state_localized(self): settings.change('language', 'German') app = main.TF2RichPresense(self.log, set_process_priority=False) app.game_state.force_zero_map_time = True app.game_state.set_bulk((False, 'mvm_rottenburg', 'Medic', '', 'Queued for MvM (Boot Camp)', True)) app.game_state.update_server_data(['Player count'], set()) app.set_gui_from_game_state() self.assertEqual((app.gui.text_state, app.gui.bg_state, app.gui.fg_state, app.gui.class_state), (('Karte: Rottenburg (Hosting)', 'Spieler: ?/?', 'Zeit auf der Karte: 0:00', '0:00 verstrichen'), ('bg_modes/mvm', 77, 172), 'fg_maps/mvm_rottenburg', 'classes/medic')) self.assertEqual(app.gui.bottom_text_queue_state, "Warteschlange für MvM (Boot Camp)") app.gui.master.destroy()
def test_main_simple(self): settings.change('wait_time_slow', 1) app = main.TF2RichPresense(self.log) self.assertEqual(repr(app), 'main.TF2RichPresense (state=init)') self.assertEqual( fix_activity_dict(app.game_state.activity()), { 'details': 'In menus', 'state': 'Not queued', 'timestamps': { 'start': 0 }, 'assets': { 'large_image': 'main_menu', 'large_text': 'In menus - TF2 Rich Presence {tf2rpvnum}', 'small_image': 'tf2_logo', 'small_text': 'Team Fortress 2' } }) app.loop_body() app.gui.safe_update() self.assertEqual(repr(app), 'main.TF2RichPresense (state=no tf2)') self.assertEqual( fix_activity_dict(app.game_state.activity()), { 'details': 'In menus', 'state': 'Not queued', 'timestamps': { 'start': 0 }, 'assets': { 'large_image': 'main_menu', 'large_text': 'In menus - TF2 Rich Presence {tf2rpvnum}', 'small_image': 'tf2_logo', 'small_text': 'Team Fortress 2' } }) self_process = psutil.Process() self.assertEqual(self_process.nice(), psutil.BELOW_NORMAL_PRIORITY_CLASS) self.assertEqual(self_process.ionice(), psutil.IOPRIO_LOW) self_process.nice(psutil.NORMAL_PRIORITY_CLASS) self_process.ionice(psutil.IOPRIO_NORMAL) app.send_rpc_activity()
def test_set_gui_from_game_state(self): app = main.TF2RichPresense(self.log, set_process_priority=False) app.game_state.force_zero_map_time = True app.game_state.set_bulk((True, '', '', '', 'Not queued', False)) app.set_gui_from_game_state() self.assertEqual((app.gui.text_state, app.gui.bg_state, app.gui.fg_state, app.gui.class_state), (('In menus', 'Not queued', '0:00 elapsed'), ('main_menu', 85, 164), 'tf2_logo', '')) app.game_state.set_bulk( (False, 'plr_hightower', 'Heavy', '', 'Not queued', True)) app.game_state.update_server_data(['Player count'], set()) app.set_gui_from_game_state() self.assertEqual( (app.gui.text_state, app.gui.bg_state, app.gui.fg_state, app.gui.class_state), (('Map: Hightower (hosting)', 'Players: ?/?', 'Time on map: 0:00', '0:00 elapsed'), ('bg_modes/payload-race', 77, 172), 'fg_maps/plr_hightower', 'classes/heavy')) app.game_state.set_bulk( (False, 'tr_dustbowl', 'Scout', '', 'Queued for Casual', True)) app.set_gui_from_game_state() self.assertEqual( (app.gui.text_state, app.gui.bg_state, app.gui.fg_state, app.gui.class_state), (('Map: Dustbowl (Training) (hosting)', 'Players: ?/?', 'Time on map: 0:00', '0:00 elapsed'), ('bg_modes/training', 77, 172), 'fg_maps/cp_dustbowl', 'classes/scout')) self.assertEqual(app.gui.bottom_text_state, { 'discord': False, 'kataiser': False, 'queued': True, 'holiday': False }) app.game_state.set_bulk( (False, 'itemtest', 'Spy', '', 'Not queued', True)) app.set_gui_from_game_state() self.assertEqual( (app.gui.text_state, app.gui.bg_state, app.gui.fg_state, app.gui.class_state), (('Map: itemtest (hosting)', 'Players: ?/?', 'Time on map: 0:00', '0:00 elapsed'), ('bg_modes/unknown', 77, 172), 'fg_maps/itemtest', 'classes/spy')) app.game_state.set_bulk((False, 'cp_steel', 'Medic', '162.254.192.155:27053', 'Not queued', False)) app.game_state.update_server_data(['Player count'], set()) app.set_gui_from_game_state() state = [ list(app.gui.text_state), app.gui.bg_state, app.gui.fg_state, app.gui.class_state ] state[0][1] = 'Players: 0/24' if 'Players: ' in state[0][ 1] and '/24' in state[0][1] else state[0][1] self.assertEqual(state, [[ 'Map: Steel', 'Players: 0/24', 'Time on map: 0:00', '0:00 elapsed' ], ('bg_modes/attack-defend', 77, 172), 'fg_maps/cp_steel', 'classes/medic']) self.assertEqual(app.gui.bottom_text_state, { 'discord': False, 'kataiser': False, 'queued': False, 'holiday': False }) app.game_state.set_bulk( (False, 'plr_highertower', 'Engineer', '', 'Not queued', True)) app.game_state.update_server_data(['Player count'], set()) app.set_gui_from_game_state() state = [ list(app.gui.text_state), app.gui.bg_state, app.gui.fg_state, app.gui.class_state ] state[0][1] = 'Players: 0/24' if 'Players: ' in state[0][ 1] and '/24' in state[0][1] else state[0][1] self.assertEqual((app.gui.text_state, app.gui.bg_state, app.gui.fg_state, app.gui.class_state), (('Map: plr_highertower (hosting)', 'Players: ?/?', 'Time on map: 0:00', '0:00 elapsed'), ('bg_modes/payload-race', 77, 172), 'fg_modes/payload-race', 'classes/engineer')) app.gui.master.destroy()
def test_interpret_console_log(self): recent_time = int(time.time()) - 10 app = main.TF2RichPresense(self.log, set_process_priority=False) self.assertEqual( app.interpret_console_log('test_resources\\console_in_menus.log', {'not Kataiser'}, float('inf'), True), (True, '', '', '', 'Not queued', False)) self.assertEqual( app.interpret_console_log('test_resources\\console_in_menus.log', {'not Kataiser'}, 4, True), (True, '', '', '', 'Not queued', False)) self.assertEqual( app.interpret_console_log( 'test_resources\\console_queued_casual.log', {'not Kataiser'}, float('inf'), True), (True, '', '', '', 'Queued for Casual', False)) self.assertEqual( app.interpret_console_log('test_resources\\console_badwater.log', {'not Kataiser'}, float('inf'), True), (False, 'pl_badwater', 'Pyro', '', 'Not queued', True)) self.assertEqual( app.interpret_console_log('test_resources\\console_badwater.log', {'not Kataiser'}, float('inf'), True, recent_time), (True, '', '', '', 'Not queued', False)) self.assertEqual( app.interpret_console_log('test_resources\\console_badwater.log', {'not Kataiser'}, 0.2, True), (True, '', '', '', 'Not queued', False)) self.assertEqual( app.interpret_console_log('test_resources\\console_custom_map.log', {'not Kataiser'}, float('inf'), True), (False, 'cp_catwalk_a5c', 'Soldier', '', 'Not queued', True)) self.assertEqual( app.interpret_console_log( 'test_resources\\console_soundemitter.log', {'not Kataiser'}, float('inf'), True), (True, '', '', '', 'Not queued', False)) self.assertEqual( app.interpret_console_log( 'test_resources\\console_queued_in_game.log', {'not Kataiser'}, float('inf'), True), (False, 'itemtest', 'Heavy', '', 'Queued for Casual', True)) self.assertEqual( app.interpret_console_log( 'test_resources\\console_canceled_load.log', {'not Kataiser'}, float('inf'), True), (True, '', '', '', 'Not queued', False)) self.assertEqual( app.interpret_console_log('test_resources\\console_chat.log', {'not Kataiser'}, float('inf'), True), (False, 'itemtest', 'Scout', '', 'Not queued', True)) self.assertEqual( app.interpret_console_log('test_resources\\console_empty.log', {'not Kataiser'}, float('inf'), True), (True, '', '', '', 'Not queued', False)) self.assertEqual( app.interpret_console_log( 'test_resources\\console_valve_server.log', {'not Kataiser'}, float('inf'), True), (False, 'pl_snowycoast', 'Pyro', '162.254.194.158:27048', 'Not queued', False)) self.assertEqual( app.interpret_console_log('test_resources\\console_tf2bd.log', {'Kataiser'}, float('inf'), True), (False, 'ctf_turbine', 'Soldier', '45.141.52.51:27015', 'Not queued', False)) self.assertEqual( app.interpret_console_log( 'test_resources\\console_community_disconnect.log', {'not Kataiser'}, float('inf'), True), (True, '', '', '', 'Not queued', False)) self.assertEqual( app.interpret_console_log( 'test_resources\\console_community_disconnect2.log', {'not Kataiser'}, float('inf'), True), (False, 'vsh_military_area_se6', 'Heavy', '45.235.98.47:27070', 'Not queued', False)) self.assertEqual( app.interpret_console_log('test_resources\\console_blanks.log', {'not Kataiser'}, float('inf'), True), (False, 'sd_doomsday_event', 'Pyro', '162.254.195.114:27046', 'Not queued', False)) self.assertEqual( app.interpret_console_log( 'test_resources\\console_map_material.log', {'not Kataiser'}, float('inf'), True), (False, 'koth_slaughter_event', '', '162.254.192.150:27051', 'Queued for Casual', False)) # tests trimming trimtest_small = 'test_resources\\console_badwater.log' trimtest_big = 'test_resources\\console_badwater_big.log' shutil.copy(trimtest_small, trimtest_big) with open(trimtest_big, 'rb+') as console_badwater_sacrifice: console_badwater_sacrifice.write(console_badwater_sacrifice.read( )) # this just doubles the file size initial_size = os.stat(trimtest_big).st_size self.assertEqual( app.interpret_console_log(trimtest_big, {'not Kataiser'}), (False, 'pl_badwater', 'Pyro', '', 'Not queued', True)) trimmed_size = os.stat(trimtest_big).st_size self.assertLess(trimmed_size, initial_size) self.assertEqual(trimmed_size, (1024**2) * 2) os.remove(trimtest_big) # tests removing error and empty lines errorstest_big = 'test_resources\\console_in_menus.log' errorstest_small = 'test_resources\\console_in_menus_small.log' shutil.copy(errorstest_big, errorstest_small) initial_size = os.stat(errorstest_small).st_size with open(errorstest_small, 'r', encoding='UTF8') as errorstest_small_unclean: self.assertTrue( 'DataTable warning' in errorstest_small_unclean.read()) app.interpret_console_log(errorstest_small, {'not Kataiser'}, float('inf')) cleaned_size = os.stat(errorstest_small).st_size self.assertLess(cleaned_size, initial_size) with open(errorstest_small, 'r', encoding='UTF8') as errorstest_small_cleaned: self.assertFalse( 'DataTable warning' in errorstest_small_cleaned.read()) self.assertEqual( app.interpret_console_log(errorstest_small, {'not Kataiser'}, float('inf')), (True, '', '', '', 'Not queued', False)) os.remove(errorstest_small) app.gui.master.destroy()
def before_loop(self, app: main.TF2RichPresense): # app.log.debug("Running custom.before_loop()") pass def modify_game_state(self, app: main.TF2RichPresense): # app.log.debug("Running custom.modify_game_state()") pass def modify_gui(self, app: main.TF2RichPresense): # app.log.debug("Running custom.modify_gui()") pass def modify_rpc_activity(self, app: main.TF2RichPresense): # app.log.debug("Running custom.modify_rpc_activity()") pass def after_loop(self, app: main.TF2RichPresense): # app.log.debug("Running custom.after_loop()") pass if __name__ == '__main__': test_app = main.TF2RichPresense(logger.Log()) test_TF2RPCustom = TF2RPCustom(main.TF2RichPresense()) test_TF2RPCustom.before_loop(test_app) test_TF2RPCustom.modify_game_state(test_app) test_TF2RPCustom.modify_gui(test_app) test_TF2RPCustom.modify_rpc_activity(test_app) test_TF2RPCustom.after_loop(test_app)