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.
5 #include "xwalk/extensions/test/xwalk_extensions_test_base.h"
7 #include "base/synchronization/lock.h"
8 #include "base/synchronization/spin_wait.h"
9 #include "content/public/browser/web_contents.h"
10 #include "content/public/test/browser_test_utils.h"
11 #include "content/public/test/test_utils.h"
12 #include "xwalk/extensions/common/xwalk_extension.h"
13 #include "xwalk/runtime/browser/runtime.h"
14 #include "xwalk/test/base/in_process_browser_test.h"
15 #include "xwalk/test/base/xwalk_test_utils.h"
17 using namespace xwalk::extensions; // NOLINT
22 base::Lock g_count_lock;
27 class CounterExtensionContext : public XWalkExtensionInstance {
29 CounterExtensionContext() {
32 virtual void HandleMessage(scoped_ptr<base::Value> msg) OVERRIDE {
33 base::AutoLock lock(g_count_lock);
38 class CounterExtension : public XWalkExtension {
44 "exports.count = function() {"
45 " extension.postMessage('PING');"
49 virtual XWalkExtensionInstance* CreateInstance() OVERRIDE {
50 return new CounterExtensionContext();
54 class XWalkExtensionsIFrameTest : public XWalkExtensionsTestBase {
56 virtual void CreateExtensionsForUIThread(
57 XWalkExtensionVector* extensions) OVERRIDE {
58 extensions->push_back(new CounterExtension);
62 IN_PROC_BROWSER_TEST_F(XWalkExtensionsIFrameTest,
63 ContextsAreCreatedForIFrames) {
64 Runtime* runtime = CreateRuntime();
65 GURL url = GetExtensionsTestURL(base::FilePath(),
66 base::FilePath().AppendASCII("counter_with_iframes.html"));
67 xwalk_test_utils::NavigateToURL(runtime, url);
68 SPIN_FOR_1_SECOND_OR_UNTIL_TRUE(g_count == 3);
69 ASSERT_EQ(g_count, 3);
72 IN_PROC_BROWSER_TEST_F(XWalkExtensionsIFrameTest,
73 ContextsAreNotCreatedForIFramesWithBlankPages) {
74 Runtime* runtime = CreateRuntime();
75 GURL url = GetExtensionsTestURL(base::FilePath(),
76 base::FilePath().AppendASCII("blank_iframes.html"));
78 // We are mainly validating the fix for the issue #602. We first create a page
79 // full of blank iframes and afterwards we navigate to another page.
80 // ModuleSystems should not be created and consequentially not deleted for the
82 xwalk_test_utils::NavigateToURL(runtime, url);
83 content::TitleWatcher title_watcher1(runtime->web_contents(), kPassString);
84 xwalk_test_utils::NavigateToURL(runtime, url);
85 content::TitleWatcher title_watcher2(runtime->web_contents(), kPassString);
88 // This test reproduces the problem found in bug
89 // https://github.com/crosswalk-project/crosswalk/issues/602. In summary, we
90 // were assuming that if at DidCreateScriptContext() a certain frame have URL
91 // "about:blank", that would also be true at WillReleaseScriptContext(), which
94 // Using document.write into an empty document will change the URL for the frame
95 // we get in WillReleaseScriptContext(). The crash in the original code was due
96 // to not creating a module system for "about:blank" pages, and then trying to
97 // delete it when releasing the script context.
98 IN_PROC_BROWSER_TEST_F(XWalkExtensionsIFrameTest,
99 IFrameUsingDocumentWriteShouldNotCrash) {
100 Runtime* runtime = CreateRuntime();
101 GURL url = GetExtensionsTestURL(base::FilePath(),
102 base::FilePath().AppendASCII("iframe_using_document_write.html"));
104 for (int i = 0; i < 5; i++) {
105 content::TitleWatcher title_watcher(runtime->web_contents(), kPassString);
106 xwalk_test_utils::NavigateToURL(runtime, url);
107 EXPECT_EQ(kPassString, title_watcher.WaitAndGetTitle());
111 // We can keep references to a function that uses one of our APIs from inside an
112 // iframe. After the iframe is destroyed, we don't expect these functions to
113 // work, but they also should not crash the Render Process. See
114 // iframe_keep_reference.html for more details.
115 IN_PROC_BROWSER_TEST_F(XWalkExtensionsIFrameTest,
116 KeepingReferenceToFunctionFromDestroyedIFrame) {
117 Runtime* runtime = CreateRuntime();
118 GURL url = GetExtensionsTestURL(
120 base::FilePath().AppendASCII("iframe_keep_reference.html"));
121 content::TitleWatcher title_watcher(runtime->web_contents(), kPassString);
122 title_watcher.AlsoWaitForTitle(kFailString);
123 xwalk_test_utils::NavigateToURL(runtime, url);
124 EXPECT_EQ(kPassString, title_watcher.WaitAndGetTitle());
125 ASSERT_EQ(g_count, 1);