OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 "chrome/browser/ui/browser_commands.h" | |
6 #include "chrome/browser/ui/browser_finder.h" | |
7 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
8 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller.h" | |
9 #include "chrome/browser/ui/webui/media_router/media_router_test.h" | |
10 #include "chrome/browser/ui/webui/media_router/media_router_ui.h" | |
11 #include "content/public/test/browser_test_utils.h" | |
12 | |
13 using content::WebContents; | |
14 | |
15 namespace media_router { | |
16 | |
17 class MediaRouterDialogControllerTest : public MediaRouterTest { | |
18 public: | |
19 MediaRouterDialogControllerTest() {} | |
20 ~MediaRouterDialogControllerTest() override {} | |
21 void OpenMediaRouterDialog(); | |
22 | |
23 protected: | |
24 WebContents* initiator_ = nullptr; | |
25 MediaRouterDialogController* dialog_controller_ = nullptr; | |
26 WebContents* media_router_dialog_ = nullptr; | |
27 | |
28 private: | |
29 DISALLOW_COPY_AND_ASSIGN(MediaRouterDialogControllerTest); | |
30 }; | |
31 | |
32 void MediaRouterDialogControllerTest::OpenMediaRouterDialog() { | |
33 // Start with one window with one tab. | |
34 EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); | |
35 EXPECT_EQ(0, browser()->tab_strip_model()->count()); | |
36 chrome::NewTab(browser()); | |
37 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | |
38 | |
39 // Create a reference to initiator contents. | |
40 initiator_ = browser()->tab_strip_model()->GetActiveWebContents(); | |
41 | |
42 MediaRouterDialogController::CreateForWebContents(initiator_); | |
43 dialog_controller_ = MediaRouterDialogController::FromWebContents(initiator_); | |
44 ASSERT_TRUE(dialog_controller_); | |
45 | |
46 // Get the media router dialog for the initiator. | |
47 media_router_dialog_ = dialog_controller_->ShowMediaRouterDialog(); | |
48 ASSERT_TRUE(media_router_dialog_); | |
49 | |
50 // New media router dialog is a constrained window, so the number of | |
51 // tabs is still 1. | |
52 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | |
53 EXPECT_NE(initiator_, media_router_dialog_); | |
54 EXPECT_EQ(media_router_dialog_, dialog_controller_->GetMediaRouterDialog()); | |
55 } | |
56 | |
57 // Create/Get a media router dialog for initiator. | |
58 TEST_F(MediaRouterDialogControllerTest, ShowMediaRouterDialog) { | |
59 OpenMediaRouterDialog(); | |
60 | |
61 // Show media router dialog for the same initiator again. | |
62 WebContents* same_media_router_dialog = | |
63 dialog_controller_->ShowMediaRouterDialog(); | |
64 | |
65 // Tab count remains the same. | |
66 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | |
67 | |
68 // Media router dialog already exists. Calling |ShowMediaRouterDialog| again | |
69 // should not have created a new media router dialog. | |
70 EXPECT_EQ(media_router_dialog_, same_media_router_dialog); | |
71 } | |
72 | |
73 // Tests multiple media router dialogs exist in the same browser for different | |
74 // initiators. If a dialog already exists for an initiator, that initiator | |
75 // gets focused. | |
76 TEST_F(MediaRouterDialogControllerTest, MultipleMediaRouterDialogs) { | |
77 // Let's start with one window and two tabs. | |
78 EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); | |
79 TabStripModel* tab_strip_model = browser()->tab_strip_model(); | |
80 ASSERT_TRUE(tab_strip_model); | |
81 | |
82 EXPECT_EQ(0, tab_strip_model->count()); | |
83 | |
84 // Create some new initiators. | |
85 chrome::NewTab(browser()); | |
86 WebContents* web_contents_1 = tab_strip_model->GetActiveWebContents(); | |
87 ASSERT_TRUE(web_contents_1); | |
88 | |
89 chrome::NewTab(browser()); | |
90 WebContents* web_contents_2 = tab_strip_model->GetActiveWebContents(); | |
91 ASSERT_TRUE(web_contents_2); | |
92 EXPECT_EQ(2, tab_strip_model->count()); | |
93 | |
94 | |
95 // Create media router dialog for |web_contents_1|. | |
96 MediaRouterDialogController::CreateForWebContents(web_contents_1); | |
97 MediaRouterDialogController* dialog_controller_1 = | |
98 MediaRouterDialogController::FromWebContents(web_contents_1); | |
99 ASSERT_TRUE(dialog_controller_1); | |
100 | |
101 WebContents* media_router_dialog_1 = | |
102 dialog_controller_1->ShowMediaRouterDialog(); | |
103 ASSERT_TRUE(media_router_dialog_1); | |
104 | |
105 EXPECT_NE(web_contents_1, media_router_dialog_1); | |
106 EXPECT_EQ(2, tab_strip_model->count()); | |
107 | |
108 // Create media router dialog for |web_contents_2|. | |
109 MediaRouterDialogController::CreateForWebContents(web_contents_2); | |
110 MediaRouterDialogController* dialog_controller_2 = | |
111 MediaRouterDialogController::FromWebContents(web_contents_2); | |
112 ASSERT_TRUE(dialog_controller_2); | |
113 | |
114 WebContents* media_router_dialog_2 = | |
115 dialog_controller_2->ShowMediaRouterDialog(); | |
116 ASSERT_TRUE(media_router_dialog_2); | |
117 | |
118 EXPECT_NE(web_contents_2, media_router_dialog_2); | |
119 EXPECT_NE(media_router_dialog_1, media_router_dialog_2); | |
120 | |
121 // 2 initiators and 2 dialogs exist in the same browser. The dialogs are | |
122 // constrained in their respective initiators. | |
123 EXPECT_EQ(2, tab_strip_model->count()); | |
124 | |
125 int tab_1_index = tab_strip_model->GetIndexOfWebContents(web_contents_1); | |
126 int tab_2_index = tab_strip_model->GetIndexOfWebContents(web_contents_2); | |
127 int media_router_dialog_1_index = | |
128 tab_strip_model->GetIndexOfWebContents(media_router_dialog_1); | |
129 int media_router_dialog_2_index = | |
130 tab_strip_model->GetIndexOfWebContents(media_router_dialog_2); | |
131 | |
132 // Constrained dialogs are not in the TabStripModel. | |
133 EXPECT_EQ(-1, media_router_dialog_1_index); | |
134 EXPECT_EQ(-1, media_router_dialog_2_index); | |
135 | |
136 // Since |media_router_dialog_2_index| was the most recently created dialog, | |
137 // its initiator should have focus. | |
138 EXPECT_EQ(tab_2_index, tab_strip_model->active_index()); | |
139 | |
140 // When we get the media router dialog for |web_contents_1|, | |
141 // |media_router_dialog_1| is activated and focused. | |
142 dialog_controller_1->ShowMediaRouterDialog(); | |
143 EXPECT_EQ(tab_1_index, tab_strip_model->active_index()); | |
144 | |
145 // When we get the media router dialog for |web_contents_2|, | |
146 // |media_router_dialog_2| is activated and focused. | |
147 dialog_controller_2->ShowMediaRouterDialog(); | |
148 EXPECT_EQ(tab_2_index, tab_strip_model->active_index()); | |
149 } | |
150 | |
151 TEST_F(MediaRouterDialogControllerTest, CloseDialogFromWebUI) { | |
152 OpenMediaRouterDialog(); | |
153 | |
154 // Close the dialog. | |
155 content::WebContentsDestroyedWatcher watcher(media_router_dialog_); | |
156 | |
157 content::WebUI* web_ui = media_router_dialog_->GetWebUI(); | |
158 ASSERT_TRUE(web_ui); | |
159 MediaRouterUI* media_router_ui = | |
160 static_cast<MediaRouterUI*>(web_ui->GetController()); | |
161 ASSERT_TRUE(media_router_ui); | |
162 media_router_ui->Close(); | |
163 | |
164 // Blocks until the media router dialog WebContents has been destroyed. | |
165 watcher.Wait(); | |
166 | |
167 // Still 1 tab in the browser. | |
168 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | |
169 | |
170 // Entry has been removed. | |
171 EXPECT_FALSE(dialog_controller_->GetMediaRouterDialog()); | |
172 | |
173 // Show the media router dialog again, creating a new one. | |
174 WebContents* media_router_dialog_2 = | |
175 dialog_controller_->ShowMediaRouterDialog(); | |
176 | |
177 // Still 1 tab in the browser. | |
178 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | |
179 | |
180 EXPECT_EQ(media_router_dialog_2, dialog_controller_->GetMediaRouterDialog()); | |
181 } | |
182 | |
183 TEST_F(MediaRouterDialogControllerTest, CloseDialogFromDialogController) { | |
184 OpenMediaRouterDialog(); | |
185 | |
186 // Close the dialog. | |
187 content::WebContentsDestroyedWatcher watcher(media_router_dialog_); | |
188 | |
189 dialog_controller_->CloseMediaRouterDialog(); | |
190 | |
191 // Blocks until the media router dialog WebContents has been destroyed. | |
192 watcher.Wait(); | |
193 | |
194 // Still 1 tab in the browser. | |
195 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | |
196 | |
197 // Entry has been removed. | |
198 EXPECT_FALSE(dialog_controller_->GetMediaRouterDialog()); | |
199 } | |
200 | |
201 TEST_F(MediaRouterDialogControllerTest, CloseInitiator) { | |
202 OpenMediaRouterDialog(); | |
203 | |
204 // Close the initiator. This should also close the dialog WebContents. | |
205 content::WebContentsDestroyedWatcher initiator_watcher(initiator_); | |
206 content::WebContentsDestroyedWatcher dialog_watcher(media_router_dialog_); | |
207 | |
208 int initiator_index = browser()->tab_strip_model() | |
209 ->GetIndexOfWebContents(initiator_); | |
210 EXPECT_NE(-1, initiator_index); | |
211 EXPECT_TRUE(browser()->tab_strip_model()->CloseWebContentsAt( | |
212 initiator_index, TabStripModel::CLOSE_NONE)); | |
213 | |
214 // Blocks until the initiator WebContents has been destroyed. | |
215 initiator_watcher.Wait(); | |
216 dialog_watcher.Wait(); | |
217 | |
218 // No tabs in the browser. | |
219 EXPECT_EQ(0, browser()->tab_strip_model()->count()); | |
220 | |
221 // The dialog controller is deleted when the WebContents is closed/destroyed. | |
222 } | |
223 | |
224 } // namespace media_router | |
OLD | NEW |