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 android.widget.cts.R; 20 21 22 import org.xmlpull.v1.XmlPullParser; 23 24 import android.app.Activity; 25 import android.cts.util.PollingCheck; 26 import android.test.ActivityInstrumentationTestCase2; 27 import android.test.UiThreadTest; 28 import android.util.AttributeSet; 29 import android.util.Xml; 30 import android.view.View; 31 import android.view.View.OnClickListener; 32 import android.widget.ListView; 33 import android.widget.ZoomButton; 34 35 public class ZoomButtonTest extends ActivityInstrumentationTestCase2<ZoomButtonCtsActivity> { 36 private ZoomButton mZoomButton; 37 private Activity mActivity; 38 39 public ZoomButtonTest() { 40 super("android.widget.cts", ZoomButtonCtsActivity.class); 41 } 42 43 @Override 44 protected void setUp() throws Exception { 45 super.setUp(); 46 47 mZoomButton = (ZoomButton) getActivity().findViewById(R.id.zoombutton_test); 48 mActivity = getActivity(); 49 } 50 51 @UiThreadTest 52 public void testConstructor() { 53 new ZoomButton(mActivity); 54 55 new ZoomButton(mActivity, null); 56 57 new ZoomButton(mActivity, null, 0); 58 59 XmlPullParser parser = mActivity.getResources().getXml(R.layout.zoombutton_layout); 60 AttributeSet attrs = Xml.asAttributeSet(parser); 61 assertNotNull(attrs); 62 new ZoomButton(mActivity, attrs); 63 new ZoomButton(mActivity, attrs, 0); 64 65 try { 66 new ZoomButton(null); 67 fail("should throw NullPointerException."); 68 } catch (NullPointerException e) { 69 } 70 71 try { 72 new ZoomButton(null, null); 73 fail("should throw NullPointerException."); 74 } catch (NullPointerException e) { 75 } 76 77 try { 78 new ZoomButton(null, null, 0); 79 fail("should throw NullPointerException."); 80 } catch (NullPointerException e) { 81 } 82 } 83 84 public void testSetEnabled() { 85 assertFalse(mZoomButton.isPressed()); 86 mZoomButton.setEnabled(true); 87 assertTrue(mZoomButton.isEnabled()); 88 assertFalse(mZoomButton.isPressed()); 89 90 mZoomButton.setPressed(true); 91 assertTrue(mZoomButton.isPressed()); 92 mZoomButton.setEnabled(true); 93 assertTrue(mZoomButton.isEnabled()); 94 assertTrue(mZoomButton.isPressed()); 95 96 mZoomButton.setEnabled(false); 97 assertFalse(mZoomButton.isEnabled()); 98 assertFalse(mZoomButton.isPressed()); 99 } 100 101 @UiThreadTest 102 public void testDispatchUnhandledMove() { 103 assertFalse(mZoomButton.dispatchUnhandledMove(new ListView(mActivity), View.FOCUS_DOWN)); 104 105 assertFalse(mZoomButton.dispatchUnhandledMove(null, View.FOCUS_DOWN)); 106 } 107 108 public void testOnLongClick() { 109 final MockOnClickListener listener = new MockOnClickListener(); 110 mZoomButton.setOnClickListener(listener); 111 mZoomButton.setEnabled(true); 112 long speed = 2000; 113 mZoomButton.setZoomSpeed(speed); 114 115 assertFalse(listener.hasOnClickCalled()); 116 mZoomButton.performLongClick(); 117 new PollingCheck(speed + 500) { 118 @Override 119 protected boolean check() { 120 return listener.hasOnClickCalled(); 121 } 122 }; 123 } 124 125 public void testOnTouchEvent() { 126 // Do not test. Implementation details. 127 } 128 129 public void testOnKeyUp() { 130 // Do not test. Implementation details. 131 } 132 133 public void testSetZoomSpeed() { 134 mZoomButton.setZoomSpeed(100); 135 136 mZoomButton.setZoomSpeed(-1); 137 // TODO: how to check? 138 } 139 140 private static class MockOnClickListener implements OnClickListener { 141 private boolean mOnClickCalled = false; 142 143 public boolean hasOnClickCalled() { 144 return mOnClickCalled; 145 } 146 147 public void onClick(View v) { 148 mOnClickCalled = true; 149 } 150 } 151 } 152