1 /* 2 * Copyright (C) 2008 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 17 package android.widget.cts; 18 19 import java.io.IOException; 20 21 import org.xmlpull.v1.XmlPullParserException; 22 23 import android.app.Activity; 24 import android.content.Context; 25 import android.content.res.XmlResourceParser; 26 import android.test.ActivityInstrumentationTestCase; 27 import android.test.UiThreadTest; 28 import android.util.AttributeSet; 29 import android.util.Xml; 30 import android.widget.DigitalClock; 31 import android.widget.LinearLayout; 32 33 import com.android.cts.stub.R; 34 import com.android.internal.util.XmlUtils; 35 36 37 /** 38 * Test {@link DigitalClock}. 39 */ 40 public class DigitalClockTest extends ActivityInstrumentationTestCase<DigitalClockStubActivity> { 41 private Activity mActivity; 42 private Context mContext; 43 44 public DigitalClockTest() { 45 super("com.android.cts.stub", DigitalClockStubActivity.class); 46 } 47 48 @Override 49 protected void setUp() throws Exception { 50 super.setUp(); 51 mActivity = getActivity(); 52 mContext = getInstrumentation().getContext(); 53 } 54 55 public void testConstructor() { 56 // new the DigitalClock instance 57 new DigitalClock(mContext); 58 59 // new the DigitalClock instance with null AttributeSet 60 new DigitalClock(mContext, null); 61 62 // new the DigitalClock instance with real AttributeSet 63 new DigitalClock(mContext, getAttributeSet(R.layout.digitalclock_layout)); 64 65 // Test constructor with null Context, in fact, DigitalClock(mContext) function will 66 //finally invoke this version. 67 try { 68 // Test with null Context 69 new DigitalClock(null, getAttributeSet(R.layout.digitalclock_layout)); 70 fail("should throw NullPointerException"); 71 } catch (Exception e) { 72 } 73 } 74 75 @UiThreadTest 76 public void testOnDetachedFromWindow() { 77 final MockDigitalClock digitalClock = createDigitalClock(); 78 79 final LinearLayout linearLayout = (LinearLayout) mActivity.findViewById( 80 R.id.digitalclock_root); 81 82 assertFalse(digitalClock.hasCalledOnAttachedToWindow()); 83 linearLayout.addView(digitalClock); 84 85 assertTrue(digitalClock.hasCalledOnAttachedToWindow()); 86 linearLayout.removeView(digitalClock); 87 } 88 89 @UiThreadTest 90 public void testOnAttachedToWindow() { 91 final MockDigitalClock digitalClock = createDigitalClock(); 92 93 final LinearLayout linearLayout = (LinearLayout) mActivity.findViewById( 94 R.id.digitalclock_root); 95 96 linearLayout.addView(digitalClock); 97 98 assertFalse(digitalClock.hasCalledOnDetachedFromWindow()); 99 100 // Clear linearLayout 101 linearLayout.removeView(digitalClock); 102 103 assertTrue(digitalClock.hasCalledOnDetachedFromWindow()); 104 } 105 106 private MockDigitalClock createDigitalClock() { 107 MockDigitalClock datePicker = new MockDigitalClock(mContext, 108 getAttributeSet(R.layout.digitalclock_layout)); 109 110 return datePicker; 111 } 112 113 private AttributeSet getAttributeSet(int resourceId) { 114 XmlResourceParser parser = mActivity.getResources().getXml(resourceId); 115 try { 116 XmlUtils.beginDocument(parser, "com.android.cts.stub.alarmclock.DigitalClock"); 117 } catch (XmlPullParserException e) { 118 fail("unexpected XmlPullParserException."); 119 } catch (IOException e) { 120 fail("unexpected IOException."); 121 } 122 AttributeSet attr = Xml.asAttributeSet(parser); 123 assertNotNull(attr); 124 return attr; 125 } 126 127 private class MockDigitalClock extends DigitalClock { 128 private boolean mCalledOnAttachedToWindow = false; 129 private boolean mCalledOnDetachedFromWindow = false; 130 131 public MockDigitalClock(Context context) { 132 super(context); 133 } 134 135 public MockDigitalClock(Context context, AttributeSet attrs) { 136 super(context, attrs); 137 } 138 139 @Override 140 protected void onAttachedToWindow() { 141 super.onAttachedToWindow(); 142 mCalledOnAttachedToWindow = true; 143 } 144 145 public boolean hasCalledOnAttachedToWindow() { 146 return mCalledOnAttachedToWindow; 147 } 148 149 @Override 150 protected void onDetachedFromWindow() { 151 super.onDetachedFromWindow(); 152 mCalledOnDetachedFromWindow = true; 153 } 154 155 public boolean hasCalledOnDetachedFromWindow() { 156 return mCalledOnDetachedFromWindow; 157 } 158 } 159 } 160