Search in sources :

Example 1 with TemplateWizardState

use of com.android.tools.idea.wizard.template.TemplateWizardState in project android by JetBrains.

the class TemplateTest method testJdk5.

public void testJdk5() throws Exception {
    if (DISABLED) {
        return;
    }
    AndroidSdkData sdkData = AndroidSdks.getInstance().tryToChooseAndroidSdk();
    assertNotNull(sdkData);
    IAndroidTarget[] targets = sdkData.getTargets();
    IAndroidTarget target = targets[targets.length - 1];
    Map<String, Object> overrides = new HashMap<>();
    overrides.put(ATTR_JAVA_VERSION, "1.5");
    NewProjectWizardState state = createNewProjectState(true, sdkData);
    // TODO: Allow null activity state!
    File activity = findTemplate("activities", "BasicActivity");
    TemplateWizardState activityState = state.getActivityTemplateState();
    assertNotNull(activity);
    activityState.setTemplateLocation(activity);
    checkApiTarget(9, 18, target, state, "Test15", null, overrides, null);
}
Also used : AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) TemplateWizardState(com.android.tools.idea.wizard.template.TemplateWizardState) IAndroidTarget(com.android.sdklib.IAndroidTarget) NewProjectWizardState(com.android.tools.idea.npw.NewProjectWizardState) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) File(java.io.File)

Example 2 with TemplateWizardState

use of com.android.tools.idea.wizard.template.TemplateWizardState in project android by JetBrains.

the class AssetStudioAssetGeneratorTest method assertThemeUsed.

private static void assertThemeUsed(ActionBarIconGenerator.Theme theme, @Nullable Color color) throws Exception {
    ArgumentCaptor<ActionBarIconGenerator.ActionBarOptions> argument = ArgumentCaptor.forClass(ActionBarIconGenerator.ActionBarOptions.class);
    ActionBarIconGenerator generator = mock(ActionBarIconGenerator.class);
    TemplateWizardState state = new TemplateWizardState();
    AssetStudioAssetGenerator studioGenerator = new AssetStudioAssetGenerator(new TemplateWizardContextAdapter(state), generator, null, null, null);
    pickImage(state);
    state.put(ATTR_ASSET_TYPE, AssetType.ACTIONBAR.name());
    state.put(ATTR_ASSET_THEME, theme.name());
    state.put(ATTR_FOREGROUND_COLOR, color);
    studioGenerator.generateImages(true);
    verify(generator, times(1)).generate(isNull(String.class), any(Map.class), eq(studioGenerator), argument.capture(), anyString());
    assertEquals(theme, argument.getValue().theme);
    if (color != null && theme.equals(ActionBarIconGenerator.Theme.CUSTOM)) {
        assertEquals(color.getRGB(), argument.getValue().customThemeColor);
    }
}
Also used : TemplateWizardState(com.android.tools.idea.wizard.template.TemplateWizardState) AssetStudioAssetGenerator(com.android.tools.idea.npw.AssetStudioAssetGenerator) Map(java.util.Map)

Example 3 with TemplateWizardState

use of com.android.tools.idea.wizard.template.TemplateWizardState in project android by JetBrains.

the class TemplateTest method createProject.

