Merge "Add GuidedStepFragment tests"
diff --git a/v17/leanback/tests/AndroidManifest.xml b/v17/leanback/tests/AndroidManifest.xml
index 5c421e3..c1c1bff 100644
--- a/v17/leanback/tests/AndroidManifest.xml
+++ b/v17/leanback/tests/AndroidManifest.xml
@@ -46,6 +46,14 @@
android:theme="@style/Theme.Leanback.Browse"
android:exported="true" />
+ <activity android:name="android.support.v17.leanback.app.GuidedStepFragmentTestActivity"
+ android:theme="@style/Theme.Leanback.GuidedStep"
+ android:exported="true" />
+
+ <activity android:name="android.support.v17.leanback.app.GuidedStepSupportFragmentTestActivity"
+ android:theme="@style/Theme.Leanback.GuidedStep"
+ android:exported="true" />
+
</application>
</manifest>
diff --git a/v17/leanback/tests/generatev4.py b/v17/leanback/tests/generatev4.py
index 4ffd361..7c33d45 100755
--- a/v17/leanback/tests/generatev4.py
+++ b/v17/leanback/tests/generatev4.py
@@ -19,10 +19,11 @@
print "Generate v4 fragment related code for leanback"
-files = ['BrowseTest']
+files = ['BrowseTest', 'GuidedStepTest']
cls = ['BrowseTest', 'Background', 'Base', 'BaseRow', 'Browse', 'Details', 'Error', 'Headers',
- 'PlaybackOverlay', 'Rows', 'Search', 'VerticalGrid', 'Branded']
+ 'PlaybackOverlay', 'Rows', 'Search', 'VerticalGrid', 'Branded',
+ 'GuidedStepTest', 'GuidedStep']
for w in files:
print "copy {}Fragment to {}SupportFragment".format(w, w)
@@ -37,11 +38,35 @@
line = line.replace('{}Fragment'.format(w), '{}SupportFragment'.format(w))
line = line.replace('android.app.Fragment', 'android.support.v4.app.Fragment')
line = line.replace('android.app.Activity', 'android.support.v4.app.FragmentActivity')
+ line = line.replace('Activity getActivity()', 'FragmentActivity getActivity()')
outfile.write(line)
file.close()
outfile.close()
-testcls = ['Browse']
+testcls = ['GuidedStep']
+
+for w in testcls:
+ print "copy {}FrgamentTestBase to {}SupportFragmentTestBase".format(w, w)
+
+ file = open('java/android/support/v17/leanback/app/{}FragmentTestBase.java'.format(w), 'r')
+ outfile = open('java/android/support/v17/leanback/app/{}SupportFragmentTestBase.java'.format(w), 'w')
+
+ outfile.write("/* This file is auto-generated from {}FrgamentTestBase.java. DO NOT MODIFY. */\n\n".format(w))
+
+ for line in file:
+ for w in cls:
+ line = line.replace('{}Fragment'.format(w), '{}SupportFragment'.format(w))
+ for w in testcls:
+ line = line.replace('{}FragmentTestBase'.format(w), '{}SupportFragmentTestBase'.format(w))
+ line = line.replace('{}FragmentTestActivity'.format(w), '{}SupportFragmentTestActivity'.format(w))
+ line = line.replace('{}TestFragment'.format(w), '{}TestSupportFragment'.format(w))
+ outfile.write(line)
+ line = line.replace('android.app.Fragment', 'android.support.v4.app.Fragment')
+ line = line.replace('android.app.Activity', 'android.support.v4.app.FragmentActivity')
+ file.close()
+ outfile.close()
+
+testcls = ['Browse', 'GuidedStep']
for w in testcls:
print "copy {}FrgamentTest to {}SupportFragmentTest".format(w, w)
@@ -49,34 +74,39 @@
file = open('java/android/support/v17/leanback/app/{}FragmentTest.java'.format(w), 'r')
outfile = open('java/android/support/v17/leanback/app/{}SupportFragmentTest.java'.format(w), 'w')
- outfile.write("/* This file is auto-generated from {}FrgamentTest.java. DO NOT MODIFY. */\n\n".format(w))
+ outfile.write("/* This file is auto-generated from {}FragmentTest.java. DO NOT MODIFY. */\n\n".format(w))
for line in file:
for w in cls:
line = line.replace('{}Fragment'.format(w), '{}SupportFragment'.format(w))
for w in testcls:
+ line = line.replace('{}FragmentTestBase'.format(w), '{}SupportFragmentTestBase'.format(w))
line = line.replace('{}FragmentTest'.format(w), '{}SupportFragmentTest'.format(w))
line = line.replace('{}FragmentTestActivity'.format(w), '{}SupportFragmentTestActivity'.format(w))
line = line.replace('{}TestFragment'.format(w), '{}TestSupportFragment'.format(w))
outfile.write(line)
+ line = line.replace('android.app.Fragment', 'android.support.v4.app.Fragment')
+ line = line.replace('android.app.Activity', 'android.support.v4.app.FragmentActivity')
file.close()
outfile.close()
+testcls = ['Browse', 'GuidedStep']
-print "copy BrowseFragmentTestActivity to BrowseSupportFragmentTestActivity"
-file = open('java/android/support/v17/leanback/app/BrowseFragmentTestActivity.java', 'r')
-outfile = open('java/android/support/v17/leanback/app/BrowseSupportFragmentTestActivity.java', 'w')
-outfile.write("/* This file is auto-generated from BrowseFragmentTestActivity.java. DO NOT MODIFY. */\n\n")
-for line in file:
- line = line.replace('BrowseTestFragment', 'BrowseTestSupportFragment')
- line = line.replace('BrowseFragmentTestActivity', 'BrowseSupportFragmentTestActivity')
- line = line.replace('android.app.Fragment', 'android.support.v4.app.Fragment')
- line = line.replace('android.app.Activity', 'android.support.v4.app.FragmentActivity')
- line = line.replace('extends Activity', 'extends FragmentActivity')
- line = line.replace('getFragmentManager', 'getSupportFragmentManager')
- outfile.write(line)
-file.close()
-outfile.close()
+for w in testcls:
+ print "copy {}FragmentTestActivity to {}SupportFragmentTestActivity".format(w, w)
+ file = open('java/android/support/v17/leanback/app/{}FragmentTestActivity.java'.format(w), 'r')
+ outfile = open('java/android/support/v17/leanback/app/{}SupportFragmentTestActivity.java'.format(w), 'w')
+ outfile.write("/* This file is auto-generated from {}FragmentTestActivity.java. DO NOT MODIFY. */\n\n".format(w))
+ for line in file:
+ line = line.replace('{}TestFragment'.format(w), '{}TestSupportFragment'.format(w))
+ line = line.replace('{}FragmentTestActivity'.format(w), '{}SupportFragmentTestActivity'.format(w))
+ line = line.replace('android.app.Fragment', 'android.support.v4.app.Fragment')
+ line = line.replace('android.app.Activity', 'android.support.v4.app.FragmentActivity')
+ line = line.replace('extends Activity', 'extends FragmentActivity')
+ line = line.replace('getFragmentManager', 'getSupportFragmentManager')
+ outfile.write(line)
+ file.close()
+ outfile.close()
print "copy ParallaxIntEffectTest to ParallaxFloatEffectTest"
file = open('java/android/support/v17/leanback/widget/ParallaxIntEffectTest.java', 'r')
diff --git a/v17/leanback/tests/java/android/support/v17/leanback/app/BrowseSupportFragmentTest.java b/v17/leanback/tests/java/android/support/v17/leanback/app/BrowseSupportFragmentTest.java
index 1c007c6..3bdfd99 100644
--- a/v17/leanback/tests/java/android/support/v17/leanback/app/BrowseSupportFragmentTest.java
+++ b/v17/leanback/tests/java/android/support/v17/leanback/app/BrowseSupportFragmentTest.java
@@ -1,4 +1,4 @@
-/* This file is auto-generated from BrowseFrgamentTest.java. DO NOT MODIFY. */
+/* This file is auto-generated from BrowseFragmentTest.java. DO NOT MODIFY. */
/*
* Copyright (C) 2015 The Android Open Source Project
diff --git a/v17/leanback/tests/java/android/support/v17/leanback/app/BrowseTestSupportFragment.java b/v17/leanback/tests/java/android/support/v17/leanback/app/BrowseTestSupportFragment.java
index b8f16d3..26be77f 100644
--- a/v17/leanback/tests/java/android/support/v17/leanback/app/BrowseTestSupportFragment.java
+++ b/v17/leanback/tests/java/android/support/v17/leanback/app/BrowseTestSupportFragment.java
@@ -111,7 +111,7 @@
listRowAdapter.add("Hello world");
listRowAdapter.add("Android TV");
listRowAdapter.add("Leanback");
- listRowAdapter.add("GuidedStepFragment");
+ listRowAdapter.add("GuidedStepSupportFragment");
}
HeaderItem header = new HeaderItem(i, "Row " + i);
mRowsAdapter.add(new ListRow(header, listRowAdapter));
diff --git a/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepFragmentTest.java b/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepFragmentTest.java
new file mode 100644
index 0000000..5c49081
--- /dev/null
+++ b/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepFragmentTest.java
@@ -0,0 +1,190 @@
+/*
+ * 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.v17.leanback.app;
+
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.ViewGroup;
+import android.view.View;
+import android.view.LayoutInflater;
+import android.view.KeyEvent;
+import android.support.test.rule.ActivityTestRule;
+import android.support.test.runner.AndroidJUnit4;
+
+import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
+import android.support.v17.leanback.widget.GuidedAction;
+
+import android.support.test.espresso.Espresso;
+import android.support.test.espresso.matcher.RootMatchers;
+import android.support.test.espresso.action.ViewActions;
+import android.support.test.InstrumentationRegistry;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import org.mockito.stubbing.Answer;
+import org.mockito.invocation.InvocationOnMock;
+
+import java.util.List;
+
+import static org.mockito.Mockito.*;
+import static org.junit.Assert.*;
+
+/**
+ * @hide from javadoc
+ */
+@RunWith(AndroidJUnit4.class)
+public class GuidedStepFragmentTest extends GuidedStepFragmentTestBase {
+
+ @Test
+ public void nextAndBack() throws Throwable {
+ GuidedStepTestFragment.Provider first = mockProvider("first");
+ doAnswer(new Answer<Void>() {
+ public Void answer(InvocationOnMock invocation) {
+ List actions = (List) invocation.getArguments()[0];
+ actions.add(new GuidedAction.Builder().id(1000).title("OK").build());
+ return null;
+ }
+ }).when(first).onCreateActions(any(List.class), any(Bundle.class));
+ doAnswer(new Answer<Void>() {
+ public Void answer(InvocationOnMock invocation) {
+ GuidedAction action = (GuidedAction) invocation.getArguments()[0];
+ GuidedStepTestFragment.Provider obj = (GuidedStepTestFragment.Provider)
+ invocation.getMock();
+ if (action.getId() == 1000) {
+ GuidedStepFragment.add(obj.getFragmentManager(),
+ new GuidedStepTestFragment("second"));
+ }
+ return null;
+ }
+ }).when(first).onGuidedActionClicked(any(GuidedAction.class));
+
+ GuidedStepTestFragment.Provider second = mockProvider("second");
+
+ GuidedStepFragmentTestActivity activity = launchTestActivity("first");
+ verify(first, times(1)).onCreate(any(Bundle.class));
+ verify(first, times(1)).onCreateGuidance(any(Bundle.class));
+ verify(first, times(1)).onCreateActions(any(List.class), any(Bundle.class));
+ verify(first, times(1)).onCreateButtonActions(any(List.class), any(Bundle.class));
+ verify(first, times(1)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class),
+ any(Bundle.class), any(View.class));
+ verify(first, times(1)).onViewStateRestored(any(Bundle.class));
+ verify(first, times(1)).onStart();
+ verify(first, times(1)).onResume();
+
+ sendKey(KeyEvent.KEYCODE_DPAD_CENTER);
+ verify(first, times(1)).onGuidedActionClicked(any(GuidedAction.class));
+
+ waitEnterTransitionFinish(second);
+ verify(first, times(1)).onPause();
+ verify(first, times(1)).onStop();
+ verify(first, times(1)).onDestroyView();
+ verify(second, times(1)).onCreate(any(Bundle.class));
+ verify(second, times(1)).onCreateGuidance(any(Bundle.class));
+ verify(second, times(1)).onCreateActions(any(List.class), any(Bundle.class));
+ verify(second, times(1)).onCreateButtonActions(any(List.class), any(Bundle.class));
+ verify(second, times(1)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class),
+ any(Bundle.class), any(View.class));
+ verify(second, times(1)).onViewStateRestored(any(Bundle.class));
+ verify(second, times(1)).onStart();
+ verify(second, times(1)).onResume();
+
+ sendKey(KeyEvent.KEYCODE_BACK);
+
+ waitEnterTransitionFinish(first);
+ verify(second, times(1)).onPause();
+ verify(second, times(1)).onStop();
+ verify(second, times(1)).onDestroyView();
+ verify(second, times(1)).onDestroy();
+ verify(first, times(1)).onCreateActions(any(List.class), any(Bundle.class));
+ verify(first, times(2)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class),
+ any(Bundle.class), any(View.class));
+ verify(first, times(2)).onViewStateRestored(any(Bundle.class));
+ verify(first, times(2)).onStart();
+ verify(first, times(2)).onResume();
+
+ sendKey(KeyEvent.KEYCODE_BACK);
+ waitActivityDestroy(activity);
+ verify(first, times(1)).onDestroy();
+ assertTrue(activity.isDestroyed());
+ }
+
+ @Test
+ public void restoreFragments() throws Throwable {
+ GuidedStepTestFragment.Provider first = mockProvider("first");
+ doAnswer(new Answer<Void>() {
+ public Void answer(InvocationOnMock invocation) {
+ List actions = (List) invocation.getArguments()[0];
+ actions.add(new GuidedAction.Builder().id(1000).title("OK").build());
+ actions.add(new GuidedAction.Builder().id(1001).editable(true).title("text")
+ .build());
+ actions.add(new GuidedAction.Builder().id(1002).editable(true).title("text")
+ .autoSaveRestoreEnabled(false).build());
+ return null;
+ }
+ }).when(first).onCreateActions(any(List.class), any(Bundle.class));
+ doAnswer(new Answer<Void>() {
+ public Void answer(InvocationOnMock invocation) {
+ GuidedAction action = (GuidedAction) invocation.getArguments()[0];
+ GuidedStepTestFragment.Provider obj = (GuidedStepTestFragment.Provider)
+ invocation.getMock();
+ if (action.getId() == 1000) {
+ GuidedStepFragment.add(obj.getFragmentManager(),
+ new GuidedStepTestFragment("second"));
+ }
+ return null;
+ }
+ }).when(first).onGuidedActionClicked(any(GuidedAction.class));
+
+ GuidedStepTestFragment.Provider second = mockProvider("second");
+
+ final GuidedStepFragmentTestActivity activity = launchTestActivity("first");
+ first.getFragment().findActionById(1001).setTitle("modified text");
+ first.getFragment().findActionById(1002).setTitle("modified text");
+ sendKey(KeyEvent.KEYCODE_DPAD_CENTER);
+ waitEnterTransitionFinish(second);
+
+ InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
+ @Override
+ public void run() {
+ activity.recreate();
+ }
+ });
+ waitEnterTransitionFinish(second);
+ verify(first, times(2)).onCreate(any(Bundle.class));
+ verify(first, times(1)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class),
+ any(Bundle.class), any(View.class));
+ verify(first, times(2)).onCreateActions(any(List.class), any(Bundle.class));
+ verify(first, times(1)).onDestroy();
+ verify(second, times(2)).onCreate(any(Bundle.class));
+ verify(second, times(2)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class),
+ any(Bundle.class), any(View.class));
+ verify(second, times(1)).onDestroy();
+ assertEquals("modified text", first.getFragment().findActionById(1001).getTitle());
+ assertEquals("text", first.getFragment().findActionById(1002).getTitle());
+
+ sendKey(KeyEvent.KEYCODE_BACK);
+ waitEnterTransitionFinish(first);
+ verify(second, times(2)).onPause();
+ verify(second, times(2)).onStop();
+ verify(second, times(2)).onDestroyView();
+ verify(second, times(2)).onDestroy();
+ verify(first, times(2)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class),
+ any(Bundle.class), any(View.class));
+ }
+}
diff --git a/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepFragmentTestActivity.java b/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepFragmentTestActivity.java
new file mode 100644
index 0000000..226cff9
--- /dev/null
+++ b/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepFragmentTestActivity.java
@@ -0,0 +1,52 @@
+/*
+ * 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.v17.leanback.app;
+
+import android.app.Activity;
+import android.content.Intent;
+import android.os.Bundle;
+
+/**
+ * @hide from javadoc
+ */
+public class GuidedStepFragmentTestActivity extends Activity {
+
+ /**
+ * Frst Test that will be included in this Activity
+ */
+ public static final String EXTRA_TEST_NAME = "testName";
+ /**
+ * True(default) to addAsRoot() for first Test, false to use add()
+ */
+ public static final String EXTRA_ADD_AS_ROOT = "addAsRoot";
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ Intent intent = getIntent();
+
+ if (savedInstanceState == null) {
+ GuidedStepTestFragment testFragment = new GuidedStepTestFragment(
+ intent.getStringExtra(EXTRA_TEST_NAME));
+ if (intent.getBooleanExtra(EXTRA_ADD_AS_ROOT, true)) {
+ GuidedStepTestFragment.addAsRoot(this, testFragment, android.R.id.content);
+ } else {
+ GuidedStepTestFragment.add(getFragmentManager(), testFragment,
+ android.R.id.content);
+ }
+ }
+ }
+}
diff --git a/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepFragmentTestBase.java b/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepFragmentTestBase.java
new file mode 100644
index 0000000..a90619b
--- /dev/null
+++ b/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepFragmentTestBase.java
@@ -0,0 +1,137 @@
+/*
+ * 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.v17.leanback.app;
+
+import android.app.Activity;
+import android.app.Fragment;
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.ViewGroup;
+import android.view.View;
+import android.view.LayoutInflater;
+import android.view.KeyEvent;
+import android.support.test.rule.ActivityTestRule;
+import android.support.test.runner.AndroidJUnit4;
+
+import android.support.v17.leanback.R;
+import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
+import android.support.v17.leanback.widget.GuidedAction;
+
+import android.support.test.espresso.Espresso;
+import android.support.test.espresso.matcher.RootMatchers;
+import android.support.test.espresso.action.ViewActions;
+import android.support.test.InstrumentationRegistry;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import org.mockito.stubbing.Answer;
+import org.mockito.invocation.InvocationOnMock;
+
+import java.util.List;
+
+import static org.mockito.Mockito.*;
+import static org.junit.Assert.*;
+
+/**
+ * @hide from javadoc
+ */
+public class GuidedStepFragmentTestBase {
+
+ static final long SMALL_DELAY = 250;
+ static final long TIMEOUT = 5000;
+
+ @Rule
+ public ActivityTestRule<GuidedStepFragmentTestActivity> activityTestRule
+ = new ActivityTestRule<>(GuidedStepFragmentTestActivity.class, false, false);
+
+ @Before
+ public void clearTests() {
+ GuidedStepTestFragment.clearTests();
+ }
+
+ public static void waitEnterTransitionFinish(GuidedStepTestFragment.Provider provider) {
+ long totalWait = 0;
+ int[] lastLocation = null;
+ int[] newLocation = new int[2];
+ while (true) {
+ GuidedStepTestFragment fragment = provider.getFragment();
+ if (fragment != null && fragment.getView() != null) {
+ View view = fragment.getView().findViewById(R.id.guidance_title);
+ if (view != null) {
+ if (lastLocation == null) {
+ // get initial location
+ lastLocation = new int[2];
+ view.getLocationInWindow(lastLocation);
+ } else {
+ // get new location and compare to old location
+ view.getLocationInWindow(newLocation);
+ if (newLocation[0] == lastLocation[0]
+ && newLocation[1] == lastLocation[1]) {
+ // location stable, animation finished
+ return;
+ }
+ lastLocation[0] = newLocation[0];
+ lastLocation[1] = newLocation[1];
+ }
+ }
+ }
+ try {
+ Thread.sleep(SMALL_DELAY);
+ } catch (InterruptedException ex) {
+ }
+ totalWait += SMALL_DELAY;
+ assertTrue("Timeout in wait GuidedStepTestFragment transition", totalWait < TIMEOUT);
+ }
+ }
+
+ public static void waitActivityDestroy(Activity activity) {
+ long totalWait = 0;
+ while (true) {
+ if (activity.isDestroyed()) {
+ return;
+ }
+ try {
+ Thread.sleep(SMALL_DELAY);
+ } catch (InterruptedException ex) {
+ }
+ totalWait += SMALL_DELAY;
+ assertTrue("Timeout in wait activity destroy", totalWait < TIMEOUT);
+ }
+ }
+
+ public static void sendKey(int keyCode) {
+ InstrumentationRegistry.getInstrumentation().sendKeyDownUpSync(keyCode);
+ }
+
+ public GuidedStepFragmentTestActivity launchTestActivity(String firstTestName) {
+ Intent intent = new Intent();
+ intent.putExtra(GuidedStepFragmentTestActivity.EXTRA_TEST_NAME, firstTestName);
+ return activityTestRule.launchActivity(intent);
+ }
+
+ public static GuidedStepTestFragment.Provider mockProvider(String testName) {
+ GuidedStepTestFragment.Provider test = mock(GuidedStepTestFragment.Provider.class);
+ when(test.getActivity()).thenCallRealMethod();
+ when(test.getFragmentManager()).thenCallRealMethod();
+ when(test.getFragment()).thenCallRealMethod();
+ GuidedStepTestFragment.setupTest(testName, test);
+ return test;
+ }
+}
+
diff --git a/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepSupportFragmentTest.java b/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepSupportFragmentTest.java
new file mode 100644
index 0000000..0876d7d
--- /dev/null
+++ b/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepSupportFragmentTest.java
@@ -0,0 +1,192 @@
+/* This file is auto-generated from GuidedStepFragmentTest.java. DO NOT MODIFY. */
+
+/*
+ * 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.v17.leanback.app;
+
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.ViewGroup;
+import android.view.View;
+import android.view.LayoutInflater;
+import android.view.KeyEvent;
+import android.support.test.rule.ActivityTestRule;
+import android.support.test.runner.AndroidJUnit4;
+
+import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
+import android.support.v17.leanback.widget.GuidedAction;
+
+import android.support.test.espresso.Espresso;
+import android.support.test.espresso.matcher.RootMatchers;
+import android.support.test.espresso.action.ViewActions;
+import android.support.test.InstrumentationRegistry;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import org.mockito.stubbing.Answer;
+import org.mockito.invocation.InvocationOnMock;
+
+import java.util.List;
+
+import static org.mockito.Mockito.*;
+import static org.junit.Assert.*;
+
+/**
+ * @hide from javadoc
+ */
+@RunWith(AndroidJUnit4.class)
+public class GuidedStepSupportFragmentTest extends GuidedStepSupportFragmentTestBase {
+
+ @Test
+ public void nextAndBack() throws Throwable {
+ GuidedStepTestSupportFragment.Provider first = mockProvider("first");
+ doAnswer(new Answer<Void>() {
+ public Void answer(InvocationOnMock invocation) {
+ List actions = (List) invocation.getArguments()[0];
+ actions.add(new GuidedAction.Builder().id(1000).title("OK").build());
+ return null;
+ }
+ }).when(first).onCreateActions(any(List.class), any(Bundle.class));
+ doAnswer(new Answer<Void>() {
+ public Void answer(InvocationOnMock invocation) {
+ GuidedAction action = (GuidedAction) invocation.getArguments()[0];
+ GuidedStepTestSupportFragment.Provider obj = (GuidedStepTestSupportFragment.Provider)
+ invocation.getMock();
+ if (action.getId() == 1000) {
+ GuidedStepSupportFragment.add(obj.getFragmentManager(),
+ new GuidedStepTestSupportFragment("second"));
+ }
+ return null;
+ }
+ }).when(first).onGuidedActionClicked(any(GuidedAction.class));
+
+ GuidedStepTestSupportFragment.Provider second = mockProvider("second");
+
+ GuidedStepSupportFragmentTestActivity activity = launchTestActivity("first");
+ verify(first, times(1)).onCreate(any(Bundle.class));
+ verify(first, times(1)).onCreateGuidance(any(Bundle.class));
+ verify(first, times(1)).onCreateActions(any(List.class), any(Bundle.class));
+ verify(first, times(1)).onCreateButtonActions(any(List.class), any(Bundle.class));
+ verify(first, times(1)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class),
+ any(Bundle.class), any(View.class));
+ verify(first, times(1)).onViewStateRestored(any(Bundle.class));
+ verify(first, times(1)).onStart();
+ verify(first, times(1)).onResume();
+
+ sendKey(KeyEvent.KEYCODE_DPAD_CENTER);
+ verify(first, times(1)).onGuidedActionClicked(any(GuidedAction.class));
+
+ waitEnterTransitionFinish(second);
+ verify(first, times(1)).onPause();
+ verify(first, times(1)).onStop();
+ verify(first, times(1)).onDestroyView();
+ verify(second, times(1)).onCreate(any(Bundle.class));
+ verify(second, times(1)).onCreateGuidance(any(Bundle.class));
+ verify(second, times(1)).onCreateActions(any(List.class), any(Bundle.class));
+ verify(second, times(1)).onCreateButtonActions(any(List.class), any(Bundle.class));
+ verify(second, times(1)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class),
+ any(Bundle.class), any(View.class));
+ verify(second, times(1)).onViewStateRestored(any(Bundle.class));
+ verify(second, times(1)).onStart();
+ verify(second, times(1)).onResume();
+
+ sendKey(KeyEvent.KEYCODE_BACK);
+
+ waitEnterTransitionFinish(first);
+ verify(second, times(1)).onPause();
+ verify(second, times(1)).onStop();
+ verify(second, times(1)).onDestroyView();
+ verify(second, times(1)).onDestroy();
+ verify(first, times(1)).onCreateActions(any(List.class), any(Bundle.class));
+ verify(first, times(2)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class),
+ any(Bundle.class), any(View.class));
+ verify(first, times(2)).onViewStateRestored(any(Bundle.class));
+ verify(first, times(2)).onStart();
+ verify(first, times(2)).onResume();
+
+ sendKey(KeyEvent.KEYCODE_BACK);
+ waitActivityDestroy(activity);
+ verify(first, times(1)).onDestroy();
+ assertTrue(activity.isDestroyed());
+ }
+
+ @Test
+ public void restoreFragments() throws Throwable {
+ GuidedStepTestSupportFragment.Provider first = mockProvider("first");
+ doAnswer(new Answer<Void>() {
+ public Void answer(InvocationOnMock invocation) {
+ List actions = (List) invocation.getArguments()[0];
+ actions.add(new GuidedAction.Builder().id(1000).title("OK").build());
+ actions.add(new GuidedAction.Builder().id(1001).editable(true).title("text")
+ .build());
+ actions.add(new GuidedAction.Builder().id(1002).editable(true).title("text")
+ .autoSaveRestoreEnabled(false).build());
+ return null;
+ }
+ }).when(first).onCreateActions(any(List.class), any(Bundle.class));
+ doAnswer(new Answer<Void>() {
+ public Void answer(InvocationOnMock invocation) {
+ GuidedAction action = (GuidedAction) invocation.getArguments()[0];
+ GuidedStepTestSupportFragment.Provider obj = (GuidedStepTestSupportFragment.Provider)
+ invocation.getMock();
+ if (action.getId() == 1000) {
+ GuidedStepSupportFragment.add(obj.getFragmentManager(),
+ new GuidedStepTestSupportFragment("second"));
+ }
+ return null;
+ }
+ }).when(first).onGuidedActionClicked(any(GuidedAction.class));
+
+ GuidedStepTestSupportFragment.Provider second = mockProvider("second");
+
+ final GuidedStepSupportFragmentTestActivity activity = launchTestActivity("first");
+ first.getFragment().findActionById(1001).setTitle("modified text");
+ first.getFragment().findActionById(1002).setTitle("modified text");
+ sendKey(KeyEvent.KEYCODE_DPAD_CENTER);
+ waitEnterTransitionFinish(second);
+
+ InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
+ @Override
+ public void run() {
+ activity.recreate();
+ }
+ });
+ waitEnterTransitionFinish(second);
+ verify(first, times(2)).onCreate(any(Bundle.class));
+ verify(first, times(1)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class),
+ any(Bundle.class), any(View.class));
+ verify(first, times(2)).onCreateActions(any(List.class), any(Bundle.class));
+ verify(first, times(1)).onDestroy();
+ verify(second, times(2)).onCreate(any(Bundle.class));
+ verify(second, times(2)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class),
+ any(Bundle.class), any(View.class));
+ verify(second, times(1)).onDestroy();
+ assertEquals("modified text", first.getFragment().findActionById(1001).getTitle());
+ assertEquals("text", first.getFragment().findActionById(1002).getTitle());
+
+ sendKey(KeyEvent.KEYCODE_BACK);
+ waitEnterTransitionFinish(first);
+ verify(second, times(2)).onPause();
+ verify(second, times(2)).onStop();
+ verify(second, times(2)).onDestroyView();
+ verify(second, times(2)).onDestroy();
+ verify(first, times(2)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class),
+ any(Bundle.class), any(View.class));
+ }
+}
diff --git a/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepSupportFragmentTestActivity.java b/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepSupportFragmentTestActivity.java
new file mode 100644
index 0000000..ea114df
--- /dev/null
+++ b/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepSupportFragmentTestActivity.java
@@ -0,0 +1,54 @@
+/* This file is auto-generated from GuidedStepFragmentTestActivity.java. DO NOT MODIFY. */
+
+/*
+ * 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.v17.leanback.app;
+
+import android.support.v4.app.FragmentActivity;
+import android.content.Intent;
+import android.os.Bundle;
+
+/**
+ * @hide from javadoc
+ */
+public class GuidedStepSupportFragmentTestActivity extends FragmentActivity {
+
+ /**
+ * Frst Test that will be included in this Activity
+ */
+ public static final String EXTRA_TEST_NAME = "testName";
+ /**
+ * True(default) to addAsRoot() for first Test, false to use add()
+ */
+ public static final String EXTRA_ADD_AS_ROOT = "addAsRoot";
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ Intent intent = getIntent();
+
+ if (savedInstanceState == null) {
+ GuidedStepTestSupportFragment testFragment = new GuidedStepTestSupportFragment(
+ intent.getStringExtra(EXTRA_TEST_NAME));
+ if (intent.getBooleanExtra(EXTRA_ADD_AS_ROOT, true)) {
+ GuidedStepTestSupportFragment.addAsRoot(this, testFragment, android.R.id.content);
+ } else {
+ GuidedStepTestSupportFragment.add(getSupportFragmentManager(), testFragment,
+ android.R.id.content);
+ }
+ }
+ }
+}
diff --git a/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepSupportFragmentTestBase.java b/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepSupportFragmentTestBase.java
new file mode 100644
index 0000000..a474f80
--- /dev/null
+++ b/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepSupportFragmentTestBase.java
@@ -0,0 +1,139 @@
+/* This file is auto-generated from GuidedStepFrgamentTestBase.java. DO NOT MODIFY. */
+
+/*
+ * 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.v17.leanback.app;
+
+import android.app.Activity;
+import android.app.Fragment;
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.ViewGroup;
+import android.view.View;
+import android.view.LayoutInflater;
+import android.view.KeyEvent;
+import android.support.test.rule.ActivityTestRule;
+import android.support.test.runner.AndroidJUnit4;
+
+import android.support.v17.leanback.R;
+import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
+import android.support.v17.leanback.widget.GuidedAction;
+
+import android.support.test.espresso.Espresso;
+import android.support.test.espresso.matcher.RootMatchers;
+import android.support.test.espresso.action.ViewActions;
+import android.support.test.InstrumentationRegistry;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import org.mockito.stubbing.Answer;
+import org.mockito.invocation.InvocationOnMock;
+
+import java.util.List;
+
+import static org.mockito.Mockito.*;
+import static org.junit.Assert.*;
+
+/**
+ * @hide from javadoc
+ */
+public class GuidedStepSupportFragmentTestBase {
+
+ static final long SMALL_DELAY = 250;
+ static final long TIMEOUT = 5000;
+
+ @Rule
+ public ActivityTestRule<GuidedStepSupportFragmentTestActivity> activityTestRule
+ = new ActivityTestRule<>(GuidedStepSupportFragmentTestActivity.class, false, false);
+
+ @Before
+ public void clearTests() {
+ GuidedStepTestSupportFragment.clearTests();
+ }
+
+ public static void waitEnterTransitionFinish(GuidedStepTestSupportFragment.Provider provider) {
+ long totalWait = 0;
+ int[] lastLocation = null;
+ int[] newLocation = new int[2];
+ while (true) {
+ GuidedStepTestSupportFragment fragment = provider.getFragment();
+ if (fragment != null && fragment.getView() != null) {
+ View view = fragment.getView().findViewById(R.id.guidance_title);
+ if (view != null) {
+ if (lastLocation == null) {
+ // get initial location
+ lastLocation = new int[2];
+ view.getLocationInWindow(lastLocation);
+ } else {
+ // get new location and compare to old location
+ view.getLocationInWindow(newLocation);
+ if (newLocation[0] == lastLocation[0]
+ && newLocation[1] == lastLocation[1]) {
+ // location stable, animation finished
+ return;
+ }
+ lastLocation[0] = newLocation[0];
+ lastLocation[1] = newLocation[1];
+ }
+ }
+ }
+ try {
+ Thread.sleep(SMALL_DELAY);
+ } catch (InterruptedException ex) {
+ }
+ totalWait += SMALL_DELAY;
+ assertTrue("Timeout in wait GuidedStepTestSupportFragment transition", totalWait < TIMEOUT);
+ }
+ }
+
+ public static void waitActivityDestroy(Activity activity) {
+ long totalWait = 0;
+ while (true) {
+ if (activity.isDestroyed()) {
+ return;
+ }
+ try {
+ Thread.sleep(SMALL_DELAY);
+ } catch (InterruptedException ex) {
+ }
+ totalWait += SMALL_DELAY;
+ assertTrue("Timeout in wait activity destroy", totalWait < TIMEOUT);
+ }
+ }
+
+ public static void sendKey(int keyCode) {
+ InstrumentationRegistry.getInstrumentation().sendKeyDownUpSync(keyCode);
+ }
+
+ public GuidedStepSupportFragmentTestActivity launchTestActivity(String firstTestName) {
+ Intent intent = new Intent();
+ intent.putExtra(GuidedStepSupportFragmentTestActivity.EXTRA_TEST_NAME, firstTestName);
+ return activityTestRule.launchActivity(intent);
+ }
+
+ public static GuidedStepTestSupportFragment.Provider mockProvider(String testName) {
+ GuidedStepTestSupportFragment.Provider test = mock(GuidedStepTestSupportFragment.Provider.class);
+ when(test.getActivity()).thenCallRealMethod();
+ when(test.getFragmentManager()).thenCallRealMethod();
+ when(test.getFragment()).thenCallRealMethod();
+ GuidedStepTestSupportFragment.setupTest(testName, test);
+ return test;
+ }
+}
+
diff --git a/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepTestFragment.java b/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepTestFragment.java
new file mode 100644
index 0000000..c530925
--- /dev/null
+++ b/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepTestFragment.java
@@ -0,0 +1,239 @@
+/*
+ * 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.v17.leanback.app;
+
+import android.app.Activity;
+import android.app.FragmentManager;
+import android.os.Bundle;
+import android.view.ViewGroup;
+import android.view.View;
+import android.view.LayoutInflater;
+
+
+import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
+import android.support.v17.leanback.widget.GuidedAction;
+
+import java.util.List;
+import java.util.HashMap;
+
+/**
+ * @hide from javadoc
+ */
+public class GuidedStepTestFragment extends GuidedStepFragment {
+
+ private static final String KEY_TEST_NAME = "key_test_name";
+
+ private static final HashMap<String, Provider> sTestMap = new HashMap<String, Provider>();
+
+ public static class Provider {
+
+ GuidedStepTestFragment mFragment;
+
+ public void onCreate(Bundle savedInstanceState) {
+ }
+
+ public void onSaveInstanceState(Bundle outState) {
+ }
+
+ public Guidance onCreateGuidance(Bundle savedInstanceState) {
+ return new Guidance("", "", "", null);
+ }
+
+ public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
+ }
+
+ public void onCreateButtonActions(List<GuidedAction> actions, Bundle savedInstanceState) {
+ }
+
+ public void onGuidedActionClicked(GuidedAction action) {
+ }
+
+ public boolean onSubGuidedActionClicked(GuidedAction action) {
+ return true;
+ }
+
+ public void onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState, View result) {
+ }
+
+ public void onDestroyView() {
+ }
+
+ public void onDestroy() {
+ }
+
+ public void onStart() {
+ }
+
+ public void onStop() {
+ }
+
+ public void onResume() {
+ }
+
+ public void onPause() {
+ }
+
+ public void onViewStateRestored(Bundle bundle) {
+ }
+
+ public void onDetach() {
+ }
+
+ public GuidedStepTestFragment getFragment() {
+ return mFragment;
+ }
+
+ public Activity getActivity() {
+ return mFragment.getActivity();
+ }
+
+ public FragmentManager getFragmentManager() {
+ return mFragment.getFragmentManager();
+ }
+ }
+
+ public static void setupTest(String testName, Provider provider) {
+ sTestMap.put(testName, provider);
+ }
+
+ public static void clearTests() {
+ sTestMap.clear();
+ }
+
+ CharSequence mTestName;
+ Provider mProvider;
+
+ public GuidedStepTestFragment() {
+ }
+
+ public GuidedStepTestFragment(String testName) {
+ setTestName(testName);
+ }
+
+ public void setTestName(CharSequence testName) {
+ mTestName = testName;
+ }
+
+ public CharSequence getTestName() {
+ return mTestName;
+ }
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ if (savedInstanceState != null) {
+ mTestName = savedInstanceState.getCharSequence(KEY_TEST_NAME, null);
+ }
+ mProvider = sTestMap.get(mTestName);
+ if (mProvider == null) {
+ throw new IllegalArgumentException("you must setupTest()");
+ }
+ mProvider.mFragment = this;
+ super.onCreate(savedInstanceState);
+ mProvider.onCreate(savedInstanceState);
+ }
+
+ @Override
+ public void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ outState.putCharSequence(KEY_TEST_NAME, mTestName);
+ mProvider.onSaveInstanceState(outState);
+ }
+
+ @Override
+ public Guidance onCreateGuidance(Bundle savedInstanceState) {
+ Guidance g = mProvider.onCreateGuidance(savedInstanceState);
+ if (g == null) {
+ g = new Guidance("", "", "", null);
+ }
+ return g;
+ }
+
+ @Override
+ public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
+ mProvider.onCreateActions(actions, savedInstanceState);
+ }
+
+ @Override
+ public void onCreateButtonActions(List<GuidedAction> actions, Bundle savedInstanceState) {
+ mProvider.onCreateButtonActions(actions, savedInstanceState);
+ }
+
+ @Override
+ public void onGuidedActionClicked(GuidedAction action) {
+ mProvider.onGuidedActionClicked(action);
+ }
+
+ @Override
+ public boolean onSubGuidedActionClicked(GuidedAction action) {
+ return mProvider.onSubGuidedActionClicked(action);
+ }
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
+ View view = super.onCreateView(inflater, container, state);
+ mProvider.onCreateView(inflater, container, state, view);
+ return view;
+ }
+
+ @Override
+ public void onDestroyView() {
+ mProvider.onDestroyView();
+ super.onDestroyView();
+ }
+
+ @Override
+ public void onDestroy() {
+ mProvider.onDestroy();
+ super.onDestroy();
+ }
+
+ @Override
+ public void onPause() {
+ mProvider.onPause();
+ super.onPause();
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+ mProvider.onResume();
+ }
+
+ @Override
+ public void onStart() {
+ super.onStart();
+ mProvider.onStart();
+ }
+
+ @Override
+ public void onStop() {
+ mProvider.onStop();
+ super.onStop();
+ }
+
+ @Override
+ public void onDetach() {
+ mProvider.onDetach();
+ super.onDetach();
+ }
+
+ @Override
+ public void onViewStateRestored(Bundle bundle) {
+ super.onViewStateRestored(bundle);
+ mProvider.onViewStateRestored(bundle);
+ }
+}
+
diff --git a/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepTestSupportFragment.java b/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepTestSupportFragment.java
new file mode 100644
index 0000000..04aa6f0
--- /dev/null
+++ b/v17/leanback/tests/java/android/support/v17/leanback/app/GuidedStepTestSupportFragment.java
@@ -0,0 +1,241 @@
+/* This file is auto-generated from GuidedStepTestFragment.java. DO NOT MODIFY. */
+
+/*
+ * 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.v17.leanback.app;
+
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentManager;
+import android.os.Bundle;
+import android.view.ViewGroup;
+import android.view.View;
+import android.view.LayoutInflater;
+
+
+import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
+import android.support.v17.leanback.widget.GuidedAction;
+
+import java.util.List;
+import java.util.HashMap;
+
+/**
+ * @hide from javadoc
+ */
+public class GuidedStepTestSupportFragment extends GuidedStepSupportFragment {
+
+ private static final String KEY_TEST_NAME = "key_test_name";
+
+ private static final HashMap<String, Provider> sTestMap = new HashMap<String, Provider>();
+
+ public static class Provider {
+
+ GuidedStepTestSupportFragment mFragment;
+
+ public void onCreate(Bundle savedInstanceState) {
+ }
+
+ public void onSaveInstanceState(Bundle outState) {
+ }
+
+ public Guidance onCreateGuidance(Bundle savedInstanceState) {
+ return new Guidance("", "", "", null);
+ }
+
+ public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
+ }
+
+ public void onCreateButtonActions(List<GuidedAction> actions, Bundle savedInstanceState) {
+ }
+
+ public void onGuidedActionClicked(GuidedAction action) {
+ }
+
+ public boolean onSubGuidedActionClicked(GuidedAction action) {
+ return true;
+ }
+
+ public void onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState, View result) {
+ }
+
+ public void onDestroyView() {
+ }
+
+ public void onDestroy() {
+ }
+
+ public void onStart() {
+ }
+
+ public void onStop() {
+ }
+
+ public void onResume() {
+ }
+
+ public void onPause() {
+ }
+
+ public void onViewStateRestored(Bundle bundle) {
+ }
+
+ public void onDetach() {
+ }
+
+ public GuidedStepTestSupportFragment getFragment() {
+ return mFragment;
+ }
+
+ public FragmentActivity getActivity() {
+ return mFragment.getActivity();
+ }
+
+ public FragmentManager getFragmentManager() {
+ return mFragment.getFragmentManager();
+ }
+ }
+
+ public static void setupTest(String testName, Provider provider) {
+ sTestMap.put(testName, provider);
+ }
+
+ public static void clearTests() {
+ sTestMap.clear();
+ }
+
+ CharSequence mTestName;
+ Provider mProvider;
+
+ public GuidedStepTestSupportFragment() {
+ }
+
+ public GuidedStepTestSupportFragment(String testName) {
+ setTestName(testName);
+ }
+
+ public void setTestName(CharSequence testName) {
+ mTestName = testName;
+ }
+
+ public CharSequence getTestName() {
+ return mTestName;
+ }
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ if (savedInstanceState != null) {
+ mTestName = savedInstanceState.getCharSequence(KEY_TEST_NAME, null);
+ }
+ mProvider = sTestMap.get(mTestName);
+ if (mProvider == null) {
+ throw new IllegalArgumentException("you must setupTest()");
+ }
+ mProvider.mFragment = this;
+ super.onCreate(savedInstanceState);
+ mProvider.onCreate(savedInstanceState);
+ }
+
+ @Override
+ public void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ outState.putCharSequence(KEY_TEST_NAME, mTestName);
+ mProvider.onSaveInstanceState(outState);
+ }
+
+ @Override
+ public Guidance onCreateGuidance(Bundle savedInstanceState) {
+ Guidance g = mProvider.onCreateGuidance(savedInstanceState);
+ if (g == null) {
+ g = new Guidance("", "", "", null);
+ }
+ return g;
+ }
+
+ @Override
+ public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
+ mProvider.onCreateActions(actions, savedInstanceState);
+ }
+
+ @Override
+ public void onCreateButtonActions(List<GuidedAction> actions, Bundle savedInstanceState) {
+ mProvider.onCreateButtonActions(actions, savedInstanceState);
+ }
+
+ @Override
+ public void onGuidedActionClicked(GuidedAction action) {
+ mProvider.onGuidedActionClicked(action);
+ }
+
+ @Override
+ public boolean onSubGuidedActionClicked(GuidedAction action) {
+ return mProvider.onSubGuidedActionClicked(action);
+ }
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
+ View view = super.onCreateView(inflater, container, state);
+ mProvider.onCreateView(inflater, container, state, view);
+ return view;
+ }
+
+ @Override
+ public void onDestroyView() {
+ mProvider.onDestroyView();
+ super.onDestroyView();
+ }
+
+ @Override
+ public void onDestroy() {
+ mProvider.onDestroy();
+ super.onDestroy();
+ }
+
+ @Override
+ public void onPause() {
+ mProvider.onPause();
+ super.onPause();
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+ mProvider.onResume();
+ }
+
+ @Override
+ public void onStart() {
+ super.onStart();
+ mProvider.onStart();
+ }
+
+ @Override
+ public void onStop() {
+ mProvider.onStop();
+ super.onStop();
+ }
+
+ @Override
+ public void onDetach() {
+ mProvider.onDetach();
+ super.onDetach();
+ }
+
+ @Override
+ public void onViewStateRestored(Bundle bundle) {
+ super.onViewStateRestored(bundle);
+ mProvider.onViewStateRestored(bundle);
+ }
+}
+