blob: a7bf7b3d1f27bdf1e2b2eff9dff59dcc0e3d6b20 [file] [log] [blame]
// Copyright 2020 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.browser.layouts;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.chrome.browser.layouts.LayoutStateProvider.LayoutStateObserver;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import java.util.concurrent.TimeoutException;
/** Utilities to aid in testing that involves switching between layouts. */
public class LayoutTestUtils {
/**
* Wait for a specified layout to be shown. If the layout is already showing, this returns
* immediately.
* @param layoutManager The {@link LayoutManager} showing the layout.
* @param type The type of layout to wait for.
* @throws TimeoutException
*/
public static void waitForLayout(LayoutManager layoutManager, @LayoutType int type)
throws TimeoutException {
CallbackHelper finishedShowingCallbackHelper = new CallbackHelper();
LayoutStateObserver observer = new LayoutStateObserver() {
@Override
public void onFinishedShowing(int layoutType) {
finishedShowingCallbackHelper.notifyCalled();
}
};
TestThreadUtils.runOnUiThreadBlocking(() -> {
if (layoutManager.isLayoutVisible(type)) {
finishedShowingCallbackHelper.notifyCalled();
return;
}
layoutManager.addObserver(observer);
});
finishedShowingCallbackHelper.waitForFirst();
TestThreadUtils.runOnUiThreadBlocking(() -> layoutManager.removeObserver(observer));
}
}