Search in sources :

Example 11 with TemplateMetadata

use of com.android.tools.idea.templates.TemplateMetadata 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 12 with TemplateMetadata

use of com.android.tools.idea.templates.TemplateMetadata 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)

Example 13 with TemplateMetadata

use of com.android.tools.idea.templates.TemplateMetadata in project android by JetBrains.

the class NewModuleWizardStateTest method testSetTemplateLocation.

public void testSetTemplateLocation() throws Exception {
    TemplateManager manager = TemplateManager.getInstance();
    List<File> templates = manager.getTemplates(Template.CATEGORY_PROJECTS);
    File androidTemplateFile = null;
    File javaTemplateFile = null;
    for (File template : templates) {
        TemplateMetadata metadata = manager.getTemplateMetadata(template);
        assertNotNull(metadata);
        assertNotNull(metadata.getTitle());
        if (metadata.getTitle().equals(TemplateWizardModuleBuilder.MODULE_NAME)) {
            androidTemplateFile = template;
        } else if (metadata.getTitle().equals("Java Library")) {
            javaTemplateFile = template;
        }
    }
    assertNotNull(androidTemplateFile);
    assertNotNull(javaTemplateFile);
    // Check that the android state is set correctly
    myState.setTemplateLocation(androidTemplateFile);
    assertNotNull(myState.getTemplate());
    assertEquals(myState.getTemplate().getRootPath(), androidTemplateFile);
    // Check that the android state is unset correctly
    myState.setTemplateLocation(javaTemplateFile);
    assertEquals(myState.getTemplate().getRootPath(), javaTemplateFile);
}
Also used : File(java.io.File) TemplateMetadata(com.android.tools.idea.templates.TemplateMetadata)

Aggregations

TemplateMetadata (com.android.tools.idea.templates.TemplateMetadata)13 File (java.io.File)9 TemplateManager (com.android.tools.idea.templates.TemplateManager)7 TemplateWizardState (com.android.tools.idea.wizard.template.TemplateWizardState)2 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 Element (org.w3c.dom.Element)2 SourceProvider (com.android.builder.model.SourceProvider)1 AndroidVersion (com.android.sdklib.AndroidVersion)1 IAndroidTarget (com.android.sdklib.IAndroidTarget)1 AndroidModuleInfo (com.android.tools.idea.model.AndroidModuleInfo)1 MetadataListItem (com.android.tools.idea.npw.ChooseTemplateStep.MetadataListItem)1 FormFactor (com.android.tools.idea.npw.FormFactor)1 NewProjectWizardState (com.android.tools.idea.npw.NewProjectWizardState)1 TemplateProcessingException (com.android.tools.idea.templates.FreemarkerUtils.TemplateProcessingException)1 TemplateUserVisibleException (com.android.tools.idea.templates.FreemarkerUtils.TemplateUserVisibleException)1 Parameter (com.android.tools.idea.templates.Parameter)1 Constraint (com.android.tools.idea.templates.Parameter.Constraint)1 Module (com.intellij.openapi.module.Module)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1