def test_session_assets(): store = AssetStore() s = SessionAssets(store) s._send_command = lambda x: None assert not s.get_used_asset_names() with open(test_filename, 'wb') as f: f.write(b'bar\n') # Add assets, check mangles name a1 = s.add_asset('foo.css', b'foo\n') a2 = s.add_asset('foo.js', test_filename) assert 'foo' in a1 and s.id in a1 and a1.endswith('.css') assert 'foo' in a2 and s.id in a2 and a2.endswith('.js') assert s.get_used_asset_names() == [a1, a2] # order in which it came # Get the asset raises(IndexError, store.load_asset, 'foo.css') raises(IndexError, store.load_asset, 'foo.js') assert store.load_asset(a1) == b'foo\n' assert store.load_asset(a2) == b'bar\n' # Use asset store.add_asset('spam.js', b'1234\x00') s.use_global_asset('spam.js') assert s.get_used_asset_names()[-1] == 'spam.js' raises(IndexError, s.use_global_asset, 'unknown-asset.js') raises(ValueError, s.add_asset, 3, b'a\n') # Add assets after loading page s.get_page() s.use_global_asset('spam.js') # prints a warning, but it does work # Global assets s.add_global_asset('eggs.js', b'12345\x00') assert s.get_used_asset_names()[-1] == 'eggs.js' assert store.load_asset('eggs.js') == b'12345\x00' raises(ValueError, s.use_global_asset, 3) # Remote assets s.use_remote_asset('http://linked.com/not/verified.js') s.use_remote_asset('http://linked.com/not/verified.css') s.use_remote_asset('http://linked.com/not/verified.css') # twice is ok raises(ValueError, s.use_remote_asset, 3) page = s.get_page() assert 'not/verified.js' in page assert 'not/verified.css' in page
def test_session_registering_model_classes(): store = AssetStore() s = SessionAssets(store) s._send_command = lambda x: None store.create_module_assets('flexx.ui.layouts') raises(ValueError, s.register_model_class, 4) # must be a Model class s.register_model_class(ui.Slider) assert len(s._known_classes) == 3 # Slider, Widget, and Model s.register_model_class(ui.Slider) # no duplicates! assert len(s._known_classes) == 3 s.register_model_class(ui.BoxLayout) s.register_model_class(ui.Button) # Get result js = s.get_js_only() assert js.count('.Button = function ') == 1 assert js.count('.Slider = function ') == 1 assert js.count('.Widget = function ') == 1 assert js.count('.BoxLayout = function ') == 1 # Check that module indeed only has layout widgets jsmodule = store.load_asset('flexx-ui-layouts.js').decode() assert jsmodule.count('.BoxLayout = function ') == 1 assert jsmodule.count('.Button = function ') == 0 assert jsmodule.count('.Widget = function ') == 0 # Check that page contains the rest page = s.get_page() assert page.count('.BoxLayout = function ') == 0 assert page.count('.Button = function ') == 1 assert page.count('.Widget = function ') == 1 # Check that a single page export has it all export = s.get_page_for_export([], True) assert export.count('.BoxLayout = function ') == 1 assert export.count('.Button = function ') == 1 assert export.count('.Widget = function ') == 1 # Patch - this func is normally provided by the Session subclass commands = [] s._send_command = lambda x: commands.append(x) # Dynamic s.register_model_class(ui.BoxLayout) assert len(commands) == 0 # already known s.register_model_class(ui.FormLayout) assert len(commands) == 0 # already in module asset # s.register_model_class(ui.Label) assert '.Label = function' in commands[0] # JS assert 'flx-' in commands[1] # CSS
def test_session_registering_model_classes(): from flexx import ui store = AssetStore() store.update_modules() s = SessionAssets(store) commands = [] s._send_command = lambda x: commands.append(x) assert not s._used_modules # Register button, pulls in all dependent modules s.register_model_class(ui.Button) assert ui.Button.__jsmodule__ in s._used_modules assert ui.BaseButton.__jsmodule__ in s._used_modules assert ui.Widget.__jsmodule__ in s._used_modules assert 'flexx.app.model' in s._used_modules # Get assets, level 9 js_assets, css_assets = s.get_assets_in_order(bundle_level=9) names = [a.name for a in js_assets] assert 'flexx.app.model.js' in names assert 'flexx.ui.widgets._button.js' in names # Get assets, level 2 js_assets, css_assets = s.get_assets_in_order(bundle_level=2) names = [a.name for a in js_assets] assert 'flexx.app.js' in names assert 'flexx.ui.js' in names assert 'flexx.ui.widgets._button.js' not in names # Get assets, level 1 js_assets, css_assets = s.get_assets_in_order(bundle_level=1) names = [a.name for a in js_assets] assert 'flexx.js' in names assert 'flexx.ui.js' not in names assert 'flexx.ui.widgets._button.js' not in names # Get page code = s.get_page() assert '<html>' in code code = s.get_page_for_export([]) assert '<html>' in code # dynamic loading ... # No commands so far assert not commands s.register_model_class(ui.html.ul) assert commands