Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(126)

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/ContentViewFocusTest.java

Issue 1141283003: Upstream oodles of Chrome for Android code into Chromium. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final patch? Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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;
6
7 import static org.chromium.base.test.util.Restriction.RESTRICTION_TYPE_PHONE;
8
9 import android.test.FlakyTest;
10 import android.test.suitebuilder.annotation.MediumTest;
11 import android.view.View;
12 import android.view.View.OnFocusChangeListener;
13
14 import com.google.android.apps.chrome.R;
15
16 import org.chromium.base.ThreadUtils;
17 import org.chromium.base.test.util.Feature;
18 import org.chromium.base.test.util.Restriction;
19 import org.chromium.base.test.util.UrlUtils;
20 import org.chromium.chrome.browser.compositor.layouts.LayoutManager;
21 import org.chromium.chrome.browser.compositor.layouts.eventfilter.EdgeSwipeEvent Filter.ScrollDirection;
22 import org.chromium.chrome.browser.compositor.layouts.eventfilter.EdgeSwipeHandl er;
23 import org.chromium.chrome.test.ChromeTabbedActivityTestBase;
24 import org.chromium.chrome.test.util.ChromeTabUtils;
25 import org.chromium.chrome.test.util.OverviewModeBehaviorWatcher;
26 import org.chromium.content.browser.test.util.Criteria;
27 import org.chromium.content.browser.test.util.CriteriaHelper;
28 import org.chromium.content.browser.test.util.TestTouchUtils;
29
30 import java.util.ArrayDeque;
31
32 /**
33 * Test suite for ContentView focus and its interaction with Tab switcher,
34 * Tab swiping, etc.
35 */
36 public class ContentViewFocusTest extends ChromeTabbedActivityTestBase {
37
38 private static final int WAIT_RESPONSE_MS = 2000;
39
40 private final ArrayDeque<Boolean> mFocusChanges = new ArrayDeque<Boolean>();
41
42 private void addFocusChangedListener(View view) {
43 view.setOnFocusChangeListener(new OnFocusChangeListener() {
44 @Override
45 public void onFocusChange(View v, boolean hasFocus) {
46 synchronized (mFocusChanges) {
47 mFocusChanges.add(Boolean.valueOf(hasFocus));
48 mFocusChanges.notify();
49 }
50 }
51 });
52 }
53
54 private boolean blockForFocusChanged() throws InterruptedException {
55 long endTime = System.currentTimeMillis() + WAIT_RESPONSE_MS * 2;
56 synchronized (mFocusChanges) {
57 while (true) {
58 if (!mFocusChanges.isEmpty()) {
59 return mFocusChanges.removeFirst();
60 }
61 long sleepTime = endTime - System.currentTimeMillis();
62 if (sleepTime <= 0) {
63 throw new RuntimeException("Didn't get event");
64 }
65 mFocusChanges.wait(sleepTime);
66 }
67 }
68 }
69
70 private boolean haveFocusChanges() {
71 synchronized (mFocusChanges) {
72 return !mFocusChanges.isEmpty();
73 }
74 }
75
76 /**
77 * Verify ContentView loses/gains focus on swiping tab.
78 *
79 * @throws Exception
80 * @MediumTest
81 * @Feature({"TabContents"})
82 * @Restriction(RESTRICTION_TYPE_PHONE)
83 * Bug: http://crbug.com/172473
84 */
85 @FlakyTest
86 public void testHideSelectionOnPhoneTabSwiping() throws Exception {
87 // Setup
88 ChromeTabUtils.newTabsFromMenu(getInstrumentation(), getActivity(), 2);
89 String url = UrlUtils.getIsolatedTestFileUrl(
90 "chrome/test/data/android/content_view_focus/content_view_focus_ long_text.html");
91 loadUrl(url);
92 View view = getActivity().getActivityTab().getContentViewCore().getConta inerView();
93
94 // Give the content view focus
95 TestTouchUtils.longClickView(getInstrumentation(), view, 50, 10);
96 assertTrue("ContentView is focused", view.hasFocus());
97
98 // Start the swipe
99 addFocusChangedListener(view);
100 final EdgeSwipeHandler edgeSwipeHandler =
101 getActivity().getLayoutManager().getTopSwipeHandler();
102 ThreadUtils.runOnUiThread(new Runnable() {
103 @Override
104 public void run() {
105 edgeSwipeHandler.swipeStarted(ScrollDirection.RIGHT, 0, 0);
106 edgeSwipeHandler.swipeUpdated(100, 0, 100, 0, 100, 0);
107 }
108 });
109
110 assertTrue("Layout still requesting Tab Android view be attached",
111 CriteriaHelper.pollForUIThreadCriteria(new Criteria() {
112 @Override
113 public boolean isSatisfied() {
114 LayoutManager driver = getActivity().getLayoutManager();
115 return !driver.getActiveLayout().shouldDisplayContentOve rlay();
116 }
117 }));
118
119 // Make sure the view loses focus. It is immediately given focus back
120 // because it's the only focusable view.
121 assertFalse("Content view didn't lose focus", blockForFocusChanged());
122
123 // End the drag
124 ThreadUtils.runOnUiThread(new Runnable() {
125 @Override
126 public void run() {
127 edgeSwipeHandler.swipeFinished();
128 }
129 });
130
131 assertTrue("Layout not requesting Tab Android view be attached",
132 CriteriaHelper.pollForUIThreadCriteria(new Criteria() {
133 @Override
134 public boolean isSatisfied() {
135 LayoutManager driver = getActivity().getLayoutManager();
136 return driver.getActiveLayout().shouldDisplayContentOver lay();
137 }
138 }));
139
140 assertTrue("Content view didn't regain focus", blockForFocusChanged());
141 assertFalse("Unexpected focus change", haveFocusChanges());
142 }
143
144 /**
145 * Verify ContentView loses/gains focus on overview mode.
146 *
147 * @throws Exception
148 * @Feature({"TabContents"})
149 */
150 @MediumTest
151 @Feature({"TabContents"})
152 @Restriction(RESTRICTION_TYPE_PHONE)
153 public void testHideSelectionOnPhoneTabSwitcher() throws Exception {
154 // Setup
155 OverviewModeBehaviorWatcher showWatcher = new OverviewModeBehaviorWatche r(
156 getActivity().getLayoutManager(), true, false);
157 OverviewModeBehaviorWatcher hideWatcher = new OverviewModeBehaviorWatche r(
158 getActivity().getLayoutManager(), false, true);
159 View currentView = getActivity().getActivityTab().getContentViewCore().g etContainerView();
160 addFocusChangedListener(currentView);
161
162 // Enter the tab switcher
163 View tabSwitcherButton = getActivity().findViewById(R.id.tab_switcher_bu tton);
164 assertNotNull("'tab_switcher_button' view is not found.", tabSwitcherBut ton);
165 singleClickView(getActivity().findViewById(R.id.tab_switcher_button));
166 showWatcher.waitForBehavior();
167
168 // Make sure the view loses focus. It is immediately given focus back
169 // because it's the only focusable view.
170 assertFalse("Content view didn't lose focus", blockForFocusChanged());
171
172 // Hide the tab switcher
173 tabSwitcherButton = getActivity().findViewById(R.id.tab_switcher_button) ;
174 assertNotNull("'tab_switcher_button' view is not found.", tabSwitcherBut ton);
175 singleClickView(getActivity().findViewById(R.id.tab_switcher_button));
176 hideWatcher.waitForBehavior();
177
178 assertTrue("Content view didn't regain focus", blockForFocusChanged());
179 assertFalse("Unexpected focus change", haveFocusChanges());
180 }
181
182 @Override
183 public void startMainActivity() throws InterruptedException {
184 startMainActivityOnBlankPage();
185 }
186 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698