e9c782ccc90bdc5fe2d86c21adb803b643aca0bf
[platform/framework/web/crosswalk.git] / src / xwalk / extensions / test / xwalk_extensions_browsertest.cc
1 // Copyright (c) 2013 Intel Corporation. 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 "xwalk/extensions/test/xwalk_extensions_test_base.h"
6
7 #include "xwalk/extensions/common/xwalk_extension.h"
8 #include "xwalk/runtime/browser/runtime.h"
9 #include "xwalk/test/base/in_process_browser_test.h"
10 #include "xwalk/test/base/xwalk_test_utils.h"
11 #include "content/public/test/browser_test_utils.h"
12 #include "content/public/test/test_utils.h"
13 #include "base/task_runner.h"
14 #include "base/time/time.h"
15
16 using namespace xwalk::extensions;  // NOLINT
17
18 namespace {
19
20 const char* kEchoAPI =
21     "var echoListener = null;"
22     "extension.setMessageListener(function(msg) {"
23     "  if (echoListener instanceof Function) {"
24     "    echoListener(msg);"
25     "  };"
26     "});"
27     "exports.echo = function(msg, callback) {"
28     "  echoListener = callback;"
29     "  extension.postMessage(msg);"
30     "};"
31     "exports.syncEcho = function(msg) {"
32     "  return extension.internal.sendSyncMessage(msg);"
33     "};";
34
35 class EchoContext : public XWalkExtensionInstance {
36  public:
37   EchoContext() {
38   }
39   virtual void HandleMessage(scoped_ptr<base::Value> msg) OVERRIDE {
40     PostMessageToJS(msg.Pass());
41   }
42   virtual void HandleSyncMessage(scoped_ptr<base::Value> msg) OVERRIDE {
43     SendSyncReplyToJS(msg.Pass());
44   }
45 };
46
47 class DelayedEchoContext : public XWalkExtensionInstance {
48  public:
49   explicit DelayedEchoContext() {
50   }
51   virtual void HandleMessage(scoped_ptr<base::Value> msg) OVERRIDE {
52     PostMessageToJS(msg.Pass());
53   }
54   virtual void HandleSyncMessage(scoped_ptr<base::Value> msg) OVERRIDE {
55     base::MessageLoop::current()->PostDelayedTask(
56         FROM_HERE, base::Bind(&DelayedEchoContext::DelayedReply,
57                               base::Unretained(this), base::Passed(&msg)),
58         base::TimeDelta::FromSeconds(1));
59   }
60
61   void DelayedReply(scoped_ptr<base::Value> reply) {
62     SendSyncReplyToJS(reply.Pass());
63   }
64 };
65
66 class EchoExtension : public XWalkExtension {
67  public:
68   EchoExtension() : XWalkExtension() {
69     set_name("echo");
70     set_javascript_api(kEchoAPI);
71   }
72
73   virtual XWalkExtensionInstance* CreateInstance() OVERRIDE {
74     s_instance_was_created = true;
75     return new EchoContext();
76   }
77
78   static bool s_instance_was_created;
79 };
80
81 bool EchoExtension::s_instance_was_created = false;
82
83 class DelayedEchoExtension : public XWalkExtension {
84  public:
85   DelayedEchoExtension() : XWalkExtension() {
86     set_name("echo");
87     set_javascript_api(kEchoAPI);
88   }
89
90   virtual XWalkExtensionInstance* CreateInstance() OVERRIDE {
91     return new DelayedEchoContext();
92   }
93 };
94
95 class ExtensionWithInvalidName : public XWalkExtension {
96  public:
97   ExtensionWithInvalidName() : XWalkExtension() {
98     set_name("invalid name with spaces");
99   }
100
101   virtual XWalkExtensionInstance* CreateInstance() OVERRIDE {
102     s_instance_was_created = true;
103     return NULL;
104   }
105
106   static bool s_instance_was_created;
107 };
108
109 bool ExtensionWithInvalidName::s_instance_was_created = false;
110
111 }  // namespace
112
113 class XWalkExtensionsTest : public XWalkExtensionsTestBase {
114  public:
115   virtual void CreateExtensionsForUIThread(
116       XWalkExtensionVector* extensions) OVERRIDE {
117     extensions->push_back(new EchoExtension);
118     extensions->push_back(new ExtensionWithInvalidName);
119   }
120 };
121
122 class XWalkExtensionsDelayedTest : public XWalkExtensionsTestBase {
123  public:
124   virtual void CreateExtensionsForUIThread(
125       XWalkExtensionVector* extensions) OVERRIDE {
126     extensions->push_back(new DelayedEchoExtension);
127   }
128 };
129
130 IN_PROC_BROWSER_TEST_F(XWalkExtensionsTest, EchoExtension) {
131   content::RunAllPendingInMessageLoop();
132   GURL url = GetExtensionsTestURL(base::FilePath(),
133       base::FilePath().AppendASCII("test_extension.html"));
134   content::TitleWatcher title_watcher(runtime()->web_contents(), kPassString);
135   title_watcher.AlsoWaitForTitle(kFailString);
136   xwalk_test_utils::NavigateToURL(runtime(), url);
137   EXPECT_EQ(kPassString, title_watcher.WaitAndGetTitle());
138 }
139
140 IN_PROC_BROWSER_TEST_F(XWalkExtensionsTest, ExtensionWithInvalidNameIgnored) {
141   content::RunAllPendingInMessageLoop();
142   GURL url = GetExtensionsTestURL(base::FilePath(),
143       base::FilePath().AppendASCII("test_extension.html"));
144   content::TitleWatcher title_watcher(runtime()->web_contents(), kPassString);
145   title_watcher.AlsoWaitForTitle(kFailString);
146   xwalk_test_utils::NavigateToURL(runtime(), url);
147   EXPECT_EQ(kPassString, title_watcher.WaitAndGetTitle());
148
149   EXPECT_TRUE(EchoExtension::s_instance_was_created);
150   EXPECT_FALSE(ExtensionWithInvalidName::s_instance_was_created);
151 }
152
153 IN_PROC_BROWSER_TEST_F(XWalkExtensionsTest, EchoExtensionSync) {
154   content::RunAllPendingInMessageLoop();
155   GURL url = GetExtensionsTestURL(base::FilePath(),
156                                   base::FilePath().AppendASCII(
157                                       "sync_echo.html"));
158   content::TitleWatcher title_watcher(runtime()->web_contents(), kPassString);
159   title_watcher.AlsoWaitForTitle(kFailString);
160   xwalk_test_utils::NavigateToURL(runtime(), url);
161   EXPECT_EQ(kPassString, title_watcher.WaitAndGetTitle());
162 }
163
164 IN_PROC_BROWSER_TEST_F(XWalkExtensionsDelayedTest, EchoExtensionSync) {
165   content::RunAllPendingInMessageLoop();
166   GURL url = GetExtensionsTestURL(base::FilePath(),
167                                   base::FilePath().AppendASCII(
168                                       "sync_echo.html"));
169   content::TitleWatcher title_watcher(runtime()->web_contents(), kPassString);
170   title_watcher.AlsoWaitForTitle(kFailString);
171   xwalk_test_utils::NavigateToURL(runtime(), url);
172   EXPECT_EQ(kPassString, title_watcher.WaitAndGetTitle());
173 }