1505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren/*
2505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren * Copyright (C) 2011 The Android Open Source Project
3505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren *
4505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren * Licensed under the Apache License, Version 2.0 (the "License");
5505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren * you may not use this file except in compliance with the License.
6505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren * You may obtain a copy of the License at
7505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren *
8505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren * http://www.apache.org/licenses/LICENSE-2.0
9505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren *
10505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren * Unless required by applicable law or agreed to in writing, software
11505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren * distributed under the License is distributed on an "AS IS" BASIS,
12505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren * See the License for the specific language governing permissions and
14505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren * limitations under the License.
15505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren */
16505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren
17505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgrenpackage android.view;
18505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren
19505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgrenimport android.content.pm.ActivityInfo;
20505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgrenimport android.os.SystemClock;
21505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgrenimport android.test.ActivityInstrumentationTestCase2;
22505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren
23505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgrenpublic class ViewAttachTest extends
24505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren        ActivityInstrumentationTestCase2<ViewAttachTestActivity> {
25505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren
26505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren    public ViewAttachTest() {
27505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren        super(ViewAttachTestActivity.class);
28505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren    }
29505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren
30505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren    /**
31505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren     * Make sure that onAttachedToWindow and onDetachedToWindow is called in the
32f76a50ce8fdc6aea22cabc77b2977a1a15a79630Ken Wakasa     * correct order. The ViewAttachTestActivity contains a view that will throw
33f76a50ce8fdc6aea22cabc77b2977a1a15a79630Ken Wakasa     * a RuntimeException if onDetachedToWindow and onAttachedToWindow are
34505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren     * called in the wrong order.
35505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren     *
36505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren     * 1. Initiate the activity 2. Perform a series of orientation changes to
37f76a50ce8fdc6aea22cabc77b2977a1a15a79630Ken Wakasa     * the activity (this will force the View hierarchy to be rebuilt,
38505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren     * generating onAttachedToWindow and onDetachedToWindow)
39505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren     *
40505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren     * Expected result: No RuntimeException is thrown from the TestView in
41505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren     * ViewFlipperTestActivity.
42505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren     *
43505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren     * @throws Throwable
44505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren     */
45505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren    public void testAttached() throws Throwable {
46505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren        final ViewAttachTestActivity activity = getActivity();
47505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren        for (int i = 0; i < 20; i++) {
48505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
49505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren            SystemClock.sleep(250);
50505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
51505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren            SystemClock.sleep(250);
52505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren        }
53505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren    }
54505bd0d60d26811ac1e61d2c39a2d5a995d2254dMartin Wallgren}
55