OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_UI_TEST_TEST_BROWSER_DIALOG_H_ |
| 6 #define CHROME_BROWSER_UI_TEST_TEST_BROWSER_DIALOG_H_ |
| 7 |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "chrome/browser/ui/browser_window.h" |
| 12 #include "chrome/test/base/in_process_browser_test.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "ui/gfx/native_widget_types.h" |
| 15 |
| 16 // TEST_BROWSER_DIALOG provides a way to register an InProcessBrowserTest |
| 17 // testing harness with a framework that collects the set of all Chrome browser |
| 18 // dialogs available, and provides a way to invoke them "interactively", in a |
| 19 // consistent way. This allows screenshots to be generated easily, with the same |
| 20 // test data, to assist with UI review. It also provides a registry of dialogs |
| 21 // so they can be systematically checked for subtle changes and regressions. |
| 22 // |
| 23 // To use TEST_BROWSER_DIALOG, a test harness should inherit from |
| 24 // DialogBrowserTest rather than InProcessBrowserTest. If the dialog under test |
| 25 // has only a single mode of operation, the only other requirement on the test |
| 26 // harness is an override: |
| 27 // |
| 28 // class FooDialogTest : public DialogBrowserTest { |
| 29 // public: |
| 30 // .. |
| 31 // // DialogBrowserTest: |
| 32 // void ShowDialog(int index) override { |
| 33 // /* Show dialog attached to browser() and leave it open. */ |
| 34 // } |
| 35 // .. |
| 36 // }; |
| 37 // |
| 38 // then in the foo_dialog_browsertest.cc, define: |
| 39 // |
| 40 // TEST_BROWSER_DIALOG(FooDialogTest); |
| 41 // |
| 42 // This adds a test case "FooDialogTest.InvokeDefault", and registers with the |
| 43 // dialog testing framework. The dialog is shown and immediately closed (the |
| 44 // test harness does not need to close it). The "InvokeDefault" test case is run |
| 45 // as part of the regular test suite. |
| 46 // |
| 47 // To get a list of all available dialogs, run the `BrowserDialogTest.Invoke` |
| 48 // test case without other arguments. I.e. |
| 49 // |
| 50 // browser_tests --gtest_filter=BrowserDialogTest.Invoke |
| 51 // |
| 52 // Dialogs listed can be shown interactively using the --dialog argument. E.g. |
| 53 // |
| 54 // browser_tests --gtest_filter=BrowserDialogTest.Invoke --interactive \ |
| 55 // --dialog=FooDialogTest.Default |
| 56 // |
| 57 // Here the name "Default" is provided by the default implementation of a |
| 58 // NameProvider() static method. A test harness can invoke multiple styles by |
| 59 // declaring its own NameProvider that returns a vector of strings. E.g. |
| 60 // |
| 61 // // static |
| 62 // std::vector<std::string> FooDialogTest::NameProvider() { |
| 63 // return {"Expired", "Valid"}; |
| 64 // } |
| 65 // |
| 66 // This registers multiple dialogs for `BrowserDialogTest.Invoke`. The |index| |
| 67 // given to the ShowDialog() override will correspond to the name provided via |
| 68 // the --dialog= switch. |
| 69 |
| 70 #define TEST_BROWSER_DIALOG(Harness) \ |
| 71 int Harness##_register_names = \ |
| 72 TestDialogInterface::Register(#Harness, &Harness::NameProvider); \ |
| 73 IN_PROC_BROWSER_TEST_F(Harness, InvokeDefault) { \ |
| 74 TestBrowserDialogRun(this, Harness::NameProvider()); \ |
| 75 } |
| 76 |
| 77 class TestDialogInterface { |
| 78 public: |
| 79 using NameProviderFunction = std::vector<std::string>(); |
| 80 |
| 81 // Provides a single name, "Default". Provide a "static" override of this to |
| 82 // register multiple dialog styles with the dialog testing framework. |
| 83 static NameProviderFunction NameProvider; |
| 84 |
| 85 // Show the dialog corresponding to |index| of the names supplied by |
| 86 // NameProvider(). |
| 87 virtual void ShowDialog(int index) = 0; |
| 88 |
| 89 // The window that owns the dialogs. Used to find where the dialog appears. |
| 90 virtual gfx::NativeWindow DialogParent() = 0; |
| 91 |
| 92 // Returns a reference to the collection of registered dialog test cases. |
| 93 static std::vector<std::string>& GetDialogTestCases(); |
| 94 |
| 95 // Called by the static initializer to register names. Only the |
| 96 // TEST_BROWSER_DIALOG macro calls this. |
| 97 static int Register(const char* harness, NameProviderFunction* name_provider); |
| 98 |
| 99 // Called by the generated test case to invoke the dialog(s). |
| 100 static void TestBrowserDialogRun( |
| 101 TestDialogInterface* harness, |
| 102 const std::vector<std::string>& available_cases); |
| 103 }; |
| 104 |
| 105 // Helper to mix in a TestDialogInterface to an existing test harness. |Base| |
| 106 // must be a descendant of InProcessBrowserTest. |
| 107 template <class Base> |
| 108 class SupportsTestDialog : public Base, public TestDialogInterface { |
| 109 public: |
| 110 SupportsTestDialog() {} |
| 111 |
| 112 // TestDialogInterface: |
| 113 gfx::NativeWindow DialogParent() override { |
| 114 return this->browser()->window()->GetNativeWindow(); |
| 115 } |
| 116 |
| 117 private: |
| 118 DISALLOW_COPY_AND_ASSIGN(SupportsTestDialog); |
| 119 }; |
| 120 |
| 121 using DialogBrowserTest = SupportsTestDialog<InProcessBrowserTest>; |
| 122 |
| 123 namespace internal { |
| 124 |
| 125 // When present on the command line, TestBrowserDialogRun will run this dialog |
| 126 // test case rather then the default dialog for the harness under test. |
| 127 constexpr const char kDialogSwitch[] = "dialog"; |
| 128 |
| 129 // When present on the command line, runs the test in an interactive mode. |
| 130 constexpr const char kInteractiveSwitch[] = "interactive"; |
| 131 |
| 132 } // namespace internal |
| 133 |
| 134 #endif // CHROME_BROWSER_UI_TEST_TEST_BROWSER_DIALOG_H_ |
OLD | NEW |