| 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/common/system/cast/tray_cast.h" | |
| 6 #include "ash/common/system/tray/system_tray.h" | |
| 7 #include "ash/common/system/tray/system_tray_delegate.h" | |
| 8 #include "ash/common/system/tray/system_tray_item.h" | |
| 9 #include "ash/common/wm_shell.h" | |
| 10 #include "ash/shell.h" | |
| 11 #include "ash/test/tray_cast_test_api.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "chrome/browser/extensions/extension_browsertest.h" | |
| 15 #include "chrome/browser/profiles/profile_manager.h" | |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 17 #include "content/public/browser/render_view_host.h" | |
| 18 #include "content/public/test/test_utils.h" | |
| 19 #include "extensions/browser/process_manager.h" | |
| 20 #include "extensions/common/feature_switch.h" | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // Execute JavaScript within the context of the extension. Returns the result | |
| 25 // of the execution. | |
| 26 std::unique_ptr<base::Value> ExecuteJavaScript( | |
| 27 const extensions::Extension* extension, | |
| 28 const std::string& javascript) { | |
| 29 extensions::ProcessManager* pm = | |
| 30 extensions::ProcessManager::Get(ProfileManager::GetActiveUserProfile()); | |
| 31 content::RenderViewHost* host = | |
| 32 pm->GetBackgroundHostForExtension(extension->id())->render_view_host(); | |
| 33 return content::ExecuteScriptAndGetValue(host->GetMainFrame(), javascript); | |
| 34 } | |
| 35 | |
| 36 // Returns the current value within a global JavaScript variable. | |
| 37 std::unique_ptr<base::Value> GetJavaScriptVariable( | |
| 38 const extensions::Extension* extension, | |
| 39 const std::string& variable) { | |
| 40 return ExecuteJavaScript(extension, | |
| 41 "(function() { return " + variable + "; })()"); | |
| 42 } | |
| 43 | |
| 44 bool GetJavaScriptStringVariable(const extensions::Extension* extension, | |
| 45 const std::string& variable, | |
| 46 std::string* result) { | |
| 47 std::unique_ptr<base::Value> value = | |
| 48 GetJavaScriptVariable(extension, variable); | |
| 49 return value->GetAsString(result); | |
| 50 } | |
| 51 | |
| 52 bool GetJavaScriptBooleanVariable(const extensions::Extension* extension, | |
| 53 const std::string& variable, | |
| 54 bool* result) { | |
| 55 std::unique_ptr<base::Value> value = | |
| 56 GetJavaScriptVariable(extension, variable); | |
| 57 return value->GetAsBoolean(result); | |
| 58 } | |
| 59 | |
| 60 // Ensures that all pending JavaScript execution callbacks are invoked. | |
| 61 void ExecutePendingJavaScript(const extensions::Extension* extension) { | |
| 62 ExecuteJavaScript(extension, std::string()); | |
| 63 } | |
| 64 | |
| 65 // Invokes tray->StartCast(id) and returns true if launchDesktopMirroring was | |
| 66 // called with the same id. This automatically creates/destroys the detail view | |
| 67 // and notifies the tray that Chrome has begun casting. | |
| 68 bool StartCastWithVerification(const extensions::Extension* extension, | |
| 69 ash::TrayCast* tray, | |
| 70 const std::string& receiver_id) { | |
| 71 ash::SystemTrayItem* system_tray_item = tray; | |
| 72 ash::TrayCastTestAPI test_tray(tray); | |
| 73 | |
| 74 // We will simulate a button click in the detail view to begin the cast, so we | |
| 75 // need to make a detail view available. | |
| 76 std::unique_ptr<views::View> detailed_view = base::WrapUnique( | |
| 77 system_tray_item->CreateDetailedView(ash::LoginStatus::USER)); | |
| 78 | |
| 79 // Clear out any old state and execute any pending JS calls created from the | |
| 80 // CreateDetailedView call. | |
| 81 ExecuteJavaScript(extension, "launchDesktopMirroringReceiverId = ''"); | |
| 82 | |
| 83 // Tell the tray item that Chrome has started casting. | |
| 84 test_tray.StartCast(receiver_id); | |
| 85 test_tray.OnCastingSessionStartedOrStopped(true); | |
| 86 | |
| 87 system_tray_item->DestroyDetailedView(); | |
| 88 detailed_view.reset(); | |
| 89 | |
| 90 std::string got_receiver_id; | |
| 91 if (!GetJavaScriptStringVariable( | |
| 92 extension, "launchDesktopMirroringReceiverId", &got_receiver_id)) | |
| 93 return false; | |
| 94 return receiver_id == got_receiver_id; | |
| 95 } | |
| 96 | |
| 97 // Invokes tray->StopCast() and returns true if stopMirroring('user-stop') | |
| 98 // was called in the extension. | |
| 99 bool StopCastWithVerification(const extensions::Extension* extension, | |
| 100 ash::TrayCastTestAPI* tray) { | |
| 101 // Clear out any old state so we can be sure that we set the value here. | |
| 102 ExecuteJavaScript(extension, "stopMirroringCalled = false"); | |
| 103 | |
| 104 // Stop casting. | |
| 105 tray->StopCast(); | |
| 106 tray->OnCastingSessionStartedOrStopped(false); | |
| 107 | |
| 108 bool result; | |
| 109 if (!GetJavaScriptBooleanVariable(extension, "stopMirroringCalled", &result)) | |
| 110 return false; | |
| 111 return result; | |
| 112 } | |
| 113 | |
| 114 // Returns the cast tray. The tray initializer may have launched some | |
| 115 // JavaScript callbacks which have not finished executing. | |
| 116 ash::TrayCast* GetTrayCast(const extensions::Extension* extension) { | |
| 117 ash::SystemTray* tray = ash::Shell::GetInstance()->GetPrimarySystemTray(); | |
| 118 | |
| 119 // Make sure we actually popup the tray, otherwise the TrayCast instance will | |
| 120 // not be created. | |
| 121 tray->ShowDefaultView(ash::BubbleCreationType::BUBBLE_CREATE_NEW); | |
| 122 | |
| 123 // Creating the tray causes some JavaScript to be executed. Let's try to make | |
| 124 // sure it is completed. | |
| 125 if (extension) | |
| 126 ExecutePendingJavaScript(extension); | |
| 127 | |
| 128 return tray->GetTrayCastForTesting(); | |
| 129 } | |
| 130 | |
| 131 class SystemTrayTrayCastChromeOSTest : public ExtensionBrowserTest { | |
| 132 protected: | |
| 133 SystemTrayTrayCastChromeOSTest() : ExtensionBrowserTest() {} | |
| 134 ~SystemTrayTrayCastChromeOSTest() override {} | |
| 135 | |
| 136 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 137 // SystemTrayTrayCastChromeOSTest tests the behavior of the system tray | |
| 138 // without Media Router, so we explicitly disable it. | |
| 139 ExtensionBrowserTest::SetUpCommandLine(command_line); | |
| 140 override_media_router_.reset(new extensions::FeatureSwitch::ScopedOverride( | |
| 141 extensions::FeatureSwitch::media_router(), false)); | |
| 142 } | |
| 143 | |
| 144 const extensions::Extension* LoadCastTestExtension() { | |
| 145 return LoadExtension(test_data_dir_.AppendASCII("tray_cast")); | |
| 146 } | |
| 147 | |
| 148 private: | |
| 149 std::unique_ptr<extensions::FeatureSwitch::ScopedOverride> | |
| 150 override_media_router_; | |
| 151 DISALLOW_COPY_AND_ASSIGN(SystemTrayTrayCastChromeOSTest); | |
| 152 }; | |
| 153 | |
| 154 } // namespace | |
| 155 | |
| 156 namespace chromeos { | |
| 157 | |
| 158 // A simple sanity check to make sure that the cast config delegate actually | |
| 159 // recognizes the cast extension. | |
| 160 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
| 161 CastTraySanityCheckTestExtensionGetsRecognized) { | |
| 162 ash::CastConfigDelegate* cast_config_delegate = | |
| 163 ash::WmShell::Get()->system_tray_delegate()->GetCastConfigDelegate(); | |
| 164 | |
| 165 EXPECT_FALSE(cast_config_delegate->HasCastExtension()); | |
| 166 const extensions::Extension* extension = LoadCastTestExtension(); | |
| 167 EXPECT_TRUE(cast_config_delegate->HasCastExtension()); | |
| 168 UninstallExtension(extension->id()); | |
| 169 } | |
| 170 | |
| 171 // Verifies that the cast tray is hidden when there is no extension installed. | |
| 172 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
| 173 CastTrayIsHiddenWhenThereIsNoExtension) { | |
| 174 ash::TrayCastTestAPI tray(GetTrayCast(nullptr)); | |
| 175 EXPECT_TRUE(tray.IsTrayInitialized()); | |
| 176 EXPECT_FALSE(tray.IsTrayVisible()); | |
| 177 } | |
| 178 | |
| 179 // Verifies that the cast tray is hidden if there are no available receivers, | |
| 180 // even if there is an extension installed. | |
| 181 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
| 182 CastTrayIsHiddenWhenThereIsAnExtensionButNoReceivers) { | |
| 183 const extensions::Extension* extension = LoadCastTestExtension(); | |
| 184 | |
| 185 ash::TrayCastTestAPI tray(GetTrayCast(extension)); | |
| 186 EXPECT_TRUE(tray.IsTrayInitialized()); | |
| 187 EXPECT_FALSE(tray.IsTrayVisible()); | |
| 188 | |
| 189 UninstallExtension(extension->id()); | |
| 190 } | |
| 191 | |
| 192 // Verifies that the cast tray is hidden if there are no available receivers, | |
| 193 // even if there is an extension installed that doesn't honor the private API. | |
| 194 IN_PROC_BROWSER_TEST_F( | |
| 195 SystemTrayTrayCastChromeOSTest, | |
| 196 CastTrayIsHiddenWhenThereIsAnOldExtensionButNoReceivers) { | |
| 197 const extensions::Extension* extension = LoadCastTestExtension(); | |
| 198 | |
| 199 // Remove the updateDevices listener. This means that the extension will | |
| 200 // act like an extension before the private-API migration. | |
| 201 ExecuteJavaScript(extension, | |
| 202 "chrome.cast.devicesPrivate.updateDevicesRequested." | |
| 203 "removeListener(sendDevices);"); | |
| 204 | |
| 205 ash::TrayCastTestAPI tray(GetTrayCast(extension)); | |
| 206 EXPECT_TRUE(tray.IsTrayInitialized()); | |
| 207 EXPECT_FALSE(tray.IsTrayVisible()); | |
| 208 | |
| 209 UninstallExtension(extension->id()); | |
| 210 } | |
| 211 | |
| 212 // Verifies that the cast tray is displayed when there are receivers available. | |
| 213 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
| 214 CastTrayIsDisplayedWhenThereIsAnExtensionWithReceivers) { | |
| 215 const extensions::Extension* extension = LoadCastTestExtension(); | |
| 216 ExecuteJavaScript(extension, "addReceiver('test_id', 'name')"); | |
| 217 | |
| 218 ash::TrayCastTestAPI tray(GetTrayCast(extension)); | |
| 219 | |
| 220 EXPECT_TRUE(tray.IsTrayInitialized()); | |
| 221 EXPECT_TRUE(tray.IsTrayVisible()); | |
| 222 | |
| 223 UninstallExtension(extension->id()); | |
| 224 } | |
| 225 | |
| 226 // Verifies that we can cast to a specific receiver, stop casting, and then cast | |
| 227 // to another receiver when there is more than one receiver | |
| 228 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
| 229 CastTrayMultipleReceivers) { | |
| 230 const extensions::Extension* extension = LoadCastTestExtension(); | |
| 231 ExecuteJavaScript(extension, "addReceiver('test_id_1', 'name')"); | |
| 232 ExecuteJavaScript(extension, "addReceiver('not_used_0', 'name1')"); | |
| 233 ExecuteJavaScript(extension, "addReceiver('test_id_0', 'name')"); | |
| 234 ExecuteJavaScript(extension, "addReceiver('not_used_1', 'name2')"); | |
| 235 | |
| 236 ash::TrayCast* tray = GetTrayCast(extension); | |
| 237 ash::TrayCastTestAPI test_tray(tray); | |
| 238 EXPECT_TRUE(StartCastWithVerification(extension, tray, "test_id_0")); | |
| 239 | |
| 240 EXPECT_TRUE(test_tray.IsTrayCastViewVisible()); | |
| 241 EXPECT_TRUE(StopCastWithVerification(extension, &test_tray)); | |
| 242 EXPECT_TRUE(test_tray.IsTraySelectViewVisible()); | |
| 243 | |
| 244 EXPECT_TRUE(StartCastWithVerification(extension, tray, "test_id_1")); | |
| 245 EXPECT_TRUE(test_tray.IsTrayCastViewVisible()); | |
| 246 EXPECT_TRUE(StopCastWithVerification(extension, &test_tray)); | |
| 247 EXPECT_TRUE(test_tray.IsTraySelectViewVisible()); | |
| 248 | |
| 249 UninstallExtension(extension->id()); | |
| 250 } | |
| 251 | |
| 252 // Verifies the stop cast button invokes the JavaScript function | |
| 253 // "stopMirroring('user-stop')". | |
| 254 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
| 255 CastTrayStopButtonStopsCast) { | |
| 256 // Add a receiver that is casting. | |
| 257 const extensions::Extension* extension = LoadCastTestExtension(); | |
| 258 ExecuteJavaScript(extension, "addReceiver('test_id', 'name', 'title', 1)"); | |
| 259 | |
| 260 ash::TrayCastTestAPI test_tray(GetTrayCast(extension)); | |
| 261 test_tray.OnCastingSessionStartedOrStopped(true); | |
| 262 ExecutePendingJavaScript(extension); | |
| 263 EXPECT_TRUE(test_tray.IsTrayCastViewVisible()); | |
| 264 | |
| 265 // Stop the cast using the UI. | |
| 266 EXPECT_TRUE(StopCastWithVerification(extension, &test_tray)); | |
| 267 EXPECT_TRUE(test_tray.IsTraySelectViewVisible()); | |
| 268 | |
| 269 UninstallExtension(extension->id()); | |
| 270 } | |
| 271 | |
| 272 // Verifies that the start cast button invokes "launchDesktopMirroring(...)". | |
| 273 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
| 274 CastTrayStartButtonStartsCast) { | |
| 275 const extensions::Extension* extension = LoadCastTestExtension(); | |
| 276 ExecuteJavaScript(extension, "addReceiver('test_id', 'name')"); | |
| 277 ash::TrayCast* tray = GetTrayCast(extension); | |
| 278 EXPECT_TRUE(StartCastWithVerification(extension, tray, "test_id")); | |
| 279 UninstallExtension(extension->id()); | |
| 280 } | |
| 281 | |
| 282 // Verifies that the CastConfigDelegate opens up a tab called "options.html". | |
| 283 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, CastTrayOpenOptions) { | |
| 284 const extensions::Extension* extension = LoadCastTestExtension(); | |
| 285 | |
| 286 ash::CastConfigDelegate* cast_config_delegate = | |
| 287 ash::WmShell::Get()->system_tray_delegate()->GetCastConfigDelegate(); | |
| 288 cast_config_delegate->LaunchCastOptions(); | |
| 289 | |
| 290 const GURL url = | |
| 291 browser()->tab_strip_model()->GetActiveWebContents()->GetURL(); | |
| 292 EXPECT_TRUE(base::StringPiece(url.GetContent()).ends_with("options.html")); | |
| 293 | |
| 294 UninstallExtension(extension->id()); | |
| 295 } | |
| 296 | |
| 297 } // namespace chromeos | |
| OLD | NEW |