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(); |
| 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 const extensions::Extension* extension = LoadCastTestExtension(); |
| 131 EXPECT_TRUE(cast_config_delegate->HasCastExtension()); |
| 132 UninstallExtension(extension->id()); |
| 133 } |
| 134 |
| 135 // Verifies that the cast tray is hidden when there is no extension installed. |
| 136 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, |
| 137 CastTrayIsHiddenWhenThereIsNoExtension) { |
| 138 ash::TrayCastTestAPI tray(GetTrayCast(nullptr)); |
| 139 EXPECT_TRUE(tray.IsTrayInitialized()); |
| 140 EXPECT_FALSE(tray.IsTrayVisible()); |
| 141 } |
| 142 |
| 143 // Verifies that the cast tray is hidden if there are no available receivers, |
| 144 // even if there is an extension installed. |
| 145 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, |
| 146 CastTrayIsHiddenWhenThereIsAnExtensionButNoReceivers) { |
| 147 const extensions::Extension* extension = LoadCastTestExtension(); |
| 148 |
| 149 ash::TrayCastTestAPI tray(GetTrayCast(extension)); |
| 150 EXPECT_TRUE(tray.IsTrayInitialized()); |
| 151 EXPECT_FALSE(tray.IsTrayVisible()); |
| 152 |
| 153 UninstallExtension(extension->id()); |
| 154 } |
| 155 |
| 156 // Verifies that the cast tray is displayed when there are receivers available. |
| 157 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, |
| 158 CastTrayIsDisplayedWhenThereIsAnExtensionWithReceivers) { |
| 159 const extensions::Extension* extension = LoadCastTestExtension(); |
| 160 ExecuteJavaScript(extension, "addReceiver('test_id', 'name')"); |
| 161 |
| 162 ash::TrayCastTestAPI tray(GetTrayCast(extension)); |
| 163 |
| 164 EXPECT_TRUE(tray.IsTrayInitialized()); |
| 165 EXPECT_TRUE(tray.IsTrayVisible()); |
| 166 |
| 167 UninstallExtension(extension->id()); |
| 168 } |
| 169 |
| 170 // Verifies that we can cast to a specific receiver, stop casting, and then cast |
| 171 // to another receiver when there is more than one receiver |
| 172 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, |
| 173 CastTrayMultipleReceivers) { |
| 174 const extensions::Extension* extension = LoadCastTestExtension(); |
| 175 ExecuteJavaScript(extension, "addReceiver('test_id_1', 'name')"); |
| 176 ExecuteJavaScript(extension, "addReceiver('not_used_0', 'name1')"); |
| 177 ExecuteJavaScript(extension, "addReceiver('test_id_0', 'name')"); |
| 178 ExecuteJavaScript(extension, "addReceiver('not_used_1', 'name2')"); |
| 179 |
| 180 ash::TrayCast* tray = GetTrayCast(extension); |
| 181 ash::TrayCastTestAPI test_tray(tray); |
| 182 EXPECT_TRUE(StartCastWithVerification(extension, tray, "test_id_0")); |
| 183 |
| 184 EXPECT_TRUE(test_tray.IsTrayCastViewVisible()); |
| 185 EXPECT_TRUE(StopCastWithVerification(extension, &test_tray)); |
| 186 EXPECT_TRUE(test_tray.IsTraySelectViewVisible()); |
| 187 |
| 188 EXPECT_TRUE(StartCastWithVerification(extension, tray, "test_id_1")); |
| 189 EXPECT_TRUE(test_tray.IsTrayCastViewVisible()); |
| 190 EXPECT_TRUE(StopCastWithVerification(extension, &test_tray)); |
| 191 EXPECT_TRUE(test_tray.IsTraySelectViewVisible()); |
| 192 |
| 193 UninstallExtension(extension->id()); |
| 194 } |
| 195 |
| 196 // Verifies the stop cast button invokes the JavaScript function |
| 197 // "stopMirroring('user-stop')". |
| 198 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, |
| 199 CastTrayStopButtonStopsCast) { |
| 200 // Add a receiver that is casting. |
| 201 const extensions::Extension* extension = LoadCastTestExtension(); |
| 202 ExecuteJavaScript(extension, "addReceiver('test_id', 'name', 'title', 1)"); |
| 203 |
| 204 ash::TrayCastTestAPI test_tray(GetTrayCast(extension)); |
| 205 test_tray.OnCastingSessionStartedOrStopped(true); |
| 206 ExecutePendingJavaScript(extension); |
| 207 EXPECT_TRUE(test_tray.IsTrayCastViewVisible()); |
| 208 |
| 209 // Stop the cast using the UI. |
| 210 EXPECT_TRUE(StopCastWithVerification(extension, &test_tray)); |
| 211 EXPECT_TRUE(test_tray.IsTraySelectViewVisible()); |
| 212 |
| 213 UninstallExtension(extension->id()); |
| 214 } |
| 215 |
| 216 // Verifies that the start cast button invokes "launchDesktopMirroring(...)". |
| 217 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, |
| 218 CastTrayStartButtonStartsCast) { |
| 219 const extensions::Extension* extension = LoadCastTestExtension(); |
| 220 ExecuteJavaScript(extension, "addReceiver('test_id', 'name')"); |
| 221 ash::TrayCast* tray = GetTrayCast(extension); |
| 222 EXPECT_TRUE(StartCastWithVerification(extension, tray, "test_id")); |
| 223 UninstallExtension(extension->id()); |
| 224 } |
| 225 |
| 226 // Verifies that the CastConfigDelegate opens up a tab called "options.html". |
| 227 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, CastTrayOpenOptions) { |
| 228 const extensions::Extension* extension = LoadCastTestExtension(); |
| 229 |
| 230 ash::CastConfigDelegate* cast_config_delegate = |
| 231 ash::Shell::GetInstance()->system_tray_delegate()->GetCastConfigDelegate(); |
| 232 cast_config_delegate->LaunchCastOptions(); |
| 233 |
| 234 const GURL url = |
| 235 browser()->tab_strip_model()->GetActiveWebContents()->GetURL(); |
| 236 EXPECT_TRUE(base::StringPiece(url.GetContent()).ends_with("options.html")); |
| 237 |
| 238 UninstallExtension(extension->id()); |
| 239 } |
| 240 |
| 241 } // namespace chromeos |
OLD | NEW |