Merge branch 'master' into lttng_2_0_control_dev
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui.tests / src / org / eclipse / linuxtools / lttng / ui / tests / control / model / component / TraceControlComponentTest.java
1 /**********************************************************************
2 * Copyright (c) 2012 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12 package org.eclipse.linuxtools.lttng.ui.tests.control.model.component;
13
14 import java.util.LinkedList;
15 import java.util.List;
16
17 import junit.framework.Test;
18 import junit.framework.TestCase;
19 import junit.framework.TestSuite;
20
21 import org.eclipse.linuxtools.internal.lttng.ui.views.control.ControlView;
22 import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.ITraceControlComponent;
23 import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.ITraceControlComponentChangedListener;
24 import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.TargetNodeState;
25 import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.impl.TraceControlComponent;
26 import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.impl.TraceControlRoot;
27 import org.eclipse.linuxtools.internal.lttng.ui.views.control.service.ILttngControlService;
28 import org.eclipse.linuxtools.internal.lttng.ui.views.control.service.LTTngControlService;
29 import org.eclipse.linuxtools.lttng.stubs.service.TestRemoteSystemProxy;
30 import org.eclipse.linuxtools.lttng.ui.tests.control.model.impl.ListenerValidator;
31 import org.eclipse.swt.graphics.Image;
32 import org.eclipse.ui.ISharedImages;
33 import org.eclipse.ui.PlatformUI;
34 import org.junit.After;
35 import org.junit.Before;
36
37 /**
38 * The class <code>TraceControlComponentTest</code> contains tests for the class <code>{@link TraceControlComponent}</code>.
39 *
40 */
41 @SuppressWarnings("nls")
42 public class TraceControlComponentTest extends TestCase {
43
44 // ------------------------------------------------------------------------
45 // Test data
46 // ------------------------------------------------------------------------
47
48 // ------------------------------------------------------------------------
49 // Static methods
50 // ------------------------------------------------------------------------
51
52 /**
53 * Returns test setup used when executing test case stand-alone.
54 * @return Test setup class
55 */
56 public static Test suite() {
57 return new ModelImplTestSetup(new TestSuite(TraceControlComponentTest.class));
58 }
59
60 // ------------------------------------------------------------------------
61 // Housekeeping
62 // ------------------------------------------------------------------------
63
64 /**
65 * Perform pre-test initialization.
66 *
67 * @throws Exception
68 * if the initialization fails for some reason
69 *
70 */
71 @Override
72 @Before
73 public void setUp() throws Exception {
74 }
75
76 /**
77 * Perform post-test clean-up.
78 *
79 * @throws Exception
80 * if the clean-up fails for some reason
81 *
82 */
83 @Override
84 @After
85 public void tearDown() throws Exception {
86 }
87
88 /**
89 * Run the TraceControlComponent(String) constructor test.
90 */
91 public void testTraceControlComponent_1()
92 throws Exception {
93
94 String name = "node";
95
96 TraceControlComponent result = new TraceControlComponent(name);
97
98 assertNotNull(result);
99 assertEquals(name, result.getName());
100 assertEquals(null, result.getParent());
101 assertEquals(false, result.hasChildren());
102 assertEquals(null, result.getImage());
103 assertEquals(null, result.getControlService());
104 assertEquals(null, result.getToolTip());
105 }
106
107 /**
108 * Run the TraceControlComponent(String,ITraceControlComponent) constructor test.
109 *
110 */
111 public void testTraceControlComponent_2()
112 throws Exception {
113 String name = "node";
114
115 ITraceControlComponent parent = new TraceControlRoot();
116 TraceControlComponent result = new TraceControlComponent(name, parent);
117
118 assertNotNull(result);
119 assertEquals(name, result.getName());
120 assertEquals(false, result.hasChildren());
121 assertEquals(null, result.getImage());
122 assertEquals(null, result.getControlService());
123 assertEquals(null, result.getToolTip());
124 }
125
126 /**
127 * Run the void addChild(ITraceControlComponent) method test.
128 *
129 * @throws Exception
130 *
131 */
132 public void testAddAndGetChild1()
133 throws Exception {
134 TraceControlComponent fixture = new TraceControlComponent("node", new TraceControlRoot());
135 fixture.setToolTip("This is the test node");
136 fixture.addChild(new TraceControlRoot());
137 ITraceControlComponent component = new TraceControlRoot();
138 fixture.addChild(component);
139
140 ITraceControlComponent child = fixture.getChild(TraceControlRoot.TRACE_CONTROL_ROOT_NAME);
141 assertNotNull(child);
142 assertEquals(TraceControlRoot.TRACE_CONTROL_ROOT_NAME, child.getName());
143 }
144
145 /**
146 * Run the void addChild(ITraceControlComponent) method test.
147 *
148 * @throws Exception
149 *
150 */
151 public void testAddAndGetChild2()
152 throws Exception {
153 TraceControlComponent fixture = new TraceControlComponent("", new TraceControlRoot());
154 fixture.setToolTip("");
155 ITraceControlComponent component = null;
156
157 fixture.addChild(component);
158 assertFalse(fixture.hasChildren());
159 }
160
161 /**
162 * Run the void addComponentListener(ITraceControlComponentChangedListener) method test.
163 *
164 * @throws Exception
165 *
166 */
167
168 public void testAddComponentListener_1()
169 throws Exception {
170 TraceControlComponent fixture = new TraceControlComponent("", (ITraceControlComponent) null);
171 fixture.setToolTip("");
172
173 ListenerValidator validator = new ListenerValidator();
174 fixture.addComponentListener(validator);
175
176 TraceControlRoot root = new TraceControlRoot();
177 fixture.addChild(root);
178 assertTrue(validator.isAddedCalled());
179
180 fixture.removeChild(root);
181 assertTrue(validator.isRemovedCalled());
182
183 fixture.fireComponentChanged(fixture);
184 assertTrue(validator.isChangedCalled());
185 }
186
187 /**
188 * Run the boolean containsChild(String) method test.
189 *
190 * @throws Exception
191 *
192 */
193 public void testContainsChild_1()
194 throws Exception {
195 TraceControlComponent fixture = new TraceControlComponent("", new TraceControlRoot());
196 fixture.setToolTip("");
197 fixture.addChild(new TraceControlRoot());
198 String name = "node";
199
200 boolean result = fixture.containsChild(name);
201
202 assertEquals(false, result);
203 }
204
205 /**
206 * Run the boolean containsChild(String) method test.
207 *
208 * @throws Exception
209 *
210 */
211 public void testContainsChild_2()
212 throws Exception {
213 TraceControlComponent fixture = new TraceControlComponent("name", new TraceControlRoot());
214 fixture.setToolTip("");
215
216 boolean result = fixture.containsChild(TraceControlRoot.TRACE_CONTROL_ROOT_NAME);
217
218 assertEquals(false, result);
219 }
220
221 /**
222 * Run the void fireCompenentAdded(ITraceControlComponent,ITraceControlComponent) method test.
223 * Run the void fireCompenentRemoved(ITraceControlComponent,ITraceControlComponent) method test.
224 * Run the void fireCompenentChanged(ITraceControlComponent) method test
225 *
226 * @throws Exception
227 *
228 */
229
230 public void testFireCompenentUpdated()
231 throws Exception {
232 ITraceControlComponent parent = new TraceControlRoot();
233
234 TraceControlComponent fixture = new TraceControlComponent("node", parent);
235 fixture.setToolTip("");
236
237 ITraceControlComponent component = new TraceControlComponent("child");
238 fixture.addChild(component);
239
240 ListenerValidator validator = new ListenerValidator();
241 fixture.addComponentListener(validator);
242
243 fixture.fireComponentAdded(parent, component);
244 assertTrue(validator.isAddedCalled());
245 assertEquals(parent.getName(), validator.getSavedParent().getName());
246 assertEquals(component.getName(), validator.getSavedChild().getName());
247
248 validator.initialize();
249
250 fixture.fireComponentRemoved(parent, component);
251 assertTrue(validator.isRemovedCalled());
252 assertEquals(parent.getName(), validator.getSavedParent().getName());
253 assertEquals(component.getName(), validator.getSavedChild().getName());
254
255 validator.initialize();
256 fixture.fireComponentChanged(fixture);
257 assertTrue(validator.isChangedCalled());
258 assertEquals(fixture.getName(), validator.getSavedComponent().getName());
259 }
260
261 /**
262 * Run the Object getAdapter(Class) method test.
263 *
264 * @throws Exception
265 *
266 */
267
268 public void testGetAdapter()
269 throws Exception {
270 TraceControlComponent fixture = new TraceControlComponent("", new TraceControlRoot());
271 fixture.setToolTip("");
272 fixture.addChild(new TraceControlRoot());
273 Class<Object> adapter = Object.class;
274
275 Object result = fixture.getAdapter(adapter);
276
277 assertEquals(null, result);
278 }
279
280 /**
281 * Run the ITraceControlComponent[] getChildren() method test.
282 *
283 * @throws Exception
284 *
285 */
286 public void testGetChildren_1()
287 throws Exception {
288 TraceControlComponent fixture = new TraceControlComponent("", new TraceControlRoot());
289 fixture.setToolTip("");
290 fixture.addChild(new TraceControlRoot());
291
292 ITraceControlComponent[] result = fixture.getChildren();
293
294 assertNotNull(result);
295 assertEquals(1, result.length);
296 assertNotNull(result[0]);
297 assertEquals("trace_control_root", result[0].getName());
298 assertEquals(null, result[0].getParent());
299 assertEquals(false, result[0].hasChildren());
300 assertEquals(null, result[0].getImage());
301 assertEquals(null, result[0].getControlService());
302 assertEquals(null, result[0].getToolTip());
303 }
304
305 /**
306 * Run the ILttngControlService getControlService()/setControlService() method test.
307 *
308 * @throws Exception
309 *
310 */
311 public void testGetAndSetControlService_1()
312 throws Exception {
313
314 TraceControlComponent parent = new TraceControlComponent("parent") {
315 ILttngControlService fService = null;
316
317 @Override
318 public void setControlService(ILttngControlService service ) {
319 fService = service;
320 }
321
322 @Override
323 public ILttngControlService getControlService() {
324 return fService;
325 }
326 };
327
328 TraceControlComponent fixture = new TraceControlComponent("", parent);
329 parent.addChild(fixture);
330 fixture.setToolTip("");
331 TraceControlComponent child = new TraceControlComponent("child", fixture);
332 fixture.addChild(child);
333
334 ILttngControlService result = fixture.getControlService();
335 assertEquals(null, result);
336
337 TestRemoteSystemProxy proxy = new TestRemoteSystemProxy();
338 ILttngControlService service = new LTTngControlService(proxy.createCommandShell());
339 fixture.setControlService(service);
340 result = fixture.getControlService();
341 assertNotNull(service);
342 assertEquals(service, result);
343
344 result = fixture.getChildren()[0].getControlService();
345 assertNotNull(service);
346 assertEquals(service, result);
347 }
348
349 /**
350 * Run the Image getImage() method test.
351 *
352 * @throws Exception
353 *
354 */
355 public void testGetImage_1()
356 throws Exception {
357 TraceControlComponent fixture = new TraceControlComponent("", new TraceControlRoot());
358 fixture.setToolTip("");
359 fixture.addChild(new TraceControlRoot());
360
361 Image result = fixture.getImage();
362 assertEquals(null, result);
363
364 fixture.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER));
365 assertNotNull(fixture.getImage());
366 }
367
368 /**
369 * Run the boolean hasChildren() method test.
370 *
371 * @throws Exception
372 *
373 */
374 public void testHasChildren_1()
375 throws Exception {
376 TraceControlComponent fixture = new TraceControlComponent("", new TraceControlRoot());
377 fixture.setToolTip("");
378 fixture.addChild(new TraceControlRoot());
379
380 boolean result = fixture.hasChildren();
381
382 assertTrue(result);
383 }
384
385 /**
386 * Run the boolean hasChildren() method test.
387 *
388 * @throws Exception
389 *
390 */
391 public void testHasChildren_2()
392 throws Exception {
393 TraceControlComponent fixture = new TraceControlComponent("", new TraceControlRoot());
394 fixture.setToolTip("");
395
396 boolean result = fixture.hasChildren();
397
398 assertFalse(result);
399 }
400
401 /**
402 * Run the void removeAllChildren() method test.
403 *
404 * @throws Exception
405 *
406 */
407 public void testRemoveAllChildren_2()
408 throws Exception {
409 TraceControlComponent fixture = new TraceControlComponent("", new TraceControlRoot());
410 fixture.setToolTip("");
411
412 fixture.addChild(new TraceControlRoot());
413 fixture.addChild(new TraceControlComponent("child"));
414
415 fixture.removeAllChildren();
416 assertFalse(fixture.hasChildren());
417 }
418
419 /**
420 * Run the void removeChild(ITraceControlComponent) method test.
421 *
422 * @throws Exception
423 *
424 */
425 public void testRemoveChild_1()
426 throws Exception {
427 TraceControlComponent fixture = new TraceControlComponent("", new TraceControlRoot());
428 fixture.setToolTip("");
429 TraceControlComponent child = new TraceControlComponent("child", fixture);
430
431 fixture.addChild(child);
432 fixture.removeChild(child);
433 assertFalse(fixture.hasChildren());
434 }
435
436 /**
437 * Run the void removeChild(ITraceControlComponent) method test.
438 *
439 * @throws Exception
440 *
441 */
442
443 public void testRemoveChild_2()
444 throws Exception {
445 TraceControlComponent fixture = new TraceControlComponent("", new TraceControlRoot());
446 fixture.setToolTip("");
447 fixture.addChild(new TraceControlRoot());
448 ITraceControlComponent component = null;
449
450 fixture.removeChild(component);
451 assertTrue(fixture.hasChildren());
452 }
453
454 /**
455 * Run the void removeComponentListener(ITraceControlComponentChangedListener) method test.
456 *
457 * @throws Exception
458 *
459 */
460 public void testRemoveComponentListener_1()
461 throws Exception {
462 TraceControlComponent fixture = new TraceControlComponent("", (ITraceControlComponent) null);
463 fixture.setToolTip("");
464
465 ListenerValidator validator = new ListenerValidator();
466 fixture.addComponentListener(validator);
467
468 // Remove listener and check that validator is not called anymore
469 validator.initialize();
470 fixture.removeComponentListener(validator);
471 TraceControlRoot root = new TraceControlRoot();
472 fixture.addChild(root);
473 assertFalse(validator.isAddedCalled());
474
475 fixture.removeChild(root);
476 assertFalse(validator.isRemovedCalled());
477
478 fixture.fireComponentChanged(fixture);
479 assertFalse(validator.isChangedCalled());
480 }
481
482 /**
483 * Run the void removeComponentListener(ITraceControlComponentChangedListener) method test.
484 *
485 * @throws Exception
486 *
487 */
488 public void testRemoveComponentListener_2()
489 throws Exception {
490 TraceControlComponent fixture = new TraceControlComponent("", new TraceControlRoot());
491 fixture.setToolTip("");
492 fixture.addChild(new TraceControlRoot());
493 ITraceControlComponentChangedListener listener = new ControlView();
494
495 fixture.removeComponentListener(listener);
496
497 }
498
499 /**
500 * Run the void setChildren(List<ITraceControlComponent>)/ITraceControlComponent[] getChildren() method test.
501 *
502 *
503 * @throws Exception
504 *
505 */
506 public void testGetAndSetChildren()
507 throws Exception {
508 TraceControlComponent fixture = new TraceControlComponent("", new TraceControlRoot());
509 fixture.setToolTip("");
510 List<ITraceControlComponent> children = new LinkedList<ITraceControlComponent>();
511 children.add(new TraceControlComponent("child1"));
512 children.add(new TraceControlComponent("child2"));
513
514 fixture.setChildren(children);
515
516 ITraceControlComponent[] result = fixture.getChildren();
517 assertEquals(2, result.length);
518 assertEquals("child1", result[0].getName());
519 assertEquals("child2", result[1].getName());
520 }
521
522 /**
523 * Run the void String getName()/setName(String) method tests.
524 *
525 * @throws Exception
526 *
527 */
528 public void testGetAndSetName()
529 throws Exception {
530 TraceControlComponent fixture = new TraceControlComponent("", new TraceControlRoot());
531 fixture.setToolTip("");
532 fixture.addChild(new TraceControlRoot());
533 String name = "node";
534
535 fixture.setName(name);
536 assertEquals(name,fixture.getName());
537
538 }
539
540 /**
541 * Run the void ITraceControlComponent getParent()/setParent(ITraceControlComponent) method tests.
542 *
543 * @throws Exception
544 *
545 */
546 public void testGetAndSetParent()
547 throws Exception {
548 TraceControlComponent fixture = new TraceControlComponent("", new TraceControlRoot());
549 fixture.setToolTip("");
550 fixture.addChild(new TraceControlRoot());
551 ITraceControlComponent parent = new TraceControlRoot();
552 parent.addChild(fixture);
553
554 fixture.setParent(parent);
555 ITraceControlComponent retrievedParent = fixture.getParent();
556 assertNotNull(retrievedParent);
557 assertEquals(parent.getName(), retrievedParent.getName());
558 assertEquals(TraceControlRoot.TRACE_CONTROL_ROOT_NAME, retrievedParent.getName());
559 assertEquals(null, retrievedParent.getParent());
560 assertEquals(true, retrievedParent.hasChildren());
561 }
562
563 /**
564 * Run the void TargetNodeState getTargetNodeState()/etTargetNodeState(TargetNodeState) method tests.
565 *
566 * @throws Exception
567 *
568 */
569 public void testGetAndSetTargetNodeState_1()
570 throws Exception {
571
572 TraceControlComponent parent = new TraceControlComponent("parent") {
573 private TargetNodeState fState;
574
575 @Override
576 public void setTargetNodeState(TargetNodeState state ) {
577 fState = state;
578 }
579
580 @Override
581 public TargetNodeState getTargetNodeState() {
582 return fState;
583 }
584 };
585
586 TraceControlComponent fixture = new TraceControlComponent("", parent);
587 parent.addChild(fixture);
588
589 fixture.setToolTip("");
590 TargetNodeState state = TargetNodeState.CONNECTED;
591
592 fixture.setTargetNodeState(state);
593 TargetNodeState result = fixture.getTargetNodeState();
594
595 assertNotNull(result);
596 assertEquals(state, result);
597 // Check also parent
598 assertEquals(state, fixture.getParent().getTargetNodeState());
599 assertEquals("CONNECTED", result.name());
600 assertEquals("CONNECTED", result.toString());
601 assertEquals(2, result.ordinal());
602
603 fixture.setTargetNodeState(TargetNodeState.DISCONNECTED);
604 result = fixture.getTargetNodeState();
605 assertNotNull(result);
606 assertEquals("DISCONNECTED", result.name());
607 assertEquals("DISCONNECTED", result.toString());
608 assertEquals(0, result.ordinal());
609
610 state = TargetNodeState.CONNECTING;
611
612 fixture.setTargetNodeState(state);
613 result = fixture.getTargetNodeState();
614 assertNotNull(result);
615 assertEquals("CONNECTING", result.name());
616 assertEquals("CONNECTING", result.toString());
617 assertEquals(3, result.ordinal());
618
619 fixture.setTargetNodeState(TargetNodeState.DISCONNECTING);
620 result = fixture.getTargetNodeState();
621 assertNotNull(result);
622 assertEquals("DISCONNECTING", result.name());
623 assertEquals("DISCONNECTING", result.toString());
624 assertEquals(1, result.ordinal());
625
626 }
627
628 /**
629 * Run the void setToolTip(String) method test.
630 *
631 * @throws Exception
632 *
633
634 */
635
636 public void testGetSndSetToolTip()
637 throws Exception {
638 TraceControlComponent fixture = new TraceControlComponent("", new TraceControlRoot());
639 fixture.setToolTip("This is a tooltip");
640 fixture.addChild(new TraceControlRoot());
641
642 String result = fixture.getToolTip();
643
644 assertEquals("This is a tooltip", result);
645 }
646 }
This page took 0.045462 seconds and 5 git commands to generate.