/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.tools.idea.tests.gui.gradle; import com.android.tools.idea.tests.gui.framework.GuiTestCase; import com.android.tools.idea.tests.gui.framework.annotation.IdeGuiTest; import com.android.tools.idea.tests.gui.framework.fixture.IdeFrameFixture; import com.intellij.openapi.module.Module; import com.intellij.openapi.roots.ContentEntry; import com.intellij.openapi.roots.ModifiableRootModel; import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.VirtualFile; import org.fest.swing.edt.GuiActionRunner; import org.fest.swing.edt.GuiQuery; import org.junit.Test; import java.io.File; import java.io.IOException; import static com.android.tools.idea.gradle.util.FilePaths.findParentContentEntry; import static com.android.tools.idea.tests.gui.framework.GuiTests.getTestProjectsRootDirPath; import static com.intellij.openapi.util.io.FileUtil.delete; import static com.intellij.openapi.vfs.VfsUtil.findFileByIoFile; import static com.intellij.openapi.vfs.VfsUtilCore.isAncestor; import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertTrue; import static org.fest.assertions.Assertions.assertThat; /** * Tests fix for issue <a href="https://code.google.com/p/android/issues/detail?id=74259">74259</a>. */ public class CentralBuildDirectoryTest extends GuiTestCase { @Test @IdeGuiTest public void testImportProjectWithCentralBuildDirectoryInRootModule() throws IOException { // In issue 74259, project sync fails because the "app" build directory is set to "CentralBuildDirectory/central/build", which is // outside the content root of the "app" module. String projectDirName = "CentralBuildDirectory"; File projectPath = new File(getTestProjectsRootDirPath(), projectDirName); // The bug appears only when the central build folder does not exist. final File centralBuildDirPath = new File(projectPath, FileUtil.join("central", "build")); File centralBuildParentDirPath = centralBuildDirPath.getParentFile(); delete(centralBuildParentDirPath); IdeFrameFixture ideFrame = importProject(projectDirName); final Module app = ideFrame.getModule("app"); // Now we have to make sure that if project import was successful, the build folder (with custom path) is excluded in the IDE (to // prevent unnecessary file indexing, which decreases performance.) VirtualFile[] excludeFolders = GuiActionRunner.execute(new GuiQuery<VirtualFile[]>() { @Override protected VirtualFile[] executeInEDT() throws Throwable { ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(app); ModifiableRootModel rootModel = moduleRootManager.getModifiableModel(); try { ContentEntry[] contentEntries = rootModel.getContentEntries(); ContentEntry parent = findParentContentEntry(centralBuildDirPath, contentEntries); assertNotNull(parent); return parent.getExcludeFolderFiles(); } finally { rootModel.dispose(); } } }); assertThat(excludeFolders).isNotEmpty(); VirtualFile centralBuildDir = findFileByIoFile(centralBuildParentDirPath, true); assertNotNull(centralBuildDir); boolean isExcluded = false; for (VirtualFile folder : excludeFolders) { if (isAncestor(centralBuildDir, folder, true)) { isExcluded = true; break; } } assertTrue(isExcluded); } }