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
|
# Copyright 2017 Jaap Karssenberg <jaap.karssenberg@gmail.com>
import tests
from zim.plugins import find_extension, PluginManager
from zim.plugins.pathbar import *
from zim.history import History
from zim.notebook import Path
from tests.mainwindow import setUpMainWindow
class TestPluginExtendsMainWindow(tests.TestCase):
def runTest(self):
plugin = PluginManager.load_plugin('pathbar')
window = setUpMainWindow(self.setUpNotebook())
extension = find_extension(window, PathBarMainWindowExtension)
for ptype in PATHBAR_TYPES:
extension.set_pathbar(ptype)
pathbar = window._zim_window_central_vbox.get_children()[0]
if ptype == PATHBAR_NONE:
self.assertNotIsInstance(pathbar, PathBar)
else:
self.assertIsInstance(pathbar, extension._klasses[ptype])
class MyPathBar(PathBar):
def get_paths(self):
return [Path(n) for n in ['aaa', 'bbb', 'ccc']]
class TestPathBar(tests.TestCase):
def testSetPage(self):
pathbar = MyPathBar(None, None, None)
pathbar.set_page(Path('bbb'))
active = [b for b in pathbar.get_scrolled_children() if b.get_active()]
self.assertEqual(len(active), 1)
self.assertEqual(active[0].zim_path, Path('bbb'))
pathbar.set_page(Path('zzz'))
active = [b for b in pathbar.get_scrolled_children() if b.get_active()]
self.assertEqual(len(active), 0)
def testActivatePage(self):
navigation = tests.MockObject(methods=('open_page',))
pathbar = MyPathBar(None, None, navigation)
button = pathbar.get_children()[2]
button.clicked()
self.assertEqual(navigation.lastMethodCall, ('open_page', Path('bbb')))
def testContextMenu(self):
notebook = self.setUpNotebook()
navigation = tests.MockObject()
pathbar = MyPathBar(None, notebook, navigation)
button = pathbar.get_children()[2]
menu = pathbar.get_button_popup(button)
self.assertIsInstance(menu, Gtk.Menu)
def testResize(self):
pathbar = MyPathBar(None, None, None)
pathbar.show_all()
width, x = pathbar.get_preferred_width()
height, x = pathbar.get_preferred_height()
assert width > 0
assert height > 0
while width < 1000:
allocation = Gdk.Rectangle()
allocation.width = width
allocation.height = height
pathbar.size_allocate(allocation)
width += 10
def testScroll(self):
pathbar = MyPathBar(None, None, None)
pathbar.show_all()
width, x = pathbar.get_preferred_width()
height, x = pathbar.get_preferred_height()
allocation = Gdk.Rectangle()
allocation.width = width
allocation.height = height
pathbar.size_allocate(allocation)
count = 0
while pathbar.scroll(DIR_FORWARD):
pathbar.size_allocate(allocation)
count += 1
while pathbar.scroll(DIR_BACKWARD):
pathbar.size_allocate(allocation)
count += 1
while pathbar.scroll(DIR_FORWARD):
pathbar.size_allocate(allocation)
count += 1
self.assertTrue(count > 0)
def testDragAndDropCallback(self):
notebook = self.setUpNotebook()
mocktarget = tests.MockObject(return_values={'name': PAGELIST_TARGET_NAME})
mockselectiondata = tests.MockObject(return_values={'get_target': mocktarget, 'set': None})
pathbar = MyPathBar(None, notebook, None)
button = pathbar.get_children()[1]
pathbar.on_drag_data_get(button, None, mockselectiondata, None, None)
self.assertEqual(mockselectiondata.lastMethodCall, ('set', mocktarget, 8, b'testnotebook?aaa\r\n'))
self.assertEqual(zim.gui.clipboard._internal_selection_data, b'testnotebook?aaa\r\n')
class TestHistoryPathBar(tests.TestCase):
def runTest(self):
notebook = self.setUpNotebook(content=('A', 'B', 'C', 'D'))
history = History(notebook)
pathbar = HistoryPathBar(history, notebook, None)
for name in ('A', 'A', 'B', 'A', 'D', 'D'):
history.append(Path(name))
pathbar.set_page(Path(name))
self.assertEqual(
[button.zim_path
for button in pathbar.get_children()
if not isinstance(button, ScrollButton)
],
list(reversed(list(history.get_history())))
)
class TestRecentPathBar(tests.TestCase):
def runTest(self):
notebook = self.setUpNotebook(content=('A', 'B', 'C', 'D'))
history = History(notebook)
pathbar = RecentPathBar(history, notebook, None)
for name in ('A', 'A', 'B', 'A', 'D', 'D'):
history.append(Path(name))
pathbar.set_page(Path(name))
self.assertEqual(
[button.zim_path
for button in pathbar.get_children()
if not isinstance(button, ScrollButton)
],
list(reversed(list(history.get_recent())))
)
import time
class TestRecentChangesPathBar(tests.TestCase):
def runTest(self):
notebook = self.setUpNotebook()
history = History(notebook)
pathbar = RecentChangesPathBar(history, notebook, None)
for name in ('A', 'A', 'B', 'A', 'D', 'D'):
p = notebook.get_page(Path(name))
text = p.dump('wiki') # prevent etag fail
p.parse('wiki', 'test 123')
time.sleep(0.01) # ensure timestamp order ...
notebook.store_page(p)
pathbar.set_page(Path(name))
self.assertEqual(
[button.zim_path
for button in pathbar.get_children()
if not isinstance(button, ScrollButton)
],
[Path(n) for n in ('B', 'A', 'D')]
)
class TestNamespacePathBar(tests.TestCase):
def runTest(self):
notebook = self.setUpNotebook(content=('A', 'B', 'C', 'D'))
history = History(notebook)
pathbar = NamespacePathBar(history, notebook, None)
for name in ('A:A1:AA1', 'B', 'C:C1', 'D'):
history.append(Path(name))
pathbar.set_page(Path(name))
self.assertEqual(
[button.zim_path
for button in pathbar.get_children()
if not isinstance(button, ScrollButton)
],
[p for p in reversed(list(Path(name).parents())) if not p.isroot] + [Path(name)]
)
|