aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/functests/sampleProjects/SampleProjectTest.java
blob: 3fb705dfb98046b34a0270c8c285e591a3db233b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
 *
 * 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.ide.eclipse.tests.functests.sampleProjects;

import com.android.SdkConstants;
import com.android.ide.eclipse.adt.AdtUtils;
import com.android.ide.eclipse.adt.internal.wizards.newproject.NewProjectCreator;
import com.android.ide.eclipse.adt.internal.wizards.newproject.NewProjectWizardState;
import com.android.ide.eclipse.adt.internal.wizards.newproject.NewProjectWizardState.Mode;
import com.android.ide.eclipse.tests.SdkLoadingTestCase;
import com.android.sdklib.IAndroidTarget;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Display;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Test case that verifies all SDK sample projects can be imported, and built in
 * Eclipse.
 * <p/>
 * TODO: add support for deploying apps onto emulator and verifying successful
 * execution there
 *
 */
public class SampleProjectTest extends SdkLoadingTestCase {

    private static final Logger sLogger = Logger.getLogger(SampleProjectTest.class.getName());

    /**
     * Finds all samples projects in set SDK and verify they can be built in Eclipse.
     * <p/>
     * TODO: add install and run on emulator test
     * @throws CoreException
     */
    public void testSamples() throws CoreException {
        // TODO: For reporting purposes, it would be better if a separate test success or failure
        // could be reported for each sample
        IAndroidTarget[] targets = getSdk().getTargets();
        for (IAndroidTarget target : targets) {
            doTestSamplesForTarget(target);
        }
    }

    private void doTestSamplesForTarget(IAndroidTarget target) throws CoreException {
        String path = target.getPath(IAndroidTarget.SAMPLES);
        File samples = new File(path);
        if (samples.isDirectory()) {
            File[] files = samples.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    doTestSampleProject(file.getName(), file.getAbsolutePath(), target);
                }
            }
        }
    }

    /**
     * Tests the sample project with the given name
     *
     * @param target - SDK target of project
     * @param name - name of sample project to test
     * @param path - absolute file system path
     * @throws CoreException
     */
    private void doTestSampleProject(String name, String path, IAndroidTarget target)
             throws CoreException {
        IProject iproject = null;
        try {
            sLogger.log(Level.INFO, String.format("Testing sample %s for target %s", name,
                    target.getName()));

            prepareProject(path, target);

            IRunnableContext context = new IRunnableContext() {
                @Override
                public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable)
                        throws InvocationTargetException, InterruptedException {
                    runnable.run(new NullProgressMonitor());
                }
            };
            NewProjectWizardState state = new NewProjectWizardState(Mode.SAMPLE);
            state.projectName = name;
            state.target = target;
            state.packageName = "com.android.samples";
            state.activityName = name;
            state.applicationName = name;
            state.chosenSample = new File(path);
            state.useDefaultLocation = false;
            state.createActivity = false;

            NewProjectCreator creator = new NewProjectCreator(state, context);
            creator.createAndroidProjects();
            iproject = validateProjectExists(name);
            validateNoProblems(iproject);
        }
        catch (CoreException e) {
            sLogger.log(Level.SEVERE,
                    String.format("Unexpected exception when creating sample project %s " +
                            "for target %s", name, target.getName()));
            throw e;
        } finally {
            if (iproject != null) {
                iproject.delete(false, true, new NullProgressMonitor());
            }
        }
    }

    private void prepareProject(String path, IAndroidTarget target) {
        if (target.getVersion().isPreview()) {
            // need to explicitly set preview's version in manifest for project to compile
            final String manifestPath = path + File.separatorChar +
                    SdkConstants.FN_ANDROID_MANIFEST_XML;
            AndroidManifestWriter manifestWriter =
                AndroidManifestWriter.parse(manifestPath);
            assertNotNull(String.format("could not read manifest %s", manifestPath),
                    manifestWriter);
            assertTrue(manifestWriter.setMinSdkVersion(target.getVersion().getApiString()));
        }
    }

    private IProject validateProjectExists(String name) {
        IProject iproject = getIProject(name);
        assertTrue(String.format("%s project not created", name), iproject.exists());
        assertTrue(String.format("%s project not opened", name), iproject.isOpen());
        return iproject;
    }

    private IProject getIProject(String name) {
        IProject iproject = ResourcesPlugin.getWorkspace().getRoot()
                .getProject(name);
        return iproject;
    }

    private void validateNoProblems(IProject iproject) throws CoreException {
        waitForBuild(iproject);

        boolean hasErrors = false;
        StringBuilder failureBuilder = new StringBuilder(String.format("%s project has errors:",
                iproject.getName()));
        IMarker[] markers = iproject.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);
        if (markers != null && markers.length > 0) {
            // the project has marker(s). even though they are "problem" we
            // don't know their severity. so we loop on them and figure if they
            // are warnings or errors
            for (IMarker m : markers) {
                int s = m.getAttribute(IMarker.SEVERITY, -1);
                if (s == IMarker.SEVERITY_ERROR) {
                    hasErrors = true;
                    failureBuilder.append("\n");
                    failureBuilder.append(m.getAttribute(IMarker.MESSAGE, ""));
                }
            }
        }
        failureBuilder.append("Project location: " + AdtUtils.getAbsolutePath(iproject));
        assertFalse(failureBuilder.toString(), hasErrors);
    }

    /**
     * Waits for build to complete.
     *
     * @param iproject
     */
    private void waitForBuild(final IProject iproject) {

        final BuiltProjectDeltaVisitor deltaVisitor = new BuiltProjectDeltaVisitor(iproject);
        IResourceChangeListener newBuildListener = new IResourceChangeListener() {

            @Override
            public void resourceChanged(IResourceChangeEvent event) {
                try {
                    event.getDelta().accept(deltaVisitor);
                }
                catch (CoreException e) {
                    fail();
                }
            }

        };
        iproject.getWorkspace().addResourceChangeListener(newBuildListener,
          IResourceChangeEvent.POST_BUILD);

        // poll build listener to determine when build is done
        // loop max of 1200 times * 50 ms = 60 seconds
        final int maxWait = 1200;
        for (int i=0; i < maxWait; i++) {
            if (deltaVisitor.isProjectBuilt()) {
                return;
            }
            try {
                Thread.sleep(50);
            }
            catch (InterruptedException e) {
                // ignore
            }
           if (Display.getCurrent() != null) {
               Display.getCurrent().readAndDispatch();
           }
        }

        sLogger.log(Level.SEVERE, "expected build event never happened?");
        fail(String.format("Expected build event never happened for %s", iproject.getName()));
    }

    /**
     * Scans a given IResourceDelta looking for a "build event" change for given IProject
     *
     */
    private class BuiltProjectDeltaVisitor implements IResourceDeltaVisitor {

        private IProject mIProject;
        private boolean  mIsBuilt;

        public BuiltProjectDeltaVisitor(IProject iproject) {
            mIProject = iproject;
            mIsBuilt = false;
        }

        @Override
        public boolean visit(IResourceDelta delta) {
            if (mIProject.equals(delta.getResource())) {
                setBuilt(true);
                return false;
            }
            return true;
        }

        private synchronized void setBuilt(boolean b) {
            mIsBuilt = b;
        }

        public synchronized boolean isProjectBuilt() {
            return mIsBuilt;
        }
    }
}