MarginLayoutParamsCompatTest.java revision 3ac77bf186f87ecad4bf0063b2f6c4384efbd56a
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 */ 16package android.support.v4.view; 17 18import android.os.Build; 19import android.support.test.runner.AndroidJUnit4; 20import android.test.AndroidTestCase; 21import android.test.suitebuilder.annotation.SmallTest; 22import android.view.ViewGroup; 23import org.junit.Test; 24import org.junit.runner.RunWith; 25 26import static org.junit.Assert.assertEquals; 27 28@RunWith(AndroidJUnit4.class) 29@SmallTest 30public class MarginLayoutParamsCompatTest { 31 @Test 32 public void testLayoutDirection() { 33 ViewGroup.MarginLayoutParams mlp = new ViewGroup.MarginLayoutParams(0, 0); 34 35 assertEquals("Default LTR layout direction", ViewCompat.LAYOUT_DIRECTION_LTR, 36 MarginLayoutParamsCompat.getLayoutDirection(mlp)); 37 38 MarginLayoutParamsCompat.setLayoutDirection(mlp, ViewCompat.LAYOUT_DIRECTION_RTL); 39 if (Build.VERSION.SDK_INT >= 17) { 40 assertEquals("RTL layout direction", ViewCompat.LAYOUT_DIRECTION_RTL, 41 MarginLayoutParamsCompat.getLayoutDirection(mlp)); 42 } else { 43 assertEquals("Still LTR layout direction on older devices", 44 ViewCompat.LAYOUT_DIRECTION_LTR, 45 MarginLayoutParamsCompat.getLayoutDirection(mlp)); 46 } 47 48 MarginLayoutParamsCompat.setLayoutDirection(mlp, ViewCompat.LAYOUT_DIRECTION_LTR); 49 assertEquals("Back to LTR layout direction", ViewCompat.LAYOUT_DIRECTION_LTR, 50 MarginLayoutParamsCompat.getLayoutDirection(mlp)); 51 } 52 53 @Test 54 public void testMappingOldMarginsToNewMarginsLtr() { 55 ViewGroup.MarginLayoutParams mlp = new ViewGroup.MarginLayoutParams(0, 0); 56 57 mlp.leftMargin = 50; 58 mlp.rightMargin = 80; 59 60 assertEquals("Mapping left to start under LTR", 50, 61 MarginLayoutParamsCompat.getMarginStart(mlp)); 62 assertEquals("Mapping right to end under LTR", 80, 63 MarginLayoutParamsCompat.getMarginEnd(mlp)); 64 } 65 66 @Test 67 public void testMappingOldMarginsToNewMarginsRtl() { 68 ViewGroup.MarginLayoutParams mlp = new ViewGroup.MarginLayoutParams(0, 0); 69 70 mlp.leftMargin = 50; 71 mlp.rightMargin = 80; 72 73 MarginLayoutParamsCompat.setLayoutDirection(mlp, ViewCompat.LAYOUT_DIRECTION_RTL); 74 75 if (Build.VERSION.SDK_INT >= 17) { 76 assertEquals("Mapping right to start under RTL", 80, 77 MarginLayoutParamsCompat.getMarginStart(mlp)); 78 assertEquals("Mapping left to end under RTL", 50, 79 MarginLayoutParamsCompat.getMarginEnd(mlp)); 80 } else { 81 assertEquals("Mapping left to start under RTL on older devices", 50, 82 MarginLayoutParamsCompat.getMarginStart(mlp)); 83 assertEquals("Mapping right to end under RTL on older devices", 80, 84 MarginLayoutParamsCompat.getMarginEnd(mlp)); 85 } 86 } 87 88 @Test 89 public void testMappingNewMarginsToNewMarginsLtr() { 90 ViewGroup.MarginLayoutParams mlp = new ViewGroup.MarginLayoutParams(0, 0); 91 92 MarginLayoutParamsCompat.setMarginStart(mlp, 50); 93 assertEquals("Resolved start margin under LTR", 50, 94 MarginLayoutParamsCompat.getMarginStart(mlp)); 95 // Check that initial end / right margins are still 0 96 assertEquals("Default end margin under LTR", 0, 97 MarginLayoutParamsCompat.getMarginEnd(mlp)); 98 99 MarginLayoutParamsCompat.setMarginEnd(mlp, 80); 100 assertEquals("Resolved end margin under LTR", 80, 101 MarginLayoutParamsCompat.getMarginEnd(mlp)); 102 // Check that start / left margins are still the same 103 assertEquals("Keeping start margin under LTR", 50, 104 MarginLayoutParamsCompat.getMarginStart(mlp)); 105 } 106 107 @Test 108 public void testMappingNewMarginsToNewMarginsRtl() { 109 ViewGroup.MarginLayoutParams mlp = new ViewGroup.MarginLayoutParams(0, 0); 110 111 // Note that unlike the test that checks mapping of left/right to start/end and has 112 // to do platform-specific checks, the checks in this test are platform-agnostic, 113 // relying on the relevant MarginLayoutParamsCompat to do the right mapping internally. 114 115 MarginLayoutParamsCompat.setLayoutDirection(mlp, ViewCompat.LAYOUT_DIRECTION_RTL); 116 MarginLayoutParamsCompat.setMarginStart(mlp, 50); 117 118 assertEquals("Resolved start margin under RTL", 50, 119 MarginLayoutParamsCompat.getMarginStart(mlp)); 120 // Check that initial end / right margins are still 0 121 assertEquals("Default end margin under RTL", 0, 122 MarginLayoutParamsCompat.getMarginEnd(mlp)); 123 124 MarginLayoutParamsCompat.setMarginEnd(mlp, 80); 125 assertEquals("Resolved end margin under RTL", 80, 126 MarginLayoutParamsCompat.getMarginEnd(mlp)); 127 // Check that start / left margins are still the same 128 assertEquals("Keeping start margin under RTL", 50, 129 MarginLayoutParamsCompat.getMarginStart(mlp)); 130 } 131 132 @Test 133 public void testResolveMarginsLtr() { 134 ViewGroup.MarginLayoutParams mlp = new ViewGroup.MarginLayoutParams(0, 0); 135 136 MarginLayoutParamsCompat.setMarginStart(mlp, 50); 137 MarginLayoutParamsCompat.resolveLayoutDirection(mlp, ViewCompat.LAYOUT_DIRECTION_LTR); 138 139 // While there's no guarantee that left/right margin fields have been set / resolved 140 // prior to the resolveLayoutDirection call, they should be now 141 assertEquals("Resolved left margin field under LTR", 50, mlp.leftMargin); 142 assertEquals("Default right margin field under LTR", 0, mlp.rightMargin); 143 144 MarginLayoutParamsCompat.setMarginEnd(mlp, 80); 145 MarginLayoutParamsCompat.resolveLayoutDirection(mlp, ViewCompat.LAYOUT_DIRECTION_LTR); 146 147 assertEquals("Resolved right margin field under LTR", 80, mlp.rightMargin); 148 assertEquals("Keeping left margin field under LTR", 50, mlp.leftMargin); 149 } 150 151 @Test 152 public void testResolveMarginsRtl() { 153 ViewGroup.MarginLayoutParams mlp = new ViewGroup.MarginLayoutParams(0, 0); 154 155 MarginLayoutParamsCompat.setMarginStart(mlp, 50); 156 MarginLayoutParamsCompat.resolveLayoutDirection(mlp, ViewCompat.LAYOUT_DIRECTION_RTL); 157 158 // While there's no guarantee that left/right margin fields have been set / resolved 159 // prior to the resolveLayoutDirection call, they should be now 160 if (Build.VERSION.SDK_INT >= 17) { 161 assertEquals("Default left margin field under RTL", 0, mlp.leftMargin); 162 assertEquals("Resolved right margin field under RTL", 50, mlp.rightMargin); 163 } else { 164 assertEquals("Resolved left margin field under RTL on older devices", 165 50, mlp.leftMargin); 166 assertEquals("Default right margin field under RTL on older devices", 167 0, mlp.rightMargin); 168 } 169 170 MarginLayoutParamsCompat.setMarginEnd(mlp, 80); 171 MarginLayoutParamsCompat.resolveLayoutDirection(mlp, ViewCompat.LAYOUT_DIRECTION_RTL); 172 173 if (Build.VERSION.SDK_INT >= 17) { 174 assertEquals("Resolved left margin field under RTL", 80, mlp.leftMargin); 175 assertEquals("Keeping right margin field under RTL", 50, mlp.rightMargin); 176 } else { 177 assertEquals("Resolved right margin field under RTL on older devices", 178 80, mlp.rightMargin); 179 assertEquals("Keeping left margin field under RTL on older devices", 180 50, mlp.leftMargin); 181 } 182 } 183} 184