Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(955)

Side by Side Diff: chrome/browser/ui/test/test_browser_dialog.h

Issue 2532793002: Add a TestBrowserDialog helper class for testing browser dialogs in a consistent way. (Closed)
Patch Set: constexpr const it is Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 `TestBrowserDialog.Invoke`
48 // test case without other arguments. I.e.
49 //
50 // browser_tests --gtest_filter=TestBrowserDialog.Invoke
51 //
52 // Dialogs listed can be shown interactively using the --dialog argument. E.g.
53 //
54 // browser_tests --gtest_filter=TestBrowserDialog.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 `TestBrowserDialog.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 // Called by the static initializer to register names. Only the
93 // TEST_BROWSER_DIALOG macro calls this.
94 static int Register(const char* harness, NameProviderFunction* name_provider);
95
96 // Called by the generated test case to invoke the dialog(s).
97 static void TestBrowserDialogRun(
98 TestDialogInterface* harness,
99 const std::vector<std::string>& available_cases);
100 };
101
102 // Helper to mix in a TestDialogInterface to an existing test harness. |Base|
103 // must be a descendant of InProcessBrowserTest.
104 template <class Base>
105 class SupportsTestDialog : public Base, public TestDialogInterface {
106 public:
107 SupportsTestDialog() {}
108
109 // TestDialogInterface:
110 gfx::NativeWindow DialogParent() override {
111 return this->browser()->window()->GetNativeWindow();
112 }
113
114 private:
115 DISALLOW_COPY_AND_ASSIGN(SupportsTestDialog);
116 };
117
118 using DialogBrowserTest = SupportsTestDialog<InProcessBrowserTest>;
Paweł Hajdan Jr. 2016/12/16 20:14:29 nit: Just checking: is "using" allowed in headers?
Peter Kasting 2016/12/17 03:53:29 Yes, this use of "using" is as a replacement for "
119
120 #endif // CHROME_BROWSER_UI_TEST_TEST_BROWSER_DIALOG_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698