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