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 "ash/shell.h" | |
6 #include "ash/system/cast/tray_cast.h" | |
7 #include "ash/system/tray/system_tray.h" | |
8 #include "ash/system/tray/system_tray_delegate.h" | |
9 #include "ash/system/tray/system_tray_item.h" | |
10 #include "ash/test/tray_cast_test_api.h" | |
11 #include "chrome/browser/extensions/extension_browsertest.h" | |
12 #include "chrome/browser/profiles/profile_manager.h" | |
13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
14 #include "content/public/browser/render_view_host.h" | |
15 #include "content/public/test/test_utils.h" | |
16 #include "extensions/browser/process_manager.h" | |
17 | |
18 namespace { | |
19 | |
20 // Execute JavaScript within the context of the extension. Returns the result | |
21 // of the execution. | |
22 scoped_ptr<base::Value> ExecuteJavaScript( | |
23 const extensions::Extension* extension, | |
24 const std::string& javascript) { | |
25 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
26 auto pm = extensions::ProcessManager::Get(profile); | |
27 content::RenderViewHost* host = | |
28 pm->GetBackgroundHostForExtension(extension->id())->render_view_host(); | |
29 return content::ExecuteScriptAndGetValue(host->GetMainFrame(), javascript); | |
30 } | |
31 | |
32 // Ensures that all pending JavaScript execution callbacks are invoked. | |
33 void ExecutePendingJavaScript(const extensions::Extension* extension) { | |
34 ExecuteJavaScript(extension, ""); | |
oshima
2015/07/20 23:07:04
std::string()
jdufault
2015/07/21 19:52:44
Done.
| |
35 } | |
36 | |
37 // Invokes tray->StartCast(id) and verifies that launchDesktopMirroring | |
38 // was called with the same id. This automatically creates/destroys the detail | |
39 // view and notifies the tray that Chrome has begun casting. | |
40 void StartCastWithVerification(const extensions::Extension* extension, | |
41 ash::TrayCast* tray, | |
42 const std::string& receiver_id) { | |
43 ash::SystemTrayItem* system_tray_item = tray; | |
oshima
2015/07/20 23:07:03
is there particular reason why you want to have th
jdufault
2015/07/21 19:52:44
Yes, the API has private visibility in ash::TrayCa
oshima
2015/07/21 21:21:04
Acknowledged.
| |
44 ash::TrayCastTestAPI test_tray(tray); | |
45 | |
46 // StartCast will simulate a button click in the detail view to begin | |
47 // the cast, so we need to make a detail view available. | |
48 system_tray_item->CreateDetailedView(ash::user::LoginStatus::LOGGED_IN_USER); | |
49 | |
50 // Clear out any old state and execute any pending JS calls created from the | |
51 // CreateDetailedView call. | |
52 ExecuteJavaScript(extension, "launchDesktopMirroringReceiverId = ''"); | |
53 | |
54 test_tray.StartCast(receiver_id); | |
55 | |
56 scoped_ptr<base::Value> get_launch_desktop_mirroring_id = | |
57 ExecuteJavaScript(extension, "getLaunchDesktopMirroringReceiverId()"); | |
58 std::string result; | |
59 EXPECT_TRUE(get_launch_desktop_mirroring_id->GetAsString(&result)); | |
60 EXPECT_EQ(receiver_id, result); | |
oshima
2015/07/20 23:07:04
EXPECT in a utility function makes it difficult to
jdufault
2015/07/21 19:52:44
Done.
| |
61 system_tray_item->DestroyDetailedView(); | |
62 | |
63 // Tell the tray item that Chrome has started casting. | |
64 test_tray.OnCastingSessionStartedOrStopped(true); | |
65 ExecutePendingJavaScript(extension); | |
66 } | |
67 | |
68 // Invokes tray->StopCast() and verifies that stopMirroring('user-stop') | |
69 // got called in the extension. | |
70 void StopCastWithVerification(const extensions::Extension* extension, | |
71 ash::TrayCastTestAPI* tray) { | |
72 ExecuteJavaScript(extension, "stopMirroringCalled = false"); | |
73 tray->StopCast(); | |
74 scoped_ptr<base::Value> wasStopMirroringCalled = | |
75 ExecuteJavaScript(extension, "wasStopMirroringCalledWithUserStop()"); | |
76 bool result; | |
77 EXPECT_TRUE(wasStopMirroringCalled->GetAsBoolean(&result)); | |
78 EXPECT_TRUE(result); | |
oshima
2015/07/20 23:07:03
ditto
jdufault
2015/07/21 19:52:44
Done.
| |
79 | |
80 // Tell the tray item that Chrome has stopped casting. | |
81 tray->OnCastingSessionStartedOrStopped(false); | |
82 ExecutePendingJavaScript(extension); | |
83 } | |
84 | |
85 // Returns the cast tray. The tray initializer may have launched some | |
86 // JavaScript callbacks which have not finished executing. | |
87 ash::TrayCast* GetTrayCast(const extensions::Extension* extension) { | |
88 ash::SystemTray* tray = ash::Shell::GetInstance()->GetPrimarySystemTray(); | |
89 // Make sure we actually popup the tray, otherwise the TrayCast instance will | |
90 // not be created. | |
91 tray->ShowDefaultView(ash::BubbleCreationType::BUBBLE_CREATE_NEW); | |
92 | |
93 // Creating the tray causes some JavaScript to be executed. Let's try to make | |
94 // sure it is completed. | |
95 if (extension) | |
96 ExecutePendingJavaScript(extension); | |
97 | |
98 return tray->GetTrayCastForTesting(); | |
99 } | |
100 | |
101 class SystemTrayTrayCastChromeOSTest : public ExtensionBrowserTest { | |
102 protected: | |
103 SystemTrayTrayCastChromeOSTest() : ExtensionBrowserTest() {} | |
104 ~SystemTrayTrayCastChromeOSTest() override {} | |
105 | |
106 const extensions::Extension* LoadCastTestExtension() { | |
107 return LoadExtension(test_data_dir_.AppendASCII("tray_cast")); | |
108 } | |
109 | |
110 private: | |
111 DISALLOW_COPY_AND_ASSIGN(SystemTrayTrayCastChromeOSTest); | |
112 }; | |
113 | |
114 // A simple sanity check to make sure that the cast config delegate actually | |
115 // recognizes the cast extension. | |
116 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
117 CastTraySanityCheckTestExtensionGetsRecognized) { | |
118 ash::CastConfigDelegate* cast_config_delegate = | |
119 ash::Shell::GetInstance()->system_tray_delegate()->GetCastConfigDelegate(); | |
120 | |
121 EXPECT_FALSE(cast_config_delegate->HasCastExtension()); | |
122 LoadCastTestExtension(); | |
123 EXPECT_TRUE(cast_config_delegate->HasCastExtension()); | |
124 } | |
125 | |
126 // Verifies that the cast tray is hidden when there is no extension installed. | |
127 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
128 CastTrayIsHiddenWhenThereIsNoExtension) { | |
129 ash::TrayCastTestAPI tray(GetTrayCast(nullptr)); | |
130 EXPECT_TRUE(tray.IsTrayInitialized()); | |
131 EXPECT_FALSE(tray.IsTrayVisible()); | |
132 } | |
133 | |
134 // Verifies that the cast tray is hidden if there are no available receivers, | |
135 // even if there is an extension installed. | |
136 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
137 CastTrayIsHiddenWhenThereIsAnExtensionButNoReceivers) { | |
138 const extensions::Extension* extension = LoadCastTestExtension(); | |
139 | |
140 ash::TrayCastTestAPI tray(GetTrayCast(extension)); | |
141 EXPECT_TRUE(tray.IsTrayInitialized()); | |
142 EXPECT_FALSE(tray.IsTrayVisible()); | |
143 } | |
144 | |
145 // Verifies that the cast tray is displayed when there are receivers available. | |
146 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
147 CastTrayIsDisplayedWhenThereIsAnExtensionWithReceivers) { | |
148 const extensions::Extension* extension = LoadCastTestExtension(); | |
149 ExecuteJavaScript(extension, "addReceiver('test_id', 'name')"); | |
150 | |
151 ash::TrayCastTestAPI tray(GetTrayCast(extension)); | |
152 | |
153 EXPECT_TRUE(tray.IsTrayInitialized()); | |
154 EXPECT_TRUE(tray.IsTrayVisible()); | |
155 } | |
156 | |
157 // Verifies that we can cast to a specific receiver, stop casting, and then cast | |
158 // to another receiver when there is more than one receiver | |
159 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
160 CastTrayMultipleReceivers) { | |
161 const extensions::Extension* extension = LoadCastTestExtension(); | |
162 ExecuteJavaScript(extension, "addReceiver('test_id_1', 'name')"); | |
163 ExecuteJavaScript(extension, "addReceiver('not_used_0', 'name1')"); | |
164 ExecuteJavaScript(extension, "addReceiver('test_id_0', 'name')"); | |
165 ExecuteJavaScript(extension, "addReceiver('not_used_1', 'name2')"); | |
166 | |
167 ash::TrayCast* tray = GetTrayCast(extension); | |
168 ash::TrayCastTestAPI test_tray(tray); | |
169 StartCastWithVerification(extension, tray, "test_id_0"); | |
170 | |
171 EXPECT_TRUE(test_tray.IsTrayCastViewVisible()); | |
172 StopCastWithVerification(extension, &test_tray); | |
173 EXPECT_TRUE(test_tray.IsTraySelectViewVisible()); | |
174 | |
175 StartCastWithVerification(extension, tray, "test_id_1"); | |
176 EXPECT_TRUE(test_tray.IsTrayCastViewVisible()); | |
177 StopCastWithVerification(extension, &test_tray); | |
178 EXPECT_TRUE(test_tray.IsTraySelectViewVisible()); | |
179 } | |
180 | |
181 // Verifies the stop cast button invokes the JavaScript function | |
182 // "stopMirroring('user-stop')". | |
183 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
184 CastTrayStopButtonStopsCast) { | |
185 // Add a receiver that is casting. | |
186 const extensions::Extension* extension = LoadCastTestExtension(); | |
187 ExecuteJavaScript(extension, "addReceiver('test_id', 'name', 'title', 1)"); | |
188 | |
189 ash::TrayCastTestAPI test_tray(GetTrayCast(extension)); | |
190 test_tray.OnCastingSessionStartedOrStopped(true); | |
191 ExecutePendingJavaScript(extension); | |
192 EXPECT_TRUE(test_tray.IsTrayCastViewVisible()); | |
193 | |
194 // Stop the cast using the UI. | |
195 StopCastWithVerification(extension, &test_tray); | |
196 EXPECT_TRUE(test_tray.IsTraySelectViewVisible()); | |
197 } | |
198 | |
199 // Verifies that the start cast button invokes "launchDesktopMirroring(...)". | |
200 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
201 CastTrayStartButtonStartsCast) { | |
202 const extensions::Extension* extension = LoadCastTestExtension(); | |
203 ExecuteJavaScript(extension, "addReceiver('test_id', 'name')"); | |
204 ash::TrayCast* tray = GetTrayCast(extension); | |
205 StartCastWithVerification(extension, tray, "test_id"); | |
206 } | |
207 | |
208 // Verifies that the CastConfigDelegate opens up a tab called "options.html". | |
209 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, CastTrayOpenOptions) { | |
210 LoadCastTestExtension(); | |
211 | |
212 ash::CastConfigDelegate* cast_config_delegate = | |
213 ash::Shell::GetInstance()->system_tray_delegate()->GetCastConfigDelegate(); | |
214 cast_config_delegate->LaunchCastOptions(); | |
215 | |
216 const GURL url = | |
217 browser()->tab_strip_model()->GetActiveWebContents()->GetURL(); | |
218 EXPECT_TRUE(base::StringPiece(url.GetContent()).ends_with("options.html")); | |
219 } | |
220 | |
221 } // namespace | |
oshima
2015/07/20 23:07:04
we usually put the test bodies outside of anonymou
jdufault
2015/07/21 19:52:44
Done.
| |
OLD | NEW |