/*******************************************************************************
 * Copyright (c) 2009, 2018 Red Hat, Inc.
 * 
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Kent Sebastian <ksebasti@redhat.com> - initial API and implementation
 *******************************************************************************/

package org.eclipse.linuxtools.oprofile.launch.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.Launch;
import org.eclipse.linuxtools.internal.oprofile.core.Oprofile;
import org.eclipse.linuxtools.internal.oprofile.core.daemon.OprofileDaemonOptions;
import org.eclipse.linuxtools.internal.oprofile.launch.OprofileLaunchPlugin;
import org.eclipse.linuxtools.internal.oprofile.launch.configuration.OprofileSetupTab;
import org.eclipse.linuxtools.oprofile.launch.tests.utils.LaunchTestingOptions;
import org.eclipse.linuxtools.oprofile.launch.tests.utils.OprofileTestingEventConfigTab;
import org.eclipse.linuxtools.oprofile.launch.tests.utils.TestingOprofileLaunchConfigurationDelegate;
import org.eclipse.linuxtools.profiling.tests.AbstractTest;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.osgi.framework.FrameworkUtil;

public class TestLaunching extends AbstractTest {
    private ILaunchConfiguration config;
    private Shell testShell;

    @Before
    public void setUp() throws Exception {
        proj = createProjectAndBuild(FrameworkUtil.getBundle(this.getClass()), "primeTest"); //$NON-NLS-1$
        config = createConfiguration(proj.getProject());
        testShell = new Shell(Display.getDefault());
        testShell.setLayout(new GridLayout());
    }

    @After
    public void tearDown() throws Exception {
        testShell.dispose();
        deleteProject(proj);
    }

    @Override
    protected ILaunchConfigurationType getLaunchConfigType() {
        return getLaunchManager().getLaunchConfigurationType(OprofileLaunchPlugin.ID_LAUNCH_PROFILE);
    }

    @Override
    protected void setProfileAttributes(ILaunchConfigurationWorkingCopy wc) {
        OprofileTestingEventConfigTab configTab = new OprofileTestingEventConfigTab();
        OprofileSetupTab setupTab = new OprofileSetupTab();
        configTab.setOprofileProject(proj.getProject());
        configTab.setDefaults(wc);
        setupTab.setDefaults(wc);
    }
    @Test
    public void testDefaultLaunch() throws CoreException {
        TestingOprofileLaunchConfigurationDelegate delegate = new TestingOprofileLaunchConfigurationDelegate();
        ILaunch launch = new Launch(config, ILaunchManager.PROFILE_MODE, null);

        LaunchTestingOptions options = new LaunchTestingOptions();
        options.setOprofileProject(proj.getProject());
        options.loadConfiguration(config);
        assertTrue(options.isValid());
        assertTrue(options.getBinaryImage().isEmpty());
        assertTrue(options.getKernelImageFile().isEmpty());
        assertEquals(OprofileDaemonOptions.SEPARATE_NONE, options.getSeparateSamples());
        Oprofile.OprofileProject.setProfilingBinary(Oprofile.OprofileProject.OPERF_BINARY);
        delegate.launch(config, ILaunchManager.PROFILE_MODE, launch, null);
    }
    @Test
    public void testEventLaunch() throws CoreException {
        TestingOprofileLaunchConfigurationDelegate delegate = new TestingOprofileLaunchConfigurationDelegate();
        ILaunch launch = new Launch(config, ILaunchManager.PROFILE_MODE, null);

        ILaunchConfigurationWorkingCopy wc = config.getWorkingCopy();
        wc.setAttribute(OprofileLaunchPlugin.ATTR_USE_DEFAULT_EVENT, false);
        wc.setAttribute(OprofileLaunchPlugin.attrConterEnabled(0), true);
        wc.setAttribute(OprofileLaunchPlugin.attrCounterCount(0), 100000);
        wc.setAttribute(OprofileLaunchPlugin.attrConterEvent(0, 0), "FAKE_EVENT"); //$NON-NLS-1$
        wc.setAttribute(OprofileLaunchPlugin.attrCounterProfileKernel(0), true);
        wc.setAttribute(OprofileLaunchPlugin.attrCounterProfileUser(0), true);
        wc.setAttribute(OprofileLaunchPlugin.attrCounterUnitMask(0), 0);
        wc.doSave();
        LaunchTestingOptions options = new LaunchTestingOptions();
        options.setOprofileProject(proj.getProject());
        options.loadConfiguration(config);
        assertTrue(options.isValid());
        assertTrue(options.getBinaryImage().isEmpty());
        assertTrue(options.getKernelImageFile().isEmpty());
        assertEquals(OprofileDaemonOptions.SEPARATE_NONE, options.getSeparateSamples());

        Oprofile.OprofileProject.setProfilingBinary(Oprofile.OprofileProject.OPERF_BINARY);
        delegate.launch(config, ILaunchManager.PROFILE_MODE, launch, null);
    }
}
