OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_tabs_module.h" | 5 #include "chrome/browser/extensions/extension_tabs_module.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chrome/browser/extensions/extension_function_test_utils.h" | 14 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 15 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
15 #include "chrome/browser/extensions/extension_tab_util.h" | 16 #include "chrome/browser/extensions/extension_tab_util.h" |
16 #include "chrome/browser/extensions/extension_tabs_module_constants.h" | 17 #include "chrome/browser/prefs/incognito_mode_prefs.h" |
| 18 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
18 #include "chrome/browser/ui/browser_window.h" | 20 #include "chrome/browser/ui/browser_window.h" |
19 #include "chrome/test/base/in_process_browser_test.h" | 21 #include "chrome/test/base/in_process_browser_test.h" |
20 #include "chrome/test/base/ui_test_utils.h" | 22 #include "chrome/test/base/ui_test_utils.h" |
21 #include "ui/gfx/rect.h" | 23 #include "ui/gfx/rect.h" |
22 | 24 |
23 using namespace extension_function_test_utils; | 25 using namespace extension_function_test_utils; |
24 | 26 |
25 namespace { | 27 namespace { |
26 | 28 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 update_tab_function->set_extension(empty_extension.get()); | 121 update_tab_function->set_extension(empty_extension.get()); |
120 // Without a callback the function will not generate a result. | 122 // Without a callback the function will not generate a result. |
121 update_tab_function->set_has_callback(true); | 123 update_tab_function->set_has_callback(true); |
122 | 124 |
123 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( | 125 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( |
124 update_tab_function.get(), | 126 update_tab_function.get(), |
125 "[null, {\"url\": \"neutrinos\"}]", | 127 "[null, {\"url\": \"neutrinos\"}]", |
126 browser())); | 128 browser())); |
127 EXPECT_EQ(base::Value::TYPE_NULL, result->GetType()); | 129 EXPECT_EQ(base::Value::TYPE_NULL, result->GetType()); |
128 } | 130 } |
| 131 |
| 132 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, |
| 133 DefaultToIncognitoWhenItIsForced) { |
| 134 static const char kArgsWithoutExplicitIncognitoParam[] = |
| 135 "[{\"url\": \"about:blank\"}]"; |
| 136 // Force Incognito mode. |
| 137 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(), |
| 138 IncognitoModePrefs::FORCED); |
| 139 // Run without an explicit "incognito" param. |
| 140 scoped_ptr<base::DictionaryValue> result(ToDictionary( |
| 141 RunFunctionAndReturnResult( |
| 142 new CreateWindowFunction(), |
| 143 kArgsWithoutExplicitIncognitoParam, |
| 144 browser(), |
| 145 INCLUDE_INCOGNITO))); |
| 146 |
| 147 // Make sure it is a new(different) window. |
| 148 EXPECT_NE(ExtensionTabUtil::GetWindowId(browser()), |
| 149 GetInteger(result.get(), "id")); |
| 150 // ... and it is incognito. |
| 151 EXPECT_TRUE(GetBoolean(result.get(), "incognito")); |
| 152 |
| 153 // Now try creating a window from incognito window. |
| 154 Browser* incognito_browser = CreateIncognitoBrowser(); |
| 155 // Run without an explicit "incognito" param. |
| 156 result.reset(ToDictionary( |
| 157 RunFunctionAndReturnResult( |
| 158 new CreateWindowFunction(), |
| 159 kArgsWithoutExplicitIncognitoParam, |
| 160 incognito_browser, |
| 161 INCLUDE_INCOGNITO))); |
| 162 // Make sure it is a new(different) window. |
| 163 EXPECT_NE(ExtensionTabUtil::GetWindowId(incognito_browser), |
| 164 GetInteger(result.get(), "id")); |
| 165 // ... and it is incognito. |
| 166 EXPECT_TRUE(GetBoolean(result.get(), "incognito")); |
| 167 } |
| 168 |
| 169 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, |
| 170 DefaultToIncognitoWhenItIsForcedAndNoArgs) { |
| 171 static const char kEmptyArgs[] = "[]"; |
| 172 // Force Incognito mode. |
| 173 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(), |
| 174 IncognitoModePrefs::FORCED); |
| 175 // Run without an explicit "incognito" param. |
| 176 scoped_ptr<base::DictionaryValue> result(ToDictionary( |
| 177 RunFunctionAndReturnResult( |
| 178 new CreateWindowFunction(), |
| 179 kEmptyArgs, |
| 180 browser(), |
| 181 INCLUDE_INCOGNITO))); |
| 182 |
| 183 // Make sure it is a new(different) window. |
| 184 EXPECT_NE(ExtensionTabUtil::GetWindowId(browser()), |
| 185 GetInteger(result.get(), "id")); |
| 186 // ... and it is incognito. |
| 187 EXPECT_TRUE(GetBoolean(result.get(), "incognito")); |
| 188 |
| 189 // Now try creating a window from incognito window. |
| 190 Browser* incognito_browser = CreateIncognitoBrowser(); |
| 191 // Run without an explicit "incognito" param. |
| 192 result.reset(ToDictionary( |
| 193 RunFunctionAndReturnResult( |
| 194 new CreateWindowFunction(), |
| 195 kEmptyArgs, |
| 196 incognito_browser, |
| 197 INCLUDE_INCOGNITO))); |
| 198 // Make sure it is a new(different) window. |
| 199 EXPECT_NE(ExtensionTabUtil::GetWindowId(incognito_browser), |
| 200 GetInteger(result.get(), "id")); |
| 201 // ... and it is incognito. |
| 202 EXPECT_TRUE(GetBoolean(result.get(), "incognito")); |
| 203 } |
| 204 |
| 205 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, |
| 206 DontCreateNormalWindowWhenIncognitoForced) { |
| 207 static const char kArgsWithExplicitIncognitoParam[] = |
| 208 "[{\"url\": \"about:blank\", \"incognito\": false }]"; |
| 209 // Force Incognito mode. |
| 210 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(), |
| 211 IncognitoModePrefs::FORCED); |
| 212 |
| 213 // Run with an explicit "incognito" param. |
| 214 EXPECT_TRUE(MatchPattern( |
| 215 RunFunctionAndReturnError( |
| 216 new CreateWindowFunction(), |
| 217 kArgsWithExplicitIncognitoParam, |
| 218 browser()), |
| 219 extension_tabs_module_constants::kIncognitoModeIsForced)); |
| 220 |
| 221 // Now try opening a normal window from incognito window. |
| 222 Browser* incognito_browser = CreateIncognitoBrowser(); |
| 223 // Run with an explicit "incognito" param. |
| 224 EXPECT_TRUE(MatchPattern( |
| 225 RunFunctionAndReturnError( |
| 226 new CreateWindowFunction(), |
| 227 kArgsWithExplicitIncognitoParam, |
| 228 incognito_browser), |
| 229 extension_tabs_module_constants::kIncognitoModeIsForced)); |
| 230 } |
| 231 |
| 232 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, |
| 233 DontCreateIncognitoWindowWhenIncognitoDisabled) { |
| 234 static const char kArgs[] = |
| 235 "[{\"url\": \"about:blank\", \"incognito\": true }]"; |
| 236 |
| 237 Browser* incognito_browser = CreateIncognitoBrowser(); |
| 238 // Disable Incognito mode. |
| 239 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(), |
| 240 IncognitoModePrefs::DISABLED); |
| 241 // Run in normal window. |
| 242 EXPECT_TRUE(MatchPattern( |
| 243 RunFunctionAndReturnError( |
| 244 new CreateWindowFunction(), |
| 245 kArgs, |
| 246 browser()), |
| 247 extension_tabs_module_constants::kIncognitoModeIsDisabled)); |
| 248 |
| 249 // Run in incognito window. |
| 250 EXPECT_TRUE(MatchPattern( |
| 251 RunFunctionAndReturnError( |
| 252 new CreateWindowFunction(), |
| 253 kArgs, |
| 254 incognito_browser), |
| 255 extension_tabs_module_constants::kIncognitoModeIsDisabled)); |
| 256 } |
OLD | NEW |