- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / extension_incognito_apitest.cc
1 // Copyright (c) 2012 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 "chrome/browser/extensions/browser_action_test_util.h"
6 #include "chrome/browser/extensions/extension_apitest.h"
7 #include "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/extensions/extension_test_message_listener.h"
9 #include "chrome/browser/extensions/user_script_master.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/common/url_constants.h"
15 #include "chrome/test/base/ui_test_utils.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/test/browser_test_utils.h"
18 #include "net/dns/mock_host_resolver.h"
19 #include "net/test/embedded_test_server/embedded_test_server.h"
20
21 using content::WebContents;
22
23 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, IncognitoNoScript) {
24   ASSERT_TRUE(StartEmbeddedTestServer());
25
26   // Loads a simple extension which attempts to change the title of every page
27   // that loads to "modified".
28   ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("incognito")
29       .AppendASCII("content_scripts")));
30
31   // Open incognito window and navigate to test page.
32   Browser* otr_browser = ui_test_utils::OpenURLOffTheRecord(
33       browser()->profile(),
34       embedded_test_server()->GetURL("/extensions/test_file.html"));
35
36   WebContents* tab = otr_browser->tab_strip_model()->GetActiveWebContents();
37
38   // Verify the script didn't run.
39   bool result = false;
40   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
41       tab,
42       "window.domAutomationController.send(document.title == 'Unmodified')",
43       &result));
44   EXPECT_TRUE(result);
45 }
46
47 #if defined(OS_WIN)
48 // This test is very flaky on XP. http://crbug.com/248821
49 #define MAYBE_IncognitoYesScript DISABLED_IncognitoYesScript
50 #else
51 #define MAYBE_IncognitoYesScript IncognitoYesScript
52 #endif
53
54 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_IncognitoYesScript) {
55   host_resolver()->AddRule("*", "127.0.0.1");
56   ASSERT_TRUE(StartEmbeddedTestServer());
57
58   // Load a dummy extension. This just tests that we don't regress a
59   // crash fix when multiple incognito- and non-incognito-enabled extensions
60   // are mixed.
61   ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("content_scripts")
62       .AppendASCII("all_frames")));
63
64   // Loads a simple extension which attempts to change the title of every page
65   // that loads to "modified".
66   ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
67       .AppendASCII("incognito").AppendASCII("content_scripts")));
68
69   // Dummy extension #2.
70   ASSERT_TRUE(LoadExtension(test_data_dir_
71       .AppendASCII("content_scripts").AppendASCII("isolated_world1")));
72
73   // Open incognito window and navigate to test page.
74   Browser* otr_browser = ui_test_utils::OpenURLOffTheRecord(
75       browser()->profile(),
76       embedded_test_server()->GetURL("/extensions/test_file.html"));
77
78   WebContents* tab = otr_browser->tab_strip_model()->GetActiveWebContents();
79
80   // Verify the script ran.
81   bool result = false;
82   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
83       tab,
84       "window.domAutomationController.send(document.title == 'modified')",
85       &result));
86   EXPECT_TRUE(result);
87 }
88
89 // Tests that an extension which is enabled for incognito mode doesn't
90 // accidentially create and incognito profile.
91 // Test disabled due to http://crbug.com/89054.
92 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_DontCreateIncognitoProfile) {
93   ASSERT_FALSE(browser()->profile()->HasOffTheRecordProfile());
94   ASSERT_TRUE(RunExtensionTestIncognito(
95       "incognito/dont_create_profile")) << message_;
96   ASSERT_FALSE(browser()->profile()->HasOffTheRecordProfile());
97 }
98
99 #if defined(OS_WIN) || defined(OS_MACOSX)
100 // http://crbug.com/120484
101 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_Incognito) {
102 #else
103 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Incognito) {
104 #endif
105   host_resolver()->AddRule("*", "127.0.0.1");
106   ASSERT_TRUE(StartEmbeddedTestServer());
107
108   ResultCatcher catcher;
109
110   // Open incognito window and navigate to test page.
111   ui_test_utils::OpenURLOffTheRecord(
112       browser()->profile(),
113       embedded_test_server()->GetURL("/extensions/test_file.html"));
114
115   ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
116       .AppendASCII("incognito").AppendASCII("apis")));
117
118   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
119 }
120
121 // Tests that the APIs in an incognito-enabled split-mode extension work
122 // properly.
123 #if defined(OS_WIN)
124 // http://crbug.com/120484
125 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_IncognitoSplitMode) {
126 #else
127 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, IncognitoSplitMode) {
128 #endif
129   host_resolver()->AddRule("*", "127.0.0.1");
130   ASSERT_TRUE(StartEmbeddedTestServer());
131
132   // We need 2 ResultCatchers because we'll be running the same test in both
133   // regular and incognito mode.
134   ResultCatcher catcher;
135   catcher.RestrictToProfile(browser()->profile());
136   ResultCatcher catcher_incognito;
137   catcher_incognito.RestrictToProfile(
138       browser()->profile()->GetOffTheRecordProfile());
139
140   ExtensionTestMessageListener listener("waiting", true);
141   ExtensionTestMessageListener listener_incognito("waiting_incognito", true);
142
143   // Open incognito window and navigate to test page.
144   ui_test_utils::OpenURLOffTheRecord(
145       browser()->profile(),
146       embedded_test_server()->GetURL("/extensions/test_file.html"));
147
148   ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
149       .AppendASCII("incognito").AppendASCII("split")));
150
151   // Wait for both extensions to be ready before telling them to proceed.
152   EXPECT_TRUE(listener.WaitUntilSatisfied());
153   EXPECT_TRUE(listener_incognito.WaitUntilSatisfied());
154   listener.Reply("go");
155   listener_incognito.Reply("go");
156
157   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
158   EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message();
159 }
160
161 // Tests that the APIs in an incognito-disabled extension don't see incognito
162 // events or callbacks.
163 #if defined(OS_WIN)
164 // http://crbug.com/120484
165 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_IncognitoDisabled) {
166 #else
167 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, IncognitoDisabled) {
168 #endif
169   host_resolver()->AddRule("*", "127.0.0.1");
170   ASSERT_TRUE(StartEmbeddedTestServer());
171
172   ResultCatcher catcher;
173
174   // Open incognito window and navigate to test page.
175   ui_test_utils::OpenURLOffTheRecord(
176       browser()->profile(),
177       embedded_test_server()->GetURL("/extensions/test_file.html"));
178
179   ASSERT_TRUE(LoadExtension(test_data_dir_
180       .AppendASCII("incognito").AppendASCII("apis_disabled")));
181
182   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
183 }
184
185 #if defined(OS_WIN) || defined(OS_MACOSX)
186 // http://crbug.com/104438.
187 #define MAYBE_IncognitoPopup DISABLED_IncognitoPopup
188 #else
189 #define MAYBE_IncognitoPopup IncognitoPopup
190 #endif
191 // Test that opening a popup from an incognito browser window works properly.
192 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_IncognitoPopup) {
193   host_resolver()->AddRule("*", "127.0.0.1");
194   ASSERT_TRUE(StartEmbeddedTestServer());
195
196   ResultCatcher catcher;
197
198   ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_
199       .AppendASCII("incognito").AppendASCII("popup")));
200
201   // Open incognito window and navigate to test page.
202   Browser* incognito_browser = ui_test_utils::OpenURLOffTheRecord(
203       browser()->profile(),
204       embedded_test_server()->GetURL("/extensions/test_file.html"));
205
206   // Simulate the incognito's browser action being clicked.
207   BrowserActionTestUtil(incognito_browser).Press(0);
208
209   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
210 }