Commit c0233a18 authored by changwan@chromium.org's avatar changwan@chromium.org

[Android] Add meta-data to point to implementations of SmartClipProvider

Also enhance SmartClipProviderTest to emulate what OEM will do.

BUG=388961
TEST=passed SmartClipProviderTest

Review URL: https://codereview.chromium.org/391813006

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@284018 0039d316-1c4b-4281-b951-d872f2087c98
parent 8ca6e2b1
......@@ -4,26 +4,36 @@
package org.chromium.chrome.browser;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.test.suitebuilder.annotation.MediumTest;
import android.view.View;
import android.view.ViewGroup;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.Feature;
import org.chromium.chrome.shell.ChromeShellActivity;
import org.chromium.chrome.shell.ChromeShellTestBase;
import org.chromium.content.browser.ContentView;
import org.chromium.chrome.shell.R;
import org.chromium.content.browser.test.util.CallbackHelper;
import java.lang.reflect.Method;
import java.util.concurrent.TimeoutException;
/**
* Tests for the ContentView.
* Tests for the SmartClipProvider.
*/
public class ContentViewTest extends ChromeShellTestBase implements Handler.Callback {
public class SmartClipProviderTest extends ChromeShellTestBase implements Handler.Callback {
// This is a key for meta-data in the package manifest. It should NOT
// change, as OEMs will use it when they look for the SmartClipProvider
// interface.
private static final String SMART_CLIP_PROVIDER_KEY =
"org.chromium.content.browser.SMART_CLIP_PROVIDER";
private static class MyCallbackHelper extends CallbackHelper {
public String getTitle() {
......@@ -66,6 +76,9 @@ public class ContentViewTest extends ChromeShellTestBase implements Handler.Call
private MyCallbackHelper mCallbackHelper;
private HandlerThread mHandlerThread;
private Handler mHandler;
private Class<?> mSmartClipProviderClass;
private Method mSetSmartClipResultHandlerMethod;
private Method mExtractSmartClipDataMethod;
@Override
public void setUp() throws Exception {
......@@ -75,6 +88,14 @@ public class ContentViewTest extends ChromeShellTestBase implements Handler.Call
mHandlerThread = new HandlerThread("ContentViewTest thread");
mHandlerThread.start();
mHandler = new Handler(mHandlerThread.getLooper(), this);
mSmartClipProviderClass = getSmartClipProviderClass();
assertNotNull(mSmartClipProviderClass);
mSetSmartClipResultHandlerMethod = mSmartClipProviderClass.getDeclaredMethod(
"setSmartClipResultHandler", new Class[] { Handler.class });
mExtractSmartClipDataMethod = mSmartClipProviderClass.getDeclaredMethod(
"extractSmartClipData",
new Class[] { Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE });
}
@Override
......@@ -101,16 +122,54 @@ public class ContentViewTest extends ChromeShellTestBase implements Handler.Call
return true;
}
// Create SmartClipProvider interface from package meta-data.
private Class<?> getSmartClipProviderClass() throws Exception {
ApplicationInfo ai = mActivity.getPackageManager().getApplicationInfo(
mActivity.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
String className = bundle.getString(SMART_CLIP_PROVIDER_KEY);
assertNotNull(className);
return Class.forName(className);
}
// Returns the first smart clip provider under the root view using DFS.
private Object findSmartClipProvider(View v) {
if (mSmartClipProviderClass.isInstance(v)) {
return v;
} else if (v instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) v;
int count = viewGroup.getChildCount();
for (int i = 0; i < count; ++i) {
View c = viewGroup.getChildAt(i);
Object found = findSmartClipProvider(c);
if (found != null)
return found;
}
}
return null;
}
@MediumTest
@Feature({"SmartClip"})
public void testSmartClipDataCallback() throws InterruptedException, TimeoutException {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
ContentView cv = (ContentView) getActivity().getActiveTab().getView();
assertNotNull(cv);
cv.setSmartClipResultHandler(mHandler);
cv.extractSmartClipData(10, 20, 100, 70);
// This emulates what OEM will be doing when they want to call
// functions on SmartClipProvider through view hierarchy.
// Implementation of SmartClipProvider such as ContentView or
// JellyBeanContentView can be found somewhere under content_container.
Object scp = findSmartClipProvider(
getActivity().findViewById(R.id.content_container));
assertNotNull(scp);
try {
mSetSmartClipResultHandlerMethod.invoke(scp, mHandler);
mExtractSmartClipDataMethod.invoke(scp, 10, 20, 100, 70);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
});
mCallbackHelper.waitForCallback(0, 1); // call count: 0 --> 1
......
......@@ -233,5 +233,8 @@
<action android:name="android.accounts.LOGIN_ACCOUNTS_CHANGED" />
</intent-filter>
</receiver>
<meta-data android:name="org.chromium.content.browser.SMART_CLIP_PROVIDER"
android:value="org.chromium.content.browser.SmartClipProvider" />
</application>
</manifest>
......@@ -130,6 +130,8 @@
android:permission="org.chromium.content_shell.permission.SANDBOX"
android:isolatedProcess="true"
android:exported="false" />
<meta-data android:name="org.chromium.content.browser.SMART_CLIP_PROVIDER"
android:value="org.chromium.content.browser.SmartClipProvider" />
</application>
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" />
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment