|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All rights reserved. |
| 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 com.google.android.apps.flexbox.test |
| 18 | + |
| 19 | +import android.content.pm.ActivityInfo |
| 20 | +import android.support.design.widget.NavigationView |
| 21 | +import android.support.test.InstrumentationRegistry |
| 22 | +import android.support.test.espresso.Espresso.onView |
| 23 | +import android.support.test.espresso.action.ViewActions.* |
| 24 | +import android.support.test.espresso.matcher.ViewMatchers.withId |
| 25 | +import android.support.test.filters.FlakyTest |
| 26 | +import android.support.test.filters.MediumTest |
| 27 | +import android.support.test.rule.ActivityTestRule |
| 28 | +import android.support.test.runner.AndroidJUnit4 |
| 29 | +import android.view.View |
| 30 | +import android.widget.ArrayAdapter |
| 31 | +import android.widget.RadioGroup |
| 32 | +import android.widget.Spinner |
| 33 | +import android.widget.TextView |
| 34 | +import com.google.android.apps.flexbox.R |
| 35 | +import com.google.android.flexbox.* |
| 36 | +import junit.framework.Assert.* |
| 37 | +import org.hamcrest.MatcherAssert.assertThat |
| 38 | +import org.hamcrest.core.Is.`is` |
| 39 | +import org.junit.Rule |
| 40 | +import org.junit.Test |
| 41 | +import org.junit.runner.RunWith |
| 42 | + |
| 43 | +/** |
| 44 | + * Integration tests for [MainActivity]. |
| 45 | + */ |
| 46 | +@RunWith(AndroidJUnit4::class) |
| 47 | +@MediumTest |
| 48 | +class MainActivityTest { |
| 49 | + |
| 50 | + @JvmField |
| 51 | + @Rule |
| 52 | + var activityRule = ActivityTestRule(MainActivity::class.java) |
| 53 | + |
| 54 | + @Test |
| 55 | + @FlakyTest |
| 56 | + fun testAddFlexItem() { |
| 57 | + val activity = activityRule.activity |
| 58 | + val flexboxLayout = activity.findViewById<FlexboxLayout>(R.id.flexbox_layout) |
| 59 | + assertNotNull(flexboxLayout) |
| 60 | + val beforeCount = flexboxLayout.childCount |
| 61 | + onView(withId(R.id.add_fab)).perform(click()) |
| 62 | + |
| 63 | + assertThat(flexboxLayout.childCount, `is`(beforeCount + 1)) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + @FlakyTest |
| 68 | + fun testRemoveFlexItem() { |
| 69 | + val activity = activityRule.activity |
| 70 | + val flexboxLayout = activity.findViewById<FlexboxLayout>(R.id.flexbox_layout) |
| 71 | + assertNotNull(flexboxLayout) |
| 72 | + val beforeCount = flexboxLayout.childCount |
| 73 | + onView(withId(R.id.remove_fab)).perform(click()) |
| 74 | + |
| 75 | + assertThat(flexboxLayout.childCount, `is`(beforeCount - 1)) |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + @FlakyTest |
| 80 | + fun testConfigurationChange() { |
| 81 | + val activity = activityRule.activity |
| 82 | + val flexboxLayout = activity.findViewById<FlexboxLayout>(R.id.flexbox_layout) |
| 83 | + assertNotNull(flexboxLayout) |
| 84 | + onView(withId(R.id.add_fab)).perform(click()) |
| 85 | + onView(withId(R.id.add_fab)).perform(click()) |
| 86 | + onView(withId(R.id.add_fab)).perform(click()) |
| 87 | + val beforeCount = flexboxLayout.childCount |
| 88 | + |
| 89 | + activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE |
| 90 | + InstrumentationRegistry.getInstrumentation().waitForIdleSync() |
| 91 | + |
| 92 | + // Verify the flex items are restored across the configuration change. |
| 93 | + assertThat(flexboxLayout.childCount, `is`(beforeCount)) |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + @FlakyTest |
| 98 | + fun testFlexDirectionSpinner() { |
| 99 | + val activity = activityRule.activity |
| 100 | + val flexboxLayout = activity.findViewById<FlexboxLayout>(R.id.flexbox_layout) |
| 101 | + assertNotNull(flexboxLayout) |
| 102 | + val navigationView = activity.findViewById<NavigationView>(R.id.nav_view) |
| 103 | + assertNotNull(navigationView) |
| 104 | + val menu = navigationView.menu |
| 105 | + val spinner = menu.findItem(R.id.menu_item_flex_direction).actionView as Spinner |
| 106 | + val spinnerAdapter = spinner.adapter as ArrayAdapter<CharSequence> |
| 107 | + |
| 108 | + val columnPosition = spinnerAdapter.getPosition(activity.getString(R.string.column)) |
| 109 | + activity.runOnUiThread { spinner.setSelection(columnPosition) } |
| 110 | + InstrumentationRegistry.getInstrumentation().waitForIdleSync() |
| 111 | + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) |
| 112 | + |
| 113 | + val rowReversePosition = spinnerAdapter.getPosition(activity.getString(R.string.row_reverse)) |
| 114 | + activity.runOnUiThread { spinner.setSelection(rowReversePosition) } |
| 115 | + InstrumentationRegistry.getInstrumentation().waitForIdleSync() |
| 116 | + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW_REVERSE)) |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + @FlakyTest |
| 121 | + fun testFlexWrapSpinner() { |
| 122 | + val activity = activityRule.activity |
| 123 | + val flexboxLayout = activity.findViewById<FlexboxLayout>(R.id.flexbox_layout) |
| 124 | + assertNotNull(flexboxLayout) |
| 125 | + val navigationView = activity.findViewById<NavigationView>(R.id.nav_view) |
| 126 | + assertNotNull(navigationView) |
| 127 | + val menu = navigationView.menu |
| 128 | + val spinner = menu.findItem(R.id.menu_item_flex_wrap).actionView as Spinner |
| 129 | + val spinnerAdapter = spinner.adapter as ArrayAdapter<CharSequence> |
| 130 | + |
| 131 | + val wrapReversePosition = spinnerAdapter.getPosition(activity.getString(R.string.wrap_reverse)) |
| 132 | + activity.runOnUiThread { spinner.setSelection(wrapReversePosition) } |
| 133 | + InstrumentationRegistry.getInstrumentation().waitForIdleSync() |
| 134 | + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP_REVERSE)) |
| 135 | + |
| 136 | + val noWrapPosition = spinnerAdapter.getPosition(activity.getString(R.string.nowrap)) |
| 137 | + activity.runOnUiThread { spinner.setSelection(noWrapPosition) } |
| 138 | + InstrumentationRegistry.getInstrumentation().waitForIdleSync() |
| 139 | + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.NOWRAP)) |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + @FlakyTest |
| 144 | + fun testJustifyContentSpinner() { |
| 145 | + val activity = activityRule.activity |
| 146 | + val flexboxLayout = activity.findViewById<View>(R.id.flexbox_layout) as FlexboxLayout |
| 147 | + assertNotNull(flexboxLayout) |
| 148 | + val navigationView = activity.findViewById<View>(R.id.nav_view) as NavigationView |
| 149 | + assertNotNull(navigationView) |
| 150 | + val menu = navigationView.menu |
| 151 | + val spinner = menu.findItem(R.id.menu_item_justify_content).actionView as Spinner |
| 152 | + val spinnerAdapter = spinner.adapter as ArrayAdapter<CharSequence> |
| 153 | + |
| 154 | + val spaceBetweenPosition = spinnerAdapter.getPosition(activity.getString(R.string.space_between)) |
| 155 | + activity.runOnUiThread { spinner.setSelection(spaceBetweenPosition) } |
| 156 | + InstrumentationRegistry.getInstrumentation().waitForIdleSync() |
| 157 | + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_BETWEEN)) |
| 158 | + |
| 159 | + val centerPosition = spinnerAdapter.getPosition(activity.getString(R.string.center)) |
| 160 | + activity.runOnUiThread { spinner.setSelection(centerPosition) } |
| 161 | + InstrumentationRegistry.getInstrumentation().waitForIdleSync() |
| 162 | + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.CENTER)) |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + @FlakyTest |
| 167 | + fun testAlignItemsSpinner() { |
| 168 | + val activity = activityRule.activity |
| 169 | + val flexboxLayout = activity.findViewById<FlexboxLayout>(R.id.flexbox_layout) |
| 170 | + assertNotNull(flexboxLayout) |
| 171 | + val navigationView = activity.findViewById<NavigationView>(R.id.nav_view) |
| 172 | + assertNotNull(navigationView) |
| 173 | + val menu = navigationView.menu |
| 174 | + val spinner = menu.findItem(R.id.menu_item_align_items).actionView as Spinner |
| 175 | + val spinnerAdapter = spinner.adapter as ArrayAdapter<CharSequence> |
| 176 | + |
| 177 | + val baselinePosition = spinnerAdapter.getPosition(activity.getString(R.string.baseline)) |
| 178 | + activity.runOnUiThread { spinner.setSelection(baselinePosition) } |
| 179 | + InstrumentationRegistry.getInstrumentation().waitForIdleSync() |
| 180 | + assertThat(flexboxLayout.alignItems, `is`(AlignItems.BASELINE)) |
| 181 | + |
| 182 | + val flexEndPosition = spinnerAdapter.getPosition(activity.getString(R.string.flex_end)) |
| 183 | + activity.runOnUiThread { spinner.setSelection(flexEndPosition) } |
| 184 | + InstrumentationRegistry.getInstrumentation().waitForIdleSync() |
| 185 | + assertThat(flexboxLayout.alignItems, `is`(AlignItems.FLEX_END)) |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + @FlakyTest |
| 190 | + fun testAlignContentSpinner() { |
| 191 | + val activity = activityRule.activity |
| 192 | + val flexboxLayout = activity.findViewById<FlexboxLayout>(R.id.flexbox_layout) |
| 193 | + assertNotNull(flexboxLayout) |
| 194 | + val navigationView = activity.findViewById<NavigationView>(R.id.nav_view) |
| 195 | + assertNotNull(navigationView) |
| 196 | + val menu = navigationView.menu |
| 197 | + val spinner = menu.findItem(R.id.menu_item_align_content).actionView as Spinner |
| 198 | + val spinnerAdapter = spinner.adapter as ArrayAdapter<CharSequence> |
| 199 | + |
| 200 | + val spaceAroundPosition = spinnerAdapter.getPosition(activity.getString(R.string.space_around)) |
| 201 | + activity.runOnUiThread { spinner.setSelection(spaceAroundPosition) } |
| 202 | + InstrumentationRegistry.getInstrumentation().waitForIdleSync() |
| 203 | + assertThat(flexboxLayout.alignContent, `is`(AlignContent.SPACE_AROUND)) |
| 204 | + |
| 205 | + val stretchPosition = spinnerAdapter.getPosition(activity.getString(R.string.stretch)) |
| 206 | + activity.runOnUiThread { spinner.setSelection(stretchPosition) } |
| 207 | + InstrumentationRegistry.getInstrumentation().waitForIdleSync() |
| 208 | + assertThat(flexboxLayout.alignContent, `is`(AlignContent.STRETCH)) |
| 209 | + } |
| 210 | + |
| 211 | + @Test |
| 212 | + @FlakyTest |
| 213 | + fun testEditFragment_changeOrder() { |
| 214 | + val activity = activityRule.activity |
| 215 | + val flexboxLayout = activity.findViewById<View>(R.id.flexbox_layout) as FlexboxLayout |
| 216 | + assertNotNull(flexboxLayout) |
| 217 | + onView(withId(R.id.textview1)).perform(click()) |
| 218 | + onView(withId(R.id.edit_text_order)).perform(replaceText("3"), closeSoftKeyboard()) |
| 219 | + onView(withId(R.id.button_ok)).perform(click()) |
| 220 | + val first = flexboxLayout.getReorderedChildAt(0) as TextView |
| 221 | + val second = flexboxLayout.getReorderedChildAt(1) as TextView |
| 222 | + val third = flexboxLayout.getReorderedChildAt(2) as TextView |
| 223 | + |
| 224 | + assertThat(first.text.toString(), `is`("2")) |
| 225 | + assertThat(second.text.toString(), `is`("3")) |
| 226 | + assertThat(third.text.toString(), `is`("1")) |
| 227 | + } |
| 228 | + |
| 229 | + @Test |
| 230 | + @FlakyTest |
| 231 | + fun testEditFragment_changeFlexGrow() { |
| 232 | + val activity = activityRule.activity |
| 233 | + val flexboxLayout = activity.findViewById<View>(R.id.flexbox_layout) as FlexboxLayout |
| 234 | + assertNotNull(flexboxLayout) |
| 235 | + onView(withId(R.id.textview1)).perform(click()) |
| 236 | + onView(withId(R.id.edit_text_flex_grow)).perform(replaceText("1"), closeSoftKeyboard()) |
| 237 | + onView(withId(R.id.button_ok)).perform(click()) |
| 238 | + val first = activity.findViewById<View>(R.id.textview1) as TextView |
| 239 | + val second = activity.findViewById<View>(R.id.textview2) as TextView |
| 240 | + val third = activity.findViewById<View>(R.id.textview3) as TextView |
| 241 | + assertNotNull(first) |
| 242 | + assertNotNull(second) |
| 243 | + assertNotNull(third) |
| 244 | + |
| 245 | + assertThat(first.width, `is`(flexboxLayout.width - second.width - third.width)) |
| 246 | + } |
| 247 | + |
| 248 | + @Test |
| 249 | + @FlakyTest |
| 250 | + fun testEditFragment_changeFlexGrowFloat() { |
| 251 | + val activity = activityRule.activity |
| 252 | + val flexboxLayout = activity.findViewById<View>(R.id.flexbox_layout) as FlexboxLayout |
| 253 | + assertNotNull(flexboxLayout) |
| 254 | + onView(withId(R.id.textview1)).perform(click()) |
| 255 | + onView(withId(R.id.edit_text_flex_grow)).perform(replaceText("1.0"), closeSoftKeyboard()) |
| 256 | + onView(withId(R.id.button_ok)).perform(click()) |
| 257 | + val first = activity.findViewById<View>(R.id.textview1) as TextView |
| 258 | + val second = activity.findViewById<View>(R.id.textview2) as TextView |
| 259 | + val third = activity.findViewById<View>(R.id.textview3) as TextView |
| 260 | + assertNotNull(first) |
| 261 | + assertNotNull(second) |
| 262 | + assertNotNull(third) |
| 263 | + |
| 264 | + assertThat(first.width, `is`(flexboxLayout.width - second.width - third.width)) |
| 265 | + } |
| 266 | + |
| 267 | + @Test |
| 268 | + @FlakyTest |
| 269 | + fun testEditFragment_changeFlexBasisPercent() { |
| 270 | + val activity = activityRule.activity |
| 271 | + val flexboxLayout = activity.findViewById<View>(R.id.flexbox_layout) as FlexboxLayout |
| 272 | + assertNotNull(flexboxLayout) |
| 273 | + onView(withId(R.id.textview1)).perform(click()) |
| 274 | + onView(withId(R.id.edit_text_flex_basis_percent)) |
| 275 | + .perform(replaceText("50"), closeSoftKeyboard()) |
| 276 | + onView(withId(R.id.button_ok)).perform(click()) |
| 277 | + val first = activity.findViewById<TextView>(R.id.textview1) |
| 278 | + val second = activity.findViewById<TextView>(R.id.textview2) |
| 279 | + val third = activity.findViewById<TextView>(R.id.textview3) |
| 280 | + assertNotNull(first) |
| 281 | + assertNotNull(second) |
| 282 | + assertNotNull(third) |
| 283 | + |
| 284 | + assertTrue(first.width - 1 <= flexboxLayout.width / 2 || flexboxLayout.width / 2 <= first.width + 1) |
| 285 | + } |
| 286 | + |
| 287 | + @Test |
| 288 | + @FlakyTest |
| 289 | + fun testSwitchRecyclerViewFragment() { |
| 290 | + val activity = activityRule.activity |
| 291 | + val flexboxLayout = activity.findViewById<FlexboxLayout>(R.id.flexbox_layout) |
| 292 | + assertNotNull(flexboxLayout) |
| 293 | + val navigationView = activity.findViewById<NavigationView>(R.id.nav_view) |
| 294 | + assertNotNull(navigationView) |
| 295 | + assertNull(activity.findViewById(R.id.recyclerview)) |
| 296 | + assertNotNull(activity.findViewById(R.id.flexbox_layout)) |
| 297 | + |
| 298 | + val radioGroup = navigationView.getHeaderView(0) |
| 299 | + .findViewById<RadioGroup>(R.id.radiogroup_container_implementation) |
| 300 | + activity.runOnUiThread { radioGroup.check(R.id.radiobutton_recyclerview) } |
| 301 | + InstrumentationRegistry.getInstrumentation().waitForIdleSync() |
| 302 | + assertNotNull(activity.findViewById(R.id.recyclerview)) |
| 303 | + assertNull(activity.findViewById(R.id.flexbox_layout)) |
| 304 | + } |
| 305 | +} |
0 commit comments