TestWebContentsObserver.java revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 3// found in the LICENSE file. 4 5package org.chromium.content.browser.test.util; 6 7import org.chromium.content.browser.ContentViewCore; 8import org.chromium.content.browser.WebContentsObserverAndroid; 9import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageFinishedHelper; 10import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageStartedHelper; 11import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnReceivedErrorHelper; 12 13/** 14 * The default WebContentsObserverAndroid used by ContentView tests. The below callbacks can be 15 * accessed by using {@link TestCallbackHelperContainer} or extending this class. 16 */ 17public class TestWebContentsObserver extends WebContentsObserverAndroid { 18 19 private OnPageStartedHelper mOnPageStartedHelper; 20 private OnPageFinishedHelper mOnPageFinishedHelper; 21 private OnReceivedErrorHelper mOnReceivedErrorHelper; 22 23 public TestWebContentsObserver(ContentViewCore contentViewCore) { 24 super(contentViewCore); 25 mOnPageStartedHelper = new OnPageStartedHelper(); 26 mOnPageFinishedHelper = new OnPageFinishedHelper(); 27 mOnReceivedErrorHelper = new OnReceivedErrorHelper(); 28 } 29 30 public OnPageStartedHelper getOnPageStartedHelper() { 31 return mOnPageStartedHelper; 32 } 33 34 public OnPageFinishedHelper getOnPageFinishedHelper() { 35 return mOnPageFinishedHelper; 36 } 37 38 public OnReceivedErrorHelper getOnReceivedErrorHelper() { 39 return mOnReceivedErrorHelper; 40 } 41 42 /** 43 * ATTENTION!: When overriding the following methods, be sure to call 44 * the corresponding methods in the super class. Otherwise 45 * {@link CallbackHelper#waitForCallback()} methods will 46 * stop working! 47 */ 48 @Override 49 public void didStartLoading(String url) { 50 super.didStartLoading(url); 51 mOnPageStartedHelper.notifyCalled(url); 52 } 53 54 @Override 55 public void didStopLoading(String url) { 56 super.didStopLoading(url); 57 mOnPageFinishedHelper.notifyCalled(url); 58 } 59 60 @Override 61 public void didFailLoad(boolean isProvisionalLoad, boolean isMainFrame, 62 int errorCode, String description, String failingUrl) { 63 super.didFailLoad(isProvisionalLoad, isMainFrame, errorCode, description, failingUrl); 64 mOnReceivedErrorHelper.notifyCalled(errorCode, description, failingUrl); 65 } 66} 67