mirror of
https://github.com/material-components/material-components-android.git
synced 2026-02-20 08:39:55 +08:00
Merge "Initial setup and basic test for NavigationView" into mnc-ub-dev am: c841e91fb5
am: 4d3e8a1b07 * commit '4d3e8a1b0784ac4e8de97b8e8ccea7fd9c05e501': Initial setup and basic test for NavigationView GitOrigin-RevId=81b6818302d4237a577a886a6e431414e72d79a3 PiperOrigin-RevId: 140559606
This commit is contained in:
parent
ac3c2581c6
commit
cef9ce4b1a
@ -38,6 +38,10 @@
|
||||
android:theme="@style/Theme.AppCompat.NoActionBar"
|
||||
android:name="android.support.design.widget.BottomSheetBehaviorActivity"/>
|
||||
|
||||
<activity
|
||||
android:theme="@style/Theme.AppCompat.NoActionBar"
|
||||
android:name="android.support.design.widget.NavigationViewActivity"/>
|
||||
|
||||
<activity
|
||||
android:name="android.support.v7.app.AppCompatActivity"/>
|
||||
|
||||
|
||||
54
tests/res/layout/design_navigation_view.xml
Normal file
54
tests/res/layout/design_navigation_view.xml
Normal file
@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Copyright (C) 2016 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<android.support.v4.widget.DrawerLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:id="@+id/drawer_layout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:fitsSystemWindows="true">
|
||||
<!-- As the main content view, the view below consumes the entire
|
||||
space available using match_parent in both dimensions. Note that
|
||||
this child does not specify android:layout_gravity attribute. -->
|
||||
<FrameLayout
|
||||
android:id="@+id/content"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
<!-- android:layout_gravity="start" tells DrawerLayout to treat
|
||||
this as a sliding drawer on the starting side, which is
|
||||
left for left-to-right locales. The navigation view extends
|
||||
the full height of the container. A
|
||||
solid background is used for contrast with the content view.
|
||||
android:fitsSystemWindows="true" tells the system to have
|
||||
DrawerLayout span the full height of the screen, including the
|
||||
system status bar on Lollipop+ versions of the plaform. -->
|
||||
<android.support.design.widget.NavigationView
|
||||
android:id="@+id/start_drawer"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_gravity="start"
|
||||
android:background="#333"
|
||||
android:fitsSystemWindows="true"
|
||||
app:menu="@menu/navigation_view_content"
|
||||
app:itemIconTint="@color/emerald_translucent"
|
||||
app:itemTextColor="@color/emerald_text"
|
||||
app:itemBackground="@color/sand_fill"
|
||||
app:itemTextAppearance="@style/TextMediumStyle" />
|
||||
|
||||
</android.support.v4.widget.DrawerLayout>
|
||||
|
||||
25
tests/res/menu/navigation_view_content.xml
Normal file
25
tests/res/menu/navigation_view_content.xml
Normal file
@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Copyright (C) 2016 Google Inc.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item android:id="@+id/destination_home"
|
||||
android:title="@string/navigate_home" />
|
||||
<item android:id="@+id/destination_profile"
|
||||
android:title="@string/navigate_profile" />
|
||||
<item android:id="@+id/destination_people"
|
||||
android:title="@string/navigate_people" />
|
||||
<item android:id="@+id/destination_settings"
|
||||
android:title="@string/navigate_settings" />
|
||||
</menu>
|
||||
21
tests/res/values/colors.xml
Normal file
21
tests/res/values/colors.xml
Normal file
@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Copyright (C) 2016 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<resources>
|
||||
<color name="emerald_translucent">#8020A060</color>
|
||||
<color name="emerald_text">#30B050</color>
|
||||
<color name="sand_fill">#F0E020</color>
|
||||
</resources>
|
||||
@ -18,4 +18,6 @@
|
||||
<dimen name="tab_width_limit_medium">100dip</dimen>
|
||||
<dimen name="tab_width_limit_large">120dip</dimen>
|
||||
<dimen name="bottom_sheet_peek_height">128dp</dimen>
|
||||
|
||||
<dimen name="text_medium_size">20sp</dimen>
|
||||
</resources>
|
||||
@ -15,4 +15,9 @@
|
||||
-->
|
||||
<resources>
|
||||
<string name="tab_layout_text">Tab text!</string>
|
||||
|
||||
<string name="navigate_home">Home</string>
|
||||
<string name="navigate_profile">Profile</string>
|
||||
<string name="navigate_people">People</string>
|
||||
<string name="navigate_settings">Settings</string>
|
||||
</resources>
|
||||
20
tests/res/values/styles.xml
Normal file
20
tests/res/values/styles.xml
Normal file
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Copyright (C) 2016 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<resources>
|
||||
<style name="TextMediumStyle" parent="@android:style/TextAppearance.Medium">
|
||||
<item name="android:textSize">@dimen/text_medium_size</item>
|
||||
</style>
|
||||
</resources>
|
||||
83
tests/src/android/support/design/testutils/DrawerLayoutActions.java
Executable file
83
tests/src/android/support/design/testutils/DrawerLayoutActions.java
Executable file
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.support.design.testutils;
|
||||
|
||||
import android.support.test.espresso.UiController;
|
||||
import android.support.test.espresso.ViewAction;
|
||||
import android.support.v4.widget.DrawerLayout;
|
||||
import android.view.View;
|
||||
import org.hamcrest.Matcher;
|
||||
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
|
||||
|
||||
public class DrawerLayoutActions {
|
||||
/**
|
||||
* Opens the drawer at the specified edge gravity.
|
||||
*/
|
||||
public static ViewAction openDrawer(final int drawerEdgeGravity) {
|
||||
return new ViewAction() {
|
||||
@Override
|
||||
public Matcher<View> getConstraints() {
|
||||
return isAssignableFrom(DrawerLayout.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Opens the drawer";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(UiController uiController, View view) {
|
||||
uiController.loopMainThreadUntilIdle();
|
||||
|
||||
DrawerLayout drawerLayout = (DrawerLayout) view;
|
||||
drawerLayout.openDrawer(drawerEdgeGravity);
|
||||
|
||||
// Wait for a full second to let the inner ViewDragHelper complete the operation
|
||||
uiController.loopMainThreadForAtLeast(1000);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the drawer at the specified edge gravity.
|
||||
*/
|
||||
public static ViewAction closeDrawer(final int drawerEdgeGravity) {
|
||||
return new ViewAction() {
|
||||
@Override
|
||||
public Matcher<View> getConstraints() {
|
||||
return isAssignableFrom(DrawerLayout.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Closes the drawer";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(UiController uiController, View view) {
|
||||
uiController.loopMainThreadUntilIdle();
|
||||
|
||||
DrawerLayout drawerLayout = (DrawerLayout) view;
|
||||
drawerLayout.closeDrawer(drawerEdgeGravity);
|
||||
|
||||
// Wait for a full second to let the inner ViewDragHelper complete the operation
|
||||
uiController.loopMainThreadForAtLeast(1000);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package android.support.design.widget;
|
||||
|
||||
import android.support.design.test.R;
|
||||
|
||||
public class NavigationViewActivity extends BaseTestActivity {
|
||||
@Override
|
||||
protected int getContentViewLayoutResId() {
|
||||
return R.layout.design_navigation_view;
|
||||
}
|
||||
}
|
||||
71
tests/src/android/support/design/widget/NavigationViewTest.java
Executable file
71
tests/src/android/support/design/widget/NavigationViewTest.java
Executable file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package android.support.design.widget;
|
||||
|
||||
import android.content.res.Resources;
|
||||
import android.support.design.test.R;
|
||||
import android.support.v4.view.GravityCompat;
|
||||
import android.support.v4.widget.DrawerLayout;
|
||||
import android.test.suitebuilder.annotation.SmallTest;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static android.support.design.testutils.DrawerLayoutActions.closeDrawer;
|
||||
import static android.support.design.testutils.DrawerLayoutActions.openDrawer;
|
||||
import static android.support.test.espresso.Espresso.onView;
|
||||
import static android.support.test.espresso.assertion.ViewAssertions.matches;
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.*;
|
||||
import static org.hamcrest.core.AllOf.allOf;
|
||||
|
||||
public class NavigationViewTest
|
||||
extends BaseInstrumentationTestCase<NavigationViewActivity> {
|
||||
private DrawerLayout mDrawerLayout;
|
||||
|
||||
private NavigationView mNavigationView;
|
||||
|
||||
public NavigationViewTest() {
|
||||
super(NavigationViewActivity.class);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
final NavigationViewActivity activity = mActivityTestRule.getActivity();
|
||||
mDrawerLayout = (DrawerLayout) activity.findViewById(R.id.drawer_layout);
|
||||
mNavigationView = (NavigationView) mDrawerLayout.findViewById(R.id.start_drawer);
|
||||
|
||||
// Close the drawer to reset the state for the next test
|
||||
onView(withId(R.id.drawer_layout)).perform(closeDrawer(GravityCompat.START));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SmallTest
|
||||
public void testBasics() {
|
||||
// Open our drawer
|
||||
onView(withId(R.id.drawer_layout)).perform(openDrawer(GravityCompat.START));
|
||||
|
||||
final Resources res = mActivityTestRule.getActivity().getResources();
|
||||
|
||||
// Check that we have the expected menu items in our NavigationView
|
||||
onView(allOf(withText(res.getString(R.string.navigate_home)),
|
||||
isDescendantOfA(withId(R.id.start_drawer)))).check(matches(isDisplayed()));
|
||||
onView(allOf(withText(res.getString(R.string.navigate_profile)),
|
||||
isDescendantOfA(withId(R.id.start_drawer)))).check(matches(isDisplayed()));
|
||||
onView(allOf(withText(res.getString(R.string.navigate_people)),
|
||||
isDescendantOfA(withId(R.id.start_drawer)))).check(matches(isDisplayed()));
|
||||
onView(allOf(withText(res.getString(R.string.navigate_settings)),
|
||||
isDescendantOfA(withId(R.id.start_drawer)))).check(matches(isDisplayed()));
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user