OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.input; | |
6 | |
7 import android.annotation.TargetApi; | |
8 import android.app.Activity; | |
9 import android.content.Intent; | |
10 import android.os.Build; | |
11 import android.provider.MediaStore; | |
12 import android.test.suitebuilder.annotation.MediumTest; | |
13 | |
14 import org.chromium.base.ThreadUtils; | |
15 import org.chromium.base.test.util.Feature; | |
16 import org.chromium.base.test.util.UrlUtils; | |
17 import org.chromium.chrome.shell.ChromeShellActivity; | |
18 import org.chromium.chrome.shell.ChromeShellActivity.ActivityWindowAndroidFactor
y; | |
19 import org.chromium.chrome.shell.ChromeShellTestBase; | |
20 import org.chromium.content.browser.ContentViewCore; | |
21 import org.chromium.content.browser.test.util.Criteria; | |
22 import org.chromium.content.browser.test.util.CriteriaHelper; | |
23 import org.chromium.content.browser.test.util.DOMUtils; | |
24 import org.chromium.ui.base.ActivityWindowAndroid; | |
25 | |
26 /** | |
27 * Integration test for select file dialog used for <input type="file" /> | |
28 */ | |
29 public class SelectFileDialogTest extends ChromeShellTestBase { | |
30 private static final String DATA_URL = UrlUtils.encodeHtmlDataUri( | |
31 "<html><head><meta name=\"viewport\"" | |
32 + "content=\"width=device-width, initial-scale=2.0, maximum-scale=2.
0\" /></head>" | |
33 + "<body><form action=\"about:blank\">" | |
34 + "<input id=\"input_file\" type=\"file\" /><br/>" | |
35 + "<input id=\"input_file_multiple\" type=\"file\" multiple /><br />
" | |
36 + "<input id=\"input_image\" type=\"file\" accept=\"image/*\" captur
e /><br/>" | |
37 + "<input id=\"input_audio\" type=\"file\" accept=\"audio/*\" captur
e />" | |
38 + "</form>" | |
39 + "</body></html>"); | |
40 | |
41 private ContentViewCore mContentViewCore; | |
42 private ActivityWindowAndroidForTest mActivityWindowAndroidForTest; | |
43 | |
44 private static class ActivityWindowAndroidForTest extends ActivityWindowAndr
oid { | |
45 public Intent lastIntent; | |
46 public IntentCallback lastCallback; | |
47 /** | |
48 * @param activity | |
49 */ | |
50 public ActivityWindowAndroidForTest(Activity activity) { | |
51 super(activity); | |
52 } | |
53 | |
54 @Override | |
55 public int showCancelableIntent(Intent intent, IntentCallback callback,
Integer errorId) { | |
56 lastIntent = intent; | |
57 lastCallback = callback; | |
58 return 1; | |
59 } | |
60 } | |
61 | |
62 private class IntentSentCriteria implements Criteria { | |
63 @Override | |
64 public boolean isSatisfied() { | |
65 return mActivityWindowAndroidForTest.lastIntent != null; | |
66 } | |
67 } | |
68 | |
69 @Override | |
70 public void setUp() throws Exception { | |
71 super.setUp(); | |
72 | |
73 ChromeShellActivity.setActivityWindowAndroidFactory(new ActivityWindowAn
droidFactory() { | |
74 @Override | |
75 public ActivityWindowAndroid getActivityWindowAndroid(Activity activ
ity) { | |
76 mActivityWindowAndroidForTest = new ActivityWindowAndroidForTest
(activity); | |
77 return mActivityWindowAndroidForTest; | |
78 } | |
79 }); | |
80 launchChromeShellWithUrl(DATA_URL); | |
81 assertTrue("Page failed to load", waitForActiveShellToBeDoneLoading()); | |
82 | |
83 mContentViewCore = getActivity().getActiveContentViewCore(); | |
84 // TODO(aurimas) remove this wait once crbug.com/179511 is fixed. | |
85 assertWaitForPageScaleFactorMatch(2); | |
86 assertTrue( | |
87 DOMUtils.waitForNonZeroNodeBounds(mContentViewCore.getWebContent
s(), "input_file")); | |
88 } | |
89 | |
90 /** | |
91 * Tests that clicks on <input type="file" /> trigger intent calls to Activi
tyWindowAndroid. | |
92 */ | |
93 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) | |
94 @MediumTest | |
95 @Feature({"TextInput", "Main"}) | |
96 public void testSelectFileAndCancelRequest() throws Throwable { | |
97 DOMUtils.clickNode(this, mContentViewCore, "input_file"); | |
98 assertTrue("SelectFileDialog never sent an intent.", | |
99 CriteriaHelper.pollForCriteria(new IntentSentCriteria())); | |
100 assertEquals(Intent.ACTION_CHOOSER, mActivityWindowAndroidForTest.lastIn
tent.getAction()); | |
101 resetActivityWindowAndroidForTest(); | |
102 | |
103 DOMUtils.clickNode(this, mContentViewCore, "input_file_multiple"); | |
104 assertTrue("SelectFileDialog never sent an intent.", | |
105 CriteriaHelper.pollForCriteria(new IntentSentCriteria())); | |
106 assertEquals(Intent.ACTION_CHOOSER, mActivityWindowAndroidForTest.lastIn
tent.getAction()); | |
107 Intent contentIntent = (Intent) | |
108 mActivityWindowAndroidForTest.lastIntent.getParcelableExtra(Inte
nt.EXTRA_INTENT); | |
109 assertNotNull(contentIntent); | |
110 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { | |
111 assertTrue(contentIntent.hasExtra(Intent.EXTRA_ALLOW_MULTIPLE)); | |
112 } | |
113 resetActivityWindowAndroidForTest(); | |
114 | |
115 DOMUtils.clickNode(this, mContentViewCore, "input_image"); | |
116 assertTrue("SelectFileDialog never sent an intent.", | |
117 CriteriaHelper.pollForCriteria(new IntentSentCriteria())); | |
118 assertEquals(MediaStore.ACTION_IMAGE_CAPTURE, | |
119 mActivityWindowAndroidForTest.lastIntent.getAction()); | |
120 resetActivityWindowAndroidForTest(); | |
121 | |
122 DOMUtils.clickNode(this, mContentViewCore, "input_audio"); | |
123 assertTrue("SelectFileDialog never sent an intent.", | |
124 CriteriaHelper.pollForCriteria(new IntentSentCriteria())); | |
125 assertEquals(MediaStore.Audio.Media.RECORD_SOUND_ACTION, | |
126 mActivityWindowAndroidForTest.lastIntent.getAction()); | |
127 resetActivityWindowAndroidForTest(); | |
128 } | |
129 | |
130 private void resetActivityWindowAndroidForTest() { | |
131 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
132 @Override | |
133 public void run() { | |
134 mActivityWindowAndroidForTest.lastCallback.onIntentCompleted( | |
135 mActivityWindowAndroidForTest, Activity.RESULT_CANCELED,
null, null); | |
136 } | |
137 }); | |
138 mActivityWindowAndroidForTest.lastCallback = null; | |
139 mActivityWindowAndroidForTest.lastIntent = null; | |
140 } | |
141 } | |
OLD | NEW |