Commit | Line | Data |
---|---|---|
73005152 BH |
1 | /********************************************************************** |
2 | * Copyright (c) 2011 Ericsson. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
abbdd66a AM |
7 | * |
8 | * Contributors: | |
73005152 BH |
9 | * Bernd Hufmann - Initial API and implementation |
10 | **********************************************************************/ | |
11 | package org.eclipse.linuxtools.tmf.ui.tests.views.uml2sd.load; | |
12 | ||
13 | import junit.framework.TestCase; | |
14 | ||
15 | import org.eclipse.jface.preference.IPreferenceStore; | |
8fd82db5 | 16 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
73005152 BH |
17 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDView; |
18 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.load.IUml2SDLoader; | |
19 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.load.LoadersManager; | |
20 | import org.eclipse.swt.widgets.Display; | |
21 | import org.eclipse.ui.PlatformUI; | |
22 | ||
23 | public class LoadersManagerTest extends TestCase { | |
24 | ||
25 | private final static String SDVIEW_WITH_ONE_LOADER = "org.eclipse.linuxtools.tmf.ui.tests.testSDView1Loader"; //$NON-NLS-1$ | |
26 | private final static String SDVIEW_WITH_MULTIPLE_LOADER = "org.eclipse.linuxtools.tmf.ui.tests.testSDView2Loaders"; //$NON-NLS-1$ | |
27 | private final static String TEST_LOADER_CLASS_NAME = "org.eclipse.linuxtools.tmf.ui.tests.uml2sd.load.TestLoaders"; //$NON-NLS-1$ | |
df0b8ff4 | 28 | private final static String TMF_UML2SD_LOADER_CLASS_NAME = "org.eclipse.linuxtools.tmf.ui.views.uml2sd.loader.TmfUml2SDSyncLoader"; //$NON-NLS-1$ |
abbdd66a | 29 | |
73005152 BH |
30 | private static final String LOADER_TAG = "uml2SDLoader"; //$NON-NLS-1$ |
31 | private static final String LOADER_PREFIX = LOADER_TAG + "."; //$NON-NLS-1$ | |
32 | ||
33 | @Override | |
34 | public void setUp() throws Exception { | |
35 | super.setUp(); | |
36 | } | |
37 | ||
38 | @Override | |
39 | public void tearDown() throws Exception { | |
40 | super.tearDown(); | |
41 | } | |
42 | ||
43 | @SuppressWarnings("nls") | |
44 | public void testLoaderManager() { | |
45 | ||
46 | SDView view = null; | |
47 | try { | |
48 | ||
49 | /* | |
abbdd66a | 50 | * Test creation of a loader (one per SD view) |
73005152 | 51 | */ |
abbdd66a | 52 | |
73005152 BH |
53 | // Open view |
54 | // Note this will create the default loader! | |
55 | view = (SDView)PlatformUI.getWorkbench() | |
56 | .getActiveWorkbenchWindow() | |
57 | .getActivePage() | |
58 | .showView(SDVIEW_WITH_ONE_LOADER); | |
59 | ||
60 | IUml2SDLoader defaultLoader = LoadersManager.getInstance().getCurrentLoader(SDVIEW_WITH_ONE_LOADER, view); | |
61 | assertNotNull("testLoaderManager", defaultLoader); | |
abbdd66a | 62 | |
73005152 BH |
63 | // Test createLoader where loader doesn't exist |
64 | assertNull("testLoaderManager", LoadersManager.getInstance().createLoader("blabla", view)); | |
65 | ||
abbdd66a | 66 | // Test createLoader |
73005152 BH |
67 | IUml2SDLoader loader = LoadersManager.getInstance().createLoader(TEST_LOADER_CLASS_NAME, view); |
68 | ||
69 | assertNotNull("testLoaderManager", loader); | |
abbdd66a | 70 | assertEquals("testLoaderManager", "Test Loader", loader.getTitleString()); |
73005152 | 71 | assertEquals("testLoaderManager", loader, LoadersManager.getInstance().getCurrentLoader(SDVIEW_WITH_ONE_LOADER)); |
abbdd66a AM |
72 | |
73 | // compare loader and default loader. Even if they are the same class, they are different instances | |
73005152 BH |
74 | assertFalse("testLoaderManager", loader==defaultLoader); |
75 | ||
76 | // test getCurrentLoader(viewId, view) | |
77 | IUml2SDLoader loader2 = LoadersManager.getInstance().getCurrentLoader(SDVIEW_WITH_ONE_LOADER, view); | |
78 | assertEquals("testLoaderManager", loader, loader2); | |
79 | ||
80 | // Hide the view | |
81 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(view); | |
82 | ||
83 | // test that view restores the previous associated loader | |
84 | view = (SDView)PlatformUI.getWorkbench() | |
85 | .getActiveWorkbenchWindow() | |
86 | .getActivePage() | |
87 | .showView(SDVIEW_WITH_ONE_LOADER); | |
88 | ||
89 | // Well, this is the only way test which loader is set | |
90 | assertEquals("testLoaderManager", "Sequence Diagram - First Page", view.getFrame().getName()); | |
abbdd66a | 91 | |
73005152 BH |
92 | // Test view == null |
93 | assertNull("testLoaderManager", LoadersManager.getInstance().createLoader(TEST_LOADER_CLASS_NAME, null)); | |
94 | ||
95 | // Hide the view | |
96 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(view); | |
97 | ||
98 | /* | |
abbdd66a | 99 | * Test creation of loaders with re-uses the same SD view |
73005152 BH |
100 | */ |
101 | ||
102 | // test that view restores the previous associated loader | |
103 | view = (SDView)PlatformUI.getWorkbench() | |
104 | .getActiveWorkbenchWindow() | |
105 | .getActivePage() | |
106 | .showView(SDVIEW_WITH_MULTIPLE_LOADER); | |
107 | ||
108 | // Test that default loader is set. Note that 2 default loaders are define in the plugin.xml and the | |
109 | // the first one should be selected. | |
110 | ||
111 | // Well, this is the only way test which loader is set | |
112 | assertEquals("testLoaderManager", "Sequence Diagram - First Page", view.getFrame().getName()); | |
113 | ||
114 | loader = LoadersManager.getInstance().getCurrentLoader(SDVIEW_WITH_MULTIPLE_LOADER, view); | |
115 | assertNotNull("testLoaderManager", loader); | |
abbdd66a | 116 | assertEquals("testLoaderManager", "Test Loader", loader.getTitleString()); |
73005152 BH |
117 | assertEquals("testLoaderManager", loader, LoadersManager.getInstance().getCurrentLoader(SDVIEW_WITH_MULTIPLE_LOADER)); |
118 | ||
abbdd66a | 119 | // Test createLoader for loader with class name TMF_UML2SD_LOADER_CLASS_NAME |
73005152 BH |
120 | loader = LoadersManager.getInstance().createLoader(TMF_UML2SD_LOADER_CLASS_NAME, view); |
121 | ||
122 | assertNotNull("testLoaderManager", loader); | |
abbdd66a | 123 | assertEquals("testLoaderManager", "Component Interactions", loader.getTitleString()); |
73005152 BH |
124 | assertEquals("testLoaderManager", loader, LoadersManager.getInstance().getCurrentLoader(SDVIEW_WITH_MULTIPLE_LOADER)); |
125 | ||
126 | // Verify if the correct loader is stored in the preferences as the current loader for this view | |
127 | assertEquals("testLoaderManager", TMF_UML2SD_LOADER_CLASS_NAME, getSavedLoader(SDVIEW_WITH_MULTIPLE_LOADER)); | |
128 | ||
abbdd66a | 129 | // Test createLoader for loader with class name TEST_LOADER_CLASS_NAME |
73005152 BH |
130 | loader = LoadersManager.getInstance().createLoader(TEST_LOADER_CLASS_NAME, view); |
131 | ||
132 | assertNotNull("testLoaderManager", loader); | |
abbdd66a | 133 | assertEquals("testLoaderManager", "Test Loader", loader.getTitleString()); |
73005152 BH |
134 | assertEquals("testLoaderManager", loader, LoadersManager.getInstance().getCurrentLoader(SDVIEW_WITH_MULTIPLE_LOADER)); |
135 | ||
136 | // Verify if the correct loader is stored in the preferences as the current loader for this view | |
137 | assertEquals("testLoaderManager", TEST_LOADER_CLASS_NAME, getSavedLoader(SDVIEW_WITH_MULTIPLE_LOADER)); | |
138 | assertEquals("testLoaderManager", TEST_LOADER_CLASS_NAME, LoadersManager.getInstance().getSavedLoader(SDVIEW_WITH_MULTIPLE_LOADER)); | |
139 | ||
140 | // Hide the view | |
141 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(view); | |
142 | ||
143 | // Test reset loader | |
144 | LoadersManager.getInstance().resetLoader(SDVIEW_WITH_MULTIPLE_LOADER); | |
145 | ||
146 | } catch (Exception e) { | |
147 | e.printStackTrace(); | |
148 | fail(); | |
149 | } | |
150 | } | |
151 | ||
152 | @SuppressWarnings("unused") | |
abbdd66a | 153 | private static void delay(long waitTimeMillis) { |
73005152 BH |
154 | Display display = Display.getCurrent(); |
155 | if (display != null) { | |
156 | long endTimeMillis = System.currentTimeMillis() + waitTimeMillis; | |
157 | while(System.currentTimeMillis() < endTimeMillis) { | |
158 | if (!display.readAndDispatch()) { | |
159 | display.sleep(); | |
160 | } | |
161 | display.update(); | |
162 | } | |
163 | } else { | |
164 | try { | |
165 | Thread.sleep(waitTimeMillis); | |
166 | } catch (InterruptedException e) { | |
167 | // Ignored | |
168 | } | |
169 | } | |
170 | } | |
171 | ||
abbdd66a | 172 | private static String getSavedLoader(String viewId) { |
8fd82db5 | 173 | IPreferenceStore p = Activator.getDefault().getPreferenceStore(); |
73005152 BH |
174 | return p.getString(LOADER_PREFIX + viewId); |
175 | } | |
176 | } |