code: def test_application():
      """Test application running"""
      app = use_app()
      print(app)  # __repr__ without app
      app.create()
      wrong = "glfw" if app.backend_name.lower() != "glfw" else "pyqt5"
      assert_raises(RuntimeError, use_app, wrong)
      app.process_events()
      print(app)  # test __repr__
  
      assert_raises(ValueError, Canvas, keys="foo")
      assert_raises(TypeError, Canvas, keys=dict(escape=1))
      assert_raises(
          ValueError, Canvas, keys=dict(escape="foo")
      )  # not an attr
  
      pos = (
          [0, 0] if app.backend_module.capability["position"] else None
      )
      size = (100, 100)
      # Use "with" statement so failures don't leave open window
      # (and test context manager behavior)
      title = "default"
      with Canvas(
          title=title, size=size, app=app, show=True, position=pos
      ) as canvas:
          context = canvas.context
          assert_true(
              canvas.create_native() is None
          )  # should be done already
          assert_is(canvas.app, app)
          assert_true(canvas.native)
          assert_equal(
              "swap_buffers", canvas.events.draw.callback_refs[-1]
          )
  
          canvas.measure_fps(0.001)
          sleep(0.002)
          canvas.update()
          app.process_events()
          assert_true(canvas.fps > 0)
  
          # Other methods
          print(canvas)  # __repr__
          assert_equal(canvas.title, title)
          canvas.title = "you"
          with use_log_level("warning", record=True, print_msg=False):
              if app.backend_module.capability["position"]:
                  # todo: disable more tests based on capability
                  canvas.position = pos
              canvas.size = size
          canvas.connect(on_mouse_move)
          assert_raises(ValueError, canvas.connect, _on_mouse_move)
          if (
              sys.platform != "darwin"
          ):  # XXX knownfail, prob. needs warmup
              canvas.show(False)
              canvas.show()
          app.process_events()
          assert_raises(ValueError, canvas.connect, on_nonexist)
          # deprecation of "paint"
          with use_log_level(
              "info", record=True, print_msg=False
          ) as log:
              olderr = sys.stderr
              try:
                  fid = StringIO()
                  sys.stderr = fid
  
                  @canvas.events.paint.connect
                  def fake(event):
                      pass
  
              finally:
                  sys.stderr = olderr
          assert_equal(len(log), 1)
          assert_in("deprecated", log[0])
  
          # screenshots
          gl.glViewport(0, 0, *size)
          ss = _screenshot()
          assert_array_equal(ss.shape, size + (4,))
          assert_equal(len(canvas._backend._vispy_get_geometry()), 4)
          if sys.platform != "win32":  # XXX knownfail for windows
              assert_array_equal(canvas.size, size)
          assert_equal(
              len(canvas.position), 2
          )  # XXX knownfail, doesn't "take"
  
          # GLOO: should have an OpenGL context already, so these should work
          vert = "void main (void) {gl_Position = pos;}"
          frag = "void main (void) {gl_FragColor = pos;}"
          program = Program(vert, frag)
          assert_raises(
              RuntimeError, program.glir.flush, context.shared.parser
          )
  
          vert = (
              "uniform vec4 pos;\nvoid main (void) {gl_Position = pos;}"
          )
          frag = "uniform vec4 pos;\nvoid main (void) {gl_FragColor = pos;}"
          program = Program(vert, frag)
          # uniform = program.uniforms[0]
          program["pos"] = [1, 2, 3, 4]
  
          vert = "attribute vec4 pos;\nvoid main (void) {gl_Position = pos;}"
          frag = "void main (void) {}"
          program = Program(vert, frag)
          # attribute = program.attributes[0]
          program["pos"] = [1, 2, 3, 4]
  
          # use a real program
          program._glir.clear()
          vert = (
              "uniform mat4 u_model;"
              "attribute vec2 a_position; attribute vec4 a_color;"
              "varying vec4 v_color;"
              "void main (void) {v_color = a_color;"
              "gl_Position = u_model * vec4(a_position, 0.0, 1.0);"
              "v_color = a_color;}"
          )
          frag = (
              "void main() {gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);}"
          )
          n, p = 250, 50
          T = np.random.uniform(0, 2 * np.pi, n)
          position = np.zeros((n, 2), dtype=np.float32)
          position[:, 0] = np.cos(T)
          position[:, 1] = np.sin(T)
          color = np.ones((n, 4), dtype=np.float32) * (1, 1, 1, 1)
          data = np.zeros(
              n * p,
              [
                  ("a_position", np.float32, 2),
                  ("a_color", np.float32, 4),
              ],
          )
          data["a_position"] = np.repeat(position, p, axis=0)
          data["a_color"] = np.repeat(color, p, axis=0)
  
          program = Program(vert, frag)
          program.bind(VertexBuffer(data))
          program["u_model"] = np.eye(4, dtype=np.float32)
          # different codepath if no call to activate()
          program.draw(gl.GL_POINTS)
          subset = IndexBuffer(np.arange(10, dtype=np.uint32))
          program.draw(gl.GL_POINTS, subset)
  
          # bad programs
          frag_bad = "varying vec4 v_colors"  # no semicolon
          program = Program(vert, frag_bad)
          assert_raises(
              RuntimeError, program.glir.flush, context.shared.parser
          )
          frag_bad = (
              None  # no fragment code. no main is not always enough
          )
          assert_raises(ValueError, Program, vert, frag_bad)
  
          # Timer
          timer = Timer(
              interval=0.001,
              connect=on_mouse_move,
              iterations=2,
              start=True,
              app=app,
          )
          timer.start()
          timer.interval = 0.002
          assert_equal(timer.interval, 0.002)
          assert_true(timer.running)
          sleep(0.003)
          assert_true(timer.elapsed >= 0.002)
          timer.stop()
          assert_true(not timer.running)
          assert_true(timer.native)
          timer.disconnect()
  
          # test that callbacks take reasonable inputs
          _test_callbacks(canvas)
  
          # cleanup
          canvas.swap_buffers()
          canvas.update()
          app.process_events()
          # put this in even though __exit__ will call it to make sure we don't
          # have problems calling it multiple times
          canvas.close()
  
imports: import sys
  from collections import namedtuple
  from io import StringIO
  from time import sleep
  import gc
  import numpy
  from numpy.testing import assert_array_equal
  import pytest
  from vispy.app import use_app, Canvas, Timer, MouseEvent, KeyEvent
  from vispy.app.base import BaseApplicationBackend
  from vispy.testing import requires_application, SkipTest, assert_is, assert_in, run_tests_if_main, assert_equal, assert_true, assert_raises, IS_TRAVIS_CI
  from vispy.util import keys, use_log_level
  from vispy.gloo.program import Program, VertexBuffer, IndexBuffer
  from vispy.gloo.util import _screenshot
  from vispy.gloo import gl
label_desc: Use image node_id 7 for calling a global function with example usage: test_application() without return types