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(). | |
34 }; | 33 }; |
35 | 34 |
36 // Helper to break out of the nested run loop that runs a test dialog. | 35 // Helper to break out of the nested run loop that runs a test dialog. |
37 class WidgetCloser : public views::WidgetObserver { | 36 class WidgetCloser : public views::WidgetObserver { |
38 public: | 37 public: |
39 WidgetCloser(views::Widget* widget, DialogAction action) | 38 WidgetCloser(views::Widget* widget, DialogAction action) |
40 : action_(action), widget_(widget), weak_ptr_factory_(this) { | 39 : widget_(widget), weak_ptr_factory_(this) { |
41 widget->AddObserver(this); | 40 widget->AddObserver(this); |
42 if (action == DialogAction::INTERACTIVE) | 41 if (action == DialogAction::INTERACTIVE) |
43 return; | 42 return; |
44 | 43 |
45 base::ThreadTaskRunnerHandle::Get()->PostTask( | 44 base::ThreadTaskRunnerHandle::Get()->PostTask( |
46 FROM_HERE, base::BindOnce(&WidgetCloser::CloseAction, | 45 FROM_HERE, base::BindOnce(&WidgetCloser::CloseNow, |
47 weak_ptr_factory_.GetWeakPtr())); | 46 weak_ptr_factory_.GetWeakPtr())); |
48 } | 47 } |
49 | 48 |
50 // WidgetObserver: | 49 // WidgetObserver: |
51 void OnWidgetDestroyed(views::Widget* widget) override { | 50 void OnWidgetDestroyed(views::Widget* widget) override { |
52 widget_->RemoveObserver(this); | 51 widget_->RemoveObserver(this); |
53 widget_ = nullptr; | 52 widget_ = nullptr; |
54 base::MessageLoop::current()->QuitNow(); | 53 base::MessageLoop::current()->QuitNow(); |
55 } | 54 } |
56 | 55 |
57 private: | 56 private: |
58 void CloseAction() { | 57 void CloseNow() { |
59 if (!widget_) | 58 if (widget_) |
60 return; | 59 widget_->CloseNow(); |
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 } | |
73 } | 60 } |
74 | 61 |
75 const DialogAction action_; | |
76 views::Widget* widget_; | 62 views::Widget* widget_; |
77 | 63 |
78 base::WeakPtrFactory<WidgetCloser> weak_ptr_factory_; | 64 base::WeakPtrFactory<WidgetCloser> weak_ptr_factory_; |
79 | 65 |
80 DISALLOW_COPY_AND_ASSIGN(WidgetCloser); | 66 DISALLOW_COPY_AND_ASSIGN(WidgetCloser); |
81 }; | 67 }; |
82 | 68 |
83 // Extracts the |name| argument for ShowDialog() from the current test case. | 69 // Extracts the |name| argument for ShowDialog() from the current test case. |
84 // E.g. for InvokeDialog_name (or DISABLED_InvokeDialog_name) returns "name". | 70 // E.g. for InvokeDialog_name (or DISABLED_InvokeDialog_name) returns "name". |
85 std::string NameFromTestCase() { | 71 std::string NameFromTestCase() { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 // ignore added Widgets that are not dialogs. | 117 // ignore added Widgets that are not dialogs. |
132 base::EraseIf(added, [](views::Widget* widget) { | 118 base::EraseIf(added, [](views::Widget* widget) { |
133 return !widget->widget_delegate()->AsDialogDelegate(); | 119 return !widget->widget_delegate()->AsDialogDelegate(); |
134 }); | 120 }); |
135 } | 121 } |
136 | 122 |
137 // This can fail if no dialog was shown, if the dialog shown wasn't a toolkit- | 123 // This can fail if no dialog was shown, if the dialog shown wasn't a toolkit- |
138 // views dialog, or if more than one child dialog was shown. | 124 // views dialog, or if more than one child dialog was shown. |
139 ASSERT_EQ(1u, added.size()); | 125 ASSERT_EQ(1u, added.size()); |
140 | 126 |
141 DialogAction action = DialogAction::CLOSE_NOW; | 127 const DialogAction action = base::CommandLine::ForCurrentProcess()->HasSwitch( |
142 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 128 internal::kInteractiveSwitch) |
143 internal::kInteractiveSwitch)) { | 129 ? DialogAction::INTERACTIVE |
144 action = DialogAction::INTERACTIVE; | 130 : DialogAction::CLOSE_NOW; |
145 } else if (AlwaysCloseAsynchronously()) { | |
146 // TODO(tapted): Iterate over close methods when non-interactive for greater | |
147 // test coverage. | |
148 action = DialogAction::CLOSE; | |
149 } | |
150 | 131 |
151 WidgetCloser closer(added[0], action); | 132 WidgetCloser closer(added[0], action); |
152 ::test::RunTestInteractively(); | 133 ::test::RunTestInteractively(); |
153 } | 134 } |
154 | |
155 bool TestBrowserDialog::AlwaysCloseAsynchronously() { | |
156 return false; | |
157 } | |
OLD | NEW |