Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / webui / web_ui_mojo_browsertest.cc
index ccef178..039db85 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+#include <limits>
+
 #include "base/command_line.h"
 #include "base/file_util.h"
 #include "base/files/file_path.h"
@@ -9,7 +11,6 @@
 #include "base/run_loop.h"
 #include "base/strings/string_util.h"
 #include "content/browser/webui/web_ui_controller_factory_registry.h"
-#include "content/common/mojo/mojo_channel_init.h"
 #include "content/public/browser/browser_context.h"
 #include "content/public/browser/render_view_host.h"
 #include "content/public/browser/web_contents.h"
 #include "content/public/test/content_browser_test_utils.h"
 #include "content/test/data/web_ui_test_mojo_bindings.mojom.h"
 #include "grit/content_resources.h"
-#include "mojo/public/bindings/js/constants.h"
-#include "mojo/public/bindings/remote_ptr.h"
+#include "mojo/common/test/test_utils.h"
+#include "mojo/public/cpp/bindings/allocation_scope.h"
+#include "mojo/public/js/bindings/constants.h"
 
 namespace content {
 namespace {
 
 bool got_message = false;
 
-// Returns the path to the mojom js bindings file.
-base::FilePath GetFilePathForJSResource(const std::string& path) {
-  std::string binding_path = "gen/" + path + ".js";
-#if defined(OS_WIN)
-  std::string tmp;
-  base::ReplaceChars(binding_path, "//", "\\", &tmp);
-  binding_path.swap(tmp);
-#endif
-  base::FilePath file_path;
-  PathService::Get(CHILD_PROCESS_EXE, &file_path);
-  return file_path.DirName().AppendASCII(binding_path);
-}
-
 // The bindings for the page are generated from a .mojom file. This code looks
 // up the generated file from disk and returns it.
 bool GetResource(const std::string& id,
@@ -55,45 +44,64 @@ bool GetResource(const std::string& id,
     return false;
 
   std::string contents;
-  CHECK(base::ReadFileToString(GetFilePathForJSResource(id), &contents,
-                               std::string::npos));
+  CHECK(base::ReadFileToString(mojo::test::GetFilePathForJSResource(id),
+                               &contents,
+                               std::string::npos)) << id;
   base::RefCountedString* ref_contents = new base::RefCountedString;
   ref_contents->data() = contents;
   callback.Run(ref_contents);
   return true;
 }
 
-// BrowserTarget implementation that quits a RunLoop when BrowserTarget::Test()
-// is called.
-class BrowserTargetImpl : public mojo::BrowserTarget {
+class BrowserTargetImpl : public BrowserTarget {
  public:
-  BrowserTargetImpl(mojo::ScopedRendererTargetHandle handle,
+  BrowserTargetImpl(mojo::ScopedMessagePipeHandle handle,
                     base::RunLoop* run_loop)
-      : client_(handle.Pass(), this),
+      : renderer_(mojo::MakeProxy<RendererTarget>(handle.Pass())),
         run_loop_(run_loop) {
-    client_->Test();
+    renderer_->SetClient(this);
   }
+
   virtual ~BrowserTargetImpl() {}
 
-  // mojo::BrowserTarget overrides:
-  virtual void Test() OVERRIDE {
-    got_message = true;
-    run_loop_->Quit();
+  // BrowserTarget overrides:
+  virtual void PingResponse() OVERRIDE {
+    NOTREACHED();
   }
 
- private:
-  mojo::RemotePtr<mojo::RendererTarget> client_;
-
+ protected:
+  RendererTargetPtr renderer_;
   base::RunLoop* run_loop_;
 
+ private:
   DISALLOW_COPY_AND_ASSIGN(BrowserTargetImpl);
 };
 
-// WebUIController that sets up mojo bindings. Additionally it creates the
-// BrowserTarget implementation at the right time.
+class PingBrowserTargetImpl : public BrowserTargetImpl {
+ public:
+  PingBrowserTargetImpl(mojo::ScopedMessagePipeHandle handle,
+                        base::RunLoop* run_loop)
+      : BrowserTargetImpl(handle.Pass(), run_loop) {
+    renderer_->Ping();
+  }
+
+  virtual ~PingBrowserTargetImpl() {}
+
+  // BrowserTarget overrides:
+  // Quit the RunLoop when called.
+  virtual void PingResponse() OVERRIDE {
+    got_message = true;
+    run_loop_->Quit();
+  }
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(PingBrowserTargetImpl);
+};
+
+// WebUIController that sets up mojo bindings.
 class TestWebUIController : public WebUIController {
  public:
-  explicit TestWebUIController(WebUI* web_ui, base::RunLoop* run_loop)
+   TestWebUIController(WebUI* web_ui, base::RunLoop* run_loop)
       : WebUIController(web_ui),
         run_loop_(run_loop) {
     content::WebUIDataSource* data_source =
@@ -102,21 +110,32 @@ class TestWebUIController : public WebUIController {
     data_source->SetRequestFilter(base::Bind(&GetResource));
   }
 
+ protected:
+  base::RunLoop* run_loop_;
+  scoped_ptr<BrowserTargetImpl> browser_target_;
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(TestWebUIController);
+};
+
+// TestWebUIController that additionally creates the ping test BrowserTarget
+// implementation at the right time.
+class PingTestWebUIController : public TestWebUIController {
+ public:
+   PingTestWebUIController(WebUI* web_ui, base::RunLoop* run_loop)
+       : TestWebUIController(web_ui, run_loop) {
+   }
+
   // WebUIController overrides:
   virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE {
-    mojo::InterfacePipe<mojo::BrowserTarget, mojo::RendererTarget> pipe;
+    mojo::MessagePipe pipe;
     browser_target_.reset(
-        new BrowserTargetImpl(pipe.handle_to_peer.Pass(), run_loop_));
-    render_view_host->SetWebUIHandle(
-        mojo::ScopedMessagePipeHandle(pipe.handle_to_self.release()));
+        new PingBrowserTargetImpl(pipe.handle0.Pass(), run_loop_));
+    render_view_host->SetWebUIHandle(pipe.handle1.Pass());
   }
 
  private:
-  base::RunLoop* run_loop_;
-
-  scoped_ptr<BrowserTargetImpl> browser_target_;
-
-  DISALLOW_COPY_AND_ASSIGN(TestWebUIController);
+  DISALLOW_COPY_AND_ASSIGN(PingTestWebUIController);
 };
 
 // WebUIControllerFactory that creates TestWebUIController.
@@ -128,7 +147,9 @@ class TestWebUIControllerFactory : public WebUIControllerFactory {
 
   virtual WebUIController* CreateWebUIControllerForURL(
       WebUI* web_ui, const GURL& url) const OVERRIDE {
-    return new TestWebUIController(web_ui, run_loop_);
+    if (url.query() == "ping")
+      return new PingTestWebUIController(web_ui, run_loop_);
+    return NULL;
   }
   virtual WebUI::TypeID GetWebUIType(BrowserContext* browser_context,
       const GURL& url) const OVERRIDE {
@@ -169,13 +190,13 @@ class WebUIMojoTest : public ContentBrowserTest {
 
 // Loads a webui page that contains mojo bindings and verifies a message makes
 // it from the browser to the page and back.
-IN_PROC_BROWSER_TEST_F(WebUIMojoTest, EndToEnd) {
+IN_PROC_BROWSER_TEST_F(WebUIMojoTest, EndToEndPing) {
   // Currently there is no way to have a generated file included in the isolate
   // files. If the bindings file doesn't exist assume we're on such a bot and
   // pass.
   // TODO(sky): remove this conditional when isolates support copying from gen.
   const base::FilePath test_file_path(
-      GetFilePathForJSResource(
+      mojo::test::GetFilePathForJSResource(
           "content/test/data/web_ui_test_mojo_bindings.mojom"));
   if (!base::PathExists(test_file_path)) {
     LOG(WARNING) << " mojom binding file doesn't exist, assuming on isolate";
@@ -186,7 +207,7 @@ IN_PROC_BROWSER_TEST_F(WebUIMojoTest, EndToEnd) {
   ASSERT_TRUE(test_server()->Start());
   base::RunLoop run_loop;
   factory()->set_run_loop(&run_loop);
-  GURL test_url(test_server()->GetURL("files/web_ui_mojo.html"));
+  GURL test_url(test_server()->GetURL("files/web_ui_mojo.html?ping"));
   NavigateToURL(shell(), test_url);
   // RunLoop is quit when message received from page.
   run_loop.Run();