| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2015 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 | * Matthew Khouzam - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events; |
| 13 | |
| 14 | import static org.junit.Assert.assertEquals; |
| 15 | import static org.junit.Assert.assertNotNull; |
| 16 | |
| 17 | import java.io.File; |
| 18 | import java.io.IOException; |
| 19 | import java.util.Collections; |
| 20 | import java.util.List; |
| 21 | |
| 22 | import org.apache.log4j.ConsoleAppender; |
| 23 | import org.apache.log4j.Logger; |
| 24 | import org.apache.log4j.SimpleLayout; |
| 25 | import org.eclipse.swt.graphics.RGB; |
| 26 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; |
| 27 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; |
| 28 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; |
| 29 | import org.eclipse.swtbot.swt.finder.results.Result; |
| 30 | import org.eclipse.swtbot.swt.finder.results.VoidResult; |
| 31 | import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; |
| 32 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable; |
| 33 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTableItem; |
| 34 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; |
| 35 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
| 36 | import org.eclipse.tracecompass.tmf.core.filter.model.ITmfFilterTreeNode; |
| 37 | import org.eclipse.tracecompass.tmf.core.io.BufferedRandomAccessFile; |
| 38 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils; |
| 39 | import org.eclipse.tracecompass.tmf.ui.views.colors.ColorSetting; |
| 40 | import org.eclipse.tracecompass.tmf.ui.views.colors.ColorSettingsManager; |
| 41 | import org.eclipse.tracecompass.tmf.ui.views.colors.ColorsView; |
| 42 | import org.junit.After; |
| 43 | import org.junit.AfterClass; |
| 44 | import org.junit.Before; |
| 45 | import org.junit.BeforeClass; |
| 46 | import org.junit.Test; |
| 47 | |
| 48 | /** |
| 49 | * Test for Colors views in trace compass |
| 50 | */ |
| 51 | public class ColorsViewTest { |
| 52 | |
| 53 | private static final class PassAll implements ITmfFilterTreeNode { |
| 54 | @Override |
| 55 | public boolean matches(ITmfEvent event) { |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public String toString(boolean explicit) { |
| 61 | return "YES"; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void setParent(ITmfFilterTreeNode parent) { |
| 66 | |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public ITmfFilterTreeNode replaceChild(int index, ITmfFilterTreeNode node) { |
| 71 | return null; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public ITmfFilterTreeNode removeChild(ITmfFilterTreeNode node) { |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public ITmfFilterTreeNode remove() { |
| 81 | return null; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public boolean hasChildren() { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public List<String> getValidChildren() { |
| 91 | return Collections.EMPTY_LIST; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public ITmfFilterTreeNode getParent() { |
| 96 | return null; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public String getNodeName() { |
| 101 | return "YES"; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public int getChildrenCount() { |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public ITmfFilterTreeNode[] getChildren() { |
| 111 | return null; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public ITmfFilterTreeNode getChild(int index) { |
| 116 | return null; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public int addChild(ITmfFilterTreeNode node) { |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public ITmfFilterTreeNode clone() { |
| 126 | return null; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | private static final String XMLSTUB_ID = "org.eclipse.linuxtools.tmf.core.tests.xmlstub"; |
| 131 | |
| 132 | private static final String TRACE_START = "<trace>"; |
| 133 | private static final String EVENT_BEGIN = "<event timestamp=\""; |
| 134 | private static final String EVENT_MIDDLE = " \" name=\"event\"><field name=\"field\" value=\""; |
| 135 | private static final String EVENT_END = "\" type=\"int\" />" + "</event>"; |
| 136 | private static final String TRACE_END = "</trace>"; |
| 137 | |
| 138 | private static final String PROJECT_NAME = "TestForFiltering"; |
| 139 | |
| 140 | /** The Log4j logger instance. */ |
| 141 | private static final Logger fLogger = Logger.getRootLogger(); |
| 142 | private static SWTWorkbenchBot fBot; |
| 143 | |
| 144 | private static String makeEvent(int ts, int val) { |
| 145 | return EVENT_BEGIN + Integer.toString(ts) + EVENT_MIDDLE + Integer.toString(val) + EVENT_END + "\n"; |
| 146 | } |
| 147 | |
| 148 | private static File fFileLocation; |
| 149 | |
| 150 | /** |
| 151 | * Initialization, creates a temp trace |
| 152 | * |
| 153 | * @throws IOException |
| 154 | * should not happen |
| 155 | */ |
| 156 | @BeforeClass |
| 157 | public static void init() throws IOException { |
| 158 | SWTBotUtils.failIfUIThread(); |
| 159 | Thread.currentThread().setName("SWTBot Thread"); // for the debugger |
| 160 | /* set up for swtbot */ |
| 161 | SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */ |
| 162 | fLogger.addAppender(new ConsoleAppender(new SimpleLayout())); |
| 163 | fBot = new SWTWorkbenchBot(); |
| 164 | |
| 165 | SWTBotUtils.closeView("welcome", fBot); |
| 166 | |
| 167 | SWTBotUtils.switchToTracingPerspective(); |
| 168 | /* finish waiting for eclipse to load */ |
| 169 | SWTBotUtils.waitForJobs(); |
| 170 | fFileLocation = File.createTempFile("sample", ".xml"); |
| 171 | try (BufferedRandomAccessFile braf = new BufferedRandomAccessFile(fFileLocation, "rw")) { |
| 172 | braf.writeBytes(TRACE_START); |
| 173 | for (int i = 0; i < 100; i++) { |
| 174 | braf.writeBytes(makeEvent(i * 100, i % 4)); |
| 175 | } |
| 176 | braf.writeBytes(TRACE_END); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Open a trace in an editor |
| 182 | */ |
| 183 | @Before |
| 184 | public void beforeTest() { |
| 185 | SWTBotUtils.createProject(PROJECT_NAME); |
| 186 | SWTBotTreeItem treeItem = SWTBotUtils.selectTracesFolder(fBot, PROJECT_NAME); |
| 187 | assertNotNull(treeItem); |
| 188 | SWTBotUtils.openTrace(PROJECT_NAME, fFileLocation.getAbsolutePath(), XMLSTUB_ID); |
| 189 | SWTBotUtils.openView(ColorsView.ID); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Delete the file |
| 194 | */ |
| 195 | @AfterClass |
| 196 | public static void cleanUp() { |
| 197 | fFileLocation.delete(); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Close the editor |
| 202 | */ |
| 203 | @After |
| 204 | public void tearDown() { |
| 205 | fBot.closeAllEditors(); |
| 206 | SWTBotUtils.deleteProject(PROJECT_NAME, fBot); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Test color by making all events yellow |
| 211 | */ |
| 212 | @Test |
| 213 | public void testYellow() { |
| 214 | SWTBotView viewBot = fBot.viewById(ColorsView.ID); |
| 215 | viewBot.setFocus(); |
| 216 | final String insert = "Insert new color setting"; |
| 217 | final String increasePriority = "Increase priority"; |
| 218 | final String decreasePriority = "Decrease priority"; |
| 219 | final String delete = "Delete color setting"; |
| 220 | viewBot.toolbarButton(insert).click(); |
| 221 | viewBot.toolbarButton(insert).click(); |
| 222 | viewBot.toolbarButton(insert).click(); |
| 223 | viewBot.toolbarButton(insert).click(); |
| 224 | viewBot.toolbarButton(increasePriority).click(); |
| 225 | viewBot.toolbarButton(decreasePriority).click(); |
| 226 | viewBot.toolbarButton(delete).click(); |
| 227 | viewBot.bot().label(0).setFocus(); |
| 228 | viewBot.toolbarButton(delete).click(); |
| 229 | viewBot.bot().label(0).setFocus(); |
| 230 | viewBot.toolbarButton(delete).click(); |
| 231 | final RGB foreground = new RGB(0, 0, 0); |
| 232 | final RGB background = new RGB(255, 255, 0); |
| 233 | // Simulate the site effects of picking a color because we cannot |
| 234 | // control native Color picker dialog in SWTBot. |
| 235 | final ColorSetting[] cs = new ColorSetting[1]; |
| 236 | UIThreadRunnable.syncExec(new VoidResult() { |
| 237 | @Override |
| 238 | public void run() { |
| 239 | cs[0] = new ColorSetting(foreground, background, foreground, new PassAll()); |
| 240 | ColorSettingsManager.setColorSettings(cs); |
| 241 | } |
| 242 | }); |
| 243 | final SWTBotTable eventsEditor = fBot.activeEditor().bot().table(); |
| 244 | eventsEditor.select(2); |
| 245 | final SWTBotTableItem tableItem = eventsEditor.getTableItem(2); |
| 246 | RGB fgc = UIThreadRunnable.syncExec(new Result<RGB>() { |
| 247 | @Override |
| 248 | public RGB run() { |
| 249 | return tableItem.widget.getForeground().getRGB(); |
| 250 | } |
| 251 | }); |
| 252 | RGB bgc = UIThreadRunnable.syncExec(new Result<RGB>() { |
| 253 | @Override |
| 254 | public RGB run() { |
| 255 | return tableItem.widget.getBackground().getRGB(); |
| 256 | } |
| 257 | }); |
| 258 | assertEquals("Fg", foreground, fgc); |
| 259 | assertEquals("Bg", background, bgc); |
| 260 | } |
| 261 | } |