Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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.content_shell; | 5 package org.chromium.content_shell; |
| 6 | 6 |
| 7 import android.content.ComponentName; | 7 import android.content.ComponentName; |
| 8 import android.content.Intent; | 8 import android.content.Intent; |
| 9 import android.net.Uri; | 9 import android.net.Uri; |
| 10 import android.test.ActivityInstrumentationTestCase2; | 10 import android.test.ActivityInstrumentationTestCase2; |
| 11 | 11 |
| 12 import org.chromium.content.browser.ContentView; | |
| 13 import org.chromium.content.browser.ContentViewClient; | |
| 14 import org.chromium.content.browser.LoadUrlParams; | |
| 15 import org.chromium.content.browser.test.TestContentViewClient; | |
| 16 | |
| 17 import java.util.HashMap; | |
| 18 import java.util.Map; | |
| 19 import java.util.concurrent.TimeUnit; | |
| 20 | |
| 12 /** | 21 /** |
| 13 * Base test class for all ContentShell based tests. | 22 * Base test class for all ContentShell based tests. |
| 14 */ | 23 */ |
| 15 public class ContentShellTestBase extends ActivityInstrumentationTestCase2<Conte ntShellActivity> { | 24 public class ContentShellTestBase extends ActivityInstrumentationTestCase2<Conte ntShellActivity> { |
| 25 private Map<ContentView, TestContentViewClient> mTestContentViewClients | |
| 26 = new HashMap<ContentView, TestContentViewClient>(); | |
|
klundberg
2012/09/07 16:15:29
I don't think you should add this to ContentShellT
Ramya
2012/09/19 21:31:57
Done.
| |
| 27 | |
| 28 protected static int WAIT_TIMEOUT_SECONDS = 15; | |
| 16 | 29 |
| 17 public ContentShellTestBase() { | 30 public ContentShellTestBase() { |
| 18 super(ContentShellActivity.class); | 31 super(ContentShellActivity.class); |
| 19 } | 32 } |
| 20 | 33 |
| 21 /** | 34 /** |
| 22 * Starts the ContentShell activity and loads the given URL. | 35 * Starts the ContentShell activity and loads the given URL. |
| 23 */ | 36 */ |
| 24 protected ContentShellActivity launchContentShellWithUrl(String url) { | 37 protected ContentShellActivity launchContentShellWithUrl(String url) { |
| 25 Intent intent = new Intent(Intent.ACTION_MAIN); | 38 Intent intent = new Intent(Intent.ACTION_MAIN); |
| 26 intent.addCategory(Intent.CATEGORY_LAUNCHER); | 39 intent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 27 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | 40 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 28 intent.setData(Uri.parse(url)); | 41 intent.setData(Uri.parse(url)); |
| 29 intent.setComponent(new ComponentName(getInstrumentation().getTargetCont ext(), | 42 intent.setComponent(new ComponentName(getInstrumentation().getTargetCont ext(), |
| 30 ContentShellActivity.class)); | 43 ContentShellActivity.class)); |
| 31 return (ContentShellActivity) getInstrumentation().startActivitySync(int ent); | 44 setActivityIntent(intent); |
| 45 return getActivity(); | |
| 46 } | |
| 47 | |
| 48 /** | |
| 49 * Sets the {@link TestContentViewClient} associated with the specified | |
| 50 * {@link ContentView}. | |
| 51 * | |
| 52 * Must be called on the UI thread. | |
| 53 */ | |
| 54 public void setTestContentViewClient( | |
| 55 final ContentView contentView, final TestContentViewClient client) { | |
| 56 contentView.setContentViewClient(client); | |
| 57 mTestContentViewClients.put(contentView, client); | |
| 58 } | |
| 59 | |
| 60 /** | |
| 61 * Gets the {@link TestContentViewClient} associated with the specified | |
| 62 * {@link ContentView}. | |
| 63 * | |
| 64 * This will not work correctly if the test code invokes | |
| 65 * {link {@link ContentView#setContentViewClient(ContentViewClient)} directl y. | |
| 66 * | |
| 67 * @return {@link TestContentViewClient} | |
| 68 */ | |
| 69 public TestContentViewClient getTestContentViewClient(ContentView contentVie w) { | |
| 70 return mTestContentViewClients.get(contentView); | |
| 71 } | |
| 72 | |
| 73 /** | |
| 74 * Loads data on the UI thread and blocks until onPageFinished is called. | |
| 75 */ | |
| 76 @Deprecated | |
| 77 protected void loadDataSync(final ContentView chromeView, final String data, | |
| 78 final String mimeType, final boolean isBase64Enc oded) | |
| 79 throws Throwable { | |
| 80 TestContentViewClient.OnPageFinishedHelper onPageFinishedHelper = | |
| 81 getTestContentViewClient(chromeView).getOnPageFinishedHelper(); | |
| 82 int currentCallCount = onPageFinishedHelper.getCallCount(); | |
| 83 loadDataAsync(chromeView, data, mimeType, isBase64Encoded); | |
| 84 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_S ECONDS, | |
| 85 TimeUnit.SECONDS); | |
|
klundberg
2012/09/07 16:15:30
This should be in a utility class, not in a test b
Ramya
2012/09/19 21:31:57
As discussed, leaving it here till the Util files
| |
| 86 } | |
| 87 | |
| 88 /** | |
| 89 * Loads data on the UI thread but does not block. | |
| 90 */ | |
| 91 @Deprecated | |
| 92 protected void loadDataAsync(final ContentView chromeView, final String data , | |
| 93 final String mimeType, final boolean isBase64En coded) | |
| 94 throws Throwable { | |
|
klundberg
2012/09/07 16:15:30
Same comment as above.
Ramya
2012/09/19 21:31:57
See comment above.
| |
| 95 runTestOnUiThread(new Runnable() { | |
| 96 @Override | |
| 97 public void run() { | |
| 98 chromeView.loadUrl(LoadUrlParams.createLoadDataParams( | |
| 99 data, mimeType, isBase64Encoded)); | |
| 100 } | |
| 101 }); | |
| 102 } | |
| 103 | |
| 104 /** | |
| 105 * Allows access to the {@link ContentView}. | |
| 106 * | |
| 107 * @return The first {@link ContentView} | |
| 108 */ | |
| 109 public ContentView getContentView() { | |
| 110 return getActivity().getActiveContentView(); | |
| 32 } | 111 } |
| 33 } | 112 } |
| OLD | NEW |