Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / ash / system_tray_tray_cast_browsertest_chromeos.cc
blob27663577141c7c70f74e70629cbfbc877cb66cf4
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 "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"
18 namespace {
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 extensions::ProcessManager* pm =
26 extensions::ProcessManager::Get(ProfileManager::GetActiveUserProfile());
27 content::RenderViewHost* host =
28 pm->GetBackgroundHostForExtension(extension->id())->render_view_host();
29 return content::ExecuteScriptAndGetValue(host->GetMainFrame(), javascript);
32 // Returns the current value within a global JavaScript variable.
33 scoped_ptr<base::Value> GetJavaScriptVariable(
34 const extensions::Extension* extension,
35 const std::string& variable) {
36 return ExecuteJavaScript(extension,
37 "(function() { return " + variable + "; })()");
40 bool GetJavaScriptStringVariable(const extensions::Extension* extension,
41 const std::string& variable,
42 std::string* result) {
43 scoped_ptr<base::Value> value = GetJavaScriptVariable(extension, variable);
44 return value->GetAsString(result);
47 bool GetJavaScriptBooleanVariable(const extensions::Extension* extension,
48 const std::string& variable,
49 bool* result) {
50 scoped_ptr<base::Value> value = GetJavaScriptVariable(extension, variable);
51 return value->GetAsBoolean(result);
54 // Ensures that all pending JavaScript execution callbacks are invoked.
55 void ExecutePendingJavaScript(const extensions::Extension* extension) {
56 ExecuteJavaScript(extension, std::string());
59 // Invokes tray->StartCast(id) and returns true if launchDesktopMirroring was
60 // called with the same id. This automatically creates/destroys the detail view
61 // and notifies the tray that Chrome has begun casting.
62 bool StartCastWithVerification(const extensions::Extension* extension,
63 ash::TrayCast* tray,
64 const std::string& receiver_id) {
65 ash::SystemTrayItem* system_tray_item = tray;
66 ash::TrayCastTestAPI test_tray(tray);
68 // We will simulate a button click in the detail view to begin the cast, so we
69 // need to make a detail view available.
70 scoped_ptr<views::View> detailed_view =
71 make_scoped_ptr(system_tray_item->CreateDetailedView(
72 ash::user::LoginStatus::LOGGED_IN_USER));
74 // Clear out any old state and execute any pending JS calls created from the
75 // CreateDetailedView call.
76 ExecuteJavaScript(extension, "launchDesktopMirroringReceiverId = ''");
78 // Tell the tray item that Chrome has started casting.
79 test_tray.StartCast(receiver_id);
80 test_tray.OnCastingSessionStartedOrStopped(true);
82 system_tray_item->DestroyDetailedView();
83 detailed_view.reset();
85 std::string got_receiver_id;
86 if (!GetJavaScriptStringVariable(
87 extension, "launchDesktopMirroringReceiverId", &got_receiver_id))
88 return false;
89 return receiver_id == got_receiver_id;
92 // Invokes tray->StopCast() and returns true if stopMirroring('user-stop')
93 // was called in the extension.
94 bool StopCastWithVerification(const extensions::Extension* extension,
95 ash::TrayCastTestAPI* tray) {
96 // Clear out any old state so we can be sure that we set the value here.
97 ExecuteJavaScript(extension, "stopMirroringCalled = false");
99 // Stop casting.
100 tray->StopCast();
101 tray->OnCastingSessionStartedOrStopped(false);
103 bool result;
104 if (!GetJavaScriptBooleanVariable(extension, "stopMirroringCalled", &result))
105 return false;
106 return result;
109 // Returns the cast tray. The tray initializer may have launched some
110 // JavaScript callbacks which have not finished executing.
111 ash::TrayCast* GetTrayCast(const extensions::Extension* extension) {
112 ash::SystemTray* tray = ash::Shell::GetInstance()->GetPrimarySystemTray();
114 // Make sure we actually popup the tray, otherwise the TrayCast instance will
115 // not be created.
116 tray->ShowDefaultView(ash::BubbleCreationType::BUBBLE_CREATE_NEW);
118 // Creating the tray causes some JavaScript to be executed. Let's try to make
119 // sure it is completed.
120 if (extension)
121 ExecutePendingJavaScript(extension);
123 return tray->GetTrayCastForTesting();
126 class SystemTrayTrayCastChromeOSTest : public ExtensionBrowserTest {
127 protected:
128 SystemTrayTrayCastChromeOSTest() : ExtensionBrowserTest() {}
129 ~SystemTrayTrayCastChromeOSTest() override {}
131 const extensions::Extension* LoadCastTestExtension() {
132 return LoadExtension(test_data_dir_.AppendASCII("tray_cast"));
135 private:
136 DISALLOW_COPY_AND_ASSIGN(SystemTrayTrayCastChromeOSTest);
139 } // namespace
141 namespace chromeos {
143 // A simple sanity check to make sure that the cast config delegate actually
144 // recognizes the cast extension.
145 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest,
146 CastTraySanityCheckTestExtensionGetsRecognized) {
147 ash::CastConfigDelegate* cast_config_delegate = ash::Shell::GetInstance()
148 ->system_tray_delegate()
149 ->GetCastConfigDelegate();
151 EXPECT_FALSE(cast_config_delegate->HasCastExtension());
152 const extensions::Extension* extension = LoadCastTestExtension();
153 EXPECT_TRUE(cast_config_delegate->HasCastExtension());
154 UninstallExtension(extension->id());
157 // Verifies that the cast tray is hidden when there is no extension installed.
158 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest,
159 CastTrayIsHiddenWhenThereIsNoExtension) {
160 ash::TrayCastTestAPI tray(GetTrayCast(nullptr));
161 EXPECT_TRUE(tray.IsTrayInitialized());
162 EXPECT_FALSE(tray.IsTrayVisible());
165 // Verifies that the cast tray is hidden if there are no available receivers,
166 // even if there is an extension installed.
167 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest,
168 CastTrayIsHiddenWhenThereIsAnExtensionButNoReceivers) {
169 const extensions::Extension* extension = LoadCastTestExtension();
171 ash::TrayCastTestAPI tray(GetTrayCast(extension));
172 EXPECT_TRUE(tray.IsTrayInitialized());
173 EXPECT_FALSE(tray.IsTrayVisible());
175 UninstallExtension(extension->id());
178 // Verifies that the cast tray is displayed when there are receivers available.
179 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest,
180 CastTrayIsDisplayedWhenThereIsAnExtensionWithReceivers) {
181 const extensions::Extension* extension = LoadCastTestExtension();
182 ExecuteJavaScript(extension, "addReceiver('test_id', 'name')");
184 ash::TrayCastTestAPI tray(GetTrayCast(extension));
186 EXPECT_TRUE(tray.IsTrayInitialized());
187 EXPECT_TRUE(tray.IsTrayVisible());
189 UninstallExtension(extension->id());
192 // Verifies that we can cast to a specific receiver, stop casting, and then cast
193 // to another receiver when there is more than one receiver
194 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest,
195 CastTrayMultipleReceivers) {
196 const extensions::Extension* extension = LoadCastTestExtension();
197 ExecuteJavaScript(extension, "addReceiver('test_id_1', 'name')");
198 ExecuteJavaScript(extension, "addReceiver('not_used_0', 'name1')");
199 ExecuteJavaScript(extension, "addReceiver('test_id_0', 'name')");
200 ExecuteJavaScript(extension, "addReceiver('not_used_1', 'name2')");
202 ash::TrayCast* tray = GetTrayCast(extension);
203 ash::TrayCastTestAPI test_tray(tray);
204 EXPECT_TRUE(StartCastWithVerification(extension, tray, "test_id_0"));
206 EXPECT_TRUE(test_tray.IsTrayCastViewVisible());
207 EXPECT_TRUE(StopCastWithVerification(extension, &test_tray));
208 EXPECT_TRUE(test_tray.IsTraySelectViewVisible());
210 EXPECT_TRUE(StartCastWithVerification(extension, tray, "test_id_1"));
211 EXPECT_TRUE(test_tray.IsTrayCastViewVisible());
212 EXPECT_TRUE(StopCastWithVerification(extension, &test_tray));
213 EXPECT_TRUE(test_tray.IsTraySelectViewVisible());
215 UninstallExtension(extension->id());
218 // Verifies the stop cast button invokes the JavaScript function
219 // "stopMirroring('user-stop')".
220 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest,
221 CastTrayStopButtonStopsCast) {
222 // Add a receiver that is casting.
223 const extensions::Extension* extension = LoadCastTestExtension();
224 ExecuteJavaScript(extension, "addReceiver('test_id', 'name', 'title', 1)");
226 ash::TrayCastTestAPI test_tray(GetTrayCast(extension));
227 test_tray.OnCastingSessionStartedOrStopped(true);
228 ExecutePendingJavaScript(extension);
229 EXPECT_TRUE(test_tray.IsTrayCastViewVisible());
231 // Stop the cast using the UI.
232 EXPECT_TRUE(StopCastWithVerification(extension, &test_tray));
233 EXPECT_TRUE(test_tray.IsTraySelectViewVisible());
235 UninstallExtension(extension->id());
238 // Verifies that the start cast button invokes "launchDesktopMirroring(...)".
239 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest,
240 CastTrayStartButtonStartsCast) {
241 const extensions::Extension* extension = LoadCastTestExtension();
242 ExecuteJavaScript(extension, "addReceiver('test_id', 'name')");
243 ash::TrayCast* tray = GetTrayCast(extension);
244 EXPECT_TRUE(StartCastWithVerification(extension, tray, "test_id"));
245 UninstallExtension(extension->id());
248 // Verifies that the CastConfigDelegate opens up a tab called "options.html".
249 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, CastTrayOpenOptions) {
250 const extensions::Extension* extension = LoadCastTestExtension();
252 ash::CastConfigDelegate* cast_config_delegate = ash::Shell::GetInstance()
253 ->system_tray_delegate()
254 ->GetCastConfigDelegate();
255 cast_config_delegate->LaunchCastOptions();
257 const GURL url =
258 browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
259 EXPECT_TRUE(base::StringPiece(url.GetContent()).ends_with("options.html"));
261 UninstallExtension(extension->id());
264 } // namespace chromeos