|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.tools.eclipse.appengine.localserver; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertFalse; |
| 20 | +import static org.junit.Assert.assertNotNull; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +import com.google.cloud.tools.eclipse.appengine.facets.AppEngineStandardFacet; |
| 24 | +import com.google.cloud.tools.eclipse.swtbot.SwtBotProjectActions; |
| 25 | +import com.google.cloud.tools.eclipse.test.util.project.TestProjectCreator; |
| 26 | +import com.google.cloud.tools.eclipse.ui.util.WorkbenchUtil; |
| 27 | +import java.io.ByteArrayInputStream; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.List; |
| 30 | +import org.eclipse.core.resources.IFile; |
| 31 | +import org.eclipse.core.resources.IProject; |
| 32 | +import org.eclipse.core.runtime.CoreException; |
| 33 | +import org.eclipse.jst.common.project.facet.core.JavaFacet; |
| 34 | +import org.eclipse.jst.j2ee.web.project.facet.WebFacetUtils; |
| 35 | +import org.eclipse.swt.widgets.Menu; |
| 36 | +import org.eclipse.swt.widgets.MenuItem; |
| 37 | +import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; |
| 38 | +import org.eclipse.swtbot.swt.finder.finders.MenuFinder; |
| 39 | +import org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory; |
| 40 | +import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; |
| 41 | +import org.eclipse.ui.IWorkbench; |
| 42 | +import org.eclipse.ui.PlatformUI; |
| 43 | +import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion; |
| 44 | +import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager; |
| 45 | +import org.hamcrest.Matcher; |
| 46 | +import org.junit.Rule; |
| 47 | +import org.junit.Test; |
| 48 | + |
| 49 | +public class RunAppEngineShortcutTest { |
| 50 | + |
| 51 | + private static final IProjectFacetVersion APP_ENGINE_STANDARD_FACET_1 = |
| 52 | + ProjectFacetsManager.getProjectFacet(AppEngineStandardFacet.ID).getVersion("1"); |
| 53 | + |
| 54 | + @Rule public TestProjectCreator javaProjectCreator = new TestProjectCreator(); |
| 55 | + @Rule public TestProjectCreator appEngineProjectCreator = new TestProjectCreator() |
| 56 | + .withFacetVersions(Arrays.asList( |
| 57 | + JavaFacet.VERSION_1_7, WebFacetUtils.WEB_25, APP_ENGINE_STANDARD_FACET_1)); |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testRunAppEngine_enabledForAppEngineProject() { |
| 61 | + IProject project = appEngineProjectCreator.getProject(); |
| 62 | + assertTrue(appEngineMenuExists(project)); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testRunAppEngine_hiddenForPlainProject() { |
| 67 | + IProject project = javaProjectCreator.getProject(); |
| 68 | + assertFalse(appEngineMenuExists(project)); |
| 69 | + } |
| 70 | + |
| 71 | + // https://github.com/GoogleCloudPlatform/google-cloud-eclipse/issues/1556 |
| 72 | + @Test |
| 73 | + public void testRunAppEngine_hiddenEvenIfAppEngineProjectFileIsOpen() throws CoreException { |
| 74 | + // Create an empty file in the App Engine project, and open it in an editor. |
| 75 | + IProject appEngineProject = appEngineProjectCreator.getProject(); |
| 76 | + IFile file = appEngineProject.getFile("textfile.txt"); |
| 77 | + file.create(new ByteArrayInputStream(new byte[0]), IFile.FORCE, null); |
| 78 | + |
| 79 | + IWorkbench workbench = PlatformUI.getWorkbench(); |
| 80 | + assertNotNull(WorkbenchUtil.openInEditor(workbench, file)); |
| 81 | + |
| 82 | + IProject javaProject = javaProjectCreator.getProject(); |
| 83 | + assertFalse(appEngineMenuExists(javaProject)); |
| 84 | + } |
| 85 | + |
| 86 | + // We need regex matching, since the actual menu name is "<number> App Engine". |
| 87 | + private static boolean appEngineMenuExists(IProject project) { |
| 88 | + SWTBotTreeItem selected = SwtBotProjectActions.selectProject( |
| 89 | + new SWTWorkbenchBot(), project.getName()); |
| 90 | + Menu runAsMenu = selected.contextMenu("Run As").widget.getMenu(); |
| 91 | + Matcher<MenuItem> matcher = WidgetMatcherFactory.withRegex("App Engine"); |
| 92 | + |
| 93 | + List<MenuItem> menuItems = new MenuFinder().findMenus(runAsMenu, matcher, false); |
| 94 | + return !menuItems.isEmpty(); |
| 95 | + } |
| 96 | +} |
0 commit comments