private static void createProject(@NotNull final NewModuleWizardState wizardState, @NotNull Project project, @Nullable AssetStudioAssetGenerator assetGenerator) {
    List<String> errors = Lists.newArrayList();
    try {
        wizardState.populateDirectoryParameters();
        String moduleName = wizardState.getString(ATTR_MODULE_NAME);
        String projectName = wizardState.getString(ATTR_APP_TITLE);
        File projectRoot = new File(wizardState.getString(ATTR_PROJECT_LOCATION));
        File moduleRoot = new File(projectRoot, moduleName);
        if (FileUtilRt.createDirectory(projectRoot)) {
            if (wizardState.getBoolean(ATTR_CREATE_ICONS) && assetGenerator != null) {
                assetGenerator.outputImagesIntoDefaultVariant(moduleRoot);
            }
            wizardState.updateParameters();
            wizardState.updateDependencies();
            // If this is a new project, instantiate the project-level files
            if (wizardState instanceof NewProjectWizardState) {
                Template projectTemplate = ((NewProjectWizardState) wizardState).getProjectTemplate();
                final RenderingContext projectContext = RenderingContext.Builder.newContext(projectTemplate, project).withOutputRoot(projectRoot).withModuleRoot(moduleRoot).withParams(wizardState.myParameters).build();
                projectTemplate.render(projectContext);
                AndroidGradleModuleUtils.setGradleWrapperExecutable(projectRoot);
            }
            final RenderingContext context = RenderingContext.Builder.newContext(wizardState.myTemplate, project).withOutputRoot(projectRoot).withModuleRoot(moduleRoot).withParams(wizardState.myParameters).build();
            wizardState.myTemplate.render(context);
            if (wizardState.getBoolean(ATTR_CREATE_ACTIVITY)) {
                TemplateWizardState activityTemplateState = wizardState.getActivityTemplateState();
                activityTemplateState.populateRelativePackage(null);
                Template template = activityTemplateState.getTemplate();
                assert template != null;
                final RenderingContext activityContext = RenderingContext.Builder.newContext(template, project).withOutputRoot(moduleRoot).withModuleRoot(moduleRoot).withParams(activityTemplateState.myParameters).build();
                template.render(activityContext);
                context.getFilesToOpen().addAll(activityContext.getFilesToOpen());
            }
        } else {
            errors.add(String.format("Unable to create directory '%1$s'.", projectRoot.getPath()));
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    assertEmpty(errors);
}
Also used : RenderingContext(com.android.tools.idea.templates.recipe.RenderingContext) TemplateWizardState(com.android.tools.idea.wizard.template.TemplateWizardState) NewProjectWizardState(com.android.tools.idea.npw.NewProjectWizardState) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) File(java.io.File) IOException(java.io.IOException)

Example 4 with TemplateWizardState

use of com.android.tools.idea.wizard.template.TemplateWizardState in project android by JetBrains.

the class TemplateTest method checkApiTarget.

/**
   * Checks creating the given project and template for the given SDK versions
   */
private void checkApiTarget(int minSdk, int targetSdk, @NonNull IAndroidTarget target, @NonNull NewProjectWizardState projectValues, @NonNull String projectNameBase, @Nullable TemplateWizardState templateValues, @Nullable Map<String, Object> overrides, @Nullable Map<String, Object> projectOverrides) throws Exception {
    Boolean createActivity = (Boolean) projectValues.get(ATTR_CREATE_ACTIVITY);
    if (createActivity == null) {
        createActivity = true;
    }
    TemplateWizardState values = createActivity ? projectValues.getActivityTemplateState() : templateValues;
    assertNotNull(values);
    projectValues.put(ATTR_MIN_API, Integer.toString(minSdk));
    projectValues.put(ATTR_MIN_API_LEVEL, minSdk);
    projectValues.put(ATTR_TARGET_API, targetSdk);
    projectValues.put(ATTR_TARGET_API_STRING, Integer.toString(targetSdk));
    projectValues.put(ATTR_BUILD_API, target.getVersion().getApiLevel());
    projectValues.put(ATTR_BUILD_API_STRING, getBuildApiString(target.getVersion()));
    assertNotNull(values);
    // Next check all other parameters, cycling through booleans and enums.
    Template templateHandler = values.getTemplate();
    assertNotNull(templateHandler);
    TemplateMetadata template = templateHandler.getMetadata();
    assertNotNull(template);
    Collection<Parameter> parameters = template.getParameters();
    if (!createActivity) {
        values.setParameterDefaults();
    }
    if (overrides != null) {
        for (Map.Entry<String, Object> entry : overrides.entrySet()) {
            values.put(entry.getKey(), entry.getValue());
        }
    }
    if (projectOverrides != null) {
        for (Map.Entry<String, Object> entry : projectOverrides.entrySet()) {
            projectValues.put(entry.getKey(), entry.getValue());
        }
    }
    String projectName;
    for (Parameter parameter : parameters) {
        if (parameter.type == Parameter.Type.SEPARATOR || parameter.type == Parameter.Type.STRING) {
            // TODO: Consider whether we should attempt some strings here
            continue;
        }
        if (overrides != null && overrides.containsKey(parameter.id)) {
            continue;
        }
        assertNotNull(parameter.id);
        // The initial (default value); revert to this one after cycling,
        Object initial = values.get(parameter.id);
        if (parameter.type == Parameter.Type.ENUM) {
            List<Element> options = parameter.getOptions();
            for (Element element : options) {
                Option option = Option.get(element);
                String optionId = option.id;
                int optionMinSdk = option.minSdk;
                int optionMinBuildApi = option.minBuild;
                int projectMinApi = projectValues.getInt(ATTR_MIN_API_LEVEL);
                int projectBuildApi = projectValues.getInt(ATTR_BUILD_API);
                if (projectMinApi >= optionMinSdk && projectBuildApi >= optionMinBuildApi) {
                    values.put(parameter.id, optionId);
                    projectName = projectNameBase + "_" + parameter.id + "_" + optionId;
                    checkProject(projectName, projectValues, templateValues);
                    if (!TEST_VARIABLE_COMBINATIONS) {
                        break;
                    }
                }
            }
        } else {
            assert parameter.type == Parameter.Type.BOOLEAN;
            if (parameter.id.equals(ATTR_IS_LAUNCHER) && createActivity) {
                // Skipping this one: always true when launched from new project
                continue;
            }
            boolean value = false;
            //noinspection ConstantConditions
            values.put(parameter.id, value);
            //noinspection ConstantConditions
            projectName = projectNameBase + "_" + parameter.id + "_" + value;
            checkProject(projectName, projectValues, templateValues);
            if (!TEST_VARIABLE_COMBINATIONS) {
                break;
            }
            value = true;
            //noinspection ConstantConditions
            values.put(parameter.id, value);
            //noinspection ConstantConditions
            projectName = projectNameBase + "_" + parameter.id + "_" + value;
            checkProject(projectName, projectValues, templateValues);
        }
        values.put(parameter.id, initial);
    }
    projectName = projectNameBase + "_default";
    checkProject(projectName, projectValues, templateValues);
}
Also used : Element(org.w3c.dom.Element) TemplateWizardState(com.android.tools.idea.wizard.template.TemplateWizardState) TemplateMetadata(com.android.tools.idea.templates.TemplateMetadata)

Example 5 with TemplateWizardState

use of com.android.tools.idea.wizard.template.TemplateWizardState in project android by JetBrains.

the class TemplateTest method checkTemplate.

private void checkTemplate(File templateFile, boolean createWithProject, @Nullable Map<String, Object> overrides, @Nullable Map<String, Object> projectOverrides) throws Exception {
    if (KNOWN_BROKEN.contains(templateFile.getName())) {
        return;
    }
    AndroidSdkData sdkData = AndroidSdks.getInstance().tryToChooseAndroidSdk();
    assertNotNull(sdkData);
    NewProjectWizardState values = createNewProjectState(createWithProject, sdkData);
    String projectNameBase = "TestProject" + templateFile.getName();
    TemplateWizardState state = values.getActivityTemplateState();
    state.setTemplateLocation(templateFile);
    // Iterate over all (valid) combinations of build target, minSdk and targetSdk
    // TODO: Assert that the SDK manager has a minimum set of SDKs installed needed to be certain
    // the test is comprehensive
    // For now make sure there's at least one
    boolean ranTest = false;
    IAndroidTarget[] targets = sdkData.getTargets();
    for (int i = targets.length - 1; i >= 0; i--) {
        IAndroidTarget target = targets[i];
        if (!target.isPlatform()) {
            continue;
        }
        if (!isInterestingApiLevel(target.getVersion().getApiLevel(), MANUAL_BUILD_API)) {
            continue;
        }
        TemplateMetadata activityMetadata = state.getTemplateMetadata();
        TemplateMetadata projectMetadata = values.getTemplateMetadata();
        assertNotNull(activityMetadata);
        assertNotNull(projectMetadata);
        int lowestSupportedApi = Math.max(projectMetadata.getMinSdk(), activityMetadata.getMinSdk());
        for (int minSdk = lowestSupportedApi; minSdk <= SdkVersionInfo.HIGHEST_KNOWN_API; minSdk++) {
            // Don't bother checking *every* single minSdk, just pick some interesting ones
            if (!isInterestingApiLevel(minSdk, MANUAL_MIN_API)) {
                continue;
            }
            for (int targetSdk = minSdk; targetSdk <= SdkVersionInfo.HIGHEST_KNOWN_API; targetSdk++) {
                if (!isInterestingApiLevel(targetSdk, MANUAL_TARGET_API)) {
                    continue;
                }
                String status = validateTemplate(projectMetadata, minSdk, target.getVersion().getApiLevel());
                if (status != null) {
                    continue;
                }
                // Also make sure activity is enabled for these versions
                status = validateTemplate(activityMetadata, minSdk, target.getVersion().getApiLevel());
                if (status != null) {
                    continue;
                }
                // Iterate over all new new project templates
                // should I try all options of theme with all platforms?
                // or just try all platforms, with one setting for each?
                // doesn't seem like I need to multiply
                // just pick the best setting that applies instead for each platform
                Collection<Parameter> parameters = projectMetadata.getParameters();
                projectParameters: for (Parameter parameter : parameters) {
                    if (overrides != null) {
                        String base = projectNameBase + "_min_" + minSdk + "_target_" + targetSdk + "_build_" + target.getVersion().getApiLevel() + "_overrides";
                        checkApiTarget(minSdk, targetSdk, target, values, base, state, overrides, projectOverrides);
                        ranTest = true;
                        break projectParameters;
                    }
                    List<Element> options = parameter.getOptions();
                    if (parameter.type == Parameter.Type.ENUM) {
                        assertNotNull(parameter.id);
                        for (Element element : options) {
                            Option option = Option.get(element);
                            String optionId = option.id;
                            int optionMinSdk = option.minSdk;
                            int optionMinBuildApi = option.minBuild;
                            if (optionMinSdk <= minSdk && optionMinBuildApi <= target.getVersion().getApiLevel()) {
                                values.put(parameter.id, optionId);
                                if (parameter.id.equals("baseTheme")) {
                                    String base = projectNameBase + "_min_" + minSdk + "_target_" + targetSdk + "_build_" + target.getVersion().getApiLevel() + "_theme_" + optionId;
                                    checkApiTarget(minSdk, targetSdk, target, values, base, state, null, null);
                                    ranTest = true;
                                    if (!TEST_VARIABLE_COMBINATIONS) {
                                        break projectParameters;
                                    }
                                }
                            }
                        }
                    }
                }
                if (TEST_JUST_ONE_TARGET_SDK_VERSION) {
                    break;
                }
            }
            if (TEST_JUST_ONE_MIN_SDK) {
                break;
            }
        }
        if (TEST_JUST_ONE_BUILD_TARGET) {
            break;
        }
    }
    assertTrue("Didn't run any tests! Make sure you have the right platforms installed.", ranTest);
}
Also used : Element(org.w3c.dom.Element) TemplateWizardState(com.android.tools.idea.wizard.template.TemplateWizardState) IAndroidTarget(com.android.sdklib.IAndroidTarget) NewProjectWizardState(com.android.tools.idea.npw.NewProjectWizardState) AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) TemplateMetadata(com.android.tools.idea.templates.TemplateMetadata)

Aggregations

TemplateWizardState (com.android.tools.idea.wizard.template.TemplateWizardState)7 NewProjectWizardState (com.android.tools.idea.npw.NewProjectWizardState)4 File (java.io.File)4 IAndroidTarget (com.android.sdklib.IAndroidTarget)3 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 AndroidSdkData (org.jetbrains.android.sdk.AndroidSdkData)3 TemplateMetadata (com.android.tools.idea.templates.TemplateMetadata)2 Element (org.w3c.dom.Element)2 AssetStudioAssetGenerator (com.android.tools.idea.npw.AssetStudioAssetGenerator)1 RenderingContext (com.android.tools.idea.templates.recipe.RenderingContext)1 IOException (java.io.IOException)1 Map (java.util.Map)1