|
1 | 1 | package info.piwai.cleanandroidcode; |
2 | 2 |
|
3 | | -import android.annotation.SuppressLint; |
4 | | -import android.app.Activity; |
| 3 | +import android.support.v4.app.Fragment; |
| 4 | +import android.support.v4.app.FragmentActivity; |
5 | 5 | import android.test.ActivityInstrumentationTestCase2; |
6 | 6 | import android.test.UiThreadTest; |
| 7 | +import android.view.View; |
7 | 8 |
|
8 | | -/* |
9 | | - * TODO use support fragment |
10 | | - */ |
11 | | -@SuppressLint("NewApi") |
12 | 9 | public class HelloAndroidActivityTest extends ActivityInstrumentationTestCase2<HelloAndroidActivity_> { |
13 | 10 |
|
14 | 11 | public HelloAndroidActivityTest() { |
15 | 12 | super(HelloAndroidActivity_.class); |
16 | 13 | } |
17 | 14 |
|
18 | | - public void testActivity() { |
19 | | - HelloAndroidActivity activity = getActivity(); |
20 | | - assertNotNull(activity); |
21 | | - } |
22 | | - |
23 | 15 | @UiThreadTest |
24 | | - public void testClickEventPropagated() { |
25 | | - Activity activity = getActivity(); |
26 | | - String beforeClick = activity.getTitle().toString(); |
27 | | - HelloFragment fragment = (HelloFragment) activity.getFragmentManager().findFragmentById(R.id.hello_fragment); |
28 | | - fragment.fragmentButton.performClick(); |
29 | | - String afterClick = activity.getTitle().toString(); |
30 | | - assertFalse(beforeClick.equals(afterClick)); |
31 | | - fragment.fragmentButton.performClick(); |
32 | | - String afterClick2 = activity.getTitle().toString(); |
33 | | - assertFalse(afterClick.equals(afterClick2)); |
| 16 | + public void test_click_on_fragment_changes_title() { |
| 17 | + FragmentActivity activity = getActivity(); |
| 18 | + Fragment fragment = activity.getSupportFragmentManager().findFragmentById(R.id.hello_fragment); |
| 19 | + View fragmentButton = fragment.getView().findViewById(R.id.fragment_button); |
| 20 | + |
| 21 | + fragmentButton.performClick(); |
| 22 | + |
| 23 | + String title = activity.getTitle().toString(); |
| 24 | + |
| 25 | + assertTrue("Title should start with [Button clicked]. Title is [" + title + "]", title.startsWith("Button clicked")); |
34 | 26 | } |
35 | 27 |
|
36 | 28 | } |
0 commit comments