summaryrefslogtreecommitdiffstats
path: root/hostsidetests/holo/src/android/holo/cts/HoloHostTest.java
blob: 5de303017c89f16f9d5cebd3c54f93615d634a63 (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
264
265
266
267
268
269
270
271
272
273
274
/*
 * 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 android.holo.cts;

import com.android.cts.tradefed.build.CtsBuildHelper;
import com.android.ddmlib.Log;
import com.android.ddmlib.Log.LogLevel;
import com.android.ddmlib.IShellOutputReceiver;
import com.android.tradefed.build.IBuildInfo;
import com.android.tradefed.device.ITestDevice;
import com.android.tradefed.testtype.DeviceTestCase;
import com.android.tradefed.testtype.IBuildReceiver;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.String;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * Test to check the Holo theme has not been changed.
 */
public class HoloHostTest extends DeviceTestCase implements IBuildReceiver {

    private static final String TAG = HoloHostTest.class.getSimpleName();

    private static final int CAPTURE_TIMEOUT = 1 * 1000;//1sec in ms

    private static final int ADB_TIMEOUT = 10 * 60 * 1000;//10mins in ms

    /** The package name of the APK. */
    private static final String PACKAGE = "android.holo.app";

    /** The file name of the APK. */
    private static final String APK = "CtsHoloDeviceApp.apk";

    /** The class name of the main activity in the APK. */
    private static final String CLASS = "HoloDeviceActivity";

    /** The command to launch the main activity. */
    private static final String START_CMD = String.format(
            "am start -W -a android.intent.action.MAIN -n %s/%s.%s", PACKAGE, PACKAGE, CLASS);

    private static final String STOP_CMD = String.format("am force-stop %s", PACKAGE);

    private static final String DENSITY_PROP = "ro.sf.lcd_density";

    // Intent extras
    protected final static String INTENT_STRING_EXTRA = " --es %s %s";

    protected final static String INTENT_BOOLEAN_EXTRA = " --ez %s %b";

    protected final static String INTENT_INTEGER_EXTRA = " --ei %s %d";

    // Intent extra keys
    private static final String EXTRA_THEME = "holo_theme_extra";

    private static final String EXTRA_LAYOUT = "holo_layout_extra";

    private static final String EXTRA_TIMEOUT = "holo_timeout_extra";

    private static final String[] THEMES = {
            "holo",
            "holo_dialog",
            "holo_dialog_minwidth",
            "holo_dialog_noactionbar",
            "holo_dialog_noactionbar_minwidth",
            "holo_dialogwhenlarge",
            "holo_dialogwhenlarge_noactionbar",
            "holo_inputmethod",
            "holo_light",
            "holo_light_darkactionbar",
            "holo_light_dialog",
            "holo_light_dialog_minwidth",
            "holo_light_dialog_noactionbar",
            "holo_light_dialog_noactionbar_minwidth",
            "holo_light_dialogwhenlarge",
            "holo_light_dialogwhenlarge_noactionbar",
            "holo_light_noactionbar",
            "holo_light_noactionbar_fullscreen",
            "holo_light_panel",
            "holo_noactionbar",
            "holo_noactionbar_fullscreen",
            "holo_panel",
            "holo_wallpaper",
            "holo_wallpaper_notitlebar"
    };

    private final int NUM_THEMES = THEMES.length;

    private static final String[] LAYOUTS = {
            "button",
            "button_pressed",
            "checkbox",
            "checkbox_checked",
            "chronometer"
    };

    private final int NUM_LAYOUTS = LAYOUTS.length;

    private final HashMap<String, File> mReferences = new HashMap<String, File>();

    /** A reference to the build. */
    private CtsBuildHelper mBuild;

    /** A reference to the device under test. */
    private ITestDevice mDevice;

    private ExecutorService mExecutionService;

    private ExecutorCompletionService<Boolean> mCompletionService;

    @Override
    public void setBuild(IBuildInfo buildInfo) {
        // Get the build, this is used to access the APK.
        mBuild = CtsBuildHelper.createBuildHelper(buildInfo);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        // Get the device, this gives a handle to run commands and install APKs.
        mDevice = getDevice();
        // Remove any previously installed versions of this APK.
        mDevice.uninstallPackage(PACKAGE);
        // Get the APK from the build.
        File app = mBuild.getTestApp(APK);
        // Install the APK on the device.
        mDevice.installPackage(app, false);

        final String zip = String.format("/%s.zip",
                getDensityBucket(Integer.parseInt(mDevice.getProperty(DENSITY_PROP))));
        Log.logAndDisplay(LogLevel.INFO, TAG, "Loading resources from " + zip);

        final ZipInputStream in = new ZipInputStream(this.getClass().getResourceAsStream(zip));
        try {
            ZipEntry ze;
            final byte[] buffer = new byte[1024];
            while ((ze = in.getNextEntry()) != null) {
                final String name = ze.getName();
                final File tmp = File.createTempFile("ref_" + name, ".png");
                final FileOutputStream out = new FileOutputStream(tmp);
                int count;
                while ((count = in.read(buffer)) != -1) {
                    out.write(buffer, 0, count);
                }
                out.flush();
                out.close();
                mReferences.put(name, tmp);
            }
        } finally {
            in.close();
        }

        mExecutionService = Executors.newFixedThreadPool(2);// 2 worker threads
        mCompletionService = new ExecutorCompletionService<Boolean>(mExecutionService);
    }

    @Override
    protected void tearDown() throws Exception {
        // Delete the temp files
        for (File ref : mReferences.values()) {
            ref.delete();
        }
        mExecutionService.shutdown();
        super.tearDown();
    }

    public void testHoloThemes() throws Exception {
        int numTasks = 0;
        for (int i = 0; i < NUM_THEMES; i++) {
            final String themeName = THEMES[i];
            for (int j = 0; j < NUM_LAYOUTS; j++) {
                final String name = String.format("%s_%s", themeName, LAYOUTS[j]);
                if (runCapture(i, j)) {
                    final File ref = mReferences.get(name + ".png");
                    mCompletionService.submit(new ComparisonTask(mDevice, ref, name));
                    numTasks++;
                } else {
                    Log.logAndDisplay(LogLevel.ERROR, TAG, "Capture failed: " + name);
                }
            }
        }
        boolean success = true;
        for (int i = 0; i < numTasks; i++) {
            success = mCompletionService.take().get() && success;
        }
        assertTrue("Failures in Holo test", success);
    }

    private boolean runCapture(int themeId, int layoutId) throws Exception {
        final StringBuilder sb = new StringBuilder(START_CMD);
        sb.append(String.format(INTENT_INTEGER_EXTRA, EXTRA_THEME, themeId));
        sb.append(String.format(INTENT_INTEGER_EXTRA, EXTRA_LAYOUT, layoutId));
        sb.append(String.format(INTENT_INTEGER_EXTRA, EXTRA_TIMEOUT, CAPTURE_TIMEOUT));
        final String startCommand = sb.toString();
        // Clear logcat
        mDevice.executeAdbCommand("logcat", "-c");
        // Stop any existing instances
        mDevice.executeShellCommand(STOP_CMD);
        // Start activity
        mDevice.executeShellCommand(startCommand);

        boolean success = false;
        boolean waiting = true;
        while (waiting) {
            // Dump logcat.
            final String logs = mDevice.executeAdbCommand("logcat", "-d", CLASS + ":I", "*:S");
            // Search for string.
            final Scanner in = new Scanner(logs);
            while (in.hasNextLine()) {
                final String line = in.nextLine();
                if (line.startsWith("I/" + CLASS)) {
                    final String s = line.split(":")[1].trim();
                    if (s.equals("OKAY")) {
                        success = true;
                        waiting = false;
                    } else if (s.equals("ERROR")) {
                        success = false;
                        waiting = false;
                    }
                }
            }
        }

        return success;
    }

    private static String getDensityBucket(int density) {
        switch (density) {
            case 120:
                return "ldpi";
            case 160:
                return "mdpi";
            case 213:
                return "tvdpi";
            case 240:
                return "hdpi";
            case 320:
                return "xhdpi";
            case 400:
                return "400dpi";
            case 480:
                return "xxhdpi";
            case 640:
                return "xxxhdpi";
            default:
                return "" + density;
        }
    }
}