1 /*******************************************************************************
2  * Copyright (c) 2016 Red Hat Inc. and others
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     Mickael Istria (Red Hat Inc.)
13  *******************************************************************************/
14 package org.eclipse.ui.editors.tests;
15 
16 import java.io.ByteArrayInputStream;
17 import java.util.Collections;
18 
19 import org.junit.After;
20 import org.junit.AfterClass;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 
26 import org.eclipse.swt.custom.StyledText;
27 import org.eclipse.swt.widgets.Control;
28 
29 import org.eclipse.core.commands.Command;
30 import org.eclipse.core.commands.ExecutionEvent;
31 
32 import org.eclipse.core.runtime.NullProgressMonitor;
33 
34 import org.eclipse.core.resources.IFile;
35 import org.eclipse.core.resources.IProject;
36 import org.eclipse.core.resources.ResourcesPlugin;
37 
38 import org.eclipse.ui.IEditorDescriptor;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.commands.ICommandService;
41 import org.eclipse.ui.intro.IIntroPart;
42 import org.eclipse.ui.part.FileEditorInput;
43 
44 import org.eclipse.ui.texteditor.AbstractTextEditor;
45 
46 /**
47  * @since 3.11
48  *
49  */
50 public class ZoomTest {
51 
52 	private static IProject project;
53 	private static IFile file;
54 	private StyledText text;
55 	private AbstractTextEditor editor;
56 	private int initialFontSize;
57 
58 	@BeforeClass
setUpBeforeClass()59 	public static void setUpBeforeClass() throws Exception {
60 		project = ResourcesPlugin.getWorkspace().getRoot().getProject("test");
61 		project.create(new NullProgressMonitor());
62 		project.open(new NullProgressMonitor());
63 		file = project.getFile("foo.txt");
64 		file.create(new ByteArrayInputStream("bar".getBytes()), true, new NullProgressMonitor());
65 	}
66 
67 	@AfterClass
tearDownAfterClass()68 	public static void tearDownAfterClass() throws Exception {
69 		file.delete(true, new NullProgressMonitor());
70 		project.delete(true, new NullProgressMonitor());
71 		TestUtil.cleanUp();
72 	}
73 
74 	@Before
setUp()75 	public void setUp() throws Exception {
76 		IIntroPart intro = PlatformUI.getWorkbench().getIntroManager().getIntro();
77 		if (intro != null) {
78 			PlatformUI.getWorkbench().getIntroManager().closeIntro(intro);
79 		}
80 
81 		IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(file.getName());
82 		editor = (AbstractTextEditor) PlatformUI.getWorkbench().getActiveWorkbenchWindow()
83 				.getActivePage().openEditor(new FileEditorInput(file), desc.getId());
84 		editor.setFocus();
85 		text = (StyledText) editor.getAdapter(Control.class);
86 		// make sure we start from a clean state
87 		initialFontSize = text.getFont().getFontData()[0].getHeight();
88 	}
89 
90 	@After
tearDown()91 	public void tearDown() throws Exception {
92 		editor.close(false);
93 		editor= null;
94 	}
95 
96 	@Test
testZoomCommand()97 	public void testZoomCommand() throws Exception {
98 		int times = 6;
99 		Command zoomInCommand = PlatformUI.getWorkbench().getService(ICommandService.class)
100 				.getCommand("org.eclipse.ui.edit.text.zoomIn");
101 		for (int i = 0; i < times; i++) {
102 			zoomInCommand.executeWithChecks(new ExecutionEvent(zoomInCommand, Collections.EMPTY_MAP, null, null));
103 		}
104 		Assert.assertEquals(this.initialFontSize + 12, text.getFont().getFontData()[0].getHeight());
105 		Command zoomOutCommand = PlatformUI.getWorkbench().getService(ICommandService.class)
106 				.getCommand("org.eclipse.ui.edit.text.zoomOut");
107 		for (int i = 0; i < times; i++) {
108 			zoomOutCommand.executeWithChecks(new ExecutionEvent(zoomOutCommand, Collections.EMPTY_MAP, null, null));
109 		}
110 		Assert.assertEquals(this.initialFontSize, text.getFont().getFontData()[0].getHeight());
111 	}
112 
113 }
114