Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / process_manager_unittest.cc
1 // Copyright 2014 The Chromium Authors. 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 "extensions/browser/process_manager.h"
6
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "content/public/browser/content_browser_client.h"
9 #include "content/public/browser/notification_service.h"
10 #include "content/public/browser/site_instance.h"
11 #include "content/public/test/test_browser_context.h"
12 #include "extensions/browser/test_extensions_browser_client.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 using content::BrowserContext;
16 using content::SiteInstance;
17 using content::TestBrowserContext;
18
19 namespace extensions {
20
21 namespace {
22
23 // An incognito version of a TestBrowserContext.
24 class TestBrowserContextIncognito : public TestBrowserContext {
25  public:
26   TestBrowserContextIncognito() {}
27   virtual ~TestBrowserContextIncognito() {}
28
29   // TestBrowserContext implementation.
30   virtual bool IsOffTheRecord() const OVERRIDE { return true; }
31
32  private:
33   DISALLOW_COPY_AND_ASSIGN(TestBrowserContextIncognito);
34 };
35
36 }  // namespace
37
38 class ProcessManagerTest : public testing::Test {
39  public:
40   ProcessManagerTest() : extensions_browser_client_(&original_context_) {
41     extensions_browser_client_.SetIncognitoContext(&incognito_context_);
42     ExtensionsBrowserClient::Set(&extensions_browser_client_);
43   }
44
45   virtual ~ProcessManagerTest() {
46     ExtensionsBrowserClient::Set(NULL);
47   }
48
49   BrowserContext* original_context() { return &original_context_; }
50   BrowserContext* incognito_context() { return &incognito_context_; }
51
52   // Returns true if the notification |type| is registered for |manager| with
53   // source |context|. Pass NULL for |context| for all sources.
54   static bool IsRegistered(ProcessManager* manager,
55                            int type,
56                            BrowserContext* context) {
57     return manager->registrar_.IsRegistered(
58         manager, type, content::Source<BrowserContext>(context));
59   }
60
61  private:
62   TestBrowserContext original_context_;
63   TestBrowserContextIncognito incognito_context_;
64   TestExtensionsBrowserClient extensions_browser_client_;
65
66   DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest);
67 };
68
69 // Test that notification registration works properly.
70 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) {
71   // Test for a normal context ProcessManager.
72   scoped_ptr<ProcessManager> manager1(
73       ProcessManager::Create(original_context()));
74
75   EXPECT_EQ(original_context(), manager1->GetBrowserContext());
76   EXPECT_EQ(0u, manager1->background_hosts().size());
77
78   // It observes other notifications from this context.
79   EXPECT_TRUE(IsRegistered(manager1.get(),
80                            chrome::NOTIFICATION_EXTENSIONS_READY,
81                            original_context()));
82   EXPECT_TRUE(IsRegistered(manager1.get(),
83                            chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
84                            original_context()));
85   EXPECT_TRUE(IsRegistered(manager1.get(),
86                            chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
87                            original_context()));
88   EXPECT_TRUE(IsRegistered(manager1.get(),
89                            chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
90                            original_context()));
91
92   // Test for an incognito context ProcessManager.
93   scoped_ptr<ProcessManager> manager2(ProcessManager::CreateIncognitoForTesting(
94       incognito_context(), original_context(), manager1.get()));
95
96   EXPECT_EQ(incognito_context(), manager2->GetBrowserContext());
97   EXPECT_EQ(0u, manager2->background_hosts().size());
98
99   // Some notifications are observed for the original context.
100   EXPECT_TRUE(IsRegistered(manager2.get(),
101                            chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
102                            original_context()));
103
104   // Some notifications are observed for the incognito context.
105   EXPECT_TRUE(IsRegistered(manager2.get(),
106                            chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
107                            incognito_context()));
108
109   // Some notifications are observed for both incognito and original.
110   EXPECT_TRUE(IsRegistered(manager2.get(),
111                            chrome::NOTIFICATION_PROFILE_DESTROYED,
112                            original_context()));
113   EXPECT_TRUE(IsRegistered(manager2.get(),
114                            chrome::NOTIFICATION_PROFILE_DESTROYED,
115                            incognito_context()));
116
117   // Some are not observed at all.
118   EXPECT_FALSE(IsRegistered(manager2.get(),
119                             chrome::NOTIFICATION_EXTENSIONS_READY,
120                             original_context()));
121
122   // This notification is observed for incognito contexts only.
123   EXPECT_TRUE(IsRegistered(manager2.get(),
124                            chrome::NOTIFICATION_PROFILE_DESTROYED,
125                            incognito_context()));
126 }
127
128 // Test that extensions get grouped in the right SiteInstance (and therefore
129 // process) based on their URLs.
130 TEST_F(ProcessManagerTest, ProcessGrouping) {
131   content::ContentBrowserClient content_browser_client;
132   content::SetBrowserClientForTesting(&content_browser_client);
133
134   // Extensions in different browser contexts should always be different
135   // SiteInstances.
136   scoped_ptr<ProcessManager> manager1(
137       ProcessManager::Create(original_context()));
138   // NOTE: This context is not associated with the TestExtensionsBrowserClient.
139   // That's OK because we're not testing regular vs. incognito behavior.
140   TestBrowserContext another_context;
141   scoped_ptr<ProcessManager> manager2(ProcessManager::Create(&another_context));
142
143   // Extensions with common origins ("scheme://id/") should be grouped in the
144   // same SiteInstance.
145   GURL ext1_url1("chrome-extension://ext1_id/index.html");
146   GURL ext1_url2("chrome-extension://ext1_id/monkey/monkey.html");
147   GURL ext2_url1("chrome-extension://ext2_id/index.html");
148
149   scoped_refptr<SiteInstance> site11 =
150       manager1->GetSiteInstanceForURL(ext1_url1);
151   scoped_refptr<SiteInstance> site12 =
152       manager1->GetSiteInstanceForURL(ext1_url2);
153   EXPECT_EQ(site11, site12);
154
155   scoped_refptr<SiteInstance> site21 =
156       manager1->GetSiteInstanceForURL(ext2_url1);
157   EXPECT_NE(site11, site21);
158
159   scoped_refptr<SiteInstance> other_profile_site =
160       manager2->GetSiteInstanceForURL(ext1_url1);
161   EXPECT_NE(site11, other_profile_site);
162 }
163
164 }  // namespace extensions