Home | History | Annotate | Download | only in view
      1 /*
      2  * Copyright 2018 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 package androidx.core.view;
     17 
     18 import static org.junit.Assert.assertEquals;
     19 
     20 import android.graphics.Rect;
     21 import android.os.Build;
     22 import android.support.test.filters.SmallTest;
     23 import android.support.test.runner.AndroidJUnit4;
     24 import android.support.v4.testutils.TestUtils;
     25 import android.view.Gravity;
     26 
     27 import org.junit.Test;
     28 import org.junit.runner.RunWith;
     29 
     30 @RunWith(AndroidJUnit4.class)
     31 @SmallTest
     32 public class GravityCompatTest {
     33     @Test
     34     public void testConstants() {
     35         // Compat constants must match core constants since they can be OR'd with
     36         // other core constants.
     37         assertEquals("Start constants", Gravity.START, GravityCompat.START);
     38         assertEquals("End constants", Gravity.END, GravityCompat.END);
     39     }
     40 
     41     @Test
     42     public void testGetAbsoluteGravity() {
     43         assertEquals("Left under LTR",
     44                 GravityCompat.getAbsoluteGravity(Gravity.LEFT, ViewCompat.LAYOUT_DIRECTION_LTR),
     45                 Gravity.LEFT);
     46         assertEquals("Right under LTR",
     47                 GravityCompat.getAbsoluteGravity(Gravity.RIGHT, ViewCompat.LAYOUT_DIRECTION_LTR),
     48                 Gravity.RIGHT);
     49         assertEquals("Left under RTL",
     50                 GravityCompat.getAbsoluteGravity(Gravity.LEFT, ViewCompat.LAYOUT_DIRECTION_RTL),
     51                 Gravity.LEFT);
     52         assertEquals("Right under RTL",
     53                 GravityCompat.getAbsoluteGravity(Gravity.RIGHT, ViewCompat.LAYOUT_DIRECTION_RTL),
     54                 Gravity.RIGHT);
     55 
     56         assertEquals("Start under LTR",
     57                 GravityCompat.getAbsoluteGravity(GravityCompat.START,
     58                         ViewCompat.LAYOUT_DIRECTION_LTR),
     59                 Gravity.LEFT);
     60         assertEquals("End under LTR",
     61                 GravityCompat.getAbsoluteGravity(GravityCompat.END,
     62                         ViewCompat.LAYOUT_DIRECTION_LTR),
     63                 Gravity.RIGHT);
     64 
     65         if (Build.VERSION.SDK_INT >= 17) {
     66             // The following tests are only expected to pass on v17+ devices
     67             assertEquals("Start under RTL",
     68                     GravityCompat.getAbsoluteGravity(GravityCompat.START,
     69                             ViewCompat.LAYOUT_DIRECTION_RTL),
     70                     Gravity.RIGHT);
     71             assertEquals("End under RTL",
     72                     GravityCompat.getAbsoluteGravity(GravityCompat.END,
     73                             ViewCompat.LAYOUT_DIRECTION_RTL),
     74                     Gravity.LEFT);
     75         } else {
     76             // And on older devices START is always LEFT, END is always RIGHT
     77             assertEquals("Start under RTL",
     78                     GravityCompat.getAbsoluteGravity(GravityCompat.START,
     79                             ViewCompat.LAYOUT_DIRECTION_RTL),
     80                     Gravity.LEFT);
     81             assertEquals("End under RTL",
     82                     GravityCompat.getAbsoluteGravity(GravityCompat.END,
     83                             ViewCompat.LAYOUT_DIRECTION_RTL),
     84                     Gravity.RIGHT);
     85         }
     86     }
     87 
     88     @Test
     89     public void testApplyNoOffsetsLtr() {
     90         Rect outRect = new Rect();
     91 
     92         // Left / top aligned under LTR direction
     93         GravityCompat.apply(Gravity.LEFT | Gravity.TOP, 100, 50,
     94                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_LTR);
     95         TestUtils.assertRectangleBounds("Left / top aligned under LTR: ",
     96                 outRect, 0, 0, 100, 50);
     97 
     98         // Center / top aligned under LTR direction
     99         GravityCompat.apply(Gravity.CENTER_HORIZONTAL | Gravity.TOP, 100, 50,
    100                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_LTR);
    101         TestUtils.assertRectangleBounds("Center / top aligned under LTR: ",
    102                 outRect, 50, 0, 150, 50);
    103 
    104         // Right / top aligned under LTR direction
    105         GravityCompat.apply(Gravity.RIGHT | Gravity.TOP, 100, 50,
    106                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_LTR);
    107         TestUtils.assertRectangleBounds("Right / top aligned under LTR: ",
    108                 outRect, 100, 0, 200, 50);
    109 
    110         // Left / center aligned under LTR direction
    111         GravityCompat.apply(Gravity.LEFT | Gravity.CENTER_VERTICAL, 100, 50,
    112                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_LTR);
    113         TestUtils.assertRectangleBounds("Left / center aligned under LTR: ",
    114                 outRect, 0, 25, 100, 75);
    115 
    116         // Center / center aligned under LTR direction
    117         GravityCompat.apply(Gravity.CENTER, 100, 50,
    118                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_LTR);
    119         TestUtils.assertRectangleBounds("Center / center aligned under LTR: ",
    120                 outRect, 50, 25, 150, 75);
    121 
    122         // Right / center aligned under LTR direction
    123         GravityCompat.apply(Gravity.RIGHT | Gravity.CENTER_VERTICAL, 100, 50,
    124                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_LTR);
    125         TestUtils.assertRectangleBounds("Right / center aligned under LTR: ",
    126                 outRect, 100, 25, 200, 75);
    127 
    128         // Left / bottom aligned under LTR direction
    129         GravityCompat.apply(Gravity.LEFT | Gravity.BOTTOM, 100, 50,
    130                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_LTR);
    131         TestUtils.assertRectangleBounds("Left / bottom aligned under LTR: ",
    132                 outRect, 0, 50, 100, 100);
    133 
    134         // Center / bottom aligned under LTR direction
    135         GravityCompat.apply(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 100, 50,
    136                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_LTR);
    137         TestUtils.assertRectangleBounds("Center / bottom aligned under LTR: ",
    138                 outRect, 50, 50, 150, 100);
    139 
    140         // Right / bottom aligned under LTR direction
    141         GravityCompat.apply(Gravity.RIGHT | Gravity.BOTTOM, 100, 50,
    142                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_LTR);
    143         TestUtils.assertRectangleBounds("Right / bottom aligned under LTR: ",
    144                 outRect, 100, 50, 200, 100);
    145 
    146         // The following tests are expected to pass on all devices since START under LTR is LEFT
    147         // and END under LTR is RIGHT on pre-v17 and v17+ versions of the platform.
    148 
    149         // Start / top aligned under LTR direction
    150         GravityCompat.apply(GravityCompat.START | Gravity.TOP, 100, 50,
    151                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_LTR);
    152         TestUtils.assertRectangleBounds("Start / top aligned under LTR: ",
    153                 outRect, 0, 0, 100, 50);
    154 
    155         // End / top aligned under LTR direction
    156         GravityCompat.apply(GravityCompat.END | Gravity.TOP, 100, 50,
    157                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_LTR);
    158         TestUtils.assertRectangleBounds("End / top aligned under LTR: ",
    159                 outRect, 100, 0, 200, 50);
    160 
    161         // Start / center aligned under LTR direction
    162         GravityCompat.apply(GravityCompat.START | Gravity.CENTER_VERTICAL, 100, 50,
    163                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_LTR);
    164         TestUtils.assertRectangleBounds("Start / center aligned under LTR: ",
    165                 outRect, 0, 25, 100, 75);
    166 
    167         // End / center aligned under LTR direction
    168         GravityCompat.apply(GravityCompat.END | Gravity.CENTER_VERTICAL, 100, 50,
    169                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_LTR);
    170         TestUtils.assertRectangleBounds("End / center aligned under LTR: ",
    171                 outRect, 100, 25, 200, 75);
    172 
    173         // Start / bottom aligned under LTR direction
    174         GravityCompat.apply(GravityCompat.START | Gravity.BOTTOM, 100, 50,
    175                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_LTR);
    176         TestUtils.assertRectangleBounds("Start / bottom aligned under LTR: ",
    177                 outRect, 0, 50, 100, 100);
    178 
    179         // End / bottom aligned under LTR direction
    180         GravityCompat.apply(GravityCompat.END | Gravity.BOTTOM, 100, 50,
    181                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_LTR);
    182         TestUtils.assertRectangleBounds("End / bottom aligned under LTR: ",
    183                 outRect, 100, 50, 200, 100);
    184     }
    185 
    186     @Test
    187     public void testApplyNoOffsetsRtl() {
    188         Rect outRect = new Rect();
    189 
    190         // The following tests are expected to pass on all devices since they are using
    191         // Gravity constants that are not RTL-aware
    192 
    193         // Left / top aligned under RTL direction
    194         GravityCompat.apply(Gravity.LEFT | Gravity.TOP, 100, 50,
    195                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    196         TestUtils.assertRectangleBounds("Left / top aligned under RTL: ",
    197                 outRect, 0, 0, 100, 50);
    198 
    199         // Center / top aligned under RTL direction
    200         GravityCompat.apply(Gravity.CENTER_HORIZONTAL | Gravity.TOP, 100, 50,
    201                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    202         TestUtils.assertRectangleBounds("Center / top aligned under RTL: ",
    203                 outRect, 50, 0, 150, 50);
    204 
    205         // Right / top aligned under RTL direction
    206         GravityCompat.apply(Gravity.RIGHT | Gravity.TOP, 100, 50,
    207                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    208         TestUtils.assertRectangleBounds("Right / top aligned under RTL: ",
    209                 outRect, 100, 0, 200, 50);
    210 
    211         // Left / center aligned under RTL direction
    212         GravityCompat.apply(Gravity.LEFT | Gravity.CENTER_VERTICAL, 100, 50,
    213                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    214         TestUtils.assertRectangleBounds("Left / center aligned under RTL: ",
    215                 outRect, 0, 25, 100, 75);
    216 
    217         // Center / center aligned under RTL direction
    218         GravityCompat.apply(Gravity.CENTER, 100, 50,
    219                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    220         TestUtils.assertRectangleBounds("Center / center aligned under RTL: ",
    221                 outRect, 50, 25, 150, 75);
    222 
    223         // Right / center aligned under RTL direction
    224         GravityCompat.apply(Gravity.RIGHT | Gravity.CENTER_VERTICAL, 100, 50,
    225                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    226         TestUtils.assertRectangleBounds("Right / center aligned under RTL: ",
    227                 outRect, 100, 25, 200, 75);
    228 
    229         // Left / bottom aligned under RTL direction
    230         GravityCompat.apply(Gravity.LEFT | Gravity.BOTTOM, 100, 50,
    231                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    232         TestUtils.assertRectangleBounds("Left / bottom aligned under RTL: ",
    233                 outRect, 0, 50, 100, 100);
    234 
    235         // Center / bottom aligned under RTL direction
    236         GravityCompat.apply(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 100, 50,
    237                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    238         TestUtils.assertRectangleBounds("Center / bottom aligned under RTL: ",
    239                 outRect, 50, 50, 150, 100);
    240 
    241         // Right / bottom aligned under RTL direction
    242         GravityCompat.apply(Gravity.RIGHT | Gravity.BOTTOM, 100, 50,
    243                 new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    244         TestUtils.assertRectangleBounds("Right / bottom aligned under RTL: ",
    245                 outRect, 100, 50, 200, 100);
    246 
    247 
    248         if (Build.VERSION.SDK_INT >= 17) {
    249             // The following tests are only expected to pass on v17+ devices since START under
    250             // RTL is RIGHT and END under RTL is LEFT only on those devices.
    251 
    252             // Start / top aligned under RTL direction
    253             GravityCompat.apply(GravityCompat.START | Gravity.TOP, 100, 50,
    254                     new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    255             TestUtils.assertRectangleBounds("Start / top aligned under RTL: ",
    256                     outRect, 100, 0, 200, 50);
    257 
    258             // End / top aligned under RTL direction
    259             GravityCompat.apply(GravityCompat.END | Gravity.TOP, 100, 50,
    260                     new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    261             TestUtils.assertRectangleBounds("End / top aligned under RTL: ",
    262                     outRect, 0, 0, 100, 50);
    263 
    264             // Start / center aligned under RTL direction
    265             GravityCompat.apply(GravityCompat.START | Gravity.CENTER_VERTICAL, 100, 50,
    266                     new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    267             TestUtils.assertRectangleBounds("Start / center aligned under RTL: ",
    268                     outRect, 100, 25, 200, 75);
    269 
    270             // End / center aligned under RTL direction
    271             GravityCompat.apply(GravityCompat.END | Gravity.CENTER_VERTICAL, 100, 50,
    272                     new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    273             TestUtils.assertRectangleBounds("End / center aligned under RTL: ",
    274                     outRect, 0, 25, 100, 75);
    275 
    276             // Start / bottom aligned under RTL direction
    277             GravityCompat.apply(GravityCompat.START | Gravity.BOTTOM, 100, 50,
    278                     new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    279             TestUtils.assertRectangleBounds("Start / bottom aligned under RTL: ",
    280                     outRect, 100, 50, 200, 100);
    281 
    282             // End / bottom aligned under RTL direction
    283             GravityCompat.apply(GravityCompat.END | Gravity.BOTTOM, 100, 50,
    284                     new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    285             TestUtils.assertRectangleBounds("End / bottom aligned under RTL: ",
    286                     outRect, 0, 50, 100, 100);
    287         } else {
    288             // And on older devices START is always LEFT, END is always RIGHT
    289 
    290             // Start / top aligned under RTL direction
    291             GravityCompat.apply(GravityCompat.START | Gravity.TOP, 100, 50,
    292                     new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    293             TestUtils.assertRectangleBounds("Start / top aligned under RTL: ",
    294                     outRect, 0, 0, 100, 50);
    295 
    296             // End / top aligned under RTL direction
    297             GravityCompat.apply(GravityCompat.END | Gravity.TOP, 100, 50,
    298                     new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    299             TestUtils.assertRectangleBounds("End / top aligned under RTL: ",
    300                     outRect, 100, 0, 200, 50);
    301 
    302             // Start / center aligned under RTL direction
    303             GravityCompat.apply(GravityCompat.START | Gravity.CENTER_VERTICAL, 100, 50,
    304                     new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    305             TestUtils.assertRectangleBounds("Start / center aligned under RTL: ",
    306                     outRect, 0, 25, 100, 75);
    307 
    308             // End / center aligned under RTL direction
    309             GravityCompat.apply(GravityCompat.END | Gravity.CENTER_VERTICAL, 100, 50,
    310                     new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    311             TestUtils.assertRectangleBounds("End / center aligned under RTL: ",
    312                     outRect, 100, 25, 200, 75);
    313 
    314             // Start / bottom aligned under RTL direction
    315             GravityCompat.apply(GravityCompat.START | Gravity.BOTTOM, 100, 50,
    316                     new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    317             TestUtils.assertRectangleBounds("Start / bottom aligned under RTL: ",
    318                     outRect, 0, 50, 100, 100);
    319 
    320             // End / bottom aligned under RTL direction
    321             GravityCompat.apply(GravityCompat.END | Gravity.BOTTOM, 100, 50,
    322                     new Rect(0, 0, 200, 100), outRect, ViewCompat.LAYOUT_DIRECTION_RTL);
    323             TestUtils.assertRectangleBounds("End / bottom aligned under RTL: ",
    324                     outRect, 100, 50, 200, 100);
    325         }
    326     }
    327 }
    328