Commit | Line | Data |
---|---|---|
fe231cf6 JR |
1 | /********************************************************************** |
2 | * Copyright (c) 2016 EfficiOS Inc. | |
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 | ||
10 | package org.eclipse.tracecompass.tmf.ui.tests.views; | |
11 | ||
12 | import static org.junit.Assert.*; | |
13 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; | |
14 | ||
15 | import java.util.Arrays; | |
16 | ||
17 | import org.eclipse.tracecompass.tmf.ui.tests.stubs.views.TmfViewStub; | |
18 | import org.eclipse.tracecompass.tmf.ui.views.TmfViewFactory; | |
19 | import org.eclipse.ui.IViewPart; | |
20 | import org.eclipse.ui.IViewReference; | |
21 | import org.eclipse.ui.IWorkbench; | |
22 | import org.eclipse.ui.IWorkbenchPage; | |
23 | import org.eclipse.ui.IWorkbenchWindow; | |
24 | import org.eclipse.ui.PlatformUI; | |
25 | import org.junit.Test; | |
26 | ||
27 | /** | |
28 | * Test the {@link org.eclipse.tracecompass.tmf.ui.views.TmfViewFactory} class | |
29 | * and its utility method. | |
30 | * | |
31 | * @author Jonathan Rajotte Julien | |
32 | */ | |
33 | public class TmfViewFactoryTest { | |
34 | ||
35 | /* The internal separator */ | |
36 | private static final String SEPARATOR = TmfViewFactory.INTERNAL_SECONDARY_ID_SEPARATOR; | |
37 | private static final String UUID = "90148656-2bd7-4f3e-8513-4def0ac76b1f"; | |
38 | ||
39 | BaseSecIdTestCase[] baseSecIdTestCases = { | |
40 | new BaseSecIdTestCase(null, null), | |
41 | new BaseSecIdTestCase(UUID, null), | |
42 | new BaseSecIdTestCase(SEPARATOR + UUID, ""), | |
43 | new BaseSecIdTestCase(SEPARATOR + "1-1-1-1-1", SEPARATOR + "1-1-1-1-1"), | |
44 | new BaseSecIdTestCase("sec_id", "sec_id"), | |
45 | new BaseSecIdTestCase("sec_id" + SEPARATOR, "sec_id" + SEPARATOR), | |
46 | new BaseSecIdTestCase("sec_id" + SEPARATOR + UUID, "sec_id"), | |
47 | new BaseSecIdTestCase("sec_id" + SEPARATOR + SEPARATOR + UUID, "sec_id" + SEPARATOR), | |
48 | new BaseSecIdTestCase("sec_id" + SEPARATOR + "third_id", "sec_id" + SEPARATOR + "third_id"), | |
49 | new BaseSecIdTestCase("sec_id" + SEPARATOR + "third_id" + SEPARATOR + UUID, "sec_id" + SEPARATOR + "third_id"), | |
50 | new BaseSecIdTestCase("sec_id" + SEPARATOR + "third_id" + SEPARATOR + "fourth_id", "sec_id" + SEPARATOR + "third_id" + SEPARATOR + "fourth_id"), | |
51 | }; | |
52 | ||
53 | private static class BaseSecIdTestCase { | |
54 | private String input; | |
55 | private String output; | |
56 | ||
57 | public BaseSecIdTestCase(String input, String secondaryId) { | |
58 | this.input = input; | |
59 | this.output = secondaryId; | |
60 | } | |
61 | ||
62 | public String getInput() { | |
63 | return input; | |
64 | } | |
65 | ||
66 | public String getOutput() { | |
67 | return output; | |
68 | } | |
69 | } | |
70 | ||
71 | /** | |
72 | * Test method for | |
73 | * {@link org.eclipse.tracecompass.tmf.ui.views.TmfViewFactory#getBaseSecId(java.lang.String)}. | |
74 | */ | |
75 | @Test | |
76 | public void testGetBaseSecId() { | |
77 | for (BaseSecIdTestCase testCase : baseSecIdTestCases) { | |
78 | String input = testCase.getInput(); | |
79 | String expect = testCase.getOutput(); | |
80 | String result = TmfViewFactory.getBaseSecId(input); | |
81 | String message = String.format("Input:%s Output: %s Expected: %s", input, result, expect); | |
82 | ||
83 | assertEquals(message, expect, result); | |
84 | } | |
85 | } | |
86 | ||
87 | /** | |
88 | * Test method for | |
89 | * {@link org.eclipse.tracecompass.tmf.ui.views.TmfViewFactory#newView(java.lang.String, boolean)}. | |
90 | */ | |
91 | @Test | |
92 | public void testNewView() { | |
93 | IViewPart firstView = TmfViewFactory.newView(checkNotNull(TmfViewStub.TMF_VIEW_STUB_ID), false); | |
94 | IViewPart sameAsFirstView = TmfViewFactory.newView(checkNotNull(TmfViewStub.TMF_VIEW_STUB_ID), false); | |
95 | IViewPart secondView = TmfViewFactory.newView(checkNotNull(TmfViewStub.TMF_VIEW_STUB_ID), true); | |
96 | IViewPart failView1 = TmfViewFactory.newView("this.is.a.failing.view.id", false); | |
97 | IViewPart failView2 = TmfViewFactory.newView("this.is.a.failing.view.id", true); | |
98 | ||
99 | assertNotNull("Failed to spawn first view", firstView); | |
100 | assertEquals("Same id returned different instance", sameAsFirstView, firstView); | |
101 | assertNotNull("Failed to open second view with suffix", secondView); | |
102 | assertNull("Expected to fail on dummy view id", failView1); | |
103 | assertNull("Expected to fail on dummy view id with suffix", failView2); | |
104 | ||
105 | /** Test for new view from a duplicate view */ | |
106 | /* Fetch duplicate view complete id */ | |
107 | IWorkbench wb = PlatformUI.getWorkbench(); | |
108 | IWorkbenchWindow win = wb.getActiveWorkbenchWindow(); | |
109 | IWorkbenchPage page = win.getActivePage(); | |
110 | IViewReference[] viewRefs = page.getViewReferences(); | |
111 | ||
112 | String fullId = null; | |
113 | for (IViewReference view : viewRefs) { | |
114 | if (view.getSecondaryId() != null && view.getId().equals(TmfViewStub.TMF_VIEW_STUB_ID)) { | |
115 | assertTrue("Instanceof a TMfViewStub", view.getView(false) instanceof TmfViewStub); | |
116 | fullId = ((TmfViewStub) view.getView(false)).getViewId(); | |
117 | break; | |
118 | } | |
119 | } | |
120 | assertNotNull(fullId); | |
121 | IViewPart thirdView = TmfViewFactory.newView(fullId, true); | |
122 | assertNotNull("Creation from a view id with suffix failed", fullId); | |
123 | assertFalse("New view from view id with suffix was not created", Arrays.asList(viewRefs).contains(thirdView)); | |
124 | } | |
125 | } |