1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/performance_manager/graph/frame_node_impl.h"
#include <vector>
#include "base/test/bind.h"
#include "base/test/run_until.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/preloading/scoped_prewarm_feature_list.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/performance_manager/graph/frame_node_impl.h"
#include "components/performance_manager/performance_manager_impl.h"
#include "components/performance_manager/public/features.h"
#include "components/performance_manager/public/graph/frame_node.h"
#include "components/performance_manager/public/graph/graph.h"
#include "components/performance_manager/public/viewport_intersection.h"
#include "content/public/test/back_forward_cache_util.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/prerender_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/rect_f.h"
namespace performance_manager {
using testing::AllOf;
using testing::Not;
using testing::UnorderedElementsAre;
MATCHER(IsMainFrame, "") {
return arg->IsMainFrame();
}
MATCHER_P(HasViewportIntersection, viewport_intersection, "") {
return arg->GetViewportIntersection() == viewport_intersection;
}
namespace {
// Returns true if the mojom::DocumentCoordinationUnit connection associated
// with `render_frame_host` is bound.
bool IsDocumentCoordinatorUnitBound(
content::RenderFrameHost* render_frame_host) {
base::WeakPtr<FrameNode> frame_node =
PerformanceManager::GetFrameNodeForRenderFrameHost(render_frame_host);
if (!frame_node) {
return false;
}
FrameNodeImpl* frame_node_impl = FrameNodeImpl::FromNode(frame_node.get());
return frame_node_impl->IsDocumentCoordinationUnitBoundForTesting();
}
class FrameNodeImplBrowserTest : public InProcessBrowserTest {
public:
~FrameNodeImplBrowserTest() override = default;
private:
// TODO(https://crbug.com/423465927): Explore a better approach to make the
// existing tests run with the prewarm feature enabled.
test::ScopedPrewarmFeatureList scoped_prewarm_feature_list_{
test::ScopedPrewarmFeatureList::PrewarmState::kDisabled};
};
class ParameterizedFrameNodeImplBrowserTest
: public FrameNodeImplBrowserTest,
public testing::WithParamInterface<bool> {
public:
ParameterizedFrameNodeImplBrowserTest() {
base::FieldTrialParams params = {
{features::kRenderedOutOfViewIsNotVisible.name,
GetParam() ? "true" : "false"}};
scoped_feature_list_.InitAndEnableFeatureWithParameters(
features::kPMProcessPriorityPolicy, params);
}
~ParameterizedFrameNodeImplBrowserTest() override = default;
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
} // namespace
IN_PROC_BROWSER_TEST_P(ParameterizedFrameNodeImplBrowserTest,
ViewportIntersection_OutOfView) {
ASSERT_TRUE(embedded_test_server()->Start());
EXPECT_EQ(1, browser()->tab_strip_model()->count());
const bool expects_intersects_viewport =
!performance_manager::features::kRenderedOutOfViewIsNotVisible.Get();
testing::Matcher<const FrameNode*> viewport_intersection_matcher =
expects_intersects_viewport
? HasViewportIntersection(ViewportIntersection::kIntersecting)
: HasViewportIntersection(ViewportIntersection::kNotIntersecting);
const GURL main_frame_url(
embedded_test_server()->GetURL("/iframe_out_of_view.html"));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), main_frame_url));
Graph* graph = PerformanceManager::GetGraph();
auto all_frame_nodes = graph->GetAllFrameNodes().AsVector();
EXPECT_THAT(
all_frame_nodes,
UnorderedElementsAre(
// One main frame, intersects with the viewport.
AllOf(IsMainFrame(),
HasViewportIntersection(ViewportIntersection::kIntersecting)),
// One child frame, intersects with the viewport depending on the
// value of the kRenderedOutOfViewIsNotVisible feature.
AllOf(Not(IsMainFrame()), viewport_intersection_matcher)));
}
INSTANTIATE_TEST_SUITE_P(,
ParameterizedFrameNodeImplBrowserTest,
testing::Bool());
IN_PROC_BROWSER_TEST_F(FrameNodeImplBrowserTest, ViewportIntersection_Hidden) {
ASSERT_TRUE(embedded_test_server()->Start());
EXPECT_EQ(1, browser()->tab_strip_model()->count());
const GURL main_frame_url(
embedded_test_server()->GetURL("/iframe_hidden.html"));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), main_frame_url));
Graph* graph = PerformanceManager::GetGraph();
auto all_frame_nodes = graph->GetAllFrameNodes().AsVector();
EXPECT_THAT(
all_frame_nodes,
UnorderedElementsAre(
// One main frame, intersects with the viewport.
AllOf(IsMainFrame(),
HasViewportIntersection(ViewportIntersection::kIntersecting)),
// One child frame, does not intersect with the viewport.
AllOf(Not(IsMainFrame()),
HasViewportIntersection(
ViewportIntersection::kNotIntersecting))));
}
IN_PROC_BROWSER_TEST_F(FrameNodeImplBrowserTest,
ViewportIntersection_PartiallyVisible) {
ASSERT_TRUE(embedded_test_server()->Start());
EXPECT_EQ(1, browser()->tab_strip_model()->count());
const GURL main_frame_url(
embedded_test_server()->GetURL("/iframe_partially_visible.html"));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), main_frame_url));
Graph* graph = PerformanceManager::GetGraph();
auto all_frame_nodes = graph->GetAllFrameNodes().AsVector();
EXPECT_THAT(
all_frame_nodes,
UnorderedElementsAre(
// One main frame, intersects with the viewport.
AllOf(IsMainFrame(),
HasViewportIntersection(ViewportIntersection::kIntersecting)),
// One child frame, also intersects with the viewport.
AllOf(Not(IsMainFrame()),
HasViewportIntersection(ViewportIntersection::kIntersecting))));
}
IN_PROC_BROWSER_TEST_F(FrameNodeImplBrowserTest, ViewportIntersection_Scaled) {
ASSERT_TRUE(embedded_test_server()->Start());
EXPECT_EQ(1, browser()->tab_strip_model()->count());
const GURL main_frame_url(
embedded_test_server()->GetURL("/iframe_scaled.html"));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), main_frame_url));
Graph* graph = PerformanceManager::GetGraph();
auto all_frame_nodes = graph->GetAllFrameNodes().AsVector();
EXPECT_THAT(
all_frame_nodes,
UnorderedElementsAre(
// One main frame, intersects with the viewport.
AllOf(IsMainFrame(),
HasViewportIntersection(ViewportIntersection::kIntersecting)),
// One child frame, also intersects with the viewport.
AllOf(Not(IsMainFrame()),
HasViewportIntersection(ViewportIntersection::kIntersecting))));
}
IN_PROC_BROWSER_TEST_F(FrameNodeImplBrowserTest, ViewportIntersection_Rotated) {
ASSERT_TRUE(embedded_test_server()->Start());
EXPECT_EQ(1, browser()->tab_strip_model()->count());
const GURL main_frame_url(
embedded_test_server()->GetURL("/iframe_rotated.html"));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), main_frame_url));
Graph* graph = PerformanceManager::GetGraph();
auto all_frame_nodes = graph->GetAllFrameNodes().AsVector();
EXPECT_THAT(
all_frame_nodes,
UnorderedElementsAre(
// One main frame, intersects with the viewport.
AllOf(IsMainFrame(),
HasViewportIntersection(ViewportIntersection::kIntersecting)),
// One child frame, also intersects with the viewport.
AllOf(Not(IsMainFrame()),
HasViewportIntersection(ViewportIntersection::kIntersecting))));
}
IN_PROC_BROWSER_TEST_F(FrameNodeImplBrowserTest, Bind_SimpleNavigation) {
ASSERT_TRUE(embedded_test_server()->Start());
EXPECT_EQ(1, browser()->tab_strip_model()->count());
const GURL kTestUrl = embedded_test_server()->GetURL("/title1.html");
content::RenderFrameHost* rfh =
ui_test_utils::NavigateToURL(browser(), kTestUrl);
ASSERT_TRUE(rfh);
EXPECT_EQ(rfh->GetLifecycleState(),
content::RenderFrameHost::LifecycleState::kActive);
ASSERT_TRUE(base::test::RunUntil(
[&]() { return IsDocumentCoordinatorUnitBound(rfh); }));
}
class FrameNodeImplBackForwardCacheBrowserTest
: public FrameNodeImplBrowserTest {
public:
FrameNodeImplBackForwardCacheBrowserTest() {
content::InitBackForwardCacheFeature(&scoped_feature_list_,
/*enable_back_forward_cache=*/true);
}
public:
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_F(FrameNodeImplBackForwardCacheBrowserTest,
Bind_BackForwardCache) {
ASSERT_TRUE(embedded_test_server()->Start());
EXPECT_EQ(1, browser()->tab_strip_model()->count());
const GURL kTestUrl = embedded_test_server()->GetURL("/title1.html");
const GURL kOtherUrl = embedded_test_server()->GetURL("/title2.html");
// Navigation to the test URL.
content::RenderFrameHost* rfh =
ui_test_utils::NavigateToURL(browser(), kTestUrl);
ASSERT_TRUE(rfh);
EXPECT_EQ(rfh->GetLifecycleState(),
content::RenderFrameHost::LifecycleState::kActive);
// Navigate to some other URL.
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), kOtherUrl));
EXPECT_EQ(rfh->GetLifecycleState(),
content::RenderFrameHost::LifecycleState::kInBackForwardCache);
// Back to the test URL.
EXPECT_TRUE(content::HistoryGoBack(
browser()->tab_strip_model()->GetActiveWebContents()));
EXPECT_EQ(rfh->GetLifecycleState(),
content::RenderFrameHost::LifecycleState::kActive);
ASSERT_TRUE(base::test::RunUntil(
[&]() { return IsDocumentCoordinatorUnitBound(rfh); }));
}
class FrameNodeImplPrerenderBrowserTest : public FrameNodeImplBrowserTest {
public:
FrameNodeImplPrerenderBrowserTest()
: prerender_test_helper_(base::BindRepeating(
&FrameNodeImplPrerenderBrowserTest::GetWebContents,
base::Unretained(this))) {}
content::WebContents* GetWebContents() const {
return browser()->tab_strip_model()->GetActiveWebContents();
}
public:
content::test::ScopedPrerenderFeatureList scoped_prerender_feature_list_;
content::test::PrerenderTestHelper prerender_test_helper_;
};
IN_PROC_BROWSER_TEST_F(FrameNodeImplPrerenderBrowserTest,
Bind_PrerenderNavigation) {
ASSERT_TRUE(embedded_test_server()->Start());
EXPECT_EQ(1, browser()->tab_strip_model()->count());
const GURL kInitialUrl = embedded_test_server()->GetURL("/empty.html");
const GURL kPrerenderUrl = embedded_test_server()->GetURL("/title1.html");
// Initial navigation. Needed so we can add prerendered frames.
content::RenderFrameHost* rfh =
ui_test_utils::NavigateToURL(browser(), kInitialUrl);
ASSERT_TRUE(rfh);
EXPECT_EQ(rfh->GetLifecycleState(),
content::RenderFrameHost::LifecycleState::kActive);
// Create the prerendered frame.
content::PrerenderHostId host_id =
prerender_test_helper_.AddPrerender(kPrerenderUrl);
content::RenderFrameHost* prerender_rfh =
prerender_test_helper_.GetPrerenderedMainFrameHost(host_id);
ASSERT_TRUE(prerender_rfh);
EXPECT_EQ(prerender_rfh->GetLifecycleState(),
content::RenderFrameHost::LifecycleState::kPrerendering);
// Navigate to the prerendered frame.
prerender_test_helper_.NavigatePrimaryPage(kPrerenderUrl);
EXPECT_EQ(prerender_rfh->GetLifecycleState(),
content::RenderFrameHost::LifecycleState::kActive);
ASSERT_TRUE(base::test::RunUntil(
[&]() { return IsDocumentCoordinatorUnitBound(prerender_rfh); }));
}
} // namespace performance_manager
|