1aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne/*
2aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * Copyright (C) 2011 The Android Open Source Project
3aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne *
4aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * Licensed under the Apache License, Version 2.0 (the "License");
5aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * you may not use this file except in compliance with the License.
6aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * You may obtain a copy of the License at
7aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne *
8aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne *      http://www.apache.org/licenses/LICENSE-2.0
9aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne *
10aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * Unless required by applicable law or agreed to in writing, software
11aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * distributed under the License is distributed on an "AS IS" BASIS,
12aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * See the License for the specific language governing permissions and
14aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne * limitations under the License.
15aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne */
16aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
17aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milnepackage com.android.test.layout;
18aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
19aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.app.Activity;
20aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.content.Context;
21aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.os.Bundle;
22aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.util.Log;
23aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.view.View;
24aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.view.ViewGroup;
25aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milneimport android.widget.Button;
26aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
27f474870fe1189f73cf8ffbaba9e524ef194b5043Philip Milneimport static android.view.Gravity.*;
28f474870fe1189f73cf8ffbaba9e524ef194b5043Philip Milne
29aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milnepublic abstract class AbstractLayoutTest extends Activity {
30aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
31f474870fe1189f73cf8ffbaba9e524ef194b5043Philip Milne    public static final String[] HORIZONTAL_NAMES = { "LEFT", "CENTER", "RIGHT", "FILL" };
32f474870fe1189f73cf8ffbaba9e524ef194b5043Philip Milne    public static final int[] HORIZONTAL_ALIGNMENTS = { LEFT, CENTER, RIGHT, FILL };
33f474870fe1189f73cf8ffbaba9e524ef194b5043Philip Milne    public static final String[] VERTICAL_NAMES = { "TOP", "CENTER", "BASELINE", "BOTTOM", "FILL" };
34f474870fe1189f73cf8ffbaba9e524ef194b5043Philip Milne    public static final int[] VERTICAL_ALIGNMENTS = { TOP, CENTER, NO_GRAVITY, BOTTOM, FILL };
35aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
36aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    public View create(Context context, String name, int size) {
37aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        Button result = new Button(context);
38aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        result.setText(name);
39aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        result.setOnClickListener(new View.OnClickListener() {
40aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            public void onClick(View v) {
41aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne                animate(v);
42aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            }
43aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        });
44aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        return result;
45aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    }
46aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
47aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    public abstract ViewGroup create(Context context);
48aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    public abstract String tag();
49aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
50aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    public void animate(View v) {
51aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        long start = System.currentTimeMillis();
52aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        int N = 1000;
53aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        for (int i = 0; i < N; i++) {
54aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
55aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            lp.topMargin = (lp.topMargin + 1) % 31;
56aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            lp.leftMargin = (lp.leftMargin + 1) % 31;
57aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
58aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            v.requestLayout();
59aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            v.invalidate();
60aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            ViewGroup p = (ViewGroup) v.getParent();
61aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne            p.layout(0, 0, 1000 + (i % 2), 500 + (i % 2));
62aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        }
63aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        Log.d(tag(), "Time: " + (float) (System.currentTimeMillis() - start) / N * 1000 + "mics");
64aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    }
65aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
66aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    protected void onCreate(Bundle savedInstanceState) {
67aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        super.onCreate(savedInstanceState);
68aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne        setContentView(create(getBaseContext()));
69aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne    }
70aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne
71aa616f31fe7c0c8e3657bb9a5889ec5e56ee5232Philip Milne}