OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.blimp.core.settings; | 5 package org.chromium.blimp.core.settings; |
6 | 6 |
7 import android.app.FragmentManager; | 7 import android.app.FragmentManager; |
8 import android.content.Intent; | 8 import android.content.Intent; |
9 import android.preference.Preference; | 9 import android.preference.Preference; |
10 import android.preference.PreferenceFragment; | 10 import android.preference.PreferenceFragment; |
11 import android.preference.PreferenceScreen; | 11 import android.preference.PreferenceScreen; |
| 12 import android.preference.SwitchPreference; |
12 import android.test.suitebuilder.annotation.SmallTest; | 13 import android.test.suitebuilder.annotation.SmallTest; |
13 | 14 |
| 15 import org.chromium.base.ContextUtils; |
14 import org.chromium.base.ThreadUtils; | 16 import org.chromium.base.ThreadUtils; |
15 import org.chromium.base.test.BaseActivityInstrumentationTestCase; | 17 import org.chromium.base.test.BaseActivityInstrumentationTestCase; |
16 import org.chromium.blimp_public.BlimpSettingsCallbacks; | 18 import org.chromium.blimp.core.MockBlimpClientContext; |
| 19 import org.chromium.blimp.core.MockBlimpClientContextDelegate; |
| 20 import org.chromium.components.sync.signin.ChromeSigninController; |
17 | 21 |
18 /** | 22 /** |
19 * Test blimp setting page. | 23 * Test blimp setting page. |
20 */ | 24 */ |
21 public class BlimpPreferencesTest extends BaseActivityInstrumentationTestCase<Mo
ckPreferences> { | 25 public class BlimpPreferencesTest extends BaseActivityInstrumentationTestCase<Mo
ckPreferences> { |
22 | 26 |
23 public BlimpPreferencesTest() { | 27 public BlimpPreferencesTest() { |
24 super(MockPreferences.class); | 28 super(MockPreferences.class); |
25 } | 29 } |
26 | 30 |
27 // Launch the testing activity from non UI thread. | 31 // Launch the testing activity from non UI thread. |
28 private void launchActivity() { | 32 private void launchActivity() { |
29 assertFalse(ThreadUtils.runningOnUiThread()); | 33 assertFalse(ThreadUtils.runningOnUiThread()); |
30 Intent intent = new Intent(Intent.ACTION_MAIN); | 34 Intent intent = new Intent(Intent.ACTION_MAIN); |
31 setActivityIntent(intent); | 35 setActivityIntent(intent); |
32 | 36 |
33 // Start the activity. | 37 // Start the activity. |
34 getActivity(); | 38 getActivity(); |
35 } | 39 } |
36 | 40 |
37 // Mock callback implementation. | 41 /** |
38 static class MockSettingCallback implements BlimpSettingsCallbacks { | 42 * Mock {@link AboutBlimpPreferences}, the main test target class. |
39 private int mRestartCalled = 0; | 43 */ |
| 44 public static class MockAboutBlimpPreferences extends AboutBlimpPreferences
{ |
| 45 private static final String BLIMP_PREF_TAG = "TestBlimpPref"; |
| 46 |
| 47 // Avoid calling native code in instrumentation test. |
40 @Override | 48 @Override |
41 public void onRestartBrowserRequested() { | 49 protected void initializeNative() {} |
42 ++mRestartCalled; | 50 @Override |
43 } | 51 protected void destroyNative() {} |
44 } | |
45 | 52 |
46 // Mock {@link AboutBlimpPreferences}, the main test target class. | |
47 static class MockAboutBlimpPreferences extends AboutBlimpPreferences { | |
48 private static final String BLIMP_PREF_TAG = "TestBlimpPref"; | |
49 public void testRestartBrowser() { | 53 public void testRestartBrowser() { |
50 super.restartBrowser(); | 54 super.restartBrowser(); |
51 } | 55 } |
52 } | 56 } |
53 | 57 |
54 private Preference attachBlimpPref(PreferenceFragment fragment, | 58 private Preference attachBlimpPref(PreferenceFragment fragment) { |
55 BlimpSettingsCallbacks callback) { | |
56 ThreadUtils.assertOnUiThread(); | 59 ThreadUtils.assertOnUiThread(); |
57 assertNotNull(fragment); | 60 assertNotNull(fragment); |
58 MockAboutBlimpPreferences.addBlimpPreferences(fragment); | 61 MockAboutBlimpPreferences.addBlimpPreferences(fragment, new MockBlimpCli
entContext()); |
59 MockAboutBlimpPreferences.registerCallback(callback); | 62 return fragment.findPreference(PreferencesUtil.PREF_BLIMP_SWITCH); |
60 return fragment.findPreference(MockAboutBlimpPreferences.PREF_BLIMP_SWIT
CH); | 63 } |
| 64 |
| 65 // Create the setting page preference. |
| 66 private MockAboutBlimpPreferences createMockBlimpPreferenceFragment() { |
| 67 FragmentManager manager = getActivity().getFragmentManager(); |
| 68 manager.beginTransaction() |
| 69 .add(new MockAboutBlimpPreferences(), MockAboutBlimpPreferences.
BLIMP_PREF_TAG) |
| 70 .commitAllowingStateLoss(); |
| 71 manager.executePendingTransactions(); |
| 72 |
| 73 MockAboutBlimpPreferences blimpFragment = |
| 74 (MockAboutBlimpPreferences) manager.findFragmentByTag( |
| 75 MockAboutBlimpPreferences.BLIMP_PREF_TAG); |
| 76 |
| 77 assertNotNull("Blimp PreferenceFragment is not found.", blimpFragment); |
| 78 |
| 79 // Call onResume to load preferences items, this simulate the actual fra
gment life cycle. |
| 80 blimpFragment.onResume(); |
| 81 |
| 82 return blimpFragment; |
61 } | 83 } |
62 | 84 |
63 /** | 85 /** |
64 * Test if blimp settings can be attached correctly, and if callback is hand
led correctly. | 86 * Test if blimp settings can be attached correctly, and if callback is hand
led correctly. |
65 */ | 87 */ |
66 @SmallTest | 88 @SmallTest |
67 public void testBlimpPref() { | 89 public void testBlimpPref() { |
68 launchActivity(); | 90 launchActivity(); |
69 | 91 |
70 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 92 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
71 @Override | 93 @Override |
72 public void run() { | 94 public void run() { |
73 PreferenceFragment mainFragment = getActivity().getMainFragment(
); | 95 PreferenceFragment mainFragment = getActivity().getMainFragment(
); |
74 assertNotNull("Mock Main preferences is null.", mainFragment); | 96 assertNotNull("Mock Main preferences is null.", mainFragment); |
75 | 97 |
76 PreferenceScreen mainScreen = mainFragment.getPreferenceScreen()
; | 98 PreferenceScreen mainScreen = mainFragment.getPreferenceScreen()
; |
77 assertNotNull("Mock Main preferences screen is null.", mainScree
n); | 99 assertNotNull("Mock Main preferences screen is null.", mainScree
n); |
78 | 100 |
79 MockSettingCallback callback = new MockSettingCallback(); | |
80 | |
81 // Check if blimp settings preference item can be attached to ma
in preferences list. | 101 // Check if blimp settings preference item can be attached to ma
in preferences list. |
82 Preference blimpPref = attachBlimpPref(mainFragment, callback); | 102 Preference blimpPref = attachBlimpPref(mainFragment); |
83 assertNotNull("Blimp preference item in main preferences list is
not found.", | 103 assertNotNull("Blimp preference item in main preferences list is
not found.", |
84 blimpPref); | 104 blimpPref); |
85 | 105 |
86 // Open blimp settings page and check if callback is handled. | 106 // Open blimp settings page and check if callback is handled. |
87 FragmentManager manager = mainFragment.getFragmentManager(); | 107 MockAboutBlimpPreferences blimpFragment = createMockBlimpPrefere
nceFragment(); |
88 manager.beginTransaction().add( | 108 MockBlimpClientContext mockClientContext = new MockBlimpClientCo
ntext(); |
89 new MockAboutBlimpPreferences(), MockAboutBlimpPreferenc
es.BLIMP_PREF_TAG) | 109 MockAboutBlimpPreferences.setDelegate(mockClientContext); |
90 .commitAllowingStateLoss(); | |
91 manager.executePendingTransactions(); | |
92 | |
93 MockAboutBlimpPreferences blimpFragment = (MockAboutBlimpPrefere
nces) | |
94 manager.findFragmentByTag(MockAboutBlimpPreferences.BLIM
P_PREF_TAG); | |
95 assertNotNull("Blimp PreferenceFragment is not found.", blimpFra
gment); | |
96 | 110 |
97 blimpFragment.testRestartBrowser(); | 111 blimpFragment.testRestartBrowser(); |
98 assertEquals("Unexpected number of callback triggered.", 1, | 112 MockBlimpClientContextDelegate mockDelegate = |
99 callback.mRestartCalled); | 113 (MockBlimpClientContextDelegate) mockClientContext.getDe
legate(); |
| 114 assertEquals("Restart browser should be called.", |
| 115 mockDelegate.restartBrowserCalled(), 1); |
100 } | 116 } |
101 }); | 117 }); |
102 } | 118 } |
| 119 |
| 120 /** |
| 121 * Test the Blimp switch preference when user signed in or signed out. |
| 122 */ |
| 123 @SmallTest |
| 124 public void testSwitchPrefSignedIn() { |
| 125 launchActivity(); |
| 126 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 127 @Override |
| 128 public void run() { |
| 129 // Mock user sign in. |
| 130 ChromeSigninController signinController = |
| 131 ChromeSigninController.get(ContextUtils.getApplicationCo
ntext()); |
| 132 signinController.setSignedInAccountName("test@gmail.com"); |
| 133 assertTrue("User should be signed in.", signinController.isSigne
dIn()); |
| 134 |
| 135 MockBlimpClientContext mockClientContext = new MockBlimpClientCo
ntext(); |
| 136 MockAboutBlimpPreferences.setDelegate(mockClientContext); |
| 137 |
| 138 MockAboutBlimpPreferences blimpFragment = createMockBlimpPrefere
nceFragment(); |
| 139 |
| 140 // Test switch preference states after the setting page is creat
ed. |
| 141 SwitchPreference switchPref = (SwitchPreference) blimpFragment.f
indPreference( |
| 142 PreferencesUtil.PREF_BLIMP_SWITCH); |
| 143 boolean canUpdate = switchPref.getOnPreferenceChangeListener().o
nPreferenceChange( |
| 144 switchPref, true); |
| 145 assertTrue("User can change switch if the user signed in.", canU
pdate); |
| 146 assertEquals( |
| 147 "Connect should be called.", mockClientContext.connectCa
lledCount(), 1); |
| 148 mockClientContext.reset(); |
| 149 |
| 150 // Test switch state after user signed out. |
| 151 signinController.setSignedInAccountName(null); |
| 152 blimpFragment.onResume(); |
| 153 |
| 154 // Need to update the reference of switchPreference since UI wil
l remove all the |
| 155 // prefs, and fill in with new ones in onResume. |
| 156 switchPref = (SwitchPreference) blimpFragment.findPreference( |
| 157 PreferencesUtil.PREF_BLIMP_SWITCH); |
| 158 canUpdate = switchPref.getOnPreferenceChangeListener().onPrefere
nceChange( |
| 159 switchPref, true); |
| 160 assertFalse("User can't change switch if the user signed out.",
canUpdate); |
| 161 |
| 162 // Test waiting for sign in and sign in call from native code. |
| 163 blimpFragment.mWaitForSignIn = true; |
| 164 blimpFragment.onSignedIn(); |
| 165 switchPref = (SwitchPreference) blimpFragment.findPreference( |
| 166 PreferencesUtil.PREF_BLIMP_SWITCH); |
| 167 assertFalse( |
| 168 "UI should no longer wait for user sign in.", blimpFragm
ent.mWaitForSignIn); |
| 169 assertTrue("User sign in call should turn on the switch.", switc
hPref.isChecked()); |
| 170 assertEquals( |
| 171 "Connect should be called.", mockClientContext.connectCa
lledCount(), 1); |
| 172 mockClientContext.reset(); |
| 173 |
| 174 // Test sign out call from native code. |
| 175 blimpFragment.onSignedOut(); |
| 176 switchPref = (SwitchPreference) blimpFragment.findPreference( |
| 177 PreferencesUtil.PREF_BLIMP_SWITCH); |
| 178 assertFalse( |
| 179 "User sign out call should turn off the switch.", switch
Pref.isChecked()); |
| 180 } |
| 181 }); |
| 182 } |
103 } | 183 } |
OLD | NEW |