1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "chrome/browser/devtools/devtools_window_testing.h"
#include "chrome/browser/extensions/api/tabs/tabs_api.h"
#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
#include "chrome/browser/extensions/extension_function_test_utils.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/prefs/incognito_mode_prefs.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/common/page_zoom.h"
#include "content/public/common/url_constants.h"
#include "extensions/browser/api_test_utils.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/test_util.h"
#include "net/test/spawned_test_server/spawned_test_server.h"
#include "ui/gfx/geometry/rect.h"
namespace extensions {
namespace keys = tabs_constants;
namespace utils = extension_function_test_utils;
namespace {
class ExtensionTabsTest : public InProcessBrowserTest {
};
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetWindow) {
int window_id = ExtensionTabUtil::GetWindowId(browser());
// Invalid window ID error.
scoped_refptr<WindowsGetFunction> function = new WindowsGetFunction();
scoped_refptr<Extension> extension(test_util::CreateEmptyExtension());
function->set_extension(extension.get());
EXPECT_TRUE(MatchPattern(
utils::RunFunctionAndReturnError(
function.get(),
base::StringPrintf("[%u]", window_id + 1),
browser()),
keys::kWindowNotFoundError));
// Basic window details.
gfx::Rect bounds;
if (browser()->window()->IsMinimized())
bounds = browser()->window()->GetRestoredBounds();
else
bounds = browser()->window()->GetBounds();
function = new WindowsGetFunction();
function->set_extension(extension.get());
scoped_ptr<base::DictionaryValue> result(utils::ToDictionary(
utils::RunFunctionAndReturnSingleResult(
function.get(),
base::StringPrintf("[%u]", window_id),
browser())));
EXPECT_EQ(window_id, api_test_utils::GetInteger(result.get(), "id"));
EXPECT_FALSE(api_test_utils::GetBoolean(result.get(), "incognito"));
EXPECT_EQ("normal", api_test_utils::GetString(result.get(), "type"));
EXPECT_EQ(bounds.x(), api_test_utils::GetInteger(result.get(), "left"));
EXPECT_EQ(bounds.y(), api_test_utils::GetInteger(result.get(), "top"));
EXPECT_EQ(bounds.width(), api_test_utils::GetInteger(result.get(), "width"));
EXPECT_EQ(bounds.height(),
api_test_utils::GetInteger(result.get(), "height"));
// With "populate" enabled.
function = new WindowsGetFunction();
function->set_extension(extension.get());
result.reset(utils::ToDictionary(
utils::RunFunctionAndReturnSingleResult(
function.get(),
base::StringPrintf("[%u, {\"populate\": true}]", window_id),
browser())));
EXPECT_EQ(window_id, api_test_utils::GetInteger(result.get(), "id"));
// "populate" was enabled so tabs should be populated.
base::ListValue* tabs = NULL;
EXPECT_TRUE(result.get()->GetList(keys::kTabsKey, &tabs));
// TODO(aa): Can't assume window is focused. On mac, calling Activate() from a
// browser test doesn't seem to do anything, so can't test the opposite
// either.
EXPECT_EQ(browser()->window()->IsActive(),
api_test_utils::GetBoolean(result.get(), "focused"));
// TODO(aa): Minimized and maximized dimensions. Is there a way to set
// minimize/maximize programmatically?
// Popup.
Browser* popup_browser = new Browser(
Browser::CreateParams(Browser::TYPE_POPUP, browser()->profile(),
browser()->host_desktop_type()));
function = new WindowsGetFunction();
function->set_extension(extension.get());
result.reset(utils::ToDictionary(
utils::RunFunctionAndReturnSingleResult(
function.get(),
base::StringPrintf(
"[%u]", ExtensionTabUtil::GetWindowId(popup_browser)),
browser())));
EXPECT_EQ("popup", api_test_utils::GetString(result.get(), "type"));
// Incognito.
Browser* incognito_browser = CreateIncognitoBrowser();
int incognito_window_id = ExtensionTabUtil::GetWindowId(incognito_browser);
// Without "include_incognito".
function = new WindowsGetFunction();
function->set_extension(extension.get());
EXPECT_TRUE(MatchPattern(
utils::RunFunctionAndReturnError(
function.get(),
base::StringPrintf("[%u]", incognito_window_id),
browser()),
keys::kWindowNotFoundError));
// With "include_incognito".
function = new WindowsGetFunction();
function->set_extension(extension.get());
result.reset(utils::ToDictionary(
utils::RunFunctionAndReturnSingleResult(
function.get(),
base::StringPrintf("[%u]", incognito_window_id),
browser(),
utils::INCLUDE_INCOGNITO)));
EXPECT_TRUE(api_test_utils::GetBoolean(result.get(), "incognito"));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetCurrentWindow) {
int window_id = ExtensionTabUtil::GetWindowId(browser());
Browser* new_browser = CreateBrowser(browser()->profile());
int new_id = ExtensionTabUtil::GetWindowId(new_browser);
// Get the current window using new_browser.
scoped_refptr<WindowsGetCurrentFunction> function =
new WindowsGetCurrentFunction();
scoped_refptr<Extension> extension(test_util::CreateEmptyExtension());
function->set_extension(extension.get());
scoped_ptr<base::DictionaryValue> result(utils::ToDictionary(
utils::RunFunctionAndReturnSingleResult(function.get(),
"[]",
new_browser)));
// The id should match the window id of the browser instance that was passed
// to RunFunctionAndReturnSingleResult.
EXPECT_EQ(new_id, api_test_utils::GetInteger(result.get(), "id"));
base::ListValue* tabs = NULL;
EXPECT_FALSE(result.get()->GetList(keys::kTabsKey, &tabs));
// Get the current window using the old window and make the tabs populated.
function = new WindowsGetCurrentFunction();
function->set_extension(extension.get());
result.reset(utils::ToDictionary(
utils::RunFunctionAndReturnSingleResult(function.get(),
"[{\"populate\": true}]",
browser())));
// The id should match the window id of the browser instance that was passed
// to RunFunctionAndReturnSingleResult.
EXPECT_EQ(window_id, api_test_utils::GetInteger(result.get(), "id"));
// "populate" was enabled so tabs should be populated.
EXPECT_TRUE(result.get()->GetList(keys::kTabsKey, &tabs));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetAllWindows) {
const size_t NUM_WINDOWS = 5;
std::set<int> window_ids;
std::set<int> result_ids;
window_ids.insert(ExtensionTabUtil::GetWindowId(browser()));
for (size_t i = 0; i < NUM_WINDOWS - 1; ++i) {
Browser* new_browser = CreateBrowser(browser()->profile());
window_ids.insert(ExtensionTabUtil::GetWindowId(new_browser));
}
// Undocked DevTools window should not be accessible.
DevToolsWindow* devtools = DevToolsWindowTesting::OpenDevToolsWindowSync(
browser()->tab_strip_model()->GetWebContentsAt(0), false /* is_docked */);
scoped_refptr<WindowsGetAllFunction> function = new WindowsGetAllFunction();
scoped_refptr<Extension> extension(test_util::CreateEmptyExtension());
function->set_extension(extension.get());
scoped_ptr<base::ListValue> result(utils::ToList(
utils::RunFunctionAndReturnSingleResult(function.get(),
"[]",
browser())));
base::ListValue* windows = result.get();
EXPECT_EQ(NUM_WINDOWS, windows->GetSize());
for (size_t i = 0; i < NUM_WINDOWS; ++i) {
base::DictionaryValue* result_window = NULL;
EXPECT_TRUE(windows->GetDictionary(i, &result_window));
result_ids.insert(api_test_utils::GetInteger(result_window, "id"));
// "populate" was not passed in so tabs are not populated.
base::ListValue* tabs = NULL;
EXPECT_FALSE(result_window->GetList(keys::kTabsKey, &tabs));
}
// The returned ids should contain all the current browser instance ids.
EXPECT_EQ(window_ids, result_ids);
result_ids.clear();
function = new WindowsGetAllFunction();
function->set_extension(extension.get());
result.reset(utils::ToList(
utils::RunFunctionAndReturnSingleResult(function.get(),
"[{\"populate\": true}]",
browser())));
windows = result.get();
EXPECT_EQ(NUM_WINDOWS, windows->GetSize());
for (size_t i = 0; i < windows->GetSize(); ++i) {
base::DictionaryValue* result_window = NULL;
EXPECT_TRUE(windows->GetDictionary(i, &result_window));
result_ids.insert(api_test_utils::GetInteger(result_window, "id"));
// "populate" was enabled so tabs should be populated.
base::ListValue* tabs = NULL;
EXPECT_TRUE(result_window->GetList(keys::kTabsKey, &tabs));
}
// The returned ids should contain all the current browser instance ids.
EXPECT_EQ(window_ids, result_ids);
DevToolsWindowTesting::CloseDevToolsWindowSync(devtools);
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, UpdateNoPermissions) {
// The test empty extension has no permissions, therefore it should not get
// tab data in the function result.
scoped_refptr<TabsUpdateFunction> update_tab_function(
new TabsUpdateFunction());
scoped_refptr<Extension> empty_extension(test_util::CreateEmptyExtension());
update_tab_function->set_extension(empty_extension.get());
// Without a callback the function will not generate a result.
update_tab_function->set_has_callback(true);
scoped_ptr<base::DictionaryValue> result(utils::ToDictionary(
utils::RunFunctionAndReturnSingleResult(
update_tab_function.get(),
"[null, {\"url\": \"about:blank\", \"pinned\": true}]",
browser())));
// The url is stripped since the extension does not have tab permissions.
EXPECT_FALSE(result->HasKey("url"));
EXPECT_TRUE(api_test_utils::GetBoolean(result.get(), "pinned"));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest,
DefaultToIncognitoWhenItIsForced) {
static const char kArgsWithoutExplicitIncognitoParam[] =
"[{\"url\": \"about:blank\"}]";
// Force Incognito mode.
IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
IncognitoModePrefs::FORCED);
// Run without an explicit "incognito" param.
scoped_refptr<WindowsCreateFunction> function(new WindowsCreateFunction());
scoped_refptr<Extension> extension(test_util::CreateEmptyExtension());
function->set_extension(extension.get());
scoped_ptr<base::DictionaryValue> result(utils::ToDictionary(
utils::RunFunctionAndReturnSingleResult(
function.get(),
kArgsWithoutExplicitIncognitoParam,
browser(),
utils::INCLUDE_INCOGNITO)));
// Make sure it is a new(different) window.
EXPECT_NE(ExtensionTabUtil::GetWindowId(browser()),
api_test_utils::GetInteger(result.get(), "id"));
// ... and it is incognito.
EXPECT_TRUE(api_test_utils::GetBoolean(result.get(), "incognito"));
// Now try creating a window from incognito window.
Browser* incognito_browser = CreateIncognitoBrowser();
// Run without an explicit "incognito" param.
function = new WindowsCreateFunction();
function->set_extension(extension.get());
result.reset(utils::ToDictionary(
utils::RunFunctionAndReturnSingleResult(
function.get(),
kArgsWithoutExplicitIncognitoParam,
incognito_browser,
utils::INCLUDE_INCOGNITO)));
// Make sure it is a new(different) window.
EXPECT_NE(ExtensionTabUtil::GetWindowId(incognito_browser),
api_test_utils::GetInteger(result.get(), "id"));
// ... and it is incognito.
EXPECT_TRUE(api_test_utils::GetBoolean(result.get(), "incognito"));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest,
DefaultToIncognitoWhenItIsForcedAndNoArgs) {
static const char kEmptyArgs[] = "[]";
// Force Incognito mode.
IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
IncognitoModePrefs::FORCED);
// Run without an explicit "incognito" param.
scoped_refptr<WindowsCreateFunction> function = new WindowsCreateFunction();
scoped_refptr<Extension> extension(test_util::CreateEmptyExtension());
function->set_extension(extension.get());
scoped_ptr<base::DictionaryValue> result(utils::ToDictionary(
utils::RunFunctionAndReturnSingleResult(function.get(),
kEmptyArgs,
browser(),
utils::INCLUDE_INCOGNITO)));
// Make sure it is a new(different) window.
EXPECT_NE(ExtensionTabUtil::GetWindowId(browser()),
api_test_utils::GetInteger(result.get(), "id"));
// ... and it is incognito.
EXPECT_TRUE(api_test_utils::GetBoolean(result.get(), "incognito"));
// Now try creating a window from incognito window.
Browser* incognito_browser = CreateIncognitoBrowser();
// Run without an explicit "incognito" param.
function = new WindowsCreateFunction();
function->set_extension(extension.get());
result.reset(utils::ToDictionary(
utils::RunFunctionAndReturnSingleResult(function.get(),
kEmptyArgs,
incognito_browser,
utils::INCLUDE_INCOGNITO)));
// Make sure it is a new(different) window.
EXPECT_NE(ExtensionTabUtil::GetWindowId(incognito_browser),
api_test_utils::GetInteger(result.get(), "id"));
// ... and it is incognito.
EXPECT_TRUE(api_test_utils::GetBoolean(result.get(), "incognito"));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest,
DontCreateNormalWindowWhenIncognitoForced) {
static const char kArgsWithExplicitIncognitoParam[] =
"[{\"url\": \"about:blank\", \"incognito\": false }]";
// Force Incognito mode.
IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
IncognitoModePrefs::FORCED);
// Run with an explicit "incognito" param.
scoped_refptr<WindowsCreateFunction> function = new WindowsCreateFunction();
scoped_refptr<Extension> extension(test_util::CreateEmptyExtension());
function->set_extension(extension.get());
EXPECT_TRUE(MatchPattern(
utils::RunFunctionAndReturnError(function.get(),
kArgsWithExplicitIncognitoParam,
browser()),
keys::kIncognitoModeIsForced));
// Now try opening a normal window from incognito window.
Browser* incognito_browser = CreateIncognitoBrowser();
// Run with an explicit "incognito" param.
function = new WindowsCreateFunction();
function->set_extension(extension.get());
EXPECT_TRUE(MatchPattern(
utils::RunFunctionAndReturnError(function.get(),
kArgsWithExplicitIncognitoParam,
incognito_browser),
keys::kIncognitoModeIsForced));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest,
DontCreateIncognitoWindowWhenIncognitoDisabled) {
static const char kArgs[] =
"[{\"url\": \"about:blank\", \"incognito\": true }]";
Browser* incognito_browser = CreateIncognitoBrowser();
// Disable Incognito mode.
IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
IncognitoModePrefs::DISABLED);
// Run in normal window.
scoped_refptr<WindowsCreateFunction> function = new WindowsCreateFunction();
scoped_refptr<Extension> extension(test_util::CreateEmptyExtension());
function->set_extension(extension.get());
EXPECT_TRUE(MatchPattern(
utils::RunFunctionAndReturnError(function.get(),
kArgs,
browser()),
keys::kIncognitoModeIsDisabled));
// Run in incognito window.
function = new WindowsCreateFunction();
function->set_extension(extension.get());
EXPECT_TRUE(MatchPattern(
utils::RunFunctionAndReturnError(function.get(),
kArgs,
incognito_browser),
keys::kIncognitoModeIsDisabled));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, QueryCurrentWindowTabs) {
const size_t kExtraWindows = 3;
for (size_t i = 0; i < kExtraWindows; ++i)
CreateBrowser(browser()->profile());
GURL url(url::kAboutBlankURL);
AddTabAtIndex(0, url, ui::PAGE_TRANSITION_LINK);
int window_id = ExtensionTabUtil::GetWindowId(browser());
// Get tabs in the 'current' window called from non-focused browser.
scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction();
function->set_extension(test_util::CreateEmptyExtension().get());
scoped_ptr<base::ListValue> result(utils::ToList(
utils::RunFunctionAndReturnSingleResult(function.get(),
"[{\"currentWindow\":true}]",
browser())));
base::ListValue* result_tabs = result.get();
// We should have one initial tab and one added tab.
EXPECT_EQ(2u, result_tabs->GetSize());
for (size_t i = 0; i < result_tabs->GetSize(); ++i) {
base::DictionaryValue* result_tab = NULL;
EXPECT_TRUE(result_tabs->GetDictionary(i, &result_tab));
EXPECT_EQ(window_id,
api_test_utils::GetInteger(result_tab, keys::kWindowIdKey));
}
// Get tabs NOT in the 'current' window called from non-focused browser.
function = new TabsQueryFunction();
function->set_extension(test_util::CreateEmptyExtension().get());
result.reset(utils::ToList(
utils::RunFunctionAndReturnSingleResult(function.get(),
"[{\"currentWindow\":false}]",
browser())));
result_tabs = result.get();
// We should have one tab for each extra window.
EXPECT_EQ(kExtraWindows, result_tabs->GetSize());
for (size_t i = 0; i < kExtraWindows; ++i) {
base::DictionaryValue* result_tab = NULL;
EXPECT_TRUE(result_tabs->GetDictionary(i, &result_tab));
EXPECT_NE(window_id,
api_test_utils::GetInteger(result_tab, keys::kWindowIdKey));
}
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, QueryAllTabsWithDevTools) {
const size_t kNumWindows = 3;
std::set<int> window_ids;
window_ids.insert(ExtensionTabUtil::GetWindowId(browser()));
for (size_t i = 0; i < kNumWindows - 1; ++i) {
Browser* new_browser = CreateBrowser(browser()->profile());
window_ids.insert(ExtensionTabUtil::GetWindowId(new_browser));
}
// Undocked DevTools window should not be accessible.
DevToolsWindow* devtools = DevToolsWindowTesting::OpenDevToolsWindowSync(
browser()->tab_strip_model()->GetWebContentsAt(0), false /* is_docked */);
// Get tabs in the 'current' window called from non-focused browser.
scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction();
function->set_extension(test_util::CreateEmptyExtension().get());
scoped_ptr<base::ListValue> result(utils::ToList(
utils::RunFunctionAndReturnSingleResult(function.get(),
"[{}]",
browser())));
std::set<int> result_ids;
base::ListValue* result_tabs = result.get();
// We should have one tab per browser except for DevTools.
EXPECT_EQ(kNumWindows, result_tabs->GetSize());
for (size_t i = 0; i < result_tabs->GetSize(); ++i) {
base::DictionaryValue* result_tab = NULL;
EXPECT_TRUE(result_tabs->GetDictionary(i, &result_tab));
result_ids.insert(
api_test_utils::GetInteger(result_tab, keys::kWindowIdKey));
}
EXPECT_EQ(window_ids, result_ids);
DevToolsWindowTesting::CloseDevToolsWindowSync(devtools);
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DontCreateTabInClosingPopupWindow) {
// Test creates new popup window, closes it right away and then tries to open
// a new tab in it. Tab should not be opened in the popup window, but in a
// tabbed browser window.
Browser* popup_browser = new Browser(
Browser::CreateParams(Browser::TYPE_POPUP, browser()->profile(),
browser()->host_desktop_type()));
int window_id = ExtensionTabUtil::GetWindowId(popup_browser);
chrome::CloseWindow(popup_browser);
scoped_refptr<TabsCreateFunction> create_tab_function(
new TabsCreateFunction());
create_tab_function->set_extension(test_util::CreateEmptyExtension().get());
// Without a callback the function will not generate a result.
create_tab_function->set_has_callback(true);
static const char kNewBlankTabArgs[] =
"[{\"url\": \"about:blank\", \"windowId\": %u}]";
scoped_ptr<base::DictionaryValue> result(utils::ToDictionary(
utils::RunFunctionAndReturnSingleResult(
create_tab_function.get(),
base::StringPrintf(kNewBlankTabArgs, window_id),
browser())));
EXPECT_NE(window_id, api_test_utils::GetInteger(result.get(), "windowId"));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, InvalidUpdateWindowState) {
int window_id = ExtensionTabUtil::GetWindowId(browser());
static const char kArgsMinimizedWithFocus[] =
"[%u, {\"state\": \"minimized\", \"focused\": true}]";
scoped_refptr<WindowsUpdateFunction> function = new WindowsUpdateFunction();
scoped_refptr<Extension> extension(test_util::CreateEmptyExtension());
function->set_extension(extension.get());
EXPECT_TRUE(MatchPattern(
utils::RunFunctionAndReturnError(
function.get(),
base::StringPrintf(kArgsMinimizedWithFocus, window_id),
browser()),
keys::kInvalidWindowStateError));
static const char kArgsMaximizedWithoutFocus[] =
"[%u, {\"state\": \"maximized\", \"focused\": false}]";
function = new WindowsUpdateFunction();
function->set_extension(extension.get());
EXPECT_TRUE(MatchPattern(
utils::RunFunctionAndReturnError(
function.get(),
base::StringPrintf(kArgsMaximizedWithoutFocus, window_id),
browser()),
keys::kInvalidWindowStateError));
static const char kArgsMinimizedWithBounds[] =
"[%u, {\"state\": \"minimized\", \"width\": 500}]";
function = new WindowsUpdateFunction();
function->set_extension(extension.get());
EXPECT_TRUE(MatchPattern(
utils::RunFunctionAndReturnError(
function.get(),
base::StringPrintf(kArgsMinimizedWithBounds, window_id),
browser()),
keys::kInvalidWindowStateError));
static const char kArgsMaximizedWithBounds[] =
"[%u, {\"state\": \"maximized\", \"width\": 500}]";
function = new WindowsUpdateFunction();
function->set_extension(extension.get());
EXPECT_TRUE(MatchPattern(
utils::RunFunctionAndReturnError(
function.get(),
base::StringPrintf(kArgsMaximizedWithBounds, window_id),
browser()),
keys::kInvalidWindowStateError));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DuplicateTab) {
content::OpenURLParams params(GURL(url::kAboutBlankURL),
content::Referrer(),
NEW_FOREGROUND_TAB,
ui::PAGE_TRANSITION_LINK,
false);
content::WebContents* web_contents = browser()->OpenURL(params);
int tab_id = ExtensionTabUtil::GetTabId(web_contents);
int window_id = ExtensionTabUtil::GetWindowIdOfTab(web_contents);
int tab_index = -1;
TabStripModel* tab_strip;
ExtensionTabUtil::GetTabStripModel(web_contents, &tab_strip, &tab_index);
scoped_refptr<TabsDuplicateFunction> duplicate_tab_function(
new TabsDuplicateFunction());
scoped_ptr<base::DictionaryValue> test_extension_value(
api_test_utils::ParseDictionary(
"{\"name\": \"Test\", \"version\": \"1.0\", \"permissions\": "
"[\"tabs\"]}"));
scoped_refptr<Extension> empty_tab_extension(
api_test_utils::CreateExtension(test_extension_value.get()));
duplicate_tab_function->set_extension(empty_tab_extension.get());
duplicate_tab_function->set_has_callback(true);
scoped_ptr<base::DictionaryValue> duplicate_result(utils::ToDictionary(
utils::RunFunctionAndReturnSingleResult(
duplicate_tab_function.get(), base::StringPrintf("[%u]", tab_id),
browser())));
int duplicate_tab_id =
api_test_utils::GetInteger(duplicate_result.get(), "id");
int duplicate_tab_window_id =
api_test_utils::GetInteger(duplicate_result.get(), "windowId");
int duplicate_tab_index =
api_test_utils::GetInteger(duplicate_result.get(), "index");
EXPECT_EQ(base::Value::TYPE_DICTIONARY, duplicate_result->GetType());
// Duplicate tab id should be different from the original tab id.
EXPECT_NE(tab_id, duplicate_tab_id);
EXPECT_EQ(window_id, duplicate_tab_window_id);
EXPECT_EQ(tab_index + 1, duplicate_tab_index);
// The test empty tab extension has tabs permissions, therefore
// |duplicate_result| should contain url, title, and faviconUrl
// in the function result.
EXPECT_TRUE(utils::HasPrivacySensitiveFields(duplicate_result.get()));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DuplicateTabNoPermission) {
content::OpenURLParams params(GURL(url::kAboutBlankURL),
content::Referrer(),
NEW_FOREGROUND_TAB,
ui::PAGE_TRANSITION_LINK,
false);
content::WebContents* web_contents = browser()->OpenURL(params);
int tab_id = ExtensionTabUtil::GetTabId(web_contents);
int window_id = ExtensionTabUtil::GetWindowIdOfTab(web_contents);
int tab_index = -1;
TabStripModel* tab_strip;
ExtensionTabUtil::GetTabStripModel(web_contents, &tab_strip, &tab_index);
scoped_refptr<TabsDuplicateFunction> duplicate_tab_function(
new TabsDuplicateFunction());
scoped_refptr<Extension> empty_extension(test_util::CreateEmptyExtension());
duplicate_tab_function->set_extension(empty_extension.get());
duplicate_tab_function->set_has_callback(true);
scoped_ptr<base::DictionaryValue> duplicate_result(utils::ToDictionary(
utils::RunFunctionAndReturnSingleResult(
duplicate_tab_function.get(), base::StringPrintf("[%u]", tab_id),
browser())));
int duplicate_tab_id =
api_test_utils::GetInteger(duplicate_result.get(), "id");
int duplicate_tab_window_id =
api_test_utils::GetInteger(duplicate_result.get(), "windowId");
int duplicate_tab_index =
api_test_utils::GetInteger(duplicate_result.get(), "index");
EXPECT_EQ(base::Value::TYPE_DICTIONARY, duplicate_result->GetType());
// Duplicate tab id should be different from the original tab id.
EXPECT_NE(tab_id, duplicate_tab_id);
EXPECT_EQ(window_id, duplicate_tab_window_id);
EXPECT_EQ(tab_index + 1, duplicate_tab_index);
// The test empty extension has no permissions, therefore |duplicate_result|
// should not contain url, title, and faviconUrl in the function result.
EXPECT_FALSE(utils::HasPrivacySensitiveFields(duplicate_result.get()));
}
// Tester class for the tabs.zoom* api functions.
class ExtensionTabsZoomTest : public ExtensionTabsTest {
public:
void SetUpOnMainThread() override;
// Runs chrome.tabs.setZoom().
bool RunSetZoom(int tab_id, double zoom_factor);
// Runs chrome.tabs.getZoom().
testing::AssertionResult RunGetZoom(int tab_id, double* zoom_factor);
// Runs chrome.tabs.setZoomSettings().
bool RunSetZoomSettings(int tab_id, const char* mode, const char* scope);
// Runs chrome.tabs.getZoomSettings().
testing::AssertionResult RunGetZoomSettings(int tab_id,
std::string* mode,
std::string* scope);
// Runs chrome.tabs.setZoom(), expecting an error.
std::string RunSetZoomExpectError(int tab_id,
double zoom_factor);
// Runs chrome.tabs.setZoomSettings(), expecting an error.
std::string RunSetZoomSettingsExpectError(int tab_id,
const char* mode,
const char* scope);
content::WebContents* OpenUrlAndWaitForLoad(const GURL& url);
private:
scoped_refptr<Extension> extension_;
};
void ExtensionTabsZoomTest::SetUpOnMainThread() {
ExtensionTabsTest::SetUpOnMainThread();
extension_ = test_util::CreateEmptyExtension();
}
bool ExtensionTabsZoomTest::RunSetZoom(int tab_id, double zoom_factor) {
scoped_refptr<TabsSetZoomFunction> set_zoom_function(
new TabsSetZoomFunction());
set_zoom_function->set_extension(extension_.get());
set_zoom_function->set_has_callback(true);
return utils::RunFunction(
set_zoom_function.get(),
base::StringPrintf("[%u, %lf]", tab_id, zoom_factor),
browser(),
extension_function_test_utils::NONE);
}
testing::AssertionResult ExtensionTabsZoomTest::RunGetZoom(
int tab_id,
double* zoom_factor) {
scoped_refptr<TabsGetZoomFunction> get_zoom_function(
new TabsGetZoomFunction());
get_zoom_function->set_extension(extension_.get());
get_zoom_function->set_has_callback(true);
scoped_ptr<base::Value> get_zoom_result(
utils::RunFunctionAndReturnSingleResult(
get_zoom_function.get(),
base::StringPrintf("[%u]", tab_id),
browser()));
if (!get_zoom_result)
return testing::AssertionFailure() << "no result";
if (!get_zoom_result->GetAsDouble(zoom_factor))
return testing::AssertionFailure() << "result was not a double";
return testing::AssertionSuccess();
}
bool ExtensionTabsZoomTest::RunSetZoomSettings(int tab_id,
const char* mode,
const char* scope) {
scoped_refptr<TabsSetZoomSettingsFunction> set_zoom_settings_function(
new TabsSetZoomSettingsFunction());
set_zoom_settings_function->set_extension(extension_.get());
std::string args;
if (scope) {
args = base::StringPrintf("[%u, {\"mode\": \"%s\", \"scope\": \"%s\"}]",
tab_id, mode, scope);
} else {
args = base::StringPrintf("[%u, {\"mode\": \"%s\"}]", tab_id, mode);
}
return utils::RunFunction(set_zoom_settings_function.get(),
args,
browser(),
extension_function_test_utils::NONE);
}
testing::AssertionResult ExtensionTabsZoomTest::RunGetZoomSettings(
int tab_id,
std::string* mode,
std::string* scope) {
DCHECK(mode);
DCHECK(scope);
scoped_refptr<TabsGetZoomSettingsFunction> get_zoom_settings_function(
new TabsGetZoomSettingsFunction());
get_zoom_settings_function->set_extension(extension_.get());
get_zoom_settings_function->set_has_callback(true);
scoped_ptr<base::DictionaryValue> get_zoom_settings_result(
utils::ToDictionary(utils::RunFunctionAndReturnSingleResult(
get_zoom_settings_function.get(),
base::StringPrintf("[%u]", tab_id),
browser())));
if (!get_zoom_settings_result)
return testing::AssertionFailure() << "no result";
*mode = api_test_utils::GetString(get_zoom_settings_result.get(), "mode");
*scope = api_test_utils::GetString(get_zoom_settings_result.get(), "scope");
return testing::AssertionSuccess();
}
std::string ExtensionTabsZoomTest::RunSetZoomExpectError(int tab_id,
double zoom_factor) {
scoped_refptr<TabsSetZoomFunction> set_zoom_function(
new TabsSetZoomFunction());
set_zoom_function->set_extension(extension_.get());
set_zoom_function->set_has_callback(true);
return utils::RunFunctionAndReturnError(
set_zoom_function.get(),
base::StringPrintf("[%u, %lf]", tab_id, zoom_factor),
browser());
}
std::string ExtensionTabsZoomTest::RunSetZoomSettingsExpectError(
int tab_id,
const char* mode,
const char* scope) {
scoped_refptr<TabsSetZoomSettingsFunction> set_zoom_settings_function(
new TabsSetZoomSettingsFunction());
set_zoom_settings_function->set_extension(extension_.get());
return utils::RunFunctionAndReturnError(set_zoom_settings_function.get(),
base::StringPrintf(
"[%u, {\"mode\": \"%s\", "
"\"scope\": \"%s\"}]",
tab_id,
mode,
scope),
browser());
}
content::WebContents* ExtensionTabsZoomTest::OpenUrlAndWaitForLoad(
const GURL& url) {
ui_test_utils::NavigateToURLWithDisposition(
browser(),
url,
NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
return browser()->tab_strip_model()->GetActiveWebContents();
}
namespace {
double GetZoomLevel(const content::WebContents* web_contents) {
return ui_zoom::ZoomController::FromWebContents(web_contents)->GetZoomLevel();
}
content::OpenURLParams GetOpenParams(const char* url) {
return content::OpenURLParams(GURL(url),
content::Referrer(),
NEW_FOREGROUND_TAB,
ui::PAGE_TRANSITION_LINK,
false);
}
} // namespace
IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, SetAndGetZoom) {
content::OpenURLParams params(GetOpenParams(url::kAboutBlankURL));
content::WebContents* web_contents = OpenUrlAndWaitForLoad(params.url);
int tab_id = ExtensionTabUtil::GetTabId(web_contents);
// Test default values before we set anything.
double zoom_factor = -1;
EXPECT_TRUE(RunGetZoom(tab_id, &zoom_factor));
EXPECT_EQ(1.0, zoom_factor);
// Test chrome.tabs.setZoom().
const double kZoomLevel = 0.8;
EXPECT_TRUE(RunSetZoom(tab_id, kZoomLevel));
EXPECT_EQ(kZoomLevel,
content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents)));
// Test chrome.tabs.getZoom().
zoom_factor = -1;
EXPECT_TRUE(RunGetZoom(tab_id, &zoom_factor));
EXPECT_EQ(kZoomLevel, zoom_factor);
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, ZoomSettings) {
// In this test we need two URLs that (1) represent real pages (i.e. they
// load without causing an error page load), (2) have different domains, and
// (3) are zoomable by the extension API (this last condition rules out
// chrome:// urls). We achieve this by noting that about:blank meets these
// requirements, allowing us to spin up a spawned http server on localhost to
// get the other domain.
net::SpawnedTestServer http_server(
net::SpawnedTestServer::TYPE_HTTP,
net::SpawnedTestServer::kLocalhost,
base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
ASSERT_TRUE(http_server.Start());
GURL url_A = http_server.GetURL("files/simple.html");
GURL url_B("about:blank");
// Tabs A1 and A2 are navigated to the same origin, while B is navigated
// to a different one.
content::WebContents* web_contents_A1 = OpenUrlAndWaitForLoad(url_A);
content::WebContents* web_contents_A2 = OpenUrlAndWaitForLoad(url_A);
content::WebContents* web_contents_B = OpenUrlAndWaitForLoad(url_B);
int tab_id_A1 = ExtensionTabUtil::GetTabId(web_contents_A1);
int tab_id_A2 = ExtensionTabUtil::GetTabId(web_contents_A2);
int tab_id_B = ExtensionTabUtil::GetTabId(web_contents_B);
ASSERT_FLOAT_EQ(
1.f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1)));
ASSERT_FLOAT_EQ(
1.f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2)));
ASSERT_FLOAT_EQ(
1.f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_B)));
// Test per-origin automatic zoom settings.
EXPECT_TRUE(RunSetZoom(tab_id_B, 1.f));
EXPECT_TRUE(RunSetZoom(tab_id_A2, 1.1f));
EXPECT_FLOAT_EQ(
1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1)));
EXPECT_FLOAT_EQ(
1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2)));
EXPECT_FLOAT_EQ(1.f,
content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_B)));
// Test per-tab automatic zoom settings.
EXPECT_TRUE(RunSetZoomSettings(tab_id_A1, "automatic", "per-tab"));
EXPECT_TRUE(RunSetZoom(tab_id_A1, 1.2f));
EXPECT_FLOAT_EQ(
1.2f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1)));
EXPECT_FLOAT_EQ(
1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2)));
// Test 'manual' mode.
EXPECT_TRUE(RunSetZoomSettings(tab_id_A1, "manual", NULL));
EXPECT_TRUE(RunSetZoom(tab_id_A1, 1.3f));
EXPECT_FLOAT_EQ(
1.3f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1)));
EXPECT_FLOAT_EQ(
1.1f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2)));
// Test 'disabled' mode, which will reset A1's zoom to 1.f.
EXPECT_TRUE(RunSetZoomSettings(tab_id_A1, "disabled", NULL));
std::string error = RunSetZoomExpectError(tab_id_A1, 1.4f);
EXPECT_TRUE(MatchPattern(error, keys::kCannotZoomDisabledTabError));
EXPECT_FLOAT_EQ(
1.f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1)));
// We should still be able to zoom A2 though.
EXPECT_TRUE(RunSetZoom(tab_id_A2, 1.4f));
EXPECT_FLOAT_EQ(
1.4f, content::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2)));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, PerTabResetsOnNavigation) {
net::SpawnedTestServer http_server(
net::SpawnedTestServer::TYPE_HTTP,
net::SpawnedTestServer::kLocalhost,
base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
ASSERT_TRUE(http_server.Start());
GURL url_A = http_server.GetURL("files/simple.html");
GURL url_B("about:blank");
content::WebContents* web_contents = OpenUrlAndWaitForLoad(url_A);
int tab_id = ExtensionTabUtil::GetTabId(web_contents);
EXPECT_TRUE(RunSetZoomSettings(tab_id, "automatic", "per-tab"));
std::string mode;
std::string scope;
EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope));
EXPECT_EQ("automatic", mode);
EXPECT_EQ("per-tab", scope);
// Navigation of tab should reset mode to per-origin.
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), url_B,
1);
EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope));
EXPECT_EQ("automatic", mode);
EXPECT_EQ("per-origin", scope);
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, GetZoomSettings) {
content::OpenURLParams params(GetOpenParams(url::kAboutBlankURL));
content::WebContents* web_contents = OpenUrlAndWaitForLoad(params.url);
int tab_id = ExtensionTabUtil::GetTabId(web_contents);
std::string mode;
std::string scope;
EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope));
EXPECT_EQ("automatic", mode);
EXPECT_EQ("per-origin", scope);
EXPECT_TRUE(RunSetZoomSettings(tab_id, "automatic", "per-tab"));
EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope));
EXPECT_EQ("automatic", mode);
EXPECT_EQ("per-tab", scope);
std::string error =
RunSetZoomSettingsExpectError(tab_id, "manual", "per-origin");
EXPECT_TRUE(MatchPattern(error,
keys::kPerOriginOnlyInAutomaticError));
error =
RunSetZoomSettingsExpectError(tab_id, "disabled", "per-origin");
EXPECT_TRUE(MatchPattern(error,
keys::kPerOriginOnlyInAutomaticError));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, CannotZoomInvalidTab) {
content::OpenURLParams params(GetOpenParams(url::kAboutBlankURL));
content::WebContents* web_contents = OpenUrlAndWaitForLoad(params.url);
int tab_id = ExtensionTabUtil::GetTabId(web_contents);
int bogus_id = tab_id + 100;
std::string error = RunSetZoomExpectError(bogus_id, 3.14159);
EXPECT_TRUE(MatchPattern(error, keys::kTabNotFoundError));
error = RunSetZoomSettingsExpectError(bogus_id, "manual", "per-tab");
EXPECT_TRUE(MatchPattern(error, keys::kTabNotFoundError));
const char kNewTestTabArgs[] = "chrome://version";
params = GetOpenParams(kNewTestTabArgs);
web_contents = browser()->OpenURL(params);
tab_id = ExtensionTabUtil::GetTabId(web_contents);
// Test chrome.tabs.setZoom().
error = RunSetZoomExpectError(tab_id, 3.14159);
EXPECT_TRUE(MatchPattern(error, manifest_errors::kCannotAccessChromeUrl));
// chrome.tabs.setZoomSettings().
error = RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab");
EXPECT_TRUE(MatchPattern(error, manifest_errors::kCannotAccessChromeUrl));
}
} // namespace extensions
|