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.
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"
13 using content::WebContents
;
15 namespace media_router
{
17 class MediaRouterDialogControllerTest
: public MediaRouterTest
{
19 MediaRouterDialogControllerTest() {}
20 ~MediaRouterDialogControllerTest() override
{}
21 void OpenMediaRouterDialog();
24 WebContents
* initiator_
= nullptr;
25 MediaRouterDialogController
* dialog_controller_
= nullptr;
26 WebContents
* media_router_dialog_
= nullptr;
29 DISALLOW_COPY_AND_ASSIGN(MediaRouterDialogControllerTest
);
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());
39 // Create a reference to initiator contents.
40 initiator_
= browser()->tab_strip_model()->GetActiveWebContents();
42 MediaRouterDialogController::CreateForWebContents(initiator_
);
43 dialog_controller_
= MediaRouterDialogController::FromWebContents(initiator_
);
44 ASSERT_TRUE(dialog_controller_
);
46 // Get the media router dialog for the initiator.
47 media_router_dialog_
= dialog_controller_
->ShowMediaRouterDialog();
48 ASSERT_TRUE(media_router_dialog_
);
50 // New media router dialog is a constrained window, so the number of
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());
57 // Create/Get a media router dialog for initiator.
58 TEST_F(MediaRouterDialogControllerTest
, ShowMediaRouterDialog
) {
59 OpenMediaRouterDialog();
61 // Show media router dialog for the same initiator again.
62 WebContents
* same_media_router_dialog
=
63 dialog_controller_
->ShowMediaRouterDialog();
65 // Tab count remains the same.
66 EXPECT_EQ(1, browser()->tab_strip_model()->count());
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
);
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
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
);
82 EXPECT_EQ(0, tab_strip_model
->count());
84 // Create some new initiators.
85 chrome::NewTab(browser());
86 WebContents
* web_contents_1
= tab_strip_model
->GetActiveWebContents();
87 ASSERT_TRUE(web_contents_1
);
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());
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
);
101 WebContents
* media_router_dialog_1
=
102 dialog_controller_1
->ShowMediaRouterDialog();
103 ASSERT_TRUE(media_router_dialog_1
);
105 EXPECT_NE(web_contents_1
, media_router_dialog_1
);
106 EXPECT_EQ(2, tab_strip_model
->count());
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
);
114 WebContents
* media_router_dialog_2
=
115 dialog_controller_2
->ShowMediaRouterDialog();
116 ASSERT_TRUE(media_router_dialog_2
);
118 EXPECT_NE(web_contents_2
, media_router_dialog_2
);
119 EXPECT_NE(media_router_dialog_1
, media_router_dialog_2
);
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());
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
);
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
);
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());
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());
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());
151 TEST_F(MediaRouterDialogControllerTest
, CloseDialogFromWebUI
) {
152 OpenMediaRouterDialog();
155 content::WebContentsDestroyedWatcher
watcher(media_router_dialog_
);
157 content::WebUI
* web_ui
= media_router_dialog_
->GetWebUI();
159 MediaRouterUI
* media_router_ui
=
160 static_cast<MediaRouterUI
*>(web_ui
->GetController());
161 ASSERT_TRUE(media_router_ui
);
162 media_router_ui
->Close();
164 // Blocks until the media router dialog WebContents has been destroyed.
167 // Still 1 tab in the browser.
168 EXPECT_EQ(1, browser()->tab_strip_model()->count());
170 // Entry has been removed.
171 EXPECT_FALSE(dialog_controller_
->GetMediaRouterDialog());
173 // Show the media router dialog again, creating a new one.
174 WebContents
* media_router_dialog_2
=
175 dialog_controller_
->ShowMediaRouterDialog();
177 // Still 1 tab in the browser.
178 EXPECT_EQ(1, browser()->tab_strip_model()->count());
180 EXPECT_EQ(media_router_dialog_2
, dialog_controller_
->GetMediaRouterDialog());
183 TEST_F(MediaRouterDialogControllerTest
, CloseDialogFromDialogController
) {
184 OpenMediaRouterDialog();
187 content::WebContentsDestroyedWatcher
watcher(media_router_dialog_
);
189 dialog_controller_
->CloseMediaRouterDialog();
191 // Blocks until the media router dialog WebContents has been destroyed.
194 // Still 1 tab in the browser.
195 EXPECT_EQ(1, browser()->tab_strip_model()->count());
197 // Entry has been removed.
198 EXPECT_FALSE(dialog_controller_
->GetMediaRouterDialog());
201 TEST_F(MediaRouterDialogControllerTest
, CloseInitiator
) {
202 OpenMediaRouterDialog();
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_
);
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
));
214 // Blocks until the initiator WebContents has been destroyed.
215 initiator_watcher
.Wait();
216 dialog_watcher
.Wait();
218 // No tabs in the browser.
219 EXPECT_EQ(0, browser()->tab_strip_model()->count());
221 // The dialog controller is deleted when the WebContents is closed/destroyed.
224 } // namespace media_router