OLD | NEW |
| (Empty) |
1 # Copyright (c) 2012 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 import os | |
5 import shutil | |
6 import tempfile | |
7 import unittest | |
8 | |
9 import chromeapp | |
10 | |
11 unittest_data_dir = os.path.relpath( | |
12 os.path.join( | |
13 os.path.dirname(__file__), | |
14 'unittest_data')) | |
15 | |
16 class TrackingAppInstance(chromeapp.AppInstance): | |
17 def __init__(self, *args): | |
18 super(TrackingAppInstance, self).__init__(*args) | |
19 self.did_install = False | |
20 | |
21 def _Install(self, *args): | |
22 self.did_install = True | |
23 return super(TrackingAppInstance, self)._Install(*args) | |
24 | |
25 class AppTest(unittest.TestCase): | |
26 def setUp(self): | |
27 self._profiles_dir = tempfile.mkdtemp() | |
28 | |
29 def tearDown(self): | |
30 shutil.rmtree(self._profiles_dir, ignore_errors=True) | |
31 | |
32 def testBasic(self): | |
33 manifest_file = os.path.join(unittest_data_dir, | |
34 'simple-working-app', 'manifest.json') | |
35 app = chromeapp.App('simple-working-app', | |
36 manifest_file, | |
37 chromeapp_profiles_dir=self._profiles_dir) | |
38 with TrackingAppInstance(app, ['hello']) as app_instance: | |
39 ret = app_instance.Run() | |
40 self.assertEquals(ret, 42) | |
41 | |
42 def testLaunchAndLaunchAgain(self): | |
43 manifest_file = os.path.join(unittest_data_dir, | |
44 'simple-working-app', 'manifest.json') | |
45 app = chromeapp.App('simple-working-app', | |
46 manifest_file, | |
47 chromeapp_profiles_dir=self._profiles_dir) | |
48 with TrackingAppInstance(app, ['hello']) as app_instance: | |
49 ret = app_instance.Run() | |
50 self.assertEquals(ret, 42) | |
51 | |
52 with TrackingAppInstance(app, ['hello']) as app_instance: | |
53 ret = app_instance.Run() | |
54 assert app_instance.did_install == False | |
55 self.assertEquals(ret, 42) | |
56 | |
57 def testAppSideUncaughtErrorObject(self): | |
58 manifest_file = os.path.join(unittest_data_dir, | |
59 'intentionally-failing-app', 'manifest.json') | |
60 app = chromeapp.App('intentionally-failing-app', | |
61 manifest_file, | |
62 chromeapp_profiles_dir=self._profiles_dir) | |
63 test = self | |
64 class MyAppInstance(chromeapp.AppInstance): | |
65 def _OnUncaughtError(self, error): | |
66 try: | |
67 test.assertEquals(error['error'], 'Uncaught Error: intentional failure
') | |
68 finally: | |
69 self.ExitRunLoop(0) | |
70 with MyAppInstance(app, '--throw-error-object') as app_instance: | |
71 ret = app_instance.Run() | |
72 | |
73 def testAppThatPrints(self): | |
74 manifest_file = os.path.join(unittest_data_dir, | |
75 'app-that-prints', 'manifest.json') | |
76 app = chromeapp.App('app-that-prints', | |
77 manifest_file, | |
78 chromeapp_profiles_dir=self._profiles_dir) | |
79 test = self | |
80 class MyAppInstance(chromeapp.AppInstance): | |
81 def _OnPrint(self, contents): | |
82 try: | |
83 test.assertEquals(len(contents), 1) | |
84 test.assertEquals(contents[0], 'Hello world') | |
85 finally: | |
86 self.ExitRunLoop(0) | |
87 with MyAppInstance(app) as app_instance: | |
88 ret = app_instance.Run() | |
89 | |
90 def testAppThatSendsEvent(self): | |
91 manifest_file = os.path.join(unittest_data_dir, | |
92 'app-that-sends-event', 'manifest.json') | |
93 app = chromeapp.App('app-that-sends-event', | |
94 manifest_file, | |
95 chromeapp_profiles_dir=self._profiles_dir) | |
96 got_event = [False] | |
97 def OnEvent(args): | |
98 arg1, arg2 = args | |
99 self.assertEquals(arg1, [1, 2, 3]) | |
100 self.assertEquals(arg2, True) | |
101 got_event[0] = True | |
102 return [314, 'hi'] | |
103 | |
104 with chromeapp.AppInstance(app) as app_instance: | |
105 self.assertFalse(app_instance.HasListener('hello-world', OnEvent)) | |
106 app_instance.AddListener('hello-world', OnEvent) | |
107 self.assertTrue(app_instance.HasListener('hello-world', OnEvent)) | |
108 ret = app_instance.Run() | |
109 self.assertEquals(0, ret) | |
110 app_instance.RemoveListener('hello-world', OnEvent) | |
111 self.assertTrue(got_event[0]) | |
OLD | NEW |