| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2016 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 | |
| 10 | package org.eclipse.tracecompass.lttng2.kernel.ui.swtbot.tests; |
| 11 | |
| 12 | import static org.junit.Assert.assertEquals; |
| 13 | import static org.junit.Assert.assertNotNull; |
| 14 | import static org.junit.Assert.fail; |
| 15 | |
| 16 | import java.io.IOException; |
| 17 | import java.net.URISyntaxException; |
| 18 | import java.nio.file.Paths; |
| 19 | |
| 20 | import org.eclipse.core.runtime.FileLocator; |
| 21 | import org.eclipse.swt.widgets.Tree; |
| 22 | import org.eclipse.swt.widgets.Widget; |
| 23 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; |
| 24 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; |
| 25 | import org.eclipse.swtbot.swt.finder.matchers.WidgetOfType; |
| 26 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree; |
| 27 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; |
| 28 | import org.eclipse.tracecompass.analysis.os.linux.core.signals.TmfCpuSelectedSignal; |
| 29 | import org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.cpuusage.CpuUsageView; |
| 30 | import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace; |
| 31 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager; |
| 32 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 33 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
| 34 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils; |
| 35 | import org.hamcrest.Matcher; |
| 36 | import org.junit.Before; |
| 37 | import org.junit.Test; |
| 38 | import org.swtchart.Chart; |
| 39 | |
| 40 | /** |
| 41 | * SWTBot tests for Resources view |
| 42 | * |
| 43 | * @author Matthew Khouzam |
| 44 | */ |
| 45 | public class ResourcesAndCpuViewTest extends KernelTestBase { |
| 46 | |
| 47 | private SWTBotView fViewBotRv; |
| 48 | private SWTBotView fViewBotCpu; |
| 49 | |
| 50 | /** |
| 51 | * Before Test |
| 52 | */ |
| 53 | @Override |
| 54 | @Before |
| 55 | public void before() { |
| 56 | fViewBotRv = fBot.viewByPartName("Resources"); |
| 57 | SWTBotUtils.openView(CpuUsageView.ID); |
| 58 | fViewBotCpu = fBot.viewById(CpuUsageView.ID); |
| 59 | fViewBotCpu.show(); |
| 60 | fViewBotRv.show(); |
| 61 | try { |
| 62 | SWTBotUtils.openTrace(TRACE_PROJECT_NAME, Paths.get(FileLocator.toFileURL(CtfTestTrace.ARM_64_BIT_HEADER.getTraceURL()).toURI()).toString(), KERNEL_TRACE_TYPE); |
| 63 | } catch (IOException | URISyntaxException e) { |
| 64 | fail(e.getMessage()); |
| 65 | } |
| 66 | SWTBotUtils.activateEditor(fBot, "bug446190"); |
| 67 | fViewBotRv.setFocus(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Simple test to check the CPU Usage view after getting signals. |
| 72 | */ |
| 73 | @Test |
| 74 | public void testSignals() { |
| 75 | Widget widget = fViewBotRv.getWidget(); |
| 76 | assertNotNull(widget); |
| 77 | ITmfTrace activeTrace = TmfTraceManager.getInstance().getActiveTrace(); |
| 78 | assertNotNull(activeTrace); |
| 79 | |
| 80 | // clear everything |
| 81 | TmfCpuSelectedSignal signal = new TmfCpuSelectedSignal(widget, -1, activeTrace); |
| 82 | broadcast(signal); |
| 83 | assertEquals("Before signal - CPU Usage Title", "CPU Usage", getTitle()); |
| 84 | assertEquals("Before signal - Thread Table", 12, getTableCount()); |
| 85 | fViewBotRv.setFocus(); |
| 86 | |
| 87 | // select cpu 1 |
| 88 | signal = new TmfCpuSelectedSignal(widget, 1, activeTrace); |
| 89 | broadcast(signal); |
| 90 | assertEquals("After signal - CPU Usage Title", "CPU Usage 1", getTitle()); |
| 91 | assertEquals("After signal - Thread Table", 4, getTableCount()); |
| 92 | |
| 93 | // select cpu 3 and 1 |
| 94 | signal = new TmfCpuSelectedSignal(widget, 3, activeTrace); |
| 95 | broadcast(signal); |
| 96 | assertEquals("After signal 2 - CPU Usage Title", "CPU Usage 1, 3", getTitle()); |
| 97 | assertEquals("After signal 2 - Thread Table", 8, getTableCount()); |
| 98 | |
| 99 | // reset |
| 100 | signal = new TmfCpuSelectedSignal(widget, -1, activeTrace); |
| 101 | broadcast(signal); |
| 102 | assertEquals("After signal clear - CPU Usage Title", "CPU Usage", getTitle()); |
| 103 | assertEquals("After signal clear - Thread Table", 12, getTableCount()); |
| 104 | } |
| 105 | |
| 106 | private static void broadcast(TmfCpuSelectedSignal signal) { |
| 107 | UIThreadRunnable.syncExec(() -> TmfSignalManager.dispatchSignal(signal)); |
| 108 | SWTBotUtils.waitForJobs(); |
| 109 | } |
| 110 | |
| 111 | private String getTitle() { |
| 112 | fViewBotCpu.setFocus(); |
| 113 | // Do some basic validation |
| 114 | Matcher<Chart> matcher = WidgetOfType.widgetOfType(Chart.class); |
| 115 | Chart chart = fViewBotCpu.bot().widget(matcher); |
| 116 | return chart.getTitle().getText(); |
| 117 | } |
| 118 | |
| 119 | private int getTableCount() { |
| 120 | fViewBotCpu.setFocus(); |
| 121 | // Do some basic validation |
| 122 | Matcher<Tree> matcher = WidgetOfType.widgetOfType(Tree.class); |
| 123 | SWTBotTree treeBot = new SWTBotTree(fViewBotCpu.bot().widget(matcher)); |
| 124 | int count = 0; |
| 125 | for (SWTBotTreeItem bot : treeBot.getAllItems()) { |
| 126 | final String text = bot.getText(); |
| 127 | if (!text.isEmpty()) { |
| 128 | count++; |
| 129 | } |
| 130 | } |
| 131 | return count; |
| 132 | } |
| 133 | } |