Chromium Code Reviews| Index: chrome/browser/ui/ash/system_tray_tray_cast_browsertest_chromeos.cc |
| diff --git a/chrome/browser/ui/ash/system_tray_tray_cast_browsertest_chromeos.cc b/chrome/browser/ui/ash/system_tray_tray_cast_browsertest_chromeos.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c47db341aee7f2e32ff283ee62cd21becf355375 |
| --- /dev/null |
| +++ b/chrome/browser/ui/ash/system_tray_tray_cast_browsertest_chromeos.cc |
| @@ -0,0 +1,227 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ash/shell.h" |
| +#include "ash/system/cast/tray_cast.h" |
| +#include "ash/system/tray/system_tray.h" |
| +#include "ash/system/tray/system_tray_delegate.h" |
| +#include "ash/system/tray/system_tray_item.h" |
| +#include "chrome/browser/extensions/extension_browsertest.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "content/public/browser/render_view_host.h" |
| +#include "content/public/test/test_utils.h" |
| +#include "extensions/browser/process_manager.h" |
| + |
| +namespace chromeos { |
| +namespace { |
| + |
| +// Execute JavaScript within the context of the extension. Returns the result |
| +// of the execution. |
| +scoped_ptr<base::Value> ExecuteJavaScript( |
| + const extensions::Extension* extension, |
| + const std::string& javascript) { |
| + Profile* profile = ProfileManager::GetActiveUserProfile(); |
| + if (!profile) { |
|
achuithb
2015/07/14 18:22:20
Can this actually happen? If not, drop the check
jdufault
2015/07/15 17:35:27
Done.
|
| + LOG(ERROR) << "Expected profile"; |
| + return nullptr; |
| + } |
| + |
| + auto pm = extensions::ProcessManager::Get(profile); |
| + content::RenderViewHost* host = |
| + pm->GetBackgroundHostForExtension(extension->id())->render_view_host(); |
| + return content::ExecuteScriptAndGetValue(host->GetMainFrame(), javascript); |
| +} |
| + |
| +// Ensures that all pending JavaScript execution callbacks are invoked. |
| +void ExecutePendingJavaScript(const extensions::Extension* extension) { |
| + ExecuteJavaScript(extension, ""); |
| +} |
| + |
| +// Invokes tray->StartCastForTest(id) and verifies that launchDesktopMirroring |
| +// was called with the same id. This automatically creates/destroys the detail |
| +// view and notifies the tray that Chrome has begun casting. |
| +void StartCastWithVerification(const extensions::Extension* extension, |
| + ash::TrayCast* tray, |
| + const std::string& id) { |
|
achuithb
2015/07/14 18:22:19
receiver_id
jdufault
2015/07/15 17:35:28
Done.
|
| + ash::SystemTrayItem* system_tray_item = tray; |
| + ash::TrayCastTestMethods* test_tray = tray; |
|
achuithb
2015/07/14 18:22:20
Is this necessary for compilation?
jdufault
2015/07/15 17:35:28
Yes, since the methods are private w.r.t. ash::Tra
achuithb
2015/07/15 18:59:32
Acknowledged.
|
| + |
| + // StartCastForTest will simulate a button click in the detail view to begin |
| + // the cast, so we need to make a detail view available. |
| + system_tray_item->CreateDetailedView(ash::user::LoginStatus::LOGGED_IN_USER); |
| + |
| + // Clear out any old state and execute any pending JS calls created from the |
| + // CreateDetailedView call. |
| + ExecuteJavaScript(extension, "launchDesktopMirroringReceiverId = ''"); |
| + |
| + test_tray->StartCastForTest(id); |
| + |
| + scoped_ptr<base::Value> getLaunchDesktopMirroringId = |
|
achuithb
2015/07/14 18:22:20
variable name should have underscores, not camel c
jdufault
2015/07/15 17:35:27
Done.
|
| + ExecuteJavaScript(extension, "getLaunchDesktopMirroringReceiverId()"); |
| + std::string result; |
| + EXPECT_TRUE(getLaunchDesktopMirroringId->GetAsString(&result)); |
| + EXPECT_EQ(id, result); |
| + system_tray_item->DestroyDetailedView(); |
| + |
| + // Tell the tray item that Chrome has started casting. |
| + test_tray->OnCastingSessionStartedOrStopped(true); |
| + ExecutePendingJavaScript(extension); |
| +} |
| + |
| +// Invokes tray->StopCastForTest() and verifies that stopMirroring('user-stop') |
| +// got called in the extension. |
| +void StopCastWithVerification(const extensions::Extension* extension, |
| + ash::TrayCastTestMethods* tray) { |
| + ExecuteJavaScript(extension, "stopMirroringCalled = false"); |
| + tray->StopCastForTest(); |
| + scoped_ptr<base::Value> wasStopMirroringCalled = |
| + ExecuteJavaScript(extension, "wasStopMirroringCalledWithUserStop()"); |
| + bool result; |
| + EXPECT_TRUE(wasStopMirroringCalled->GetAsBoolean(&result)); |
| + EXPECT_TRUE(result); |
| + |
| + // Tell the tray item that Chrome has stopped casting. |
| + tray->OnCastingSessionStartedOrStopped(false); |
| + ExecutePendingJavaScript(extension); |
| +} |
| + |
| +// Returns the cast tray. The tray initializer may have launched some |
| +// JavaScript callbacks which have not finished executing. |
| +ash::TrayCast* GetTrayCast(const extensions::Extension* extension) { |
| + ash::SystemTray* tray = ash::Shell::GetInstance()->GetPrimarySystemTray(); |
| + // Make sure we actually popup the tray, otherwise the TrayCast instance will |
| + // not be created. |
| + tray->ShowDefaultView(ash::BubbleCreationType::BUBBLE_CREATE_NEW); |
| + |
| + // Creating the tray causes some JavaScript to be executed. Let's try to make |
| + // sure it is completed. |
| + if (extension) |
| + ExecutePendingJavaScript(extension); |
| + |
| + return tray->GetTrayCastForTesting(); |
| +} |
| + |
| +} // namespace |
| + |
| +class SystemTrayTrayCastChromeOSTest : public ExtensionBrowserTest { |
|
achuithb
2015/07/14 18:22:19
This cannot be in the anonymous namespace, is that
jdufault
2015/07/15 17:35:28
Oh, I just was following the pattern of another br
achuithb
2015/07/15 18:59:32
I think you want the IN_PROC_BROWSER_TEST_F to be
jdufault
2015/07/15 19:46:37
I've been testing with
--gtest_filter=SystemTrayT
|
| + protected: |
| + SystemTrayTrayCastChromeOSTest() : ExtensionBrowserTest() {} |
| + ~SystemTrayTrayCastChromeOSTest() override {} |
| + |
| + const extensions::Extension* LoadCastTestExtension() { |
| + return LoadExtension(test_data_dir_.AppendASCII("tray_cast")); |
| + } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(SystemTrayTrayCastChromeOSTest); |
| +}; |
| + |
| +// A simple sanity check to make sure that the cast config delegate actually |
| +// recognizes the cast extension. |
| +IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, |
| + CastTraySanityCheckTestExtensionGetsRecognized) { |
| + ash::CastConfigDelegate* cast_config_delegate = |
| + ash::Shell::GetInstance()->system_tray_delegate()->GetCastConfigDelegate(); |
| + |
| + EXPECT_FALSE(cast_config_delegate->HasCastExtension()); |
| + LoadCastTestExtension(); |
| + EXPECT_TRUE(cast_config_delegate->HasCastExtension()); |
| +} |
| + |
| +// Verifies that the cast tray is hidden when there is no extension installed. |
| +IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, |
| + CastTrayIsHiddenWhenThereIsNoExtension) { |
| + ash::TrayCastTestMethods* tray = GetTrayCast(nullptr); |
| + EXPECT_TRUE(tray->IsTrayInitializedForTest()); |
| + EXPECT_FALSE(tray->IsTrayVisibleForTest()); |
| +} |
| + |
| +// Verifies that the cast tray is hidden if there are no available receivers, |
| +// even if there is an extension installed. |
| +IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, |
| + CastTrayIsHiddenWhenThereIsAnExtensionButNoReceivers) { |
| + const extensions::Extension* extension = LoadCastTestExtension(); |
| + |
| + ash::TrayCastTestMethods* tray = GetTrayCast(extension); |
| + EXPECT_TRUE(tray->IsTrayInitializedForTest()); |
| + EXPECT_FALSE(tray->IsTrayVisibleForTest()); |
| +} |
| + |
| +// Verifies that the cast tray is displayed when there are receivers available. |
| +IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, |
| + CastTrayIsDisplayedWhenThereIsAnExtensionWithReceivers) { |
| + const extensions::Extension* extension = LoadCastTestExtension(); |
| + ExecuteJavaScript(extension, "addReceiver('test_id', 'name')"); |
| + |
| + ash::TrayCastTestMethods* tray = GetTrayCast(extension); |
| + |
| + EXPECT_TRUE(tray->IsTrayInitializedForTest()); |
| + EXPECT_TRUE(tray->IsTrayVisibleForTest()); |
| +} |
| + |
| +// Verifies that we can cast to a specific receiver, stop casting, and then cast |
| +// to another receiver when there is more than one receiver |
| +IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, |
| + CastTrayMultipleReceivers) { |
| + const extensions::Extension* extension = LoadCastTestExtension(); |
| + ExecuteJavaScript(extension, "addReceiver('test_id_1', 'name')"); |
|
achuithb
2015/07/14 18:22:20
Use different names for variety
jdufault
2015/07/15 17:35:27
I want to verify that the having the same name doe
achuithb
2015/07/15 18:59:32
Acknowledged.
|
| + ExecuteJavaScript(extension, "addReceiver('not_used_0', 'name')"); |
| + ExecuteJavaScript(extension, "addReceiver('test_id_0', 'name')"); |
| + ExecuteJavaScript(extension, "addReceiver('not_used_1', 'name')"); |
| + |
| + ash::TrayCast* tray = GetTrayCast(extension); |
| + ash::TrayCastTestMethods* test_tray = tray; |
| + StartCastWithVerification(extension, tray, "test_id_0"); |
| + |
| + EXPECT_TRUE(test_tray->IsTrayCastViewVisibleForTest()); |
| + StopCastWithVerification(extension, GetTrayCast(extension)); |
| + EXPECT_TRUE(test_tray->IsTraySelectViewVisibleForTest()); |
| + |
| + StartCastWithVerification(extension, GetTrayCast(extension), "test_id_1"); |
| + EXPECT_TRUE(test_tray->IsTrayCastViewVisibleForTest()); |
| + StopCastWithVerification(extension, GetTrayCast(extension)); |
| + EXPECT_TRUE(test_tray->IsTraySelectViewVisibleForTest()); |
| +} |
| + |
| +// Verifies the stop cast button invokes the JavaScript function |
| +// "stopMirroring('user-stop')". |
| +IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, |
| + CastTrayStopButtonStopsCast) { |
| + // Add a receiver that is casting. |
| + const extensions::Extension* extension = LoadCastTestExtension(); |
| + ExecuteJavaScript(extension, "addReceiver('test_id', 'name', 'title', 1)"); |
| + |
| + ash::TrayCastTestMethods* test_tray = GetTrayCast(extension); |
| + test_tray->OnCastingSessionStartedOrStopped(true); |
| + ExecutePendingJavaScript(extension); |
| + EXPECT_TRUE(test_tray->IsTrayCastViewVisibleForTest()); |
| + |
| + // Stop the cast using the UI. |
| + StopCastWithVerification(extension, test_tray); |
| + EXPECT_TRUE(test_tray->IsTraySelectViewVisibleForTest()); |
| +} |
| + |
| +// Verifies that the start cast button invokes "launchDesktopMirroring(...)". |
| +IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, |
| + CastTrayStartButtonStartsCast) { |
| + const extensions::Extension* extension = LoadCastTestExtension(); |
| + ExecuteJavaScript(extension, "addReceiver('test_id', 'name')"); |
| + ash::TrayCast* tray = GetTrayCast(extension); |
| + StartCastWithVerification(extension, tray, "test_id"); |
| +} |
| + |
| +// Verifies that the CastConfigDelegate opens up a tab called "options.html". |
| +IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, CastTrayOpenOptions) { |
| + LoadCastTestExtension(); |
| + |
| + ash::CastConfigDelegate* cast_config_delegate = |
| + ash::Shell::GetInstance()->system_tray_delegate()->GetCastConfigDelegate(); |
| + cast_config_delegate->LaunchCastOptions(); |
| + |
| + GURL url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL(); |
|
achuithb
2015/07/14 18:22:20
const
jdufault
2015/07/15 17:35:28
Done.
|
| + EXPECT_TRUE(base::StringPiece(url.GetContent()).ends_with("options.html")); |
| +} |
| + |
| +} // namespace chromeos |