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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/picture_in_picture/picture_in_picture_bounds_cache.h"
#include <memory>
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "content/public/browser/web_contents.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/display/display.h"
namespace {
class PictureInPictureBoundsCacheTest : public ChromeRenderViewHostTestHarness {
public:
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
SetContents(CreateTestWebContents());
NavigateAndCommit(GURL("https://www.pictureinpicture.com"));
}
void TearDown() override {
DeleteContents();
ChromeRenderViewHostTestHarness::TearDown();
}
display::Display& display() { return display_; }
private:
std::unique_ptr<content::WebContents> child_web_contents_;
display::Display display_{1};
};
} // namespace
TEST_F(PictureInPictureBoundsCacheTest, HasNoInitialGuess) {
const gfx::Size requested_size(320, 240);
auto initial_bounds = PictureInPictureBoundsCache::GetBoundsForNewWindow(
web_contents(), display(), requested_size);
EXPECT_FALSE(initial_bounds);
}
TEST_F(PictureInPictureBoundsCacheTest,
RemembersMostRecentBoundsForEmptyInitialSize) {
PictureInPictureBoundsCache::GetBoundsForNewWindow(web_contents(), display(),
{});
const gfx::Rect bounds_1(10, 20, 30, 40);
PictureInPictureBoundsCache::UpdateCachedBounds(web_contents(), bounds_1);
const gfx::Rect bounds_2(50, 60, 70, 80);
PictureInPictureBoundsCache::UpdateCachedBounds(web_contents(), bounds_2);
// This also verifies that an empty request size matches with a cache that has
// an empty request size.
auto initial_bounds = PictureInPictureBoundsCache::GetBoundsForNewWindow(
web_contents(), display(), {});
EXPECT_TRUE(initial_bounds);
EXPECT_EQ(*initial_bounds, bounds_2);
}
TEST_F(PictureInPictureBoundsCacheTest,
RemembersMostRecentBoundsForNonEmptyInitialSize) {
const gfx::Size requested_size(320, 240);
PictureInPictureBoundsCache::GetBoundsForNewWindow(web_contents(), display(),
requested_size);
const gfx::Rect bounds_1(10, 20, 30, 40);
PictureInPictureBoundsCache::UpdateCachedBounds(web_contents(), bounds_1);
const gfx::Rect bounds_2(50, 60, 70, 80);
PictureInPictureBoundsCache::UpdateCachedBounds(web_contents(), bounds_2);
auto initial_bounds = PictureInPictureBoundsCache::GetBoundsForNewWindow(
web_contents(), display(), requested_size);
EXPECT_TRUE(initial_bounds);
EXPECT_EQ(*initial_bounds, bounds_2);
}
TEST_F(PictureInPictureBoundsCacheTest, DisplayMismatchDoesNotProvideBounds) {
// If the display id changes, then the cache should miss.
const gfx::Size requested_size(320, 240);
PictureInPictureBoundsCache::GetBoundsForNewWindow(web_contents(), display(),
requested_size);
const gfx::Rect bounds(10, 20, 30, 40);
PictureInPictureBoundsCache::UpdateCachedBounds(web_contents(), bounds);
display::Display display_2(2);
auto initial_bounds = PictureInPictureBoundsCache::GetBoundsForNewWindow(
web_contents(), display_2, requested_size);
EXPECT_FALSE(initial_bounds);
}
TEST_F(PictureInPictureBoundsCacheTest, SizeMismatchDoesNotProvideBounds) {
// If the request specifies an exact size, then it must match the cached
// requested size.
const gfx::Size requested_size_1(320, 240);
PictureInPictureBoundsCache::GetBoundsForNewWindow(web_contents(), display(),
requested_size_1);
const gfx::Rect bounds(10, 20, 30, 40);
PictureInPictureBoundsCache::UpdateCachedBounds(web_contents(), bounds);
const gfx::Size requested_size_2(100, 100);
ASSERT_NE(requested_size_1, requested_size_2);
auto initial_bounds = PictureInPictureBoundsCache::GetBoundsForNewWindow(
web_contents(), display(), requested_size_2);
EXPECT_FALSE(initial_bounds);
}
TEST_F(PictureInPictureBoundsCacheTest,
EmptyRequestSizeMatchesWithNonEmptyCacheSize) {
// If the request does not specify a size, then it should match with any
// request size stored in the cache.
PictureInPictureBoundsCache::GetBoundsForNewWindow(web_contents(), display(),
gfx::Size(320, 240));
const gfx::Rect bounds(10, 20, 30, 40);
PictureInPictureBoundsCache::UpdateCachedBounds(web_contents(), bounds);
auto initial_bounds = PictureInPictureBoundsCache::GetBoundsForNewWindow(
web_contents(), display(), {});
EXPECT_TRUE(initial_bounds);
EXPECT_EQ(*initial_bounds, bounds);
}
TEST_F(PictureInPictureBoundsCacheTest,
NonEmptyRequestSizeMismatchesWithEmptyCacheSize) {
// A non-empty request size will not match with a cache for an empty size.
PictureInPictureBoundsCache::GetBoundsForNewWindow(web_contents(), display(),
{});
PictureInPictureBoundsCache::UpdateCachedBounds(web_contents(),
gfx::Rect(10, 20, 30, 40));
auto initial_bounds = PictureInPictureBoundsCache::GetBoundsForNewWindow(
web_contents(), display(), gfx::Size(10, 20));
EXPECT_FALSE(initial_bounds);
}
TEST_F(PictureInPictureBoundsCacheTest,
DifferentOriginsMismatchAndOnlyOneIsCached) {
GURL gurl("https://www.pictureinpicture.com");
NavigateAndCommit(gurl);
PictureInPictureBoundsCache::GetBoundsForNewWindow(web_contents(), display(),
{});
const gfx::Rect bounds(10, 20, 30, 40);
PictureInPictureBoundsCache::UpdateCachedBounds(web_contents(), bounds);
NavigateAndCommit(GURL("https://www.example.com"));
auto initial_bounds = PictureInPictureBoundsCache::GetBoundsForNewWindow(
web_contents(), display(), {});
EXPECT_FALSE(initial_bounds);
// Navigating back should also miss, because example.com opened a new pip
// window. The cache now reflects example.com .
NavigateAndCommit(gurl);
initial_bounds = PictureInPictureBoundsCache::GetBoundsForNewWindow(
web_contents(), display(), {});
EXPECT_FALSE(initial_bounds);
}
TEST_F(PictureInPictureBoundsCacheTest, NavigatingThereAndBackStillMatches) {
GURL gurl("https://www.pictureinpicture.com");
NavigateAndCommit(gurl);
PictureInPictureBoundsCache::GetBoundsForNewWindow(web_contents(), display(),
{});
const gfx::Rect bounds(10, 20, 30, 40);
PictureInPictureBoundsCache::UpdateCachedBounds(web_contents(), bounds);
// Don't open a new pip window this time.
NavigateAndCommit(GURL("https://www.example.com"));
// Navigating back should not miss, because nobody opened a pip window after
// we navigated away.
NavigateAndCommit(gurl);
auto initial_bounds = PictureInPictureBoundsCache::GetBoundsForNewWindow(
web_contents(), display(), {});
EXPECT_TRUE(initial_bounds);
EXPECT_EQ(*initial_bounds, bounds);
}
TEST_F(PictureInPictureBoundsCacheTest, SameOriginsDifferentPathsMatch) {
NavigateAndCommit(GURL("https://www.pictureinpicture.com/look_sword.html"));
PictureInPictureBoundsCache::GetBoundsForNewWindow(web_contents(), display(),
{});
const gfx::Rect bounds(10, 20, 30, 40);
PictureInPictureBoundsCache::UpdateCachedBounds(web_contents(), bounds);
NavigateAndCommit(GURL(
"https://www.pictureinpicture.com/elvish_sword_of_great_antiquity.html"));
auto initial_bounds = PictureInPictureBoundsCache::GetBoundsForNewWindow(
web_contents(), display(), {});
EXPECT_TRUE(initial_bounds);
EXPECT_EQ(*initial_bounds, bounds);
}
TEST_F(PictureInPictureBoundsCacheTest, ClearsCachedBounds) {
const gfx::Size requested_size(320, 240);
PictureInPictureBoundsCache::GetBoundsForNewWindow(web_contents(), display(),
requested_size);
const gfx::Rect bounds(10, 20, 30, 40);
PictureInPictureBoundsCache::UpdateCachedBounds(web_contents(), bounds);
// Verify initial bounds are set.
auto initial_bounds = PictureInPictureBoundsCache::GetBoundsForNewWindow(
web_contents(), display(), requested_size);
EXPECT_TRUE(initial_bounds);
EXPECT_EQ(*initial_bounds, bounds);
// Verify cached bounds are cleared.
PictureInPictureBoundsCache::ClearCachedBounds(web_contents());
initial_bounds = PictureInPictureBoundsCache::GetBoundsForNewWindow(
web_contents(), display(), requested_size);
EXPECT_FALSE(initial_bounds);
}
|