<commit_before>/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project committers and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package org.eclipse.mylyn.monitor.tests;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import junit.framework.TestCase;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.window.WindowManager;
import org.eclipse.mylyn.internal.monitor.ui.IMonitoredWindow;
import org.eclipse.mylyn.internal.monitor.usage.InteractionEventLogger;
import org.eclipse.mylyn.internal.monitor.usage.UiUsageMonitorPlugin;
import org.eclipse.mylyn.monitor.ui.MonitorUiPlugin;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.XMLMemento;
import org.eclipse.ui.internal.IWorkbenchConstants;
import org.eclipse.ui.internal.Workbench;
import org.eclipse.ui.internal.WorkbenchWindow;

/**
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 */
public class MultiWindowMonitorTest extends TestCase {

	private class ContextAwareWorkbenchWindow extends WorkbenchWindow implements IMonitoredWindow {

		private boolean monitored = true;

		public ContextAwareWorkbenchWindow(int number, boolean monitored) {
			super(number);
			this.monitored  = monitored;
		}

		public boolean isMonitored() {
			return monitored;
		}

		public boolean isPerspectiveManaged() {
			return false;
		}	
	}
	
	private class MockWorkbench {
		private Workbench wBench;
		private WindowManager parentManager = null;
		
		private MockWorkbench() {
			wBench = Workbench.getInstance();

			Field wManagerField;
			try {
				wManagerField = Workbench.class.getDeclaredField("windowManager");
				wManagerField.setAccessible(true);
				parentManager = (WindowManager) wManagerField.get(wBench);

			} catch (Exception e) {
			}
		}

		private int getNewWindowNumber() {
			Window[] windows = parentManager.getWindows();
			int count = windows.length;

			boolean checkArray[] = new boolean[count];
			for (int nX = 0; nX < count; nX++) {
				if(windows[nX] instanceof WorkbenchWindow) {
					WorkbenchWindow ww = (WorkbenchWindow) windows[nX];
					int index = ww.getNumber() - 1;
					if (index >= 0 && index < count) {
						checkArray[index] = true;
					}
				}
			}

			for (int index = 0; index < count; index++) {
				if (!checkArray[index]) {
					return index + 1;
				}
			}
			return count + 1;
		}

		private ContextAwareWorkbenchWindow newWorkbenchWindow(boolean isMonitored) {
			return new ContextAwareWorkbenchWindow(getNewWindowNumber(), isMonitored);
		}


		public ContextAwareWorkbenchWindow restoreState(IMemento memento, boolean isMonitored) {

			ContextAwareWorkbenchWindow newWindow = newWorkbenchWindow(isMonitored);
			newWindow.create();

			parentManager.add(newWindow);

			boolean opened = false;

			try {
				newWindow.restoreState(memento, null);
				newWindow.open();
				opened = true;
			} finally {
				if (!opened) {
					newWindow.close();
				}
			}

			return newWindow;
		}
	}
	
	private InteractionEventLogger logger = UiUsageMonitorPlugin.getDefault().getInteractionLogger();

	private MockSelectionMonitor selectionMonitor = new MockSelectionMonitor();

	private IWorkbenchWindow window1;

	private IWorkbenchWindow window2;
	private IWorkbenchWindow window3;
	private IWorkbenchWindow window4;

	private boolean monitoringWasEnabled;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		monitoringWasEnabled = UiUsageMonitorPlugin.getDefault().isMonitoringEnabled();
		UiUsageMonitorPlugin.getDefault().stopMonitoring();
		window1 = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		assertTrue(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window1));
		window2 = duplicateWindow(window1);
		assertNotNull(window2);
		assertTrue(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window2));
		window3 = createContextAwareWindow(true, window1);
		assertNotNull(window3);
		assertTrue(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window3));
		window4 = createContextAwareWindow(false, window1);
		assertNotNull(window4);
		assertFalse(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window4));
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		window2.close();<commit_msg>NEW - bug 213544: Fix failing tests for 2.2 https://bugs.eclipse.org/bugs/show_bug.cgi?id=213544<commit_after>/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project committers and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package org.eclipse.mylyn.monitor.tests;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import junit.framework.TestCase;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.window.WindowManager;
import org.eclipse.mylyn.internal.monitor.ui.IMonitoredWindow;
import org.eclipse.mylyn.internal.monitor.usage.InteractionEventLogger;
import org.eclipse.mylyn.internal.monitor.usage.UiUsageMonitorPlugin;
import org.eclipse.mylyn.monitor.ui.MonitorUiPlugin;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.XMLMemento;
import org.eclipse.ui.internal.IWorkbenchConstants;
import org.eclipse.ui.internal.Workbench;
import org.eclipse.ui.internal.WorkbenchWindow;

/**
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 */
public class MultiWindowMonitorTest extends TestCase {

	private class ContextAwareWorkbenchWindow extends WorkbenchWindow implements IMonitoredWindow {

		private boolean monitored = true;

		public ContextAwareWorkbenchWindow(int number, boolean monitored) {
			super(number);
			this.monitored  = monitored;
		}

		public boolean isMonitored() {
			return monitored;
		}

		public boolean isPerspectiveManaged() {
			return false;
		}	
	}
	
	private class MockWorkbench {
		private Workbench wBench;
		private WindowManager parentManager = null;
		
		private MockWorkbench() {
			wBench = Workbench.getInstance();

			Field wManagerField;
			try {
				wManagerField = Workbench.class.getDeclaredField("windowManager");
				wManagerField.setAccessible(true);
				parentManager = (WindowManager) wManagerField.get(wBench);

			} catch (Exception e) {
			}
		}

		private int getNewWindowNumber() {
			Window[] windows = parentManager.getWindows();
			int count = windows.length;

			boolean checkArray[] = new boolean[count];
			for (int nX = 0; nX < count; nX++) {
				if(windows[nX] instanceof WorkbenchWindow) {
					WorkbenchWindow ww = (WorkbenchWindow) windows[nX];
					int index = ww.getNumber() - 1;
					if (index >= 0 && index < count) {
						checkArray[index] = true;
					}
				}
			}

			for (int index = 0; index < count; index++) {
				if (!checkArray[index]) {
					return index + 1;
				}
			}
			return count + 1;
		}

		private ContextAwareWorkbenchWindow newWorkbenchWindow(boolean isMonitored) {
			return new ContextAwareWorkbenchWindow(getNewWindowNumber(), isMonitored);
		}


		public ContextAwareWorkbenchWindow restoreState(IMemento memento, boolean isMonitored) {

			ContextAwareWorkbenchWindow newWindow = newWorkbenchWindow(isMonitored);
			newWindow.create();

			parentManager.add(newWindow);

			boolean opened = false;

			try {
				newWindow.restoreState(memento, null);
				newWindow.open();
				opened = true;
			} finally {
				if (!opened) {
					newWindow.close();
				}
			}

			return newWindow;
		}
	}
	
	private InteractionEventLogger logger = UiUsageMonitorPlugin.getDefault().getInteractionLogger();

	private MockSelectionMonitor selectionMonitor = new MockSelectionMonitor();

	private IWorkbenchWindow window1;

	private IWorkbenchWindow window2;
	private IWorkbenchWindow window3;
	private IWorkbenchWindow window4;

	private boolean monitoringWasEnabled;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		monitoringWasEnabled = UiUsageMonitorPlugin.getDefault().isMonitoringEnabled();
		UiUsageMonitorPlugin.getDefault().stopMonitoring();
		
		// make sure the MonitorUiPlugin is fully initialized
		while (PlatformUI.getWorkbench().getDisplay().readAndDispatch()) {
		}

		window1 = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		assertTrue(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window1));
		window2 = duplicateWindow(window1);
		assertNotNull(window2);
		assertTrue(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window2));
		window3 = createContextAwareWindow(true, window1);
		assertNotNull(window3);
		assertTrue(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window3));
		window4 = createContextAwareWindow(false, window1);
		assertNotNull(window4);
		assertFalse(MonitorUiPlugin.getDefault().getMonitoredWindows().contains(window4));
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		window2.close();
{'fraction_non_alphanumeric': 0.13166970988030027, 'fraction_numerical': 0.008926759991884763, 'mean_word_length': 4.9751515151515155, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6207788', 'n_tokens_mistral': 3231, 'n_tokens_neox': 2778, 'n_words': 604}