1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import unittest
6
7# pylint: disable=F0401
8import mojo.embedder
9from mojo import system
10
11
12def _Increment(array):
13  def _Closure():
14    array.append(0)
15  return _Closure
16
17
18class RunLoopTest(unittest.TestCase):
19
20  def setUp(self):
21    mojo.embedder.Init()
22
23  def testRunLoop(self):
24    loop = system.RunLoop()
25    array = []
26    for _ in xrange(10):
27      loop.PostDelayedTask(_Increment(array))
28    loop.RunUntilIdle()
29    self.assertEquals(len(array), 10)
30
31  def testRunLoopWithException(self):
32    loop = system.RunLoop()
33    def Throw():
34      raise Exception("error")
35    array = []
36    loop.PostDelayedTask(Throw)
37    loop.PostDelayedTask(_Increment(array))
38    with self.assertRaisesRegexp(Exception, '^error$'):
39      loop.Run()
40    self.assertEquals(len(array), 0)
41    loop.RunUntilIdle()
42    self.assertEquals(len(array), 1)
43