Skip to main content
summaryrefslogtreecommitdiffstats
blob: e986f4a359dc17001f6596e9ea79a1d63b031a99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*******************************************************************************
 *  Copyright (c) 2008, 2017 IBM Corporation 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
 * 
 *  Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.ui.dialogs;

import java.lang.reflect.Field;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.jobs.*;
import org.eclipse.equinox.internal.p2.ui.ProvUI;
import org.eclipse.equinox.internal.p2.ui.sdk.RevertProfilePageWithCompare;
import org.eclipse.equinox.internal.p2.ui.viewers.ProvElementContentProvider;
import org.eclipse.equinox.p2.tests.ui.AbstractProvisioningUITest;
import org.eclipse.equinox.p2.ui.RevertProfilePage;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.AbstractTableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

/**
 * Tests for the Installation History page.
 * If nothing else, this test ensures that installation history can be hosted
 * somewhere besides the about dialog
 */
public class InstallationHistoryPageTest extends AbstractProvisioningUITest {
	volatile int jobs;
	volatile int done;

	class TestDialog extends Dialog {
		RevertProfilePage page;

		TestDialog() {
			super(ProvUI.getDefaultParentShell());
		}

		@Override
		protected Control createDialogArea(Composite parent) {
			Composite composite = new Composite(parent, SWT.NONE);
			page = new RevertProfilePage();
			page.createControl(composite);
			return composite;
		}
	}

	class TestDialog2 extends Dialog {
		RevertProfilePageWithCompare page;

		TestDialog2() {
			super(ProvUI.getDefaultParentShell());
		}

		@Override
		protected Control createDialogArea(Composite parent) {
			Composite composite = new Composite(parent, SWT.NONE);
			page = new RevertProfilePageWithCompare();
			page.createControl(composite);
			return composite;
		}
	}

	/**
	 * Hammers the background fetch
	 */
	public void disabledtestDialogBackgroundFetch() {
		TestDialog dialog = new TestDialog();
		dialog.setBlockOnOpen(false);
		dialog.open();
		try {
			Field viewerField = RevertProfilePage.class.getDeclaredField("configsViewer");
			viewerField.setAccessible(true);
			AbstractTableViewer viewer = (AbstractTableViewer) viewerField.get(dialog.page);
			ProvElementContentProvider provider = (ProvElementContentProvider) viewer.getContentProvider();
			Object input = viewer.getInput();
			Field jobField = ProvElementContentProvider.class.getDeclaredField("fetchJob");
			jobField.setAccessible(true);
			Field jobFamily = ProvElementContentProvider.class.getDeclaredField("fetchFamily");
			jobFamily.setAccessible(true);

			jobs = 0;
			done = 0;
			final Object family = jobFamily.get(provider);

			Job.getJobManager().addJobChangeListener(new JobChangeAdapter() {
				@Override
				public void done(IJobChangeEvent e) {
					if (e.getJob().belongsTo(family)) {
						done++;
						int status = e.getResult().getSeverity();
						if (status != IStatus.CANCEL && status != IStatus.OK) {
							// something unexpected happened.
							fail("Fetch job failed unexpectedly " + e.getResult().getMessage());
						}
					}
				}
			});

			// hammer the elements repetitively to start multiple fast running fetch jobs
			for (int i = 0; i < 5; i++) {
				provider.getElements(input);
				Job job = (Job) jobField.get(provider);
				if (job != null) {
					jobs++;
				}
			}
			// We need to wait for all the fetch jobs to finish and then verify that they did
			Job.getJobManager().join(family, null);
			assertTrue("Something's wrong.  No fetch occurred", jobs > 0);
			assertEquals("Not all jobs finished as expected", jobs, done);
		} catch (Exception e) {
			fail("Failure during reflection", e);
		} finally {
			dialog.close();
		}
	}

	/**
	 * Tests the dialog - just launches it for now
	 */
	public void testDialog() {
		TestDialog dialog = new TestDialog();
		dialog.setBlockOnOpen(false);
		dialog.open();
		dialog.close();
	}

	/**
	 * Tests the dialog - just launches it for now
	 */
	public void testDialogWithCompare() {
		TestDialog2 dialog = new TestDialog2();
		dialog.setBlockOnOpen(false);
		dialog.open();

		dialog.close();
	}
}

Back to the top