Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / test / remoting / remote_desktop_browsertest.cc
index c8049cd..3d9f4a7 100644 (file)
@@ -5,11 +5,13 @@
 #include "chrome/test/remoting/remote_desktop_browsertest.h"
 
 #include "base/command_line.h"
+#include "base/file_util.h"
+#include "base/json/json_reader.h"
+#include "base/path_service.h"
 #include "chrome/browser/extensions/extension_service.h"
 #include "chrome/browser/extensions/unpacked_installer.h"
 #include "chrome/browser/ui/extensions/application_launch.h"
 #include "chrome/common/chrome_switches.h"
-#include "chrome/common/extensions/extension_file_util.h"
 #include "chrome/test/remoting/key_code_conv.h"
 #include "chrome/test/remoting/page_load_notification_observer.h"
 #include "chrome/test/remoting/waiter.h"
@@ -64,6 +66,20 @@ void RemoteDesktopBrowserTest::VerifyInternetAccess() {
   EXPECT_EQ(GetCurrentURL().host(), "www.google.com");
 }
 
+void RemoteDesktopBrowserTest::OpenClientBrowserPage() {
+  // Open the client browser page in a new tab
+  ui_test_utils::NavigateToURLWithDisposition(
+      browser(),
+      GURL(http_server() + "/clientpage.html"),
+      NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
+
+  // Save this web content for later reference
+  client_web_content_ = browser()->tab_strip_model()->GetActiveWebContents();
+
+  // Go back to the previous tab that has chromoting opened
+  browser()->tab_strip_model()->SelectPreviousTab();
+}
+
 bool RemoteDesktopBrowserTest::HtmlElementVisible(const std::string& name) {
   _ASSERT_TRUE(HtmlElementExists(name));
 
@@ -104,7 +120,7 @@ void RemoteDesktopBrowserTest::InstallChromotingAppUnpacked() {
   installer->set_prompt_for_plugins(false);
 
   content::WindowedNotificationObserver observer(
-      chrome::NOTIFICATION_EXTENSION_LOADED,
+      chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
       content::NotificationService::AllSources());
 
   installer->Load(webapp_unpacked_);
@@ -179,15 +195,17 @@ void RemoteDesktopBrowserTest::LaunchChromotingApp() {
   if (web_contents != active_web_contents())
     web_contents_stack_.push_back(web_contents);
 
+  app_web_content_ = web_contents;
+
   if (is_platform_app()) {
-    EXPECT_EQ(GetFirstShellWindowWebContents(), active_web_contents());
+    EXPECT_EQ(GetFirstAppWindowWebContents(), active_web_contents());
   } else {
     // For apps v1 only, the DOMOperationObserver is not ready at the LOAD_STOP
     // event. A half second wait is necessary for the subsequent javascript
     // injection to work.
     // TODO(weitaosu): Find out whether there is a more appropriate notification
     // to wait for so we can get rid of this wait.
-    ASSERT_TRUE(TimeoutWaiter(base::TimeDelta::FromMilliseconds(500)).Wait());
+    ASSERT_TRUE(TimeoutWaiter(base::TimeDelta::FromSeconds(5)).Wait());
   }
 
   EXPECT_EQ(Chromoting_Main_URL(), GetCurrentURL());
@@ -265,7 +283,7 @@ void RemoteDesktopBrowserTest::Approve() {
     // TODO(weitaosu): Revoke the permission at the beginning of the test so
     // that we can test first-time experience here.
     ConditionalTimeoutWaiter waiter(
-        base::TimeDelta::FromSeconds(3),
+        base::TimeDelta::FromSeconds(7),
         base::TimeDelta::FromSeconds(1),
         base::Bind(
             &RemoteDesktopBrowserTest::IsAuthenticatedInWindow,
@@ -453,6 +471,12 @@ void RemoteDesktopBrowserTest::Cleanup() {
     UninstallChromotingApp();
     VerifyChromotingLoaded(false);
   }
+
+  // TODO(chaitali): Remove this additional timeout after we figure out
+  // why this is needed for the v1 app to work.
+  // Without this timeout the test fail with a "CloseWebContents called for
+  // tab not in our strip" error for the v1 app.
+  ASSERT_TRUE(TimeoutWaiter(base::TimeDelta::FromSeconds(2)).Wait());
 }
 
 void RemoteDesktopBrowserTest::Auth() {
@@ -545,6 +569,7 @@ void RemoteDesktopBrowserTest::ParseCommandLine() {
   me2me_pin_ = command_line->GetSwitchValueASCII(kMe2MePin);
   remote_host_name_ = command_line->GetSwitchValueASCII(kRemoteHostName);
   extension_name_ = command_line->GetSwitchValueASCII(kExtensionName);
+  http_server_ = command_line->GetSwitchValueASCII(kHttpServer);
 
   no_cleanup_ = command_line->HasSwitch(kNoCleanup);
   no_install_ = command_line->HasSwitch(kNoInstall);
@@ -619,6 +644,58 @@ std::string RemoteDesktopBrowserTest::ExecuteScriptAndExtractString(
   return result;
 }
 
+// static
+bool RemoteDesktopBrowserTest::LoadScript(
+    content::WebContents* web_contents,
+    const base::FilePath::StringType& path) {
+  std::string script;
+  base::FilePath src_dir;
+  _ASSERT_TRUE(PathService::Get(base::DIR_EXE, &src_dir));
+  base::FilePath script_path = src_dir.Append(path);
+
+  if (!base::ReadFileToString(script_path, &script)) {
+    LOG(ERROR) << "Failed to load script " << script_path.value();
+    return false;
+  }
+
+  return content::ExecuteScript(web_contents, script);
+}
+
+// static
+void RemoteDesktopBrowserTest::RunJavaScriptTest(
+    content::WebContents* web_contents,
+    const std::string& testName,
+    const std::string& testData) {
+  std::string result;
+  std::string script = "browserTest.runTest(browserTest." + testName + ", " +
+                       testData + ");";
+
+  LOG(INFO) << "Executing " << script;
+
+  ASSERT_TRUE(
+      content::ExecuteScriptAndExtractString(web_contents, script, &result));
+
+  // Read in the JSON
+  base::JSONReader reader;
+  scoped_ptr<base::Value> value;
+  value.reset(reader.Read(result, base::JSON_ALLOW_TRAILING_COMMAS));
+
+  // Convert to dictionary
+  base::DictionaryValue* dict_value = NULL;
+  ASSERT_TRUE(value->GetAsDictionary(&dict_value));
+
+  bool succeeded;
+  std::string error_message;
+  std::string stack_trace;
+
+  // Extract the fields
+  ASSERT_TRUE(dict_value->GetBoolean("succeeded", &succeeded));
+  ASSERT_TRUE(dict_value->GetString("error_message", &error_message));
+  ASSERT_TRUE(dict_value->GetString("stack_trace", &stack_trace));
+
+  EXPECT_TRUE(succeeded) << error_message << "\n" << stack_trace;
+}
+
 void RemoteDesktopBrowserTest::ClickOnControl(const std::string& name) {
   ASSERT_TRUE(HtmlElementVisible(name));
 
@@ -706,4 +783,13 @@ bool RemoteDesktopBrowserTest::IsAuthenticatedInWindow(
       web_contents, "remoting.identity.isAuthenticated()");
 }
 
+// static
+bool RemoteDesktopBrowserTest::IsHostActionComplete(
+    content::WebContents* client_web_content,
+    std::string host_action_var) {
+  return ExecuteScriptAndExtractBool(
+      client_web_content,
+      host_action_var);
+}
+
 }  // namespace remoting