1 /* 2 * Copyright (C) 2017 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.view.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNull; 21 import static org.junit.Assert.assertSame; 22 23 import android.app.Instrumentation; 24 import android.content.res.ColorStateList; 25 import android.graphics.Color; 26 import android.graphics.PorterDuff; 27 import android.support.test.InstrumentationRegistry; 28 import android.support.test.annotation.UiThreadTest; 29 import android.support.test.filters.MediumTest; 30 import android.support.test.rule.ActivityTestRule; 31 import android.support.test.runner.AndroidJUnit4; 32 import android.view.Menu; 33 import android.view.MenuItem; 34 35 import org.junit.Before; 36 import org.junit.Rule; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 40 @MediumTest 41 @RunWith(AndroidJUnit4.class) 42 public class MenuItemTest { 43 private Instrumentation mInstrumentation; 44 private MenuItemCtsActivity mActivity; 45 private Menu mMenu; 46 47 @Rule 48 public ActivityTestRule<MenuItemCtsActivity> mActivityRule = 49 new ActivityTestRule<>(MenuItemCtsActivity.class); 50 51 52 @UiThreadTest 53 @Before 54 public void setup() { 55 mInstrumentation = InstrumentationRegistry.getInstrumentation(); 56 mActivity = mActivityRule.getActivity(); 57 58 mActivity.getMainToolbar().inflateMenu(R.menu.menu_regular); 59 mMenu = mActivity.getMainToolbar().getMenu(); 60 } 61 62 @Test 63 public void testAccessIconTint() { 64 // Note that this test is not marked as @UiThreadTest. Updating MenuItem does not 65 // immediately update the displayed content, and even though the getters are expected 66 // to immediately return the just-set value, using instrumentation to wait for the 67 // update to propagate makes this test more in line with the "real" application 68 // experience. 69 MenuItem firstItem = mMenu.getItem(0); 70 MenuItem secondItem = mMenu.getItem(1); 71 MenuItem thirdItem = mMenu.getItem(2); 72 73 // These are the default set in layout XML 74 assertEquals(Color.WHITE, firstItem.getIconTintList().getDefaultColor()); 75 assertNull(firstItem.getIconTintMode()); 76 assertNull(secondItem.getIconTintList()); 77 assertEquals(PorterDuff.Mode.SCREEN, secondItem.getIconTintMode()); 78 assertNull(thirdItem.getIconTintList()); 79 assertNull(thirdItem.getIconTintMode()); 80 81 // Change tint color list and mode and verify that they are returned by the getters 82 ColorStateList colors = ColorStateList.valueOf(Color.RED); 83 mInstrumentation.runOnMainSync(() -> { 84 firstItem.setIconTintList(colors); 85 firstItem.setIconTintMode(PorterDuff.Mode.XOR); 86 }); 87 mInstrumentation.waitForIdleSync(); 88 assertSame(colors, firstItem.getIconTintList()); 89 assertEquals(PorterDuff.Mode.XOR, firstItem.getIconTintMode()); 90 91 // Ensure the tint is preserved across drawable changes. 92 mInstrumentation.runOnMainSync(() -> firstItem.setIcon(R.drawable.icon_yellow)); 93 mInstrumentation.waitForIdleSync(); 94 assertSame(colors, firstItem.getIconTintList()); 95 assertEquals(PorterDuff.Mode.XOR, firstItem.getIconTintMode()); 96 97 // Change tint color list and mode again and verify that they are returned by the getters 98 ColorStateList colorsNew = ColorStateList.valueOf(Color.MAGENTA); 99 mInstrumentation.runOnMainSync(() -> { 100 firstItem.setIconTintList(colorsNew); 101 firstItem.setIconTintMode(PorterDuff.Mode.SRC_IN); 102 }); 103 mInstrumentation.waitForIdleSync(); 104 assertSame(colorsNew, firstItem.getIconTintList()); 105 assertEquals(PorterDuff.Mode.SRC_IN, firstItem.getIconTintMode()); 106 } 107 } 108