forked from robotframework/RIDE
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtest_updatenotifier.py
324 lines (280 loc) · 14 KB
/
test_updatenotifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# Copyright 2008-2015 Nokia Networks
# Copyright 2016- Robot Framework Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import typing
import unittest
import time
import urllib
from robotide.application.updatenotifier import UpdateNotifierController, UpdateDialog
CHECKFORUPDATES = 'check for updates'
LASTUPDATECHECK = 'last update check'
import wx
from wx.lib.agw.aui import AuiManager
import wx.lib.agw.aui as aui
from multiprocessing import shared_memory
from utest.resources import datafilereader, MessageRecordingLoadObserver
from robotide.ui.mainframe import ActionRegisterer, ToolBar
from robotide.ui.actiontriggers import MenuBar, ShortcutRegistry
from robotide.application import Project
from utest.resources import FakeSettings
from robotide.publish import PUBLISHER
from robotide.ui.treeplugin import Tree
from robotide.ui.notebook import NoteBook
from robotide.editor import texteditor
from robotide.namespace.namespace import Namespace
app = wx.App()
nb_style = aui.AUI_NB_DEFAULT_STYLE | aui.AUI_NB_WINDOWLIST_BUTTON | aui.AUI_NB_TAB_EXTERNAL_MOVE \
| aui.AUI_NB_SUB_NOTEBOOK | aui.AUI_NB_SMART_TABS
MYTESTOVERRIDE = 'My Overriding Test Teardown'
class MainFrame(wx.Frame):
book = None
def __init__(self):
wx.Frame.__init__(self, parent=None, title='Test App')
self.CreateStatusBar()
class MyApp(wx.App):
frame = None
namespace = None
project = None
settings = None
book = None
panel = None
tree = None
def __init__(self, redirect=False, filename=None, usebestvisual=False, clearsigint=True):
super().__init__(redirect, filename, usebestvisual, clearsigint)
self.actions = None
self.toolbar = None
self._mgr = None
def OnInit(self): # Overrides wx method
self.frame = MainFrame()
self.SetTopWindow(self.frame)
self.settings = FakeSettings()
# self.settings.add_section("Text Edit")
self.namespace = Namespace(self.settings)
self.book = NoteBook(self.frame, self, nb_style)
self._mgr = aui.AuiManager()
# tell AuiManager to manage this frame
self._mgr.SetManagedWindow(self.frame)
self.book.SetBackgroundColour((255, 255, 255))
self.book.SetForegroundColour((0, 0, 0))
self._mgr.AddPane(self.book,
aui.AuiPaneInfo().Name("notebook_editors").
CenterPane().PaneBorder(False))
mb = MenuBar(self.frame)
self.toolbar = ToolBar(self.frame)
self.toolbar.SetMinSize(wx.Size(100, 60))
self.toolbar.SetBackgroundColour((255, 255, 255))
self.toolbar.SetForegroundColour((0, 0, 0))
mb.m_frame.SetBackgroundColour((255, 255, 255))
mb.m_frame.SetForegroundColour((0, 0, 0))
self._mgr.AddPane(self.toolbar, aui.AuiPaneInfo().Name("maintoolbar").
ToolbarPane().Top())
self.frame.actions = ActionRegisterer(self._mgr, mb, self.toolbar, ShortcutRegistry(self.frame))
self.tree = Tree(self.frame, self.frame.actions, self.settings)
self.tree.SetMinSize(wx.Size(275, 250))
self.frame.SetMinSize(wx.Size(600, 400))
self._mgr.AddPane(self.tree,
aui.AuiPaneInfo().Name("tree_content").Caption("Test Suites").CloseButton(False).
LeftDockable())
mb.take_menu_bar_into_use()
self._mgr.Update()
return True
class UpdateNotifierTestCase(unittest.TestCase):
def setUp(self):
self._callback_called = False
self._newest_version = None
self._url = None
self.app = MyApp()
settings = self.app.settings
self.frame = self.app.frame
self.frame.actions = ActionRegisterer(AuiManager(self.frame), MenuBar(self.frame), ToolBar(self.frame),
ShortcutRegistry(self.frame))
self.frame.tree = Tree(self.frame, self.frame.actions, settings)
self.app.project = Project(self.app.namespace, self.app.settings)
self.plugin = texteditor.TextEditorPlugin(self.app)
self.app.project.load_datafile(datafilereader.SIMPLE_PROJECT, MessageRecordingLoadObserver())
self.notebook = self.app.book
self.app.tree.populate(self.app.project)
self.source = self.app.tree.controller
self.app.frame.SetStatusText("File:" + self.app.project.data.source)
# Uncomment next line (and MainLoop in tests) if you want to see the app
self.frame.Show()
def tearDown(self):
self.plugin.unsubscribe_all()
PUBLISHER.unsubscribe_all()
self.app.project.close()
wx.CallAfter(self.app.ExitMainLoop)
self.app.MainLoop() # With this here, there is no Segmentation fault
# wx.CallAfter(wx.Exit)
self.app.Destroy()
self.app = None
def _callback(self, version, url, settings, notebook):
__ = notebook
self.assertFalse(self._callback_called)
self._callback_called = True
self.assertIsNotNone(version)
self._newest_version = version
self.assertIsNotNone(url)
self._url = url
self.assertEqual(dict, type(settings))
@staticmethod
def _update_notifier_controller(settings, notebook, current, new, url='some url'):
ctrl = UpdateNotifierController(settings, notebook)
ctrl.VERSION = current
def _new():
return new
ctrl._get_newest_version = _new
def _url():
return url
ctrl._get_download_url = _url
def _null():
return None
ctrl._get_rf_pypi_data = _null
return ctrl
@staticmethod
def internal_settings(check_for_updates: typing.Union[bool, None] = True,
last_update_check: typing.Union[float, None] = time.time() - 60 * 60 * 24 * 7 - 1):
return {CHECKFORUPDATES: check_for_updates,
LASTUPDATECHECK: last_update_check}
def test_normal_update(self):
settings = self.internal_settings()
ctrl = self._update_notifier_controller(settings, self.notebook, '0', '1', 'http://xyz.abc.efg.di')
ctrl.notify_update_if_needed(self._callback)
self.assertTrue(self._callback_called)
self.assertEqual('1', self._newest_version)
self.assertEqual('http://xyz.abc.efg.di', self._url)
self.assertTrue(settings[CHECKFORUPDATES])
self.assertGreater(settings[LASTUPDATECHECK], time.time() - 1)
# Uncomment next lines if you want to see the app
# wx.CallLater(5000, self.app.ExitMainLoop)
# self.app.MainLoop()
def test_update_when_trunk_version(self):
settings = self.internal_settings()
ctrl = self._update_notifier_controller(settings, self.notebook, '2.0', '2.0.1')
ctrl.notify_update_if_needed(self._callback)
self.assertTrue(self._callback_called)
self.assertEqual('2.0.1', self._newest_version)
self.assertTrue(settings[CHECKFORUPDATES])
self.assertGreater(settings[LASTUPDATECHECK], time.time() - 1)
def test_last_update_done_less_than_a_week_ago(self):
original_time = time.time() - 60 * 60 * 24 * 3
settings = self.internal_settings(last_update_check=original_time)
ctrl = UpdateNotifierController(settings, self.notebook)
ctrl.notify_update_if_needed(self._callback)
self.assertTrue(settings[CHECKFORUPDATES])
self.assertEqual(original_time, settings[LASTUPDATECHECK])
self.assertFalse(self._callback_called)
def test_check_for_updates_is_false(self):
settings = self.internal_settings(check_for_updates=False)
original_time = settings[LASTUPDATECHECK]
ctrl = UpdateNotifierController(settings, self.notebook)
ctrl.notify_update_if_needed(self._callback)
self.assertFalse(settings[CHECKFORUPDATES])
self.assertEqual(original_time, settings[LASTUPDATECHECK])
self.assertFalse(self._callback_called)
def test_no_update_found(self):
settings = self.internal_settings()
ctrl = self._update_notifier_controller(settings, self.notebook, '0.55', '0.55')
ctrl.notify_update_if_needed(self._callback)
self.assertGreater(settings[LASTUPDATECHECK], time.time() - 1)
self.assertFalse(self._callback_called)
def test_no_update_found_dev(self):
settings = self.internal_settings()
ctrl = self._update_notifier_controller(settings, self.notebook, '0.56', '0.56')
ctrl.notify_update_if_needed(self._callback, ignore_check_condition=False, show_no_update=False)
self.assertGreater(settings[LASTUPDATECHECK], time.time() - 1)
self.assertFalse(self._callback_called)
def test_no_update_found_dev_notify(self):
settings = self.internal_settings()
ctrl = self._update_notifier_controller(settings, self.notebook, '0.55', '0.55')
ctrl.notify_update_if_needed(self._callback, ignore_check_condition=True, show_no_update=True)
self.assertFalse(self._callback_called)
def test_first_run_sets_settings_correctly_and_checks_for_updates(self):
settings = self.internal_settings(check_for_updates=None, last_update_check=None)
ctrl = self._update_notifier_controller(settings, self.notebook,'1.0.2', '1.0.2')
ctrl.notify_update_if_needed(self._callback)
self.assertGreater(settings[LASTUPDATECHECK], time.time() - 1)
self.assertTrue(settings[CHECKFORUPDATES])
self.assertFalse(self._callback_called)
def test_first_run_sets_settings_correctly_and_finds_an_update(self):
settings = self.internal_settings(check_for_updates=None, last_update_check=None)
ctrl = self._update_notifier_controller(settings, self.notebook, '1.2', '2.0')
ctrl.notify_update_if_needed(self._callback)
self.assertGreater(settings[LASTUPDATECHECK], time.time() - 1)
self.assertTrue(settings[CHECKFORUPDATES])
self.assertTrue(self._callback_called)
def test_checking_timeouts(self):
settings = self.internal_settings()
ctrl = UpdateNotifierController(settings, self.notebook)
def throw_timeout_error():
raise urllib.error.URLError('timeout')
ctrl._get_newest_version = throw_timeout_error
ctrl.notify_update_if_needed(self._callback)
self.assertGreater(settings[LASTUPDATECHECK], time.time() - 10) # The dialog timeout in 10 seconds
self.assertFalse(self._callback_called)
def test_download_url_checking_timeouts(self):
settings = self.internal_settings()
ctrl = UpdateNotifierController(settings, self.notebook)
ctrl.VERSION = '0'
ctrl._get_newest_version = lambda: '1'
def throw_timeout_error(*args):
_ = args
raise urllib.error.URLError('timeout')
ctrl._get_download_url = throw_timeout_error
ctrl.notify_update_if_needed(self._callback)
self.assertGreater(settings[LASTUPDATECHECK], time.time() - 1)
self.assertFalse(self._callback_called)
def test_server_returns_no_versions(self):
settings = self.internal_settings()
ctrl = self._update_notifier_controller(settings, self.notebook, '1.2.2', None)
ctrl.notify_update_if_needed(self._callback)
self.assertGreater(settings[LASTUPDATECHECK], time.time() - 1)
self.assertTrue(settings[CHECKFORUPDATES])
self.assertFalse(self._callback_called)
def test_server_returns_older_version(self):
settings = self.internal_settings()
ctrl = self._update_notifier_controller(settings, self.notebook, '0.44', '0.43.1')
ctrl.notify_update_if_needed(self._callback)
self.assertGreater(settings[LASTUPDATECHECK], time.time() - 1)
self.assertTrue(settings[CHECKFORUPDATES])
self.assertFalse(self._callback_called)
def test_forced_check_released(self):
settings = self.internal_settings()
ctrl = self._update_notifier_controller(settings, self.notebook, '0.43.0', '0.43.1')
ctrl.notify_update_if_needed(self._callback, ignore_check_condition=True)
self.assertGreater(settings[LASTUPDATECHECK], time.time() - 19) # The dialog timeout in 20 seconds
self.assertTrue(settings[CHECKFORUPDATES])
self.assertTrue(self._callback_called)
def test_forced_check_development(self):
settings = self.internal_settings()
ctrl = self._update_notifier_controller(settings, self.notebook, '0.44dev12', '0.44.dev14')
ctrl.notify_update_if_needed(self._callback, ignore_check_condition=True)
self.assertGreater(settings[LASTUPDATECHECK], time.time() - 20) # The dialog timeout in 20 seconds
self.assertTrue(settings[CHECKFORUPDATES])
self.assertTrue(self._callback_called)
def test_forced_check_development_ok(self):
settings = self.internal_settings()
ctrl = self._update_notifier_controller(settings, self.notebook, '0.44dev12', '0.44.dev12')
ctrl.notify_update_if_needed(self._callback, ignore_check_condition=False)
self.assertGreater(settings[LASTUPDATECHECK], time.time() - 20) # The dialog timeout in 20 seconds
self.assertTrue(settings[CHECKFORUPDATES])
self.assertFalse(self._callback_called)
def test_normal_update_dialog(self):
""" This is not actually doing a test """
settings = self.internal_settings()
ctrl=UpdateDialog('1.0.0', 'http://localhost', settings, self.notebook,False)
wx.CallLater(3000, ctrl.EndModal,wx.CANCEL)
ctrl.ShowModal()
ctrl.Destroy()
if __name__ == '__main__':
unittest.main()