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

Unified Diff: chrome/test/android/javatests/src/org/chromium/chrome/test/util/browser/TabTitleObserver.java

Issue 985863003: Upstream TabTitleObserver (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add observer only when not notified already Created 5 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/test/android/javatests/src/org/chromium/chrome/test/util/browser/TabTitleObserver.java
diff --git a/chrome/test/android/javatests/src/org/chromium/chrome/test/util/browser/TabTitleObserver.java b/chrome/test/android/javatests/src/org/chromium/chrome/test/util/browser/TabTitleObserver.java
new file mode 100644
index 0000000000000000000000000000000000000000..386a2c1b2fff50d234319d21eef9cc19408839f2
--- /dev/null
+++ b/chrome/test/android/javatests/src/org/chromium/chrome/test/util/browser/TabTitleObserver.java
@@ -0,0 +1,65 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+package org.chromium.chrome.test.util.browser;
+
+import android.text.TextUtils;
+
+import org.chromium.base.ThreadUtils;
+import org.chromium.chrome.browser.EmptyTabObserver;
+import org.chromium.chrome.browser.Tab;
+import org.chromium.content.browser.test.util.CallbackHelper;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * A utility class to get notified of title change in a tab.
+ */
+public class TabTitleObserver extends EmptyTabObserver {
+ private final String mExpectedTitle;
+ private final CallbackHelper mCallback;
+
+ /**
+ * A constructor.
+ *
+ * @param tab The tab to be observed.
+ * @param expectedTitle The expected title to wait for.
+ */
+ public TabTitleObserver(final Tab tab, final String expectedTitle) {
+ mExpectedTitle = expectedTitle;
+ mCallback = new CallbackHelper();
+ ThreadUtils.runOnUiThreadBlocking(new Runnable() {
+ @Override
+ public void run() {
+ if (!notifyCallbackIfTitleMatches(tab)) {
+ tab.addObserver(TabTitleObserver.this);
+ }
+ }
+ });
+ }
+
+ /**
+ * Wait for title update up to the given number of seconds.
+ *
+ * @param seconds The number of seconds to wait.
+ * @throws InterruptedException
+ * @throws TimeoutException
+ */
+ public void waitForTitleUpdate(int seconds) throws InterruptedException, TimeoutException {
+ mCallback.waitForCallback(0, 1, seconds, TimeUnit.SECONDS);
+ }
+
+ private boolean notifyCallbackIfTitleMatches(Tab tab) {
+ if (TextUtils.equals(tab.getTitle(), mExpectedTitle)) {
+ mCallback.notifyCalled();
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public void onTitleUpdated(Tab tab) {
+ notifyCallbackIfTitleMatches(tab);
+ }
+}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698