2015-04-24 15:48:32 +02:00
|
|
|
// Copyright (c) 2015 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
#include "include/base/cef_callback.h"
|
2015-04-24 15:48:32 +02:00
|
|
|
#include "include/wrapper/cef_closure_task.h"
|
2016-11-18 18:31:21 +01:00
|
|
|
#include "tests/ceftests/test_handler.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/gtest/include/gtest/gtest.h"
|
2015-04-24 15:48:32 +02:00
|
|
|
|
2022-08-11 22:17:33 +02:00
|
|
|
// Set to 1 to enable verbose debugging info logging.
|
|
|
|
#define VERBOSE_DEBUGGING 0
|
|
|
|
|
2015-04-24 15:48:32 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char kTestHTMLWithRegions[] =
|
|
|
|
"<html>"
|
|
|
|
" <body>"
|
|
|
|
" <div style=\"position: absolute; top: 50px; left: 50px; width: 200px; "
|
|
|
|
"height: 200px; background-color: red; -webkit-app-region: drag;\">"
|
|
|
|
" <div style=\"position: absolute; top: 50%; left: 50%; "
|
|
|
|
"transform: translate(-50%, -50%); width: 50px; height: 50px; "
|
|
|
|
"background-color: blue; -webkit-app-region: no-drag;\">"
|
|
|
|
" </div>"
|
|
|
|
" </div>"
|
|
|
|
" </body>"
|
|
|
|
"</html>";
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
const char kTestHTMLWithoutRegions[] = "<html><body>Hello World!</body></html>";
|
2015-04-24 15:48:32 +02:00
|
|
|
|
|
|
|
const char kTestHTMLWithChangingRegions[] =
|
|
|
|
"<html>"
|
|
|
|
" <body>"
|
|
|
|
" <div id=\"layer\" style=\"position: absolute; top: 50px; left: 50px; "
|
|
|
|
"width: 200px; height: 200px; background-color: red; "
|
|
|
|
"-webkit-app-region: drag;\">"
|
|
|
|
" <div style=\"position: absolute; top: 50%; left: 50%; "
|
|
|
|
"transform: translate(-50%, -50%); width: 50px; height: 50px; "
|
|
|
|
"background-color: blue; -webkit-app-region: no-drag;\">"
|
|
|
|
" </div>"
|
|
|
|
" </div>"
|
|
|
|
" <script>"
|
|
|
|
" window.setTimeout(function() {"
|
|
|
|
" var layer = document.getElementById('layer');"
|
|
|
|
" layer.style.top = '0px';"
|
|
|
|
" layer.style.left = '0px';"
|
|
|
|
" }, 500);"
|
|
|
|
" </script>"
|
|
|
|
" </body>"
|
|
|
|
"</html>";
|
|
|
|
|
2021-09-15 13:40:08 +02:00
|
|
|
class DraggableRegionsTestHandler : public TestHandler,
|
|
|
|
public CefDragHandler,
|
|
|
|
public CefFrameHandler {
|
2015-04-24 15:48:32 +02:00
|
|
|
public:
|
2021-09-15 13:40:08 +02:00
|
|
|
// Test steps executed in order.
|
|
|
|
enum Step {
|
|
|
|
// Nav 1: Two regions (get notification).
|
|
|
|
kStepWithRegions = 1,
|
|
|
|
// Nav 2: Starts with the same region as Nav 1 (no notification),
|
|
|
|
// then a changed region (get notification).
|
|
|
|
kStepWithChangingRegions,
|
|
|
|
// Nav 3: No regions (get notification).
|
|
|
|
kStepWithoutRegions,
|
|
|
|
// GoBack: Two regions (get notification), then a changed region (get
|
|
|
|
// notification). Note the first notification is not sent if
|
|
|
|
// BackForwardCache is enabled.
|
|
|
|
kStepWithChangingRegions2,
|
|
|
|
kStepWithChangingRegions3,
|
|
|
|
// GoForward: No regions (get notification).
|
|
|
|
kStepWithoutRegions2,
|
|
|
|
|
|
|
|
kStepMax = kStepWithoutRegions2,
|
|
|
|
};
|
|
|
|
|
|
|
|
explicit DraggableRegionsTestHandler(bool same_origin)
|
|
|
|
: same_origin_(same_origin) {}
|
2015-04-24 15:48:32 +02:00
|
|
|
|
|
|
|
void RunTest() override {
|
|
|
|
// Add HTML documents with and without draggable regions.
|
2021-09-15 13:40:08 +02:00
|
|
|
AddResource(GetURL(kStepWithRegions), kTestHTMLWithRegions, "text/html");
|
|
|
|
AddResource(GetURL(kStepWithChangingRegions), kTestHTMLWithChangingRegions,
|
|
|
|
"text/html");
|
|
|
|
AddResource(GetURL(kStepWithoutRegions), kTestHTMLWithoutRegions,
|
2015-04-24 15:48:32 +02:00
|
|
|
"text/html");
|
|
|
|
|
|
|
|
// Create the browser
|
2021-09-15 13:40:08 +02:00
|
|
|
CreateBrowser(GetURL(kStepWithRegions));
|
2015-04-24 15:48:32 +02:00
|
|
|
|
|
|
|
// Time out the test after a reasonable period of time.
|
|
|
|
SetTestTimeout();
|
|
|
|
}
|
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
CefRefPtr<CefDragHandler> GetDragHandler() override { return this; }
|
2021-09-15 13:40:08 +02:00
|
|
|
CefRefPtr<CefFrameHandler> GetFrameHandler() override { return this; }
|
2019-05-24 22:23:43 +02:00
|
|
|
|
2015-04-24 15:48:32 +02:00
|
|
|
void OnDraggableRegionsChanged(
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
2019-05-24 22:23:43 +02:00
|
|
|
CefRefPtr<CefFrame> frame,
|
2015-04-24 15:48:32 +02:00
|
|
|
const std::vector<CefDraggableRegion>& regions) override {
|
2021-09-15 13:40:08 +02:00
|
|
|
EXPECT_UI_THREAD();
|
2015-04-24 15:48:32 +02:00
|
|
|
EXPECT_TRUE(browser->IsSame(GetBrowser()));
|
2019-05-24 22:23:43 +02:00
|
|
|
EXPECT_TRUE(frame->IsMain());
|
2015-04-24 15:48:32 +02:00
|
|
|
|
2021-09-15 13:40:08 +02:00
|
|
|
draggable_regions_changed_ct_++;
|
2015-04-24 15:48:32 +02:00
|
|
|
|
2022-08-11 22:17:33 +02:00
|
|
|
#if VERBOSE_DEBUGGING
|
|
|
|
LOG(INFO) << "step " << step_ << " regions.size " << regions.size()
|
|
|
|
<< " url " << frame->GetURL().ToString();
|
|
|
|
if (regions.size() == 2) {
|
|
|
|
LOG(INFO) << " region[0] x " << regions[0].bounds.x << " y "
|
|
|
|
<< regions[0].bounds.y << " width " << regions[0].bounds.width
|
|
|
|
<< " height " << regions[0].bounds.height;
|
|
|
|
LOG(INFO) << " region[1] x " << regions[1].bounds.x << " y "
|
|
|
|
<< regions[1].bounds.y << " width " << regions[1].bounds.width
|
|
|
|
<< " height " << regions[1].bounds.height;
|
|
|
|
}
|
|
|
|
#endif // VERBOSE_DEBUGGING
|
|
|
|
|
2015-04-24 15:48:32 +02:00
|
|
|
switch (step_) {
|
|
|
|
case kStepWithRegions:
|
2021-09-15 13:40:08 +02:00
|
|
|
case kStepWithChangingRegions2:
|
2022-06-17 15:28:55 +02:00
|
|
|
EXPECT_EQ(2U, regions.size()) << step_;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (regions.size() != 2U) {
|
2022-06-17 15:28:55 +02:00
|
|
|
break;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-07-28 00:57:07 +02:00
|
|
|
EXPECT_NEAR(50, regions[0].bounds.x, 1);
|
|
|
|
EXPECT_NEAR(50, regions[0].bounds.y, 1);
|
|
|
|
EXPECT_NEAR(200, regions[0].bounds.width, 1);
|
|
|
|
EXPECT_NEAR(200, regions[0].bounds.height, 1);
|
2015-04-24 15:48:32 +02:00
|
|
|
EXPECT_EQ(1, regions[0].draggable);
|
2021-07-28 00:57:07 +02:00
|
|
|
EXPECT_NEAR(125, regions[1].bounds.x, 1);
|
|
|
|
EXPECT_NEAR(125, regions[1].bounds.y, 1);
|
|
|
|
EXPECT_NEAR(50, regions[1].bounds.width, 1);
|
|
|
|
EXPECT_NEAR(50, regions[1].bounds.height, 1);
|
2015-04-24 15:48:32 +02:00
|
|
|
EXPECT_EQ(0, regions[1].draggable);
|
|
|
|
break;
|
2021-09-15 13:40:08 +02:00
|
|
|
case kStepWithChangingRegions:
|
|
|
|
case kStepWithChangingRegions3:
|
2022-06-17 15:28:55 +02:00
|
|
|
EXPECT_EQ(2U, regions.size()) << step_;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (regions.size() != 2U) {
|
2022-06-17 15:28:55 +02:00
|
|
|
break;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-04-24 15:48:32 +02:00
|
|
|
EXPECT_EQ(0, regions[0].bounds.x);
|
|
|
|
EXPECT_EQ(0, regions[0].bounds.y);
|
2021-07-28 00:57:07 +02:00
|
|
|
EXPECT_NEAR(200, regions[0].bounds.width, 1);
|
|
|
|
EXPECT_NEAR(200, regions[0].bounds.height, 1);
|
2015-04-24 15:48:32 +02:00
|
|
|
EXPECT_EQ(1, regions[0].draggable);
|
2021-07-28 00:57:07 +02:00
|
|
|
EXPECT_NEAR(75, regions[1].bounds.x, 1);
|
|
|
|
EXPECT_NEAR(75, regions[1].bounds.y, 1);
|
|
|
|
EXPECT_NEAR(50, regions[1].bounds.width, 2);
|
|
|
|
EXPECT_NEAR(50, regions[1].bounds.height, 2);
|
2015-04-24 15:48:32 +02:00
|
|
|
EXPECT_EQ(0, regions[1].draggable);
|
|
|
|
break;
|
|
|
|
case kStepWithoutRegions:
|
2021-09-15 13:40:08 +02:00
|
|
|
case kStepWithoutRegions2:
|
2022-06-17 15:28:55 +02:00
|
|
|
EXPECT_TRUE(regions.empty()) << step_;
|
2015-04-24 15:48:32 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
NextTest(browser);
|
|
|
|
}
|
|
|
|
|
2021-09-15 13:40:08 +02:00
|
|
|
void OnFrameAttached(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
bool reattached) override {
|
|
|
|
EXPECT_UI_THREAD();
|
|
|
|
EXPECT_TRUE(browser->IsSame(GetBrowser()));
|
|
|
|
EXPECT_TRUE(frame->IsMain());
|
|
|
|
|
|
|
|
if (reattached) {
|
|
|
|
// When BackForwardCache is enabled and we go back to
|
|
|
|
// kTestHTMLWithChangingRegions, draggable regions will already be in the
|
|
|
|
// final position because the page content is not reloaded.
|
|
|
|
if (step_ == kStepWithChangingRegions2) {
|
|
|
|
step_ = kStepWithChangingRegions3;
|
|
|
|
expected_draggable_regions_changed_ct_--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 15:48:32 +02:00
|
|
|
void DestroyTest() override {
|
2021-09-15 13:40:08 +02:00
|
|
|
EXPECT_EQ(expected_draggable_regions_changed_ct_,
|
|
|
|
draggable_regions_changed_ct_);
|
2015-04-24 15:48:32 +02:00
|
|
|
|
|
|
|
TestHandler::DestroyTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void NextTest(CefRefPtr<CefBrowser> browser) {
|
|
|
|
CefRefPtr<CefFrame> frame(browser->GetMainFrame());
|
|
|
|
|
|
|
|
switch (step_) {
|
|
|
|
case kStepWithRegions:
|
2021-09-15 13:40:08 +02:00
|
|
|
step_ = kStepWithChangingRegions;
|
|
|
|
frame->LoadURL(GetURL(kStepWithChangingRegions));
|
2015-04-24 15:48:32 +02:00
|
|
|
break;
|
2021-09-15 13:40:08 +02:00
|
|
|
case kStepWithChangingRegions:
|
2015-04-24 15:48:32 +02:00
|
|
|
step_ = kStepWithoutRegions;
|
2021-09-15 13:40:08 +02:00
|
|
|
frame->LoadURL(GetURL(kStepWithoutRegions));
|
2015-04-24 15:48:32 +02:00
|
|
|
break;
|
|
|
|
case kStepWithoutRegions: {
|
2021-09-15 13:40:08 +02:00
|
|
|
step_ = kStepWithChangingRegions2;
|
|
|
|
browser->GoBack();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kStepWithChangingRegions2: {
|
|
|
|
step_ = kStepWithChangingRegions3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kStepWithChangingRegions3: {
|
|
|
|
step_ = kStepWithoutRegions2;
|
|
|
|
browser->GoForward();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kStepWithoutRegions2: {
|
|
|
|
DestroyTest();
|
2015-04-24 15:48:32 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 13:40:08 +02:00
|
|
|
std::string GetURL(Step step) const {
|
|
|
|
// When |same_origin_| is true every other URL gets a different origin.
|
|
|
|
switch (step) {
|
|
|
|
case kStepWithRegions:
|
2023-04-29 00:17:03 +02:00
|
|
|
return same_origin_ ? "https://test.com/regions"
|
|
|
|
: "https://test2.com/regions";
|
2021-09-15 13:40:08 +02:00
|
|
|
case kStepWithChangingRegions:
|
|
|
|
case kStepWithChangingRegions2:
|
|
|
|
case kStepWithChangingRegions3:
|
2023-04-29 00:17:03 +02:00
|
|
|
return "https://test.com/changing-regions";
|
2021-09-15 13:40:08 +02:00
|
|
|
case kStepWithoutRegions:
|
|
|
|
case kStepWithoutRegions2:
|
2023-04-29 00:17:03 +02:00
|
|
|
return same_origin_ ? "https://test.com/no-regions"
|
|
|
|
: "https://test2.com/no-regions";
|
2021-09-15 13:40:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NOTREACHED();
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool same_origin_;
|
2015-04-24 15:48:32 +02:00
|
|
|
|
2021-09-15 13:40:08 +02:00
|
|
|
Step step_ = kStepWithRegions;
|
|
|
|
int draggable_regions_changed_ct_ = 0;
|
|
|
|
int expected_draggable_regions_changed_ct_ = kStepMax;
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(DraggableRegionsTestHandler);
|
2015-04-24 15:48:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2021-09-15 13:40:08 +02:00
|
|
|
// Verify that draggable regions work in the same origin.
|
|
|
|
TEST(DraggableRegionsTest, DraggableRegionsSameOrigin) {
|
|
|
|
CefRefPtr<DraggableRegionsTestHandler> handler =
|
|
|
|
new DraggableRegionsTestHandler(/*same_origin=*/true);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
ReleaseAndWaitForDestructor(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that draggable regions work cross-origin.
|
|
|
|
TEST(DraggableRegionsTest, DraggableRegionsCrossOrigin) {
|
2015-04-24 15:48:32 +02:00
|
|
|
CefRefPtr<DraggableRegionsTestHandler> handler =
|
2021-09-15 13:40:08 +02:00
|
|
|
new DraggableRegionsTestHandler(/*same_origin=*/false);
|
2015-04-24 15:48:32 +02:00
|
|
|
handler->ExecuteTest();
|
|
|
|
ReleaseAndWaitForDestructor(handler);
|
|
|
|
}
|