blob: 6e5a68d5afeeddd8b59a4b11f05df1410b517925 [file] [log] [blame]
/*
* Copyright 2017 The Bazel Authors. All rights reserved.
*
* 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.google.idea.blaze.android.sync;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.google.common.collect.ImmutableList;
import com.google.idea.blaze.android.AndroidIntegrationTestCleanupHelper;
import com.google.idea.blaze.android.AndroidIntegrationTestSetupRule;
import com.google.idea.blaze.android.sdk.BlazeSdkProvider;
import com.google.idea.blaze.android.sdk.MockBlazeSdkProvider;
import com.google.idea.blaze.base.ideinfo.AndroidIdeInfo;
import com.google.idea.blaze.base.ideinfo.TargetIdeInfo;
import com.google.idea.blaze.base.ideinfo.TargetMap;
import com.google.idea.blaze.base.ideinfo.TargetMapBuilder;
import com.google.idea.blaze.base.model.BlazeProjectData;
import com.google.idea.blaze.base.model.primitives.WorkspacePath;
import com.google.idea.blaze.base.model.primitives.WorkspaceType;
import com.google.idea.blaze.base.sync.BlazeSyncIntegrationTestCase;
import com.google.idea.blaze.base.sync.BlazeSyncParams;
import com.google.idea.blaze.base.sync.BlazeSyncParams.SyncMode;
import com.google.idea.blaze.base.sync.data.BlazeDataStorage;
import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.SdkTypeId;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.android.facet.AndroidFacet;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Android-specific sync integration tests. */
@RunWith(JUnit4.class)
public class AndroidSyncTest extends BlazeSyncIntegrationTestCase {
@Rule
public final AndroidIntegrationTestSetupRule androidSetupRule =
new AndroidIntegrationTestSetupRule();
@Before
public void setup() {
mockSdk("android-25", "Android 25 SDK");
}
@After
public final void doTeardown() {
AndroidIntegrationTestCleanupHelper.cleanUp(getProject());
}
private void mockSdk(String targetHash, String sdkName) {
SdkTypeId sdkType = mock(SdkTypeId.class);
when(sdkType.getName()).thenReturn("Android SDK");
Sdk sdk = mock(Sdk.class);
when(sdk.getName()).thenReturn(sdkName);
when(sdk.getSdkType()).thenReturn(sdkType);
MockBlazeSdkProvider sdkProvider = (MockBlazeSdkProvider) BlazeSdkProvider.getInstance();
sdkProvider.addSdk(targetHash, sdk);
}
@Test
public void testSimpleSync() throws Exception {
setProjectView(
"directories:",
" java/com/google",
"targets:",
" //java/com/google:lib",
"android_sdk_platform: android-25");
workspace.createFile(
new WorkspacePath("java/com/google/Source.java"),
"package com.google;",
"public class Source {}");
workspace.createFile(
new WorkspacePath("java/com/google/Other.java"),
"package com.google;",
"public class Other {}");
VirtualFile javaRoot = workspace.createDirectory(new WorkspacePath("java/com/google"));
TargetMap targetMap =
TargetMapBuilder.builder()
.addTarget(
TargetIdeInfo.builder()
.setBuildFile(sourceRoot("java/com/google/BUILD"))
.setLabel("//java/com/google:lib")
.setKind("android_library")
.setAndroidInfo(
AndroidIdeInfo.builder()
.setManifestFile(sourceRoot("java/com/google/AndroidManifest.xml"))
.addResource(sourceRoot("java/com/google/res/values/strings.xml"))
.setGenerateResourceClass(true))
.addSource(sourceRoot("java/com/google/Source.java"))
.addSource(sourceRoot("java/com/google/Other.java")))
.build();
setTargetMap(targetMap);
runBlazeSync(
new BlazeSyncParams.Builder("Sync", SyncMode.INCREMENTAL)
.addProjectViewTargets(true)
.build());
errorCollector.assertNoIssues();
BlazeProjectData blazeProjectData =
BlazeProjectDataManager.getInstance(getProject()).getBlazeProjectData();
assertThat(blazeProjectData).isNotNull();
assertThat(blazeProjectData.targetMap).isEqualTo(targetMap);
assertThat(blazeProjectData.workspaceLanguageSettings.getWorkspaceType())
.isEqualTo(WorkspaceType.ANDROID);
ImmutableList<ContentEntry> contentEntries = getWorkspaceContentEntries();
assertThat(contentEntries).hasSize(1);
assertThat(findContentEntry(javaRoot)).isNotNull();
assertThat(findContentEntry(javaRoot).getSourceFolders()).hasLength(1);
// Check that the workspace is set to android
Module workspaceModule = getModuleCreatedDuringSync(BlazeDataStorage.WORKSPACE_MODULE_NAME);
assertThat(workspaceModule).isNotNull();
assertThat(AndroidFacet.getInstance(workspaceModule)).isNotNull();
// Check that a resource module was created
Module resourceModule = getModuleCreatedDuringSync("java.com.google.lib");
assertThat(resourceModule).isNotNull();
assertThat(AndroidFacet.getInstance(resourceModule)).isNotNull();
}
}