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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
// Copyright 2022 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/test/interaction/interactive_browser_test_internal.h"
#include <compare>
#include <functional>
#include <memory>
#include <sstream>
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/scoped_observation.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/test/base/devtools_agent_coverage_observer.h"
#include "chrome/test/base/test_switches.h"
#include "chrome/test/interaction/interaction_test_util_browser.h"
#include "chrome/test/interaction/tracked_element_webcontents.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/interaction/element_tracker.h"
#include "ui/base/interaction/framework_specific_implementation.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/views/interaction/interactive_views_test_internal.h"
#include "ui/views/interaction/widget_focus_observer.h"
#include "ui/views/widget/widget.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "ash/shell.h"
#include "ui/aura/window.h"
#endif
namespace internal {
// Focus supplier that watches for browser activation specifically. For some
// reason, on some platforms, in some circumstances, native widget activation
// isn't properly communicated, so this serves as a backup.
class BrowserWidgetFocusSupplier
: public views::test::internal::WidgetFocusSupplier,
public BrowserListObserver,
public views::WidgetObserver {
public:
BrowserWidgetFocusSupplier() {
for (Browser* browser : *BrowserList::GetInstance()) {
ObserveBrowserActivationChange(browser);
}
observation_.Observe(BrowserList::GetInstance());
}
~BrowserWidgetFocusSupplier() override {
for (Browser* browser : *BrowserList::GetInstance()) {
OnBrowserRemoved(browser);
}
}
DECLARE_FRAMEWORK_SPECIFIC_METADATA()
void OnBrowserAdded(Browser* browser) override {
ObserveBrowserActivationChange(browser);
}
void OnBrowserRemoved(Browser* browser) override {
if (auto* const view = BrowserView::GetBrowserViewForBrowser(browser)) {
if (auto* const widget = view->GetWidget()) {
widget->RemoveObserver(this);
}
}
}
void OnWidgetActivationChanged(views::Widget* widget, bool active) override {
if (active) {
if (gfx::NativeView native_view = widget->GetNativeView()) {
OnWidgetFocusChanged(native_view);
}
}
}
protected:
views::Widget::Widgets GetAllWidgets() const override {
#if BUILDFLAG(IS_CHROMEOS)
// On Ash, this call is required to include shell/desktop widgets in
// addition to other widgets - see documentation in widget_test_aura.cc.
views::Widget::Widgets result;
for (const auto& window : ash::Shell::GetAllRootWindows()) {
result.merge(views::Widget::GetAllChildWidgets(window->GetRootWindow()));
}
return result;
#else
return views::Widget::Widgets();
#endif
}
private:
void ObserveBrowserActivationChange(Browser* browser) {
if (auto* const view = BrowserView::GetBrowserViewForBrowser(browser)) {
if (auto* const widget = view->GetWidget()) {
widget->AddObserver(this);
}
}
}
base::ScopedObservation<BrowserList, BrowserListObserver> observation_{this};
};
DEFINE_FRAMEWORK_SPECIFIC_METADATA(BrowserWidgetFocusSupplier)
InteractiveBrowserTestPrivate::InteractiveBrowserTestPrivate(
std::unique_ptr<InteractionTestUtilBrowser> test_util)
: InteractiveViewsTestPrivate(std::move(test_util)) {}
InteractiveBrowserTestPrivate::~InteractiveBrowserTestPrivate() = default;
void InteractiveBrowserTestPrivate::DoTestSetUp() {
InteractiveViewsTestPrivate::DoTestSetUp();
widget_focus_suppliers().MaybeRegister<BrowserWidgetFocusSupplier>();
}
void InteractiveBrowserTestPrivate::DoTestTearDown() {
// Release any remaining instrumented WebContents.
instrumented_web_contents_.clear();
// If the test has elected to engage WebUI code coverage, write out the
// resulting data.
if (coverage_observer_) {
const auto* const test_info =
testing::UnitTest::GetInstance()->current_test_info();
std::string test_name =
base::StrCat({test_info->test_suite_name(), ".", test_info->name()});
// Parameterized tests tend to have slashes in them, which can interfere
// with file system paths. Change them to something else.
std::replace(test_name.begin(), test_name.end(), '/', '_');
LOG(INFO) << "Writing out WebUI code coverage data. If this causes the "
"test to time out (b/273290598), you may want to disable "
"coverage until the performance can be improved. If the "
"test crashes, a page touched by the test is likely still "
"incompatible with coverage (see b/273545898).";
coverage_observer_->CollectCoverage(test_name);
}
InteractiveViewsTestPrivate::DoTestTearDown();
}
void InteractiveBrowserTestPrivate::MaybeStartWebUICodeCoverage() {
if (coverage_observer_) {
return;
}
base::CommandLine* const command_line =
base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kDevtoolsCodeCoverage)) {
base::FilePath devtools_code_coverage_dir =
command_line->GetSwitchValuePath(switches::kDevtoolsCodeCoverage);
coverage_observer_ = std::make_unique<DevToolsAgentCoverageObserver>(
devtools_code_coverage_dir);
LOG(WARNING) << "Starting WebUI code coverage. This may cause the test to "
"take longer, possibly resulting in timeouts. Also, due to "
"issues with the coverage logic, some WebUI pages may not "
"be compatible with WebUI code coverage.";
}
}
void InteractiveBrowserTestPrivate::AddInstrumentedWebContents(
std::unique_ptr<WebContentsInteractionTestUtil> instrumented_web_contents) {
for (const auto& existing : instrumented_web_contents_) {
CHECK_NE(instrumented_web_contents->page_identifier(),
existing->page_identifier());
}
instrumented_web_contents_.emplace_back(std::move(instrumented_web_contents))
.get();
}
bool InteractiveBrowserTestPrivate::IsInstrumentedWebContents(
ui::ElementIdentifier element_id) const {
for (const auto& existing : instrumented_web_contents_) {
if (existing->page_identifier() == element_id) {
return true;
}
}
return false;
}
bool InteractiveBrowserTestPrivate::UninstrumentWebContents(
ui::ElementIdentifier to_remove) {
for (auto it = instrumented_web_contents_.begin();
it != instrumented_web_contents_.end(); ++it) {
if ((*it)->page_identifier() == to_remove) {
instrumented_web_contents_.erase(it);
return true;
}
}
return false;
}
std::string InteractiveBrowserTestPrivate::DeepQueryToString(
const WebContentsInteractionTestUtil::DeepQuery& deep_query) {
std::ostringstream oss;
oss << "{";
for (size_t i = 0; i < deep_query.size(); ++i) {
if (i) {
oss << ", ";
}
oss << "\"" << deep_query[i] << "\"";
}
oss << "}";
return oss.str();
}
gfx::NativeWindow InteractiveBrowserTestPrivate::GetNativeWindowFromElement(
ui::TrackedElement* el) const {
gfx::NativeWindow window = gfx::NativeWindow();
// For instrumented WebContents, we can get the native window directly from
// the contents object.
if (el->IsA<TrackedElementWebContents>()) {
auto* const util = el->AsA<TrackedElementWebContents>()->owner();
window = util->web_contents()->GetTopLevelNativeWindow();
}
// If that did not work, fall back to the base implementation.
if (!window)
window = InteractiveViewsTestPrivate::GetNativeWindowFromElement(el);
return window;
}
gfx::NativeWindow InteractiveBrowserTestPrivate::GetNativeWindowFromContext(
ui::ElementContext context) const {
// Defer to the base implementation first, since there may be a cached value
// that is more accurate than what can be inferred from the context.
gfx::NativeWindow window =
InteractiveViewsTestPrivate::GetNativeWindowFromContext(context);
// If that didn't work, fall back to the top-level browser window for the
// context (assuming there is one).
if (!window) {
if (Browser* const browser =
InteractionTestUtilBrowser::GetBrowserFromContext(context)) {
if (BrowserView* const browser_view =
BrowserView::GetBrowserViewForBrowser(browser)) {
window = browser_view->GetNativeWindow();
}
}
}
return window;
}
std::string InteractiveBrowserTestPrivate::DebugDescribeContext(
ui::ElementContext context) const {
if (const auto* browser =
InteractionTestUtilBrowser::GetBrowserFromContext(context)) {
std::string type;
switch (browser->type()) {
case Browser::TYPE_APP:
type = "App window";
break;
case Browser::TYPE_APP_POPUP:
type = "Popup app window";
break;
case Browser::TYPE_NORMAL:
type = "Tabbed browser window";
break;
case Browser::TYPE_DEVTOOLS:
type = "Devtools window";
break;
case Browser::TYPE_PICTURE_IN_PICTURE:
type = "Picture-in-picture window";
break;
default:
type = "Other browser window";
break;
}
if (browser->SupportsWindowFeature(Browser::FEATURE_TABSTRIP)) {
type += base::StringPrintf(", %d tab(s) (active: %d)",
browser->tab_strip_model()->count(),
browser->tab_strip_model()->active_index());
}
return base::StringPrintf(
"%s%s profile %s%s at %s",
(browser->window()->IsActive() ? "[ACTIVE] " : ""), type,
browser->profile()->GetDebugName(),
(browser->profile()->IsOffTheRecord() ? " (off-the-record)" : ""),
DebugDumpBounds(browser->window()->GetBounds()));
} else {
return InteractiveViewsTestPrivate::DebugDescribeContext(context);
}
}
InteractiveBrowserTestPrivate::DebugTreeNode
InteractiveBrowserTestPrivate::DebugDumpElement(
const ui::TrackedElement* el) const {
if (const auto* contents = el->AsA<TrackedElementWebContents>()) {
auto* const web_contents = contents->owner()->web_contents();
int index = TabStripModel::kNoTab;
if (const auto* browser =
InteractionTestUtilBrowser::GetBrowserFromContext(el->context())) {
index = browser->tab_strip_model()->GetIndexOfWebContents(web_contents);
}
return DebugTreeNode(base::StringPrintf(
"WebContents %s - %s at %s with URL \"%s\"",
(index == TabStripModel::kNoTab
? "in secondary UI"
: base::StringPrintf("in tab %d", index).c_str()),
el->identifier().GetName(), DebugDumpBounds(el->GetScreenBounds()),
web_contents->GetURL().spec().c_str()));
} else {
return InteractiveViewsTestPrivate::DebugDumpElement(el);
}
}
MatchableValue::MatchableValue() noexcept = default;
MatchableValue::MatchableValue(const base::Value& value) noexcept
: value_(value.Clone()) {}
MatchableValue::MatchableValue(base::Value&& value) noexcept
: value_(std::move(value)) {}
MatchableValue::MatchableValue(const MatchableValue& value) noexcept
: value_(value.value_.Clone()) {}
MatchableValue::MatchableValue(MatchableValue&&) noexcept = default;
MatchableValue& MatchableValue::operator=(const base::Value& value) noexcept {
value_ = value.Clone();
return *this;
}
MatchableValue& MatchableValue::operator=(base::Value&& value) noexcept {
value_ = std::move(value);
return *this;
}
MatchableValue& MatchableValue::operator=(
const MatchableValue& value) noexcept {
if (this != &value) {
value_ = value.value_.Clone();
}
return *this;
}
MatchableValue& MatchableValue::operator=(MatchableValue&&) noexcept = default;
MatchableValue::~MatchableValue() = default;
void CheckValueTypes(const MatchableValue& source,
const MatchableValue& target) {
using Type = base::Value::Type;
const auto source_type = source.value().type();
const auto target_type = target.value().type();
if (target_type == Type::DOUBLE &&
(source_type == Type::DOUBLE || source_type == Type::INTEGER)) {
// This is an allowed conversion.
return;
}
// Explicitly don't allow downcast to integer for comparison.
if (target_type == Type::INTEGER) {
CHECK_NE(source_type, Type::DOUBLE)
<< "JS returned a floating-point value (" << source
<< ") but comparison was with an integer (" << target
<< "). If there is any chance the value will be floating-point, "
"compare to a double value instead.";
}
// Otherwise, the types *must* match.
CHECK_EQ(source_type, target_type) << "Type mismatch attempting to compare "
<< source << " (from JS) and " << target;
}
bool MatchableValue::operator==(const MatchableValue& other) const {
CheckValueTypes(*this, other);
if (other.value_.type() == base::Value::Type::DOUBLE) {
return value_.GetDouble() == other.value_.GetDouble();
}
return value_ == other.value_;
}
namespace {
template <template <typename...> class Op>
bool MatchableValueCompare(const MatchableValue& lhs,
const MatchableValue& rhs) {
CheckValueTypes(lhs, rhs);
switch (rhs.value().type()) {
case base::Value::Type::DOUBLE:
return Op<double>()(lhs.value().GetDouble(), rhs.value().GetDouble());
case base::Value::Type::INTEGER:
return Op<double>()(lhs.value().GetInt(), rhs.value().GetInt());
case base::Value::Type::STRING:
return Op<std::string>()(lhs.value().GetString(),
rhs.value().GetString());
default:
NOTREACHED() << "Target value " << rhs << " (" << rhs.value().type()
<< ") does not support greater than/less than comparison.";
}
}
} // namespace
MatchableValue::operator std::string() const {
return value_.GetString();
}
bool MatchableValue::operator<(const MatchableValue& other) const {
return MatchableValueCompare<std::less>(*this, other);
}
bool MatchableValue::operator>(const MatchableValue& other) const {
return MatchableValueCompare<std::greater>(*this, other);
}
bool MatchableValue::operator<=(const MatchableValue& other) const {
return MatchableValueCompare<std::less_equal>(*this, other);
}
bool MatchableValue::operator>=(const MatchableValue& other) const {
return MatchableValueCompare<std::greater_equal>(*this, other);
}
std::ostream& operator<<(std::ostream& out, const MatchableValue& value) {
return out << value.value();
}
bool IsTruthyMatcher::MatchAndExplain(
const internal::MatchableValue& x,
testing::MatchResultListener* listener) const {
return WebContentsInteractionTestUtil::IsTruthy(x.value());
}
void IsTruthyMatcher::DescribeTo(std::ostream* os) const {
*os << "is truthy";
}
void IsTruthyMatcher::DescribeNegationTo(std::ostream* os) const {
*os << "is falsy";
}
} // namespace internal
|