1 /* 2 * Copyright (C) 2015 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.util.cts; 18 19 import static org.junit.Assert.*; 20 21 import android.graphics.Point; 22 import android.util.FloatProperty; 23 import android.util.IntProperty; 24 import android.util.Property; 25 26 import androidx.test.filters.SmallTest; 27 import androidx.test.runner.AndroidJUnit4; 28 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 32 @SmallTest 33 @RunWith(AndroidJUnit4.class) 34 public class PropertyTest { 35 private float mFloatValue = -1; 36 private int mIntValue = -2; 37 private Point mPointValue = new Point(-3, -4); 38 39 @Test testProperty()40 public void testProperty() throws Exception { 41 float testFloatValue = 5; 42 Point testPointValue = new Point(10, 20); 43 44 assertFalse(getFloatProp() == testFloatValue); 45 assertFalse(getPointProp().equals(testPointValue)); 46 assertEquals(getFloatProp(), RAW_FLOAT_PROP.get(this), 0f); 47 assertEquals(getPointProp(), RAW_POINT_PROP.get(this)); 48 49 RAW_FLOAT_PROP.set(this, testFloatValue); 50 assertEquals(mFloatValue, RAW_FLOAT_PROP.get(this), 0f); 51 52 RAW_POINT_PROP.set(this, testPointValue); 53 assertEquals(testPointValue, RAW_POINT_PROP.get(this)); 54 } 55 56 @Test testFloatProperty()57 public void testFloatProperty() throws Exception { 58 assertFalse(getFloatProp() == 5); 59 assertEquals(getFloatProp(), FLOAT_PROP.get(this), 0f); 60 61 FLOAT_PROP.set(this, 5f); 62 assertEquals(5f, FLOAT_PROP.get(this), 0f); 63 64 FLOAT_PROP.setValue(this, 10); 65 assertEquals(10f, FLOAT_PROP.get(this), 0f); 66 } 67 68 @Test testIntProperty()69 public void testIntProperty() throws Exception { 70 assertFalse(getIntProp() == 5); 71 assertEquals(getIntProp(), INT_PROP.get(this).intValue()); 72 73 INT_PROP.set(this, 5); 74 assertEquals(5, INT_PROP.get(this).intValue()); 75 76 INT_PROP.setValue(this, 10); 77 assertEquals(10, INT_PROP.get(this).intValue()); 78 } 79 80 // Utility methods to get/set instance values. Used by Property classes below. 81 setFloatProp(float value)82 private void setFloatProp(float value) { 83 mFloatValue = value; 84 } 85 getFloatProp()86 private float getFloatProp() { 87 return mFloatValue; 88 } 89 setIntProp(int value)90 private void setIntProp(int value) { 91 mIntValue = value; 92 } 93 getIntProp()94 private int getIntProp() { 95 return mIntValue; 96 } 97 setPointProp(Point value)98 private void setPointProp(Point value) { 99 mPointValue = value; 100 } 101 getPointProp()102 private Point getPointProp() { 103 return mPointValue; 104 } 105 106 // Properties. RAW subclass from the generic Property class, the others subclass from 107 // the primitive-friendly IntProperty and FloatProperty subclasses. 108 109 private static final Property<PropertyTest, Point> RAW_POINT_PROP = 110 new Property<PropertyTest, Point>(Point.class, "rawPoint") { 111 @Override 112 public void set(PropertyTest object, Point value) { 113 object.setPointProp(value); 114 } 115 116 @Override 117 public Point get(PropertyTest object) { 118 return object.getPointProp(); 119 } 120 }; 121 122 private static final Property<PropertyTest, Float> RAW_FLOAT_PROP = 123 new Property<PropertyTest, Float>(Float.class, "rawFloat") { 124 @Override 125 public void set(PropertyTest object, Float value) { 126 object.setFloatProp(value); 127 } 128 129 @Override 130 public Float get(PropertyTest object) { 131 return object.getFloatProp(); 132 } 133 }; 134 135 private static final FloatProperty<PropertyTest> FLOAT_PROP = 136 new FloatProperty<PropertyTest>("float") { 137 138 @Override 139 public void setValue(PropertyTest object, float value) { 140 object.setFloatProp(value); 141 } 142 143 @Override 144 public Float get(PropertyTest object) { 145 return object.getFloatProp(); 146 } 147 }; 148 149 private static final IntProperty<PropertyTest> INT_PROP = 150 new IntProperty<PropertyTest>("int") { 151 152 @Override 153 public void setValue(PropertyTest object, int value) { 154 object.setIntProp(value); 155 } 156 157 @Override 158 public Integer get(PropertyTest object) { 159 return object.getIntProp(); 160 } 161 }; 162 } 163