Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d19f4a9844602f2759bd527ebff7c6f01abc0ad9 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2008, 2016 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.ua.tests.help.preferences;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Observable;
import java.util.Observer;

import org.eclipse.help.IHelpResource;
import org.eclipse.help.internal.base.BookmarkManager;
import org.eclipse.help.internal.base.BookmarkManager.BookmarkEvent;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/*
 * Test the BookmarkManager
 */
public class BookmarksTest {

	private static final String ECLIPSE = "eclipse";
	private static final String HTTP_ECLIPSE = "http://www.eclipse.org";
	private static final String HELP = "help";
	private static final String HTTP_HELP = "http://help.eclipse.org";
	private static final String BUGZILLA = "bugzilla";
	private static final String HTTP_BUGZILLA = "https://bugs.eclipse.org/bugs/";

	private class BookmarkObserver implements Observer {

		public Object o;
		public Object arg;
		public int eventCount = 0;

		@Override
		public void update(Observable o, Object arg) {
			++eventCount;
			this.o = o;
			this.arg = arg;
		}

		public BookmarkManager.BookmarkEvent getEvent() {
			return (BookmarkEvent) arg;
		}

	}

	private BookmarkManager manager;
	private BookmarkObserver observer;

	@Before
	public void setUp() throws Exception {
		manager = new BookmarkManager();
		manager.removeAllBookmarks();
		observer = new BookmarkObserver();
		manager.addObserver(observer);
	}

	@After
	public void tearDown() throws Exception {
		manager = null;
		observer = null;
	}

	@Test
	public void testRemoveAll() {
		manager.removeAllBookmarks();
		assertEquals(1, observer.eventCount);
		assertEquals(manager, observer.o);
		assertEquals(BookmarkManager.REMOVE_ALL, observer.getEvent().getType());
		assertNull(observer.getEvent().getBookmark());
		IHelpResource[] bookmarks = manager.getBookmarks();
		assertEquals(0, bookmarks.length);
		assertEquals(1, observer.eventCount);
	}

	@Test
	public void testAddBookmarks() {
		manager.addBookmark(HTTP_ECLIPSE, ECLIPSE);
		assertEquals(1, observer.eventCount);
		assertTrue(observer.arg instanceof BookmarkManager.BookmarkEvent);
		BookmarkManager.BookmarkEvent event = observer.getEvent();
		assertEquals(event.getType(), BookmarkManager.ADD);
		assertEquals(ECLIPSE, event.getBookmark().getLabel());
		assertEquals(HTTP_ECLIPSE, event.getBookmark().getHref());
		manager.addBookmark(HTTP_BUGZILLA, BUGZILLA);
		BookmarkManager manager2 = new BookmarkManager();
		IHelpResource[] bookmarks = manager2.getBookmarks();
		assertEquals(2, bookmarks.length);
		assertEquals(ECLIPSE, bookmarks[0].getLabel());
		assertEquals(BUGZILLA, bookmarks[1].getLabel());
		assertEquals(HTTP_ECLIPSE, bookmarks[0].getHref());
		assertEquals(HTTP_BUGZILLA, bookmarks[1].getHref());
		assertEquals(2, observer.eventCount);
		assertEquals(manager, observer.o);
	}

	@Test
	public void testRemoveBookmarks() {
		manager.addBookmark(HTTP_ECLIPSE, ECLIPSE);
		assertEquals(1, observer.eventCount);
		manager.addBookmark(HTTP_BUGZILLA, BUGZILLA);
		manager.addBookmark(HTTP_HELP, HELP);
		assertEquals(3, observer.eventCount);
		manager.removeBookmark(HTTP_ECLIPSE, BUGZILLA);  // No such bookmark
		assertEquals(observer.eventCount, 3);
		manager.removeBookmark(HTTP_ECLIPSE, ECLIPSE);
		assertEquals(4, observer.eventCount);
		BookmarkManager.BookmarkEvent event = observer.getEvent();
		assertEquals(event.getType(), BookmarkManager.REMOVE);
		assertEquals(ECLIPSE, event.getBookmark().getLabel());
		assertEquals(HTTP_ECLIPSE, event.getBookmark().getHref());
		assertEquals(manager, observer.o);
		manager.removeBookmark(new BookmarkManager.Bookmark(BUGZILLA, HTTP_BUGZILLA));
		assertEquals(5, observer.eventCount);
		event = observer.getEvent();
		assertEquals(event.getType(), BookmarkManager.REMOVE);
		assertEquals(BUGZILLA, event.getBookmark().getLabel());
		assertEquals(HTTP_BUGZILLA, event.getBookmark().getHref());
		assertEquals(1, manager.getBookmarks().length);
	}
}

Back to the top