|
| 1 | +""" |
| 2 | +Test the notification area and widgets |
| 3 | +""" |
| 4 | +import pytest |
| 5 | + |
| 6 | +from .utils import wait_for_selector, wait_for_script_to_return_true |
| 7 | + |
| 8 | + |
| 9 | +def get_widget(notebook, name): |
| 10 | + return notebook.browser.execute_script( |
| 11 | + f"return IPython.notification_area.get_widget('{name}') !== undefined" |
| 12 | + ) |
| 13 | + |
| 14 | + |
| 15 | +def widget(notebook, name): |
| 16 | + return notebook.browser.execute_script( |
| 17 | + f"return IPython.notification_area.widget('{name}') !== undefined" |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +def new_notification_widget(notebook, name): |
| 22 | + return notebook.browser.execute_script( |
| 23 | + f"return IPython.notification_area.new_notification_widget('{name}') !== undefined" |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +def widget_has_class(notebook, name, class_name): |
| 28 | + return notebook.browser.execute_script( |
| 29 | + f""" |
| 30 | + var w = IPython.notification_area.get_widget('{name}'); |
| 31 | + return w.element.hasClass('{class_name}'); |
| 32 | + """ |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def widget_message(notebook, name): |
| 37 | + return notebook.browser.execute_script( |
| 38 | + f""" |
| 39 | + var w = IPython.notification_area.get_widget('{name}'); |
| 40 | + return w.get_message(); |
| 41 | + """ |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +def test_notification(notebook): |
| 46 | + # check that existing widgets are there |
| 47 | + assert get_widget(notebook, "kernel") and widget(notebook, "kernel"),\ |
| 48 | + "The kernel notification widget exists" |
| 49 | + assert get_widget(notebook, "notebook") and widget(notebook, "notebook"),\ |
| 50 | + "The notebook notification widget exists" |
| 51 | + |
| 52 | + # try getting a non-existent widget |
| 53 | + with pytest.raises(Exception): |
| 54 | + get_widget(notebook, "foo") |
| 55 | + |
| 56 | + # try creating a non-existent widget |
| 57 | + assert widget(notebook, "bar"), "widget: new widget is created" |
| 58 | + |
| 59 | + # try creating a widget that already exists |
| 60 | + with pytest.raises(Exception): |
| 61 | + new_notification_widget(notebook, "kernel") |
| 62 | + |
| 63 | + # test creating 'info', 'warning' and 'danger' messages |
| 64 | + for level in ("info", "warning", "danger"): |
| 65 | + notebook.browser.execute_script(f""" |
| 66 | + var tnw = IPython.notification_area.widget('test'); |
| 67 | + tnw.{level}('test {level}'); |
| 68 | + """) |
| 69 | + wait_for_selector(notebook.browser, "#notification_test", visible=True) |
| 70 | + |
| 71 | + assert widget_has_class(notebook, "test", level), f"{level}: class is correct" |
| 72 | + assert widget_message(notebook, "test") == f"test {level}", f"{level}: message is correct" |
| 73 | + |
| 74 | + # test message timeout |
| 75 | + notebook.browser.execute_script(""" |
| 76 | + var tnw = IPython.notification_area.widget('test'); |
| 77 | + tnw.set_message('test timeout', 1000); |
| 78 | + """) |
| 79 | + wait_for_selector(notebook.browser, "#notification_test", visible=True) |
| 80 | + |
| 81 | + assert widget_message(notebook, "test") == "test timeout", "timeout: message is correct" |
| 82 | + wait_for_selector(notebook.browser, "#notification_test", obscures=True) |
| 83 | + assert widget_message(notebook, "test") == "", "timeout: message was cleared" |
| 84 | + |
| 85 | + # test click callback |
| 86 | + notebook.browser.execute_script(""" |
| 87 | + var tnw = IPython.notification_area.widget('test'); |
| 88 | + tnw._clicked = false; |
| 89 | + tnw.set_message('test click', undefined, function () { |
| 90 | + tnw._clicked = true; |
| 91 | + return true; |
| 92 | + }); |
| 93 | + """) |
| 94 | + wait_for_selector(notebook.browser, "#notification_test", visible=True) |
| 95 | + |
| 96 | + assert widget_message(notebook, "test") == "test click", "callback: message is correct" |
| 97 | + |
| 98 | + notebook.browser.find_element_by_id("notification_test").click() |
| 99 | + wait_for_script_to_return_true(notebook.browser, |
| 100 | + 'return IPython.notification_area.widget("test")._clicked;') |
| 101 | + wait_for_selector(notebook.browser, "#notification_test", obscures=True) |
| 102 | + |
| 103 | + assert widget_message(notebook, "test") == "", "callback: message was cleared" |
0 commit comments