X Tutup
# Copyright (c) 2016 CEF Python, see the Authors file. # All rights reserved. Licensed under BSD 3-clause license. # Project website: https://github.com/cztomczak/cefpython """General testing of CEF Python.""" import unittest # noinspection PyUnresolvedReferences import _test_runner from _common import * from cefpython3 import cefpython as cef import glob import os import sys g_datauri_data = """

Main test

""" g_datauri = cef.GetDataUrl(g_datauri_data) class MainTest_IsolatedTest(unittest.TestCase): def test_main(self): """Main entry point. All the code must run inside one single test, otherwise strange things happen.""" print("") print("CEF Python {ver}".format(ver=cef.__version__)) print("Python {ver}".format(ver=sys.version[:6])) # Test initialization of CEF settings = { "debug": False, "log_severity": cef.LOGSEVERITY_ERROR, "log_file": "", } if not LINUX: # On Linux you get a lot of "X error received" messages # from Chromium's "x11_util.cc", so do not show them. settings["log_severity"] = cef.LOGSEVERITY_WARNING if "--debug" in sys.argv: settings["debug"] = True settings["log_severity"] = cef.LOGSEVERITY_INFO if "--debug-warning" in sys.argv: settings["debug"] = True settings["log_severity"] = cef.LOGSEVERITY_WARNING cef.Initialize(settings) subtest_message("cef.Initialize() ok") # High DPI on Windows if WINDOWS: self.assertIsInstance(cef.DpiAware.GetSystemDpi(), tuple) window_size = cef.DpiAware.CalculateWindowSize(800, 600) self.assertIsInstance(window_size, tuple) self.assertGreater(window_size[0], 0) # It seems that in v49 Chromium sets process DPI awareness # by default, so IsProcessDpiAware returns True. # self.assertFalse(cef.DpiAware.IsProcessDpiAware()) subtest_message("cef.DpiAware ok") # Global handler global_handler = GlobalHandler(self) cef.SetGlobalClientCallback("OnAfterCreated", global_handler._OnAfterCreated) subtest_message("cef.SetGlobalClientCallback() ok") # Create browser browser_settings = { "inherit_client_handlers_for_popups": False, } browser = cef.CreateBrowserSync(url=g_datauri, settings=browser_settings) self.assertIsNotNone(browser, "Browser object") browser.SetFocus(True) subtest_message("cef.CreateBrowserSync() ok") # Client handlers client_handlers = [LoadHandler(self, g_datauri), DisplayHandler(self)] for handler in client_handlers: browser.SetClientHandler(handler) subtest_message("browser.SetClientHandler() ok") # Javascript bindings external = External(self) bindings = cef.JavascriptBindings( bindToFrames=False, bindToPopups=False) bindings.SetFunction("js_code_completed", js_code_completed) bindings.SetFunction("test_function", external.test_function) bindings.SetProperty("test_property1", external.test_property1) bindings.SetProperty("test_property2", external.test_property2) # Property with a function value can also be bound. CEF Python # supports passing functions as callbacks when called from # javascript, and as a side effect any value and in this case # a property can also be a function. bindings.SetProperty("test_property3_function", external.test_property3_function) bindings.SetProperty("cefpython_version", cef.GetVersion()) bindings.SetObject("external", external) browser.SetJavascriptBindings(bindings) subtest_message("browser.SetJavascriptBindings() ok") # Cookie manager self.assertIsInstance(cef.CookieManager.CreateManager(path=""), cef.PyCookieManager) self.assertIsInstance(cef.CookieManager.GetGlobalManager(), cef.PyCookieManager) subtest_message("cef.CookieManager ok") # Window Utils if WINDOWS: hwnd = 1 # When using 0 getting issues with OnautoResize self.assertFalse(cef.WindowUtils.IsWindowHandle(hwnd)) cef.WindowUtils.OnSetFocus(hwnd, 0, 0, 0) cef.WindowUtils.OnSize(hwnd, 0, 0, 0) cef.WindowUtils.OnEraseBackground(hwnd, 0, 0, 0) cef.WindowUtils.GetParentHandle(hwnd) cef.WindowUtils.SetTitle(browser, "Main test") subtest_message("cef.WindowUtils ok") elif LINUX: cef.WindowUtils.InstallX11ErrorHandlers() subtest_message("cef.WindowUtils ok") elif MAC: hwnd = 0 cef.WindowUtils.GetParentHandle(hwnd) cef.WindowUtils.IsWindowHandle(hwnd) subtest_message("cef.WindowUtils ok") # Run message loop run_message_loop() # Make sure popup browser was destroyed self.assertIsInstance(cef.GetBrowserByIdentifier(MAIN_BROWSER_ID), cef.PyBrowser) self.assertIsNone(cef.GetBrowserByIdentifier(POPUP_BROWSER_ID)) subtest_message("cef.GetBrowserByIdentifier() ok") # Close browser and clean reference browser.CloseBrowser(True) del browser subtest_message("browser.CloseBrowser() ok") # Give it some time to close before checking asserts # and calling shutdown. do_message_loop_work(25) # noinspection PyTypeChecker check_auto_asserts(self, [] + client_handlers + [global_handler, external]) # Test shutdown of CEF cef.Shutdown() subtest_message("cef.Shutdown() ok") # Display summary show_test_summary(__file__) sys.stdout.flush() class External(object): """Javascript 'window.external' object.""" def __init__(self, test_case): self.test_case = test_case # Test binding properties to the 'window' object. self.test_property1 = "Test binding property to the 'window' object" self.test_property2 = {"key1": self.test_property1, "key2": ["Inside list", 1, 2]} # Asserts for True/False will be checked just before shutdown self.test_for_True = True # Test whether asserts are working correctly self.test_function_True = False self.test_property3_function_True = False self.test_callbacks_True = False self.py_callback_True = False def test_function(self): """Test binding function to the 'window' object.""" self.test_function_True = True def test_property3_function(self): """Test binding function to the 'window' object.""" self.test_property3_function_True = True def test_callbacks(self, js_callback): """Test both javascript and python callbacks.""" def py_callback(msg_from_js): self.py_callback_True = True self.test_case.assertEqual(msg_from_js, "String sent from Javascript") self.test_callbacks_True = True js_callback.Call("String sent from Python", py_callback) if __name__ == "__main__": _test_runner.main(os.path.basename(__file__))
X Tutup