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 3 // found in the LICENSE file. 4 5 #include "chrome/test/ui/ui_test.h" 6 7 #include "base/test/test_timeouts.h" 8 #include "chrome/app/chrome_command_ids.h" 9 #include "chrome/common/url_constants.h" 10 #include "chrome/test/automation/browser_proxy.h" 11 #include "chrome/test/automation/tab_proxy.h" 12 #include "chrome/test/automation/window_proxy.h" 13 14 class BookmarksUITest : public UITest { 15 public: 16 BookmarksUITest() { 17 dom_automation_enabled_ = true; 18 } 19 20 bool WaitForBookmarksUI(TabProxy* tab) { 21 return WaitUntilJavaScriptCondition(tab, L"", 22 L"domAutomationController.send(" 23 L" location.protocol == 'chrome-extension:' && " 24 L" document.readyState == 'complete')", 25 TestTimeouts::huge_test_timeout_ms()); 26 } 27 28 scoped_refptr<TabProxy> GetBookmarksUITab() { 29 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); 30 EXPECT_TRUE(browser.get()); 31 if (!browser.get()) 32 return NULL; 33 scoped_refptr<TabProxy> tab = browser->GetActiveTab(); 34 EXPECT_TRUE(tab.get()); 35 if (!tab.get()) 36 return NULL; 37 bool success = tab->NavigateToURL(GURL(chrome::kChromeUIBookmarksURL)); 38 EXPECT_TRUE(success); 39 if (!success) 40 return NULL; 41 success = WaitForBookmarksUI(tab); 42 EXPECT_TRUE(success); 43 if (!success) 44 return NULL; 45 return tab; 46 } 47 48 void AssertIsBookmarksPage(TabProxy* tab) { 49 // tab->GetCurrentURL is not up to date. 50 GURL url; 51 std::wstring out; 52 ASSERT_TRUE(tab->ExecuteAndExtractString(L"", 53 L"domAutomationController.send(location.protocol)", &out)); 54 ASSERT_EQ(L"chrome-extension:", out); 55 ASSERT_TRUE(tab->ExecuteAndExtractString(L"", 56 L"domAutomationController.send(location.pathname)", &out)); 57 ASSERT_EQ(L"/main.html", out); 58 } 59 }; 60 61 // http://code.google.com/p/chromium/issues/detail?id=39532 62 TEST_F(BookmarksUITest, FLAKY_ShouldRedirectToExtension) { 63 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); 64 ASSERT_TRUE(browser.get()); 65 66 int tab_count = -1; 67 ASSERT_TRUE(browser->GetTabCount(&tab_count)); 68 ASSERT_EQ(1, tab_count); 69 70 // Navigate to chrome 71 scoped_refptr<TabProxy> tab = browser->GetActiveTab(); 72 ASSERT_TRUE(tab.get()); 73 74 ASSERT_TRUE(tab->NavigateToURL(GURL(chrome::kChromeUIBookmarksURL))); 75 76 // At this point the URL is chrome://bookmarks. We need to wait for the 77 // redirect to happen. 78 ASSERT_TRUE(WaitForBookmarksUI(tab)); 79 80 AssertIsBookmarksPage(tab); 81 } 82 83 TEST_F(BookmarksUITest, CommandOpensBookmarksTab) { 84 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); 85 ASSERT_TRUE(browser.get()); 86 87 int tab_count = -1; 88 ASSERT_TRUE(browser->GetTabCount(&tab_count)); 89 ASSERT_EQ(1, tab_count); 90 91 // Bring up the bookmarks manager tab. 92 ASSERT_TRUE(browser->RunCommand(IDC_SHOW_BOOKMARK_MANAGER)); 93 ASSERT_TRUE(browser->GetTabCount(&tab_count)); 94 ASSERT_EQ(2, tab_count); 95 96 scoped_refptr<TabProxy> tab = browser->GetActiveTab(); 97 ASSERT_TRUE(tab.get()); 98 99 ASSERT_TRUE(WaitForBookmarksUI(tab)); 100 101 AssertIsBookmarksPage(tab); 102 } 103 104 TEST_F(BookmarksUITest, CommandAgainGoesBackToBookmarksTab) { 105 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); 106 ASSERT_TRUE(browser.get()); 107 108 int tab_count = -1; 109 ASSERT_TRUE(browser->GetTabCount(&tab_count)); 110 ASSERT_EQ(1, tab_count); 111 112 // Bring up the bookmarks manager tab. 113 ASSERT_TRUE(browser->RunCommand(IDC_SHOW_BOOKMARK_MANAGER)); 114 ASSERT_TRUE(browser->WaitForTabToBecomeActive( 115 1, TestTimeouts::action_max_timeout_ms())); 116 ASSERT_TRUE(browser->GetTabCount(&tab_count)); 117 ASSERT_EQ(2, tab_count); 118 119 scoped_refptr<TabProxy> tab = browser->GetActiveTab(); 120 ASSERT_TRUE(tab.get()); 121 ASSERT_TRUE(WaitForBookmarksUI(tab)); 122 AssertIsBookmarksPage(tab); 123 124 // Switch to first tab and run command again. 125 ASSERT_TRUE(browser->ActivateTab(0)); 126 ASSERT_TRUE(browser->WaitForTabToBecomeActive( 127 0, TestTimeouts::action_max_timeout_ms())); 128 ASSERT_TRUE(browser->RunCommand(IDC_SHOW_BOOKMARK_MANAGER)); 129 130 // Ensure the bookmarks ui tab is active. 131 ASSERT_TRUE(browser->WaitForTabToBecomeActive( 132 1, TestTimeouts::action_max_timeout_ms())); 133 ASSERT_TRUE(browser->GetTabCount(&tab_count)); 134 ASSERT_EQ(2, tab_count); 135 } 136 137 TEST_F(BookmarksUITest, TwoCommandsOneTab) { 138 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); 139 ASSERT_TRUE(browser.get()); 140 141 int tab_count = -1; 142 ASSERT_TRUE(browser->GetTabCount(&tab_count)); 143 ASSERT_EQ(1, tab_count); 144 145 ASSERT_TRUE(browser->RunCommand(IDC_SHOW_BOOKMARK_MANAGER)); 146 ASSERT_TRUE(browser->RunCommand(IDC_SHOW_BOOKMARK_MANAGER)); 147 ASSERT_TRUE(browser->GetTabCount(&tab_count)); 148 ASSERT_EQ(2, tab_count); 149 } 150 151 TEST_F(BookmarksUITest, BookmarksLoaded) { 152 scoped_refptr<TabProxy> tab = GetBookmarksUITab(); 153 ASSERT_TRUE(tab.get()); 154 } 155