OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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 #include "components/web_modal/popup_manager.h" | |
6 | |
7 #include <map> | |
8 | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "components/web_modal/single_popup_manager.h" | |
11 #include "content/public/test/test_renderer_host.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace web_modal { | |
15 | |
16 // Tracks persistent state changes of the native WC-modal dialog manager. | |
17 class NativePopupTracker { | |
18 public: | |
19 enum DialogState { | |
Finnur
2014/05/16 12:38:14
PopupState?
| |
20 UNKNOWN, | |
21 NOT_SHOWN, | |
22 SHOWN, | |
23 HIDDEN, | |
24 CLOSED | |
25 }; | |
26 | |
27 NativePopupTracker() : state_(UNKNOWN), was_shown_(false) {} | |
28 | |
29 void SetState(DialogState state) { | |
30 state_ = state; | |
31 if (state_ == SHOWN) | |
32 was_shown_ = true; | |
33 } | |
34 | |
Finnur
2014/05/16 12:38:14
private:
... and ...
DISALLOW_COPY_AND_ASSIGN()
| |
35 DialogState state_; | |
36 bool was_shown_; | |
37 }; | |
38 | |
39 class TestNativePopupManager : public SinglePopupManager { | |
40 public: | |
41 TestNativePopupManager( | |
42 NativePopup dialog, | |
43 SinglePopupManagerDelegate* delegate, | |
44 NativePopupTracker* tracker) | |
45 : delegate_(delegate), | |
46 dialog_(dialog), | |
47 tracker_(tracker) { | |
48 if (tracker_) | |
Finnur
2014/05/16 12:38:14
You seem guaranteed to have tracker_ until you sto
| |
49 tracker_->SetState(NativePopupTracker::NOT_SHOWN); | |
50 } | |
51 | |
52 virtual content::WebContents* IsBoundToWebContents() OVERRIDE { | |
53 return NULL; | |
54 } | |
55 | |
56 virtual void Show() OVERRIDE { | |
57 if (tracker_) | |
58 tracker_->SetState(NativePopupTracker::SHOWN); | |
59 } | |
60 virtual void Hide() OVERRIDE { | |
61 if (tracker_) | |
62 tracker_->SetState(NativePopupTracker::HIDDEN); | |
63 } | |
Finnur
2014/05/16 12:38:14
Nit: Spacing is a little inconsistent here (someti
| |
64 virtual void Close() OVERRIDE { | |
65 if (tracker_) | |
66 tracker_->SetState(NativePopupTracker::CLOSED); | |
67 delegate_->WillClose(dialog_); | |
68 } | |
69 virtual void Focus() OVERRIDE { | |
70 } | |
71 virtual void Pulse() OVERRIDE { | |
72 } | |
73 virtual NativePopup popup() OVERRIDE { | |
74 return dialog_; | |
75 } | |
76 | |
77 virtual bool HasUserGesture() OVERRIDE { | |
78 return false; | |
79 } | |
80 virtual bool MayBeOverlapped() OVERRIDE { | |
81 return false; | |
82 } | |
83 | |
84 void StopTracking() { | |
85 tracker_ = NULL; | |
86 } | |
87 | |
88 private: | |
89 SinglePopupManagerDelegate* delegate_; | |
90 NativePopup dialog_; | |
Finnur
2014/05/16 12:38:14
popup_?
| |
91 NativePopupTracker* tracker_; | |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(TestNativePopupManager); | |
94 }; | |
95 | |
96 class PopupManagerTest : public content::RenderViewHostTestHarness { | |
97 public: | |
98 PopupManagerTest() | |
99 : next_dialog_id(1), | |
100 manager(NULL) { | |
101 } | |
102 | |
103 virtual void SetUp() { | |
104 content::RenderViewHostTestHarness::SetUp(); | |
105 | |
106 manager = new PopupManager(); | |
107 } | |
108 | |
109 virtual void TearDown() { | |
110 content::RenderViewHostTestHarness::TearDown(); | |
111 manager->CloseAllPopups(); | |
112 delete manager; | |
Finnur
2014/05/16 12:38:14
The OCD part of me wants to see TearDown be last,
| |
113 } | |
114 | |
115 protected: | |
116 NativePopup MakeFakeDialog() { | |
117 // PopupManager treats the NativePopup as an opaque type, so creating | |
118 // fake NativePopups using reinterpret_cast is valid. | |
119 return reinterpret_cast<NativePopup>(next_dialog_id++); | |
120 } | |
121 | |
122 int next_dialog_id; | |
123 PopupManager* manager; | |
Finnur
2014/05/16 12:38:14
nit: Missing underscore for members. Also, use sco
| |
124 | |
125 DISALLOW_COPY_AND_ASSIGN(PopupManagerTest); | |
Finnur
2014/05/16 12:38:14
nit: Should you not have private: above this?
| |
126 }; | |
127 | |
128 // Test that the dialog is shown immediately when the delegate indicates the web | |
129 // contents is visible. | |
130 TEST_F(PopupManagerTest, WebContentsVisible) { | |
131 // Dialog should be shown while WebContents is visible. | |
132 const NativePopup dialog = MakeFakeDialog(); | |
133 | |
134 NativePopupTracker tracker; | |
135 TestNativePopupManager* native_manager = | |
136 new TestNativePopupManager(dialog, manager, &tracker); | |
137 manager->ShowPopup(scoped_ptr<SinglePopupManager>(native_manager).Pass()); | |
138 | |
139 EXPECT_EQ(NativePopupTracker::SHOWN, tracker.state_); | |
140 EXPECT_TRUE(manager->IsPopupActiveInCurrentWebContents()); | |
141 EXPECT_TRUE(tracker.was_shown_); | |
142 | |
143 native_manager->StopTracking(); | |
144 } | |
145 | |
146 } // namespace web_modal | |
OLD | NEW |