Chromium Code Reviews| 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. E.g. This allows screenshots to be generated easily, with the | |
|
Peter Kasting
2016/12/15 00:28:55
Nit: Just remove "E.g."
tapted
2016/12/15 06:08:22
Done.
| |
| 20 // same test data, to assist with UI review. It also provides a registry of | |
| 21 // dialogs so they can be systematically checked for subtle changes and | |
| 22 // regressions. | |
| 23 // | |
| 24 // To use TEST_BROWSER_DIALOG, a test harness inherits from DialogBrowserTest | |
|
Peter Kasting
2016/12/15 00:28:55
Nit: inherits -> should inherit
tapted
2016/12/15 06:08:22
Done.
| |
| 25 // rather than InProcessBrowserTest. If the dialog under test has only a single | |
| 26 // mode of operation, the only other requirement on the test harness is an | |
| 27 // override: | |
| 28 // | |
| 29 // class FooDialogTest : public DialogBrowserTest { | |
| 30 // public: | |
| 31 // .. | |
| 32 // // TestDialogInterface: | |
|
Peter Kasting
2016/12/15 00:28:56
Nit: TestDialogInterface -> DialogBrowserTest
tapted
2016/12/15 06:08:22
Done.
| |
| 33 // void ShowDialog(int index) override { | |
| 34 // /* Show dialog attached to browser() and leave it open. */ | |
| 35 // } | |
| 36 // .. | |
| 37 // }; | |
| 38 // | |
| 39 // then in the foo_dialog_browsertest.cc, define: | |
| 40 // | |
| 41 // TEST_BROWSER_DIALOG(FooDialogTest); | |
| 42 // | |
| 43 // This adds a test case "FooDialogTest.InvokeDefault", and registers with the | |
| 44 // dialog testing framework. The dialog is shown and immediately closed (the | |
| 45 // test harness does not need to close it). The "InvokeDefault" test case is run | |
| 46 // as part of the regular test suite. | |
| 47 // | |
| 48 // | |
|
Peter Kasting
2016/12/15 00:28:55
Nit: Not clear to me why some places have two blan
tapted
2016/12/15 06:08:22
Got rid of the double-blank lines. (I think I was
| |
| 49 // To get a list of all available dialogs, run the `TestBrowserDialog.Invoke` | |
| 50 // test case without other arguments. I.e. | |
| 51 // | |
| 52 // browser_tests --gtest_filter=TestBrowserDialog.Invoke | |
| 53 // | |
| 54 // | |
| 55 // Dialogs listed can be shown interactively using the --dialog argument. E.g. | |
| 56 // | |
| 57 // browser_tests --gtest_filter=TestBrowserDialog.Invoke --interactive \ | |
| 58 // --dialog=FooDialogTest.Default | |
| 59 // | |
| 60 // | |
| 61 // Here the name "Default" is provided by the default implementation of a | |
| 62 // NameProvider() static method. A test harness can invoke multiple styles by | |
| 63 // declaring its own NameProvider that returns a vector of strings. E.g. | |
| 64 // | |
| 65 // // static | |
| 66 // std::vector<std::string> FooDialogTest::NameProvider() { | |
| 67 // return {"Expired", "Valid"}; | |
| 68 // } | |
| 69 // | |
| 70 // This registers multiple dialogs for `TestBrowserDialog.Invoke`. The |index| | |
| 71 // given to the ShowDialog() override will correspond to the name provided via | |
| 72 // the --dialog= switch. | |
| 73 #define TEST_BROWSER_DIALOG(Harness) \ | |
|
Peter Kasting
2016/12/15 00:28:55
Nit: Blank line above this so the comment is visua
tapted
2016/12/15 06:08:22
Done.
| |
| 74 int Harness##_register_names = \ | |
| 75 TestDialogInterface::Register(#Harness, &Harness::NameProvider); \ | |
|
Peter Kasting
2016/12/15 00:28:56
This violates the Google style guide, but I don't
tapted
2016/12/15 06:08:22
Yeah, I was aware of this, but it wasn't a casual
| |
| 76 IN_PROC_BROWSER_TEST_F(Harness, InvokeDefault) { \ | |
| 77 TestBrowserDialogRun(this, Harness::NameProvider()); \ | |
| 78 } | |
| 79 | |
| 80 class TestDialogInterface { | |
| 81 public: | |
| 82 using NameProviderFunction = std::vector<std::string> (*)(); | |
|
Peter Kasting
2016/12/15 00:28:55
Nit: Unless I am mistaken, if you declare this typ
tapted
2016/12/15 06:08:23
Done.
Peter Kasting
2016/12/15 08:12:28
I realize now this suggestion didn't actually help
| |
| 83 | |
| 84 // Provides a single name, "Default". Provide a "static" override of this to | |
| 85 // register multiple dialog styles with the dialog testing framework. | |
| 86 static std::vector<std::string> NameProvider(); | |
| 87 | |
| 88 // Show the dialog corresponding to |index| of the names supplied by | |
| 89 // NameProvider(). | |
| 90 virtual void ShowDialog(int index) = 0; | |
| 91 | |
| 92 // The window that owns the dialogs. Used to find where the dialog appears. | |
| 93 virtual gfx::NativeWindow DialogParent() = 0; | |
| 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(); | |
|
Peter Kasting
2016/12/15 00:28:56
Nit: Can't see why this-> is needed here. Does it
tapted
2016/12/15 06:08:22
It's needed because browser() is a member of a tem
Peter Kasting
2016/12/15 08:12:28
Ah, that makes sense. I figured it was due to tem
| |
| 115 } | |
| 116 | |
| 117 private: | |
| 118 DISALLOW_COPY_AND_ASSIGN(SupportsTestDialog); | |
| 119 }; | |
| 120 | |
| 121 using DialogBrowserTest = SupportsTestDialog<InProcessBrowserTest>; | |
| 122 | |
| 123 #endif // CHROME_BROWSER_UI_TEST_TEST_BROWSER_DIALOG_H_ | |
| OLD | NEW |