OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/test/test_browser_dialog.h" | 5 #include "chrome/browser/ui/test/test_browser_dialog.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/test/gtest_util.h" | 10 #include "base/test/gtest_util.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 #endif | 23 #endif |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 // An automatic action for WidgetCloser to post to the RunLoop. | 27 // An automatic action for WidgetCloser to post to the RunLoop. |
28 // TODO(tapted): Explore asynchronous Widget::Close() and DialogClientView:: | 28 // TODO(tapted): Explore asynchronous Widget::Close() and DialogClientView:: |
29 // {Accept,Cancel}Window() approaches to test other dialog lifetimes. | 29 // {Accept,Cancel}Window() approaches to test other dialog lifetimes. |
30 enum class DialogAction { | 30 enum class DialogAction { |
31 INTERACTIVE, // Run interactively. | 31 INTERACTIVE, // Run interactively. |
32 CLOSE_NOW, // Call Widget::CloseNow(). | 32 CLOSE_NOW, // Call Widget::CloseNow(). |
| 33 CLOSE, // Call Widget::Close(). |
33 }; | 34 }; |
34 | 35 |
35 // Helper to break out of the nested run loop that runs a test dialog. | 36 // Helper to break out of the nested run loop that runs a test dialog. |
36 class WidgetCloser : public views::WidgetObserver { | 37 class WidgetCloser : public views::WidgetObserver { |
37 public: | 38 public: |
38 WidgetCloser(views::Widget* widget, DialogAction action) | 39 WidgetCloser(views::Widget* widget, DialogAction action) |
39 : widget_(widget), weak_ptr_factory_(this) { | 40 : action_(action), widget_(widget), weak_ptr_factory_(this) { |
40 widget->AddObserver(this); | 41 widget->AddObserver(this); |
41 if (action == DialogAction::INTERACTIVE) | 42 if (action == DialogAction::INTERACTIVE) |
42 return; | 43 return; |
43 | 44 |
44 base::ThreadTaskRunnerHandle::Get()->PostTask( | 45 base::ThreadTaskRunnerHandle::Get()->PostTask( |
45 FROM_HERE, base::BindOnce(&WidgetCloser::CloseNow, | 46 FROM_HERE, base::BindOnce(&WidgetCloser::CloseAction, |
46 weak_ptr_factory_.GetWeakPtr())); | 47 weak_ptr_factory_.GetWeakPtr())); |
47 } | 48 } |
48 | 49 |
49 // WidgetObserver: | 50 // WidgetObserver: |
50 void OnWidgetDestroyed(views::Widget* widget) override { | 51 void OnWidgetDestroyed(views::Widget* widget) override { |
51 widget_->RemoveObserver(this); | 52 widget_->RemoveObserver(this); |
52 widget_ = nullptr; | 53 widget_ = nullptr; |
53 base::MessageLoop::current()->QuitNow(); | 54 base::MessageLoop::current()->QuitNow(); |
54 } | 55 } |
55 | 56 |
56 private: | 57 private: |
57 void CloseNow() { | 58 void CloseAction() { |
58 if (widget_) | 59 if (!widget_) |
59 widget_->CloseNow(); | 60 return; |
| 61 |
| 62 switch (action_) { |
| 63 case DialogAction::CLOSE_NOW: |
| 64 widget_->CloseNow(); |
| 65 break; |
| 66 case DialogAction::CLOSE: |
| 67 widget_->Close(); |
| 68 break; |
| 69 case DialogAction::INTERACTIVE: |
| 70 NOTREACHED(); |
| 71 break; |
| 72 } |
60 } | 73 } |
61 | 74 |
| 75 const DialogAction action_; |
62 views::Widget* widget_; | 76 views::Widget* widget_; |
63 | 77 |
64 base::WeakPtrFactory<WidgetCloser> weak_ptr_factory_; | 78 base::WeakPtrFactory<WidgetCloser> weak_ptr_factory_; |
65 | 79 |
66 DISALLOW_COPY_AND_ASSIGN(WidgetCloser); | 80 DISALLOW_COPY_AND_ASSIGN(WidgetCloser); |
67 }; | 81 }; |
68 | 82 |
69 // Extracts the |name| argument for ShowDialog() from the current test case. | 83 // Extracts the |name| argument for ShowDialog() from the current test case. |
70 // E.g. for InvokeDialog_name (or DISABLED_InvokeDialog_name) returns "name". | 84 // E.g. for InvokeDialog_name (or DISABLED_InvokeDialog_name) returns "name". |
71 std::string NameFromTestCase() { | 85 std::string NameFromTestCase() { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 // ignore added Widgets that are not dialogs. | 131 // ignore added Widgets that are not dialogs. |
118 base::EraseIf(added, [](views::Widget* widget) { | 132 base::EraseIf(added, [](views::Widget* widget) { |
119 return !widget->widget_delegate()->AsDialogDelegate(); | 133 return !widget->widget_delegate()->AsDialogDelegate(); |
120 }); | 134 }); |
121 } | 135 } |
122 | 136 |
123 // This can fail if no dialog was shown, if the dialog shown wasn't a toolkit- | 137 // This can fail if no dialog was shown, if the dialog shown wasn't a toolkit- |
124 // views dialog, or if more than one child dialog was shown. | 138 // views dialog, or if more than one child dialog was shown. |
125 ASSERT_EQ(1u, added.size()); | 139 ASSERT_EQ(1u, added.size()); |
126 | 140 |
127 const DialogAction action = base::CommandLine::ForCurrentProcess()->HasSwitch( | 141 DialogAction action = DialogAction::CLOSE_NOW; |
128 internal::kInteractiveSwitch) | 142 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
129 ? DialogAction::INTERACTIVE | 143 internal::kInteractiveSwitch)) { |
130 : DialogAction::CLOSE_NOW; | 144 action = DialogAction::INTERACTIVE; |
| 145 } else if (AlwaysCloseAsynchronously()) { |
| 146 // TODO(tapted): Iterate over close methods when non-interactive for greater |
| 147 // test coverage. |
| 148 action = DialogAction::CLOSE; |
| 149 } |
131 | 150 |
132 WidgetCloser closer(added[0], action); | 151 WidgetCloser closer(added[0], action); |
133 ::test::RunTestInteractively(); | 152 ::test::RunTestInteractively(); |
134 } | 153 } |
| 154 |
| 155 bool TestBrowserDialog::AlwaysCloseAsynchronously() { |
| 156 return false; |
| 157 } |
OLD | NEW |