| Index: chrome/browser/ui/test/test_browser_dialog.cc
|
| diff --git a/chrome/browser/ui/test/test_browser_dialog.cc b/chrome/browser/ui/test/test_browser_dialog.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..fdb5d8ded51e45d49a0e48223d43c6a67682e2d9
|
| --- /dev/null
|
| +++ b/chrome/browser/ui/test/test_browser_dialog.cc
|
| @@ -0,0 +1,142 @@
|
| +// Copyright 2016 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.
|
| +
|
| +#include "chrome/browser/ui/test/test_browser_dialog.h"
|
| +
|
| +#include "base/command_line.h"
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "chrome/browser/platform_util.h"
|
| +#include "ui/base/material_design/material_design_controller.h"
|
| +#include "ui/base/test/material_design_controller_test_api.h"
|
| +#include "ui/base/test/user_interactive_test_case.h"
|
| +#include "ui/base/ui_base_switches.h"
|
| +#include "ui/views/widget/widget.h"
|
| +#include "ui/views/widget/widget_observer.h"
|
| +
|
| +namespace {
|
| +
|
| +// An automatic action for WidgetCloser to post to the RunLoop.
|
| +// TODO(tapted): Explore asynchronous Widget::Close() and DialogClientView::
|
| +// {Accept,Cancel}Window() approaches to test other dialog lifetimes.
|
| +enum class DialogAction {
|
| + INTERACTIVE, // Run interactively.
|
| + CLOSE_NOW, // Call Widget::CloseNow().
|
| +};
|
| +
|
| +// Helper to break out of the nested run loop that runs a test dialog.
|
| +class WidgetCloser : public views::WidgetObserver {
|
| + public:
|
| + WidgetCloser(views::Widget* widget, DialogAction action)
|
| + : widget_(widget), weak_ptr_factory_(this) {
|
| + widget->AddObserver(this);
|
| + if (action == DialogAction::INTERACTIVE)
|
| + return;
|
| +
|
| + base::ThreadTaskRunnerHandle::Get()->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&WidgetCloser::CloseNow, weak_ptr_factory_.GetWeakPtr()));
|
| + }
|
| +
|
| + // WidgetObserver:
|
| + void OnWidgetDestroyed(views::Widget* widget) override {
|
| + widget_->RemoveObserver(this);
|
| + widget_ = nullptr;
|
| + base::MessageLoop::current()->QuitNow();
|
| + }
|
| +
|
| + private:
|
| + void CloseNow() {
|
| + if (widget_)
|
| + widget_->CloseNow();
|
| + }
|
| +
|
| + views::Widget* widget_;
|
| +
|
| + base::WeakPtrFactory<WidgetCloser> weak_ptr_factory_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(WidgetCloser);
|
| +};
|
| +
|
| +// Returns the test case portion of a --dialog= switch argument.
|
| +std::string CaseFromDialogDescription(const std::string& argument) {
|
| + std::string::size_type dot = argument.find('.');
|
| + return dot == std::string::npos ? std::string() : argument.substr(dot + 1);
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +// static
|
| +std::vector<std::string> TestDialogInterface::NameProvider() {
|
| + return {"Default"};
|
| +}
|
| +
|
| +// static
|
| +std::vector<std::string>& TestDialogInterface::GetDialogTestCases() {
|
| + CR_DEFINE_STATIC_LOCAL(std::vector<std::string>, all_cases, ());
|
| + return all_cases;
|
| +}
|
| +
|
| +// static
|
| +int TestDialogInterface::Register(const char* harness,
|
| + NameProviderFunction* name_provider) {
|
| + const std::string prefix = harness + std::string(".");
|
| + std::vector<std::string>& all_cases = GetDialogTestCases();
|
| + for (const std::string& name : name_provider())
|
| + all_cases.push_back(prefix + name);
|
| + return 1;
|
| +}
|
| +
|
| +// static
|
| +void TestDialogInterface::TestBrowserDialogRun(
|
| + TestDialogInterface* harness,
|
| + const std::vector<std::string>& available_cases) {
|
| + const base::CommandLine& command_line =
|
| + *base::CommandLine::ForCurrentProcess();
|
| +
|
| +#if defined(OS_MACOSX)
|
| + // The rest of this method assumes the child dialog is toolkit-views. So, for
|
| + // Mac, it will only work if --secondary-ui-md is passed. Without this, a
|
| + // Cocoa dialog will be created, which TestDialogInterface doesn't support.
|
| + // Force SecondaryUiMaterial() on Mac to get coverage on the bots. Leave it
|
| + // optional elsewhere so that the non-MD dialog can be invoked to compare.
|
| + ui::test::MaterialDesignControllerTestAPI md_test_api(
|
| + ui::MaterialDesignController::GetMode());
|
| + md_test_api.SetSecondaryUiMaterial(true);
|
| +#endif
|
| +
|
| + int dialog_index = 0;
|
| + std::string dialog_name =
|
| + command_line.GetSwitchValueASCII(internal::kDialogSwitch);
|
| + if (!dialog_name.empty()) {
|
| + const std::string case_name = CaseFromDialogDescription(dialog_name);
|
| + auto it =
|
| + std::find(available_cases.begin(), available_cases.end(), case_name);
|
| + ASSERT_NE(it, available_cases.end());
|
| + dialog_index = std::distance(available_cases.begin(), it);
|
| + }
|
| +
|
| + gfx::NativeView parent =
|
| + platform_util::GetViewForWindow(harness->DialogParent());
|
| + views::Widget::Widgets widgets_before;
|
| + views::Widget::GetAllChildWidgets(parent, &widgets_before);
|
| +
|
| + harness->ShowDialog(dialog_index);
|
| + views::Widget::Widgets widgets_after;
|
| + views::Widget::GetAllChildWidgets(parent, &widgets_after);
|
| +
|
| + auto added = base::STLSetDifference<std::vector<views::Widget*>>(
|
| + widgets_after, widgets_before);
|
| +
|
| + // This can fail if no dialog was shown, if the dialog shown wasn't a toolkit-
|
| + // views dialog, or if more than one child dialog was shown.
|
| + ASSERT_EQ(1u, added.size());
|
| +
|
| + const DialogAction action =
|
| + command_line.HasSwitch(internal::kInteractiveSwitch)
|
| + ? DialogAction::INTERACTIVE
|
| + : DialogAction::CLOSE_NOW;
|
| +
|
| + WidgetCloser closer(added[0], action);
|
| + ::test::RunTestInteractively();
|
| +}
|
|
|