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