1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
19import android.content.pm.ActivityInfo;
20import android.os.SystemClock;
21import android.test.ActivityInstrumentationTestCase2;
22
23public class ViewAttachTest extends
24        ActivityInstrumentationTestCase2<ViewAttachTestActivity> {
25
26    public ViewAttachTest() {
27        super(ViewAttachTestActivity.class);
28    }
29
30    /**
31     * Make sure that onAttachedToWindow and onDetachedToWindow is called in the
32     * correct order. The ViewAttachTestActivity contains a view that will throw
33     * a RuntimeException if onDetachedToWindow and onAttachedToWindow are
34     * called in the wrong order.
35     *
36     * 1. Initiate the activity 2. Perform a series of orientation changes to
37     * the activity (this will force the View hierarchy to be rebuilt,
38     * generating onAttachedToWindow and onDetachedToWindow)
39     *
40     * Expected result: No RuntimeException is thrown from the TestView in
41     * ViewFlipperTestActivity.
42     *
43     * @throws Throwable
44     */
45    public void testAttached() throws Throwable {
46        final ViewAttachTestActivity activity = getActivity();
47        for (int i = 0; i < 20; i++) {
48            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
49            SystemClock.sleep(250);
50            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
51            SystemClock.sleep(250);
52        }
53    }
54}
55