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 "base/native_library.h"
6 #include "base/path_service.h"
7 #include "xwalk/extensions/test/xwalk_extensions_test_base.h"
8 #include "xwalk/runtime/browser/runtime.h"
9 #include "xwalk/runtime/common/xwalk_notification_types.h"
10 #include "xwalk/test/base/xwalk_test_utils.h"
11 #include "content/public/browser/notification_service.h"
12 #include "content/public/test/browser_test_utils.h"
13 #include "content/public/test/test_utils.h"
15 using xwalk::NativeAppWindow;
17 using xwalk::extensions::XWalkExtensionVector;
19 class ExternalExtensionMultiProcessTest : public XWalkExtensionsTestBase {
21 ExternalExtensionMultiProcessTest()
22 : register_extensions_count_(0) {}
24 virtual ~ExternalExtensionMultiProcessTest() {
25 original_runtimes_.clear();
26 notification_observer_.reset();
29 // SetUpOnMainThread is called after BrowserMainRunner was initialized and
30 // just before RunTestOnMainThread.
31 virtual void SetUpOnMainThread() OVERRIDE {
32 notification_observer_.reset(
33 new content::WindowedNotificationObserver(
34 xwalk::NOTIFICATION_RUNTIME_OPENED,
35 content::NotificationService::AllSources()));
36 original_runtimes_.assign(runtimes().begin(), runtimes().end());
39 // Block UI thread until a new Runtime instance is created.
40 Runtime* WaitForSingleNewRuntime() {
41 notification_observer_->Wait();
42 for (RuntimeList::const_iterator it = runtimes().begin();
43 it != runtimes().end(); ++it) {
44 RuntimeList::iterator target =
45 std::find(original_runtimes_.begin(), original_runtimes_.end(), *it);
46 // Not found means a new one.
47 if (target == original_runtimes_.end()) {
48 original_runtimes_.push_back(*it);
55 // This will be called everytime a new RenderProcess has been created.
56 virtual void CreateExtensionsForUIThread(
57 XWalkExtensionVector* extensions) OVERRIDE {
58 register_extensions_count_++;
61 int CountRegisterExtensions() {
62 return register_extensions_count_;
66 RuntimeList original_runtimes_;
67 scoped_ptr<content::WindowedNotificationObserver> notification_observer_;
69 int register_extensions_count_;
72 IN_PROC_BROWSER_TEST_F(ExternalExtensionMultiProcessTest,
73 OpenLinkInNewRuntimeAndSameRP) {
74 size_t len = runtimes().size();
76 GURL url = GetExtensionsTestURL(base::FilePath(),
77 base::FilePath().AppendASCII("same_rp.html"));
79 xwalk_test_utils::NavigateToURL(runtime(), url);
80 WaitForLoadStop(runtime()->web_contents());
82 EXPECT_EQ(1, CountRegisterExtensions());
84 SimulateMouseClick(runtime()->web_contents(), 0,
85 WebKit::WebMouseEvent::ButtonLeft);
86 content::RunAllPendingInMessageLoop();
87 Runtime* second = WaitForSingleNewRuntime();
88 EXPECT_TRUE(NULL != second);
89 EXPECT_NE(runtime(), second);
90 EXPECT_EQ(len + 1, runtimes().size());
91 EXPECT_EQ(1, CountRegisterExtensions());
94 IN_PROC_BROWSER_TEST_F(ExternalExtensionMultiProcessTest,
95 OpenLinkInNewRuntimeAndNewRP) {
96 size_t len = runtimes().size();
98 GURL url = GetExtensionsTestURL(base::FilePath(),
99 base::FilePath().AppendASCII("new_rp.html"));
101 xwalk_test_utils::NavigateToURL(runtime(), url);
102 WaitForLoadStop(runtime()->web_contents());
104 EXPECT_EQ(1, CountRegisterExtensions());
106 SimulateMouseClick(runtime()->web_contents(), 0,
107 WebKit::WebMouseEvent::ButtonLeft);
108 content::RunAllPendingInMessageLoop();
109 Runtime* second = WaitForSingleNewRuntime();
110 EXPECT_TRUE(NULL != second);
111 EXPECT_NE(runtime(), second);
112 EXPECT_EQ(len + 1, runtimes().size());
113 EXPECT_EQ(2, CountRegisterExtensions());
116 IN_PROC_BROWSER_TEST_F(ExternalExtensionMultiProcessTest,
117 CreateNewRuntimeAndNewRP) {
118 size_t len = runtimes().size();
120 GURL url = GetExtensionsTestURL(base::FilePath(),
121 base::FilePath().AppendASCII("new_rp.html"));
123 xwalk_test_utils::NavigateToURL(runtime(), url);
124 WaitForLoadStop(runtime()->web_contents());
125 EXPECT_EQ(1, CountRegisterExtensions());
127 Runtime* new_runtime = Runtime::CreateWithDefaultWindow(
128 runtime()->runtime_context(), url);
129 EXPECT_EQ(new_runtime, WaitForSingleNewRuntime());
130 EXPECT_NE(runtime(), new_runtime);
131 content::RunAllPendingInMessageLoop();
132 EXPECT_EQ(len + 1, runtimes().size());
133 EXPECT_EQ(2, CountRegisterExtensions());