OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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.chrome.browser; | 5 package org.chromium.chrome.browser; |
6 | 6 |
7 import android.annotation.TargetApi; | 7 import android.annotation.TargetApi; |
8 import android.content.pm.ApplicationInfo; | 8 import android.content.pm.ApplicationInfo; |
9 import android.content.pm.PackageManager; | 9 import android.content.pm.PackageManager; |
| 10 import android.graphics.Rect; |
10 import android.os.Build; | 11 import android.os.Build; |
11 import android.os.Bundle; | 12 import android.os.Bundle; |
12 import android.os.Handler; | 13 import android.os.Handler; |
13 import android.os.HandlerThread; | 14 import android.os.HandlerThread; |
14 import android.os.Message; | 15 import android.os.Message; |
15 import android.support.test.filters.MediumTest; | 16 import android.support.test.filters.MediumTest; |
16 import android.view.View; | 17 import android.view.View; |
17 import android.view.ViewGroup; | 18 import android.view.ViewGroup; |
18 | 19 |
19 import org.chromium.base.ThreadUtils; | 20 import org.chromium.base.ThreadUtils; |
(...skipping 26 matching lines...) Expand all Loading... |
46 } | 47 } |
47 | 48 |
48 public String getText() { | 49 public String getText() { |
49 return mText; | 50 return mText; |
50 } | 51 } |
51 | 52 |
52 public String getHtml() { | 53 public String getHtml() { |
53 return mHtml; | 54 return mHtml; |
54 } | 55 } |
55 | 56 |
56 public void notifyCalled(String title, String url, String text, String h
tml) { | 57 public Rect getRect() { |
| 58 return mRect; |
| 59 } |
| 60 |
| 61 public void notifyCalled(String title, String url, String text, String h
tml, Rect rect) { |
57 mTitle = title; | 62 mTitle = title; |
58 mUrl = url; | 63 mUrl = url; |
59 mText = text; | 64 mText = text; |
60 mHtml = html; | 65 mHtml = html; |
| 66 mRect = rect; |
61 super.notifyCalled(); | 67 super.notifyCalled(); |
62 } | 68 } |
63 | 69 |
64 private String mTitle; | 70 private String mTitle; |
65 private String mUrl; | 71 private String mUrl; |
66 private String mText; | 72 private String mText; |
67 private String mHtml; | 73 private String mHtml; |
| 74 private Rect mRect; |
68 } | 75 } |
69 | 76 |
70 private ChromeActivity mActivity; | 77 private ChromeActivity mActivity; |
71 private MyCallbackHelper mCallbackHelper; | 78 private MyCallbackHelper mCallbackHelper; |
72 private HandlerThread mHandlerThread; | 79 private HandlerThread mHandlerThread; |
73 private Handler mHandler; | 80 private Handler mHandler; |
74 private Class<?> mSmartClipProviderClass; | 81 private Class<?> mSmartClipProviderClass; |
75 private Method mSetSmartClipResultHandlerMethod; | 82 private Method mSetSmartClipResultHandlerMethod; |
76 private Method mExtractSmartClipDataMethod; | 83 private Method mExtractSmartClipDataMethod; |
77 | 84 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 125 |
119 // Implements Handler.Callback | 126 // Implements Handler.Callback |
120 @Override | 127 @Override |
121 public boolean handleMessage(Message msg) { | 128 public boolean handleMessage(Message msg) { |
122 Bundle bundle = msg.getData(); | 129 Bundle bundle = msg.getData(); |
123 assertNotNull(bundle); | 130 assertNotNull(bundle); |
124 String url = bundle.getString("url"); | 131 String url = bundle.getString("url"); |
125 String title = bundle.getString("title"); | 132 String title = bundle.getString("title"); |
126 String text = bundle.getString("text"); | 133 String text = bundle.getString("text"); |
127 String html = bundle.getString("html"); | 134 String html = bundle.getString("html"); |
| 135 Rect rect = bundle.getParcelable("rect"); |
128 // We don't care about other values for now. | 136 // We don't care about other values for now. |
129 mCallbackHelper.notifyCalled(title, url, text, html); | 137 mCallbackHelper.notifyCalled(title, url, text, html, rect); |
130 return true; | 138 return true; |
131 } | 139 } |
132 | 140 |
133 // Create SmartClipProvider interface from package meta-data. | 141 // Create SmartClipProvider interface from package meta-data. |
134 private Class<?> getSmartClipProviderClass() throws Exception { | 142 private Class<?> getSmartClipProviderClass() throws Exception { |
135 ApplicationInfo ai = mActivity.getPackageManager().getApplicationInfo( | 143 ApplicationInfo ai = mActivity.getPackageManager().getApplicationInfo( |
136 mActivity.getPackageName(), PackageManager.GET_META_DATA); | 144 mActivity.getPackageName(), PackageManager.GET_META_DATA); |
137 Bundle bundle = ai.metaData; | 145 Bundle bundle = ai.metaData; |
138 String className = bundle.getString(SMART_CLIP_PROVIDER_KEY); | 146 String className = bundle.getString(SMART_CLIP_PROVIDER_KEY); |
139 assertNotNull(className); | 147 assertNotNull(className); |
(...skipping 13 matching lines...) Expand all Loading... |
153 if (found != null) return found; | 161 if (found != null) return found; |
154 } | 162 } |
155 } | 163 } |
156 return null; | 164 return null; |
157 } | 165 } |
158 | 166 |
159 @MediumTest | 167 @MediumTest |
160 @Feature({"SmartClip"}) | 168 @Feature({"SmartClip"}) |
161 @RetryOnFailure | 169 @RetryOnFailure |
162 public void testSmartClipDataCallback() throws InterruptedException, Timeout
Exception { | 170 public void testSmartClipDataCallback() throws InterruptedException, Timeout
Exception { |
| 171 final Rect rect = new Rect(10, 20, 110, 190); |
163 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 172 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
164 @Override | 173 @Override |
165 public void run() { | 174 public void run() { |
166 // This emulates what OEM will be doing when they want to call | 175 // This emulates what OEM will be doing when they want to call |
167 // functions on SmartClipProvider through view hierarchy. | 176 // functions on SmartClipProvider through view hierarchy. |
168 | 177 |
169 Object scp = | 178 Object scp = |
170 findSmartClipProvider(getActivity().findViewById(android
.R.id.content)); | 179 findSmartClipProvider(getActivity().findViewById(android
.R.id.content)); |
171 assertNotNull(scp); | 180 assertNotNull(scp); |
172 try { | 181 try { |
173 mSetSmartClipResultHandlerMethod.invoke(scp, mHandler); | 182 mSetSmartClipResultHandlerMethod.invoke(scp, mHandler); |
174 mExtractSmartClipDataMethod.invoke(scp, 10, 20, 100, 70); | 183 mExtractSmartClipDataMethod.invoke( |
| 184 scp, rect.left, rect.top, rect.width(), rect.height(
)); |
175 } catch (Exception e) { | 185 } catch (Exception e) { |
176 e.printStackTrace(); | 186 e.printStackTrace(); |
177 fail(); | 187 fail(); |
178 } | 188 } |
179 } | 189 } |
180 }); | 190 }); |
181 mCallbackHelper.waitForCallback(0, 1); // call count: 0 --> 1 | 191 mCallbackHelper.waitForCallback(0, 1); // call count: 0 --> 1 |
182 assertEquals("about:blank", mCallbackHelper.getTitle()); | 192 assertEquals("about:blank", mCallbackHelper.getTitle()); |
183 assertEquals("about:blank", mCallbackHelper.getUrl()); | 193 assertEquals("about:blank", mCallbackHelper.getUrl()); |
184 assertNotNull(mCallbackHelper.getText()); | 194 assertNotNull(mCallbackHelper.getText()); |
185 assertNotNull(mCallbackHelper.getHtml()); | 195 assertNotNull(mCallbackHelper.getHtml()); |
| 196 assertNotNull(mCallbackHelper.getRect()); |
| 197 assertEquals(rect.left, mCallbackHelper.getRect().left); |
| 198 assertEquals(rect.top, mCallbackHelper.getRect().top); |
| 199 assertEquals(rect.width(), mCallbackHelper.getRect().width()); |
| 200 assertEquals(rect.height(), mCallbackHelper.getRect().height()); |
186 } | 201 } |
187 | 202 |
188 @MediumTest | 203 @MediumTest |
189 @Feature({"SmartClip"}) | 204 @Feature({"SmartClip"}) |
190 @RetryOnFailure | 205 @RetryOnFailure |
191 public void testSmartClipNoHandlerDoesntCrash() throws InterruptedException,
TimeoutException { | 206 public void testSmartClipNoHandlerDoesntCrash() throws InterruptedException,
TimeoutException { |
192 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 207 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
193 @Override | 208 @Override |
194 public void run() { | 209 public void run() { |
195 Object scp = | 210 Object scp = |
(...skipping 10 matching lines...) Expand all Loading... |
206 mExtractSmartClipDataMethod.invoke(scp, 10, 20, 100, 70); | 221 mExtractSmartClipDataMethod.invoke(scp, 10, 20, 100, 70); |
207 } catch (Exception e) { | 222 } catch (Exception e) { |
208 e.printStackTrace(); | 223 e.printStackTrace(); |
209 fail(); | 224 fail(); |
210 } | 225 } |
211 } | 226 } |
212 }); | 227 }); |
213 mCallbackHelper.waitForCallback(0, 1); // call count: 0 --> 1 | 228 mCallbackHelper.waitForCallback(0, 1); // call count: 0 --> 1 |
214 } | 229 } |
215 } | 230 } |
OLD | NEW |