f554653cee8b6da6ea5a5c3d5c9f8c4dba8ae494
[platform/framework/web/crosswalk.git] / src / xwalk / extensions / test / external_extension_multi_process.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 "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"
14
15 using xwalk::NativeAppWindow;
16 using xwalk::Runtime;
17 using xwalk::extensions::XWalkExtensionVector;
18
19 class ExternalExtensionMultiProcessTest : public XWalkExtensionsTestBase {
20  public:
21   ExternalExtensionMultiProcessTest()
22     : register_extensions_count_(0) {}
23
24   virtual ~ExternalExtensionMultiProcessTest() {
25     original_runtimes_.clear();
26     notification_observer_.reset();
27   }
28
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());
37   }
38
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);
49         return *it;
50       }
51     }
52     return NULL;
53   }
54
55   // This will be called everytime a new RenderProcess has been created.
56   virtual void CreateExtensionsForUIThread(
57       XWalkExtensionVector* extensions) OVERRIDE {
58     register_extensions_count_++;
59   }
60
61   int CountRegisterExtensions() {
62     return register_extensions_count_;
63   }
64
65  private:
66   RuntimeList original_runtimes_;
67   scoped_ptr<content::WindowedNotificationObserver> notification_observer_;
68
69   int register_extensions_count_;
70 };
71
72 IN_PROC_BROWSER_TEST_F(ExternalExtensionMultiProcessTest,
73     OpenLinkInNewRuntimeAndSameRP) {
74   size_t len = runtimes().size();
75
76   GURL url = GetExtensionsTestURL(base::FilePath(),
77                                   base::FilePath().AppendASCII("same_rp.html"));
78
79   xwalk_test_utils::NavigateToURL(runtime(), url);
80   WaitForLoadStop(runtime()->web_contents());
81
82   EXPECT_EQ(1, CountRegisterExtensions());
83
84   SimulateMouseClick(runtime()->web_contents(), 0,
85       blink::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());
92 }
93
94 IN_PROC_BROWSER_TEST_F(ExternalExtensionMultiProcessTest,
95     OpenLinkInNewRuntimeAndNewRP) {
96   size_t len = runtimes().size();
97
98   GURL url = GetExtensionsTestURL(base::FilePath(),
99                                   base::FilePath().AppendASCII("new_rp.html"));
100
101   xwalk_test_utils::NavigateToURL(runtime(), url);
102   WaitForLoadStop(runtime()->web_contents());
103
104   EXPECT_EQ(1, CountRegisterExtensions());
105
106   SimulateMouseClick(runtime()->web_contents(), 0,
107       blink::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());
114 }
115
116 IN_PROC_BROWSER_TEST_F(ExternalExtensionMultiProcessTest,
117     CreateNewRuntimeAndNewRP) {
118   size_t len = runtimes().size();
119
120   GURL url = GetExtensionsTestURL(base::FilePath(),
121                                   base::FilePath().AppendASCII("new_rp.html"));
122
123   xwalk_test_utils::NavigateToURL(runtime(), url);
124   WaitForLoadStop(runtime()->web_contents());
125   EXPECT_EQ(1, CountRegisterExtensions());
126
127   Runtime* new_runtime = Runtime::CreateWithDefaultWindow(
128       GetRuntimeContext(), url, runtime_registry());
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());
134 }