diff options
author | sky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-14 15:51:10 +0000 |
---|---|---|
committer | sky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-14 15:51:10 +0000 |
commit | be3877f74da87b5be2b549f733b5705c9607ec82 (patch) | |
tree | a00fa6c37daf0467801e2cbb06660b360a6d449e /chrome/browser | |
parent | 57a020e92927a379ab514db5836cb4550e6e444b (diff) | |
download | chromium_src-be3877f74da87b5be2b549f733b5705c9607ec82.zip chromium_src-be3877f74da87b5be2b549f733b5705c9607ec82.tar.gz chromium_src-be3877f74da87b5be2b549f733b5705c9607ec82.tar.bz2 |
Provides the infrastructure for Browser unit tests that create a
BrowserWindow with only a TabStrip. I also converted two ui tests over
to unit tests to make sure it all worked. I had to add a bunch of null
checks to Browser and a couple of other places.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/17386
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8007 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/browser.cc | 37 | ||||
-rw-r--r-- | chrome/browser/browser.h | 7 | ||||
-rw-r--r-- | chrome/browser/browser_commands_unittest.cc | 74 | ||||
-rw-r--r-- | chrome/browser/browser_uitest.cc | 90 | ||||
-rw-r--r-- | chrome/browser/browser_window.h | 1 | ||||
-rw-r--r-- | chrome/browser/tab_contents.cc | 9 | ||||
-rw-r--r-- | chrome/browser/views/frame/browser_view.cc | 3 | ||||
-rw-r--r-- | chrome/browser/views/tabs/tab_strip.cc | 3 | ||||
-rw-r--r-- | chrome/browser/window_sizer.cc | 3 |
9 files changed, 125 insertions, 102 deletions
diff --git a/chrome/browser/browser.cc b/chrome/browser/browser.cc index 61ea2da..6f5cb590 100644 --- a/chrome/browser/browser.cc +++ b/chrome/browser/browser.cc @@ -246,6 +246,8 @@ void Browser::CreateBrowserWindow() { // Show the First Run information bubble if we've been told to. PrefService* local_state = g_browser_process->local_state(); + if (!local_state) + return; if (local_state->IsPrefRegistered(prefs::kShouldShowFirstRunBubble) && local_state->GetBoolean(prefs::kShouldShowFirstRunBubble)) { // Reset the preference so we don't show the bubble for subsequent windows. @@ -1430,10 +1432,13 @@ void Browser::TabSelectedAt(TabContents* old_contents, UpdateCommandsForTabState(); // Reset the status bubble. - GetStatusBubble()->Hide(); + StatusBubble* status_bubble = GetStatusBubble(); + if (status_bubble) { + status_bubble->Hide(); - // Show the loading state (if any). - GetStatusBubble()->SetStatus(GetSelectedTabContents()->GetStatusText()); + // Show the loading state (if any). + status_bubble->SetStatus(GetSelectedTabContents()->GetStatusText()); + } // Update sessions. Don't force creation of sessions. If sessions doesn't // exist, the change will be picked up by sessions when created. @@ -1567,7 +1572,8 @@ void Browser::OpenURLFromTab(TabContents* source, // The TabContents might have changed as part of the navigation (ex: new // tab page can become WebContents). new_contents = current_tab->controller()->active_contents(); - GetStatusBubble()->Hide(); + if (GetStatusBubble()) + GetStatusBubble()->Hide(); // Synchronously update the location bar. This allows us to immediately // have the URL bar update when the user types something, rather than @@ -1681,7 +1687,8 @@ void Browser::LoadingStateChanged(TabContents* source) { if (source == GetSelectedTabContents()) { UpdateStopGoState(source->is_loading()); - GetStatusBubble()->SetStatus(GetSelectedTabContents()->GetStatusText()); + if (GetStatusBubble()) + GetStatusBubble()->SetStatus(GetSelectedTabContents()->GetStatusText()); } } @@ -1729,6 +1736,9 @@ void Browser::URLStarredChanged(TabContents* source, bool starred) { } void Browser::ContentsMouseEvent(TabContents* source, UINT message) { + if (!GetStatusBubble()) + return; + if (source == GetSelectedTabContents()) { if (message == WM_MOUSEMOVE) { GetStatusBubble()->MouseMoved(); @@ -1739,6 +1749,9 @@ void Browser::ContentsMouseEvent(TabContents* source, UINT message) { } void Browser::UpdateTargetURL(TabContents* source, const GURL& url) { + if (!GetStatusBubble()) + return; + if (source == GetSelectedTabContents()) { PrefService* prefs = profile_->GetPrefs(); GetStatusBubble()->SetURL(url, prefs->GetString(prefs::kAcceptLanguages)); @@ -2052,14 +2065,22 @@ void Browser::UpdateCommandsForTabState() { } void Browser::UpdateStopGoState(bool is_loading) { - GetGoButton()->ChangeMode(is_loading ? + GoButton* go_button = GetGoButton(); + if (!go_button) + return; + + go_button->ChangeMode(is_loading ? GoButton::MODE_STOP : GoButton::MODE_GO); controller_.UpdateCommandEnabled(IDC_GO, !is_loading); controller_.UpdateCommandEnabled(IDC_STOP, is_loading); } void Browser::SetStarredButtonToggled(bool starred) { - window_->GetStarButton()->SetToggled(starred); + ToolbarStarToggle* star_button = window_->GetStarButton(); + if (!star_button) + return; + + star_button->SetToggled(starred); } /////////////////////////////////////////////////////////////////////////////// @@ -2153,7 +2174,7 @@ void Browser::ProcessPendingUIUpdates() { // Updating the URL happens synchronously in ScheduleUIUpdate. - if (flags & TabContents::INVALIDATE_LOAD) + if (flags & TabContents::INVALIDATE_LOAD && GetStatusBubble()) GetStatusBubble()->SetStatus(GetSelectedTabContents()->GetStatusText()); if (invalidate_tab) { // INVALIDATE_TITLE or INVALIDATE_FAVICON. diff --git a/chrome/browser/browser.h b/chrome/browser/browser.h index f3f2eda..fa3927d 100644 --- a/chrome/browser/browser.h +++ b/chrome/browser/browser.h @@ -82,6 +82,13 @@ class Browser : public TabStripModelDelegate, const std::vector<std::wstring>& user_data_dir_profiles() const { return g_browser_process->user_data_dir_profiles(); } + // Sets the BrowserWindow. This is intended for testing and generally not + // useful outside of testing. Use CreateBrowserWindow outside of testing, or + // the static convenience methods that create a BrowserWindow for you. + void set_window(BrowserWindow* window) { + DCHECK(!window_); + window_ = window; + } BrowserWindow* window() const { return window_; } ToolbarModel* toolbar_model() { return &toolbar_model_; } const SessionID& session_id() const { return session_id_; } diff --git a/chrome/browser/browser_commands_unittest.cc b/chrome/browser/browser_commands_unittest.cc new file mode 100644 index 0000000..13aa318 --- /dev/null +++ b/chrome/browser/browser_commands_unittest.cc @@ -0,0 +1,74 @@ +// Copyright (c) 2006-2008 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 "chrome/app/chrome_dll_resource.h" +#include "chrome/browser/browser.h" +#include "chrome/browser/browser_list.h" +#include "chrome/browser/navigation_controller.h" +#include "chrome/browser/navigation_entry.h" +#include "chrome/test/browser_with_test_window_test.h" + +typedef BrowserWithTestWindowTest BrowserCommandsTest; + +// Tests IDC_SELECT_TAB_0, IDC_SELECT_NEXT_TAB, IDC_SELECT_PREVIOUS_TAB and +// IDC_SELECT_LAST_TAB. +TEST_F(BrowserCommandsTest, TabNavigationAccelerators) { + // Create three tabs. + AddTestingTab(browser()); + AddTestingTab(browser()); + AddTestingTab(browser()); + + // Select the second tab. + browser()->SelectTabContentsAt(1, false); + + // Navigate to the first tab using an accelerator. + browser()->ExecuteCommand(IDC_SELECT_TAB_0); + ASSERT_EQ(0, browser()->selected_index()); + + // Navigate to the second tab using the next accelerators. + browser()->ExecuteCommand(IDC_SELECT_NEXT_TAB); + ASSERT_EQ(1, browser()->selected_index()); + + // Navigate back to the first tab using the previous accelerators. + browser()->ExecuteCommand(IDC_SELECT_PREVIOUS_TAB); + ASSERT_EQ(0, browser()->selected_index()); + + // Navigate to the last tab using the select last accelerator. + browser()->ExecuteCommand(IDC_SELECT_LAST_TAB); + ASSERT_EQ(2, browser()->selected_index()); +} + +// Tests IDC_DUPLICATE_TAB. +TEST_F(BrowserCommandsTest, DuplicateTab) { + GURL url1 = test_url_with_path("1"); + GURL url2 = test_url_with_path("2"); + GURL url3 = test_url_with_path("3"); + + // Navigate to the three urls, then go back. + AddTestingTab(browser()); + browser()->OpenURL(url1, GURL(), CURRENT_TAB, PageTransition::TYPED); + browser()->OpenURL(url2, GURL(), CURRENT_TAB, PageTransition::TYPED); + browser()->OpenURL(url3, GURL(), CURRENT_TAB, PageTransition::TYPED); + + size_t initial_window_count = BrowserList::size(); + + // Duplicate the tab. + browser()->ExecuteCommand(IDC_DUPLICATE_TAB); + + // The duplicated tab should not end up in a new window. + int window_count = BrowserList::size(); + ASSERT_EQ(initial_window_count, window_count); + + // And we should have a newly duplicated tab. + ASSERT_EQ(2, browser()->tab_count()); + + // Verify the stack of urls. + NavigationController* controller = + browser()->GetTabContentsAt(1)->controller(); + ASSERT_EQ(3, controller->GetEntryCount()); + ASSERT_EQ(2, controller->GetCurrentEntryIndex()); + ASSERT_TRUE(url1 == controller->GetEntryAtIndex(0)->url()); + ASSERT_TRUE(url2 == controller->GetEntryAtIndex(1)->url()); + ASSERT_TRUE(url3 == controller->GetEntryAtIndex(2)->url()); +} diff --git a/chrome/browser/browser_uitest.cc b/chrome/browser/browser_uitest.cc index b3ad491..4574c17 100644 --- a/chrome/browser/browser_uitest.cc +++ b/chrome/browser/browser_uitest.cc @@ -123,49 +123,6 @@ TEST_F(BrowserTest, WindowsSessionEnd) { ASSERT_TRUE(exited_cleanly); } -// Tests the accelerators for tab navigation. Specifically IDC_SELECT_NEXT_TAB, -// IDC_SELECT_PREV_TAB, IDC_SELECT_TAB_0, and IDC_SELECT_LAST_TAB. -TEST_F(BrowserTest, TabNavigationAccelerators) { - scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0)); - ASSERT_TRUE(window.get()); - - // Create two new tabs. This way we'll have at least three tabs to navigate - // to. - int old_tab_count = -1; - ASSERT_TRUE(window->GetTabCount(&old_tab_count)); - ASSERT_TRUE(window->ApplyAccelerator(IDC_NEW_TAB)); - int new_tab_count; - ASSERT_TRUE(window->WaitForTabCountToChange(old_tab_count, - &new_tab_count, - action_max_timeout_ms())); - ASSERT_TRUE(window->ApplyAccelerator(IDC_NEW_TAB)); - old_tab_count = new_tab_count; - ASSERT_TRUE(window->WaitForTabCountToChange(old_tab_count, - &new_tab_count, - action_max_timeout_ms())); - ASSERT_GE(new_tab_count, 2); - - // Activate the second tab. - ASSERT_TRUE(window->ActivateTab(1)); - - // Navigate to the first tab using an accelerator. - ASSERT_TRUE(window->ApplyAccelerator(IDC_SELECT_TAB_0)); - ASSERT_TRUE(window->WaitForTabToBecomeActive(0, action_max_timeout_ms())); - - // Navigate to the second tab using the next accelerators. - ASSERT_TRUE(window->ApplyAccelerator(IDC_SELECT_NEXT_TAB)); - ASSERT_TRUE(window->WaitForTabToBecomeActive(1, action_max_timeout_ms())); - - // Navigate back to the first tab using the previous accelerators. - ASSERT_TRUE(window->ApplyAccelerator(IDC_SELECT_PREVIOUS_TAB)); - ASSERT_TRUE(window->WaitForTabToBecomeActive(0, action_max_timeout_ms())); - - // Navigate to the last tab using the select last accelerator. - ASSERT_TRUE(window->ApplyAccelerator(IDC_SELECT_LAST_TAB)); - ASSERT_TRUE(window->WaitForTabToBecomeActive(new_tab_count - 1, - action_max_timeout_ms())); -} - TEST_F(BrowserTest, JavascriptAlertActivatesTab) { scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0)); int start_index; @@ -183,53 +140,6 @@ TEST_F(BrowserTest, JavascriptAlertActivatesTab) { action_max_timeout_ms())); } -TEST_F(BrowserTest, DuplicateTab) { - std::wstring path_prefix = test_data_directory_; - file_util::AppendToPath(&path_prefix, L"session_history"); - path_prefix += FilePath::kSeparators[0]; - GURL url1 = net::FilePathToFileURL(path_prefix + L"bot1.html"); - GURL url2 = net::FilePathToFileURL(path_prefix + L"bot2.html"); - GURL url3 = GURL("about:blank"); - - scoped_ptr<BrowserProxy> browser_proxy(automation()->GetBrowserWindow(0)); - - // Navigate to the three urls, then go back. - scoped_ptr<TabProxy> tab_proxy(browser_proxy->GetTab(0)); - tab_proxy->NavigateToURL(url1); - tab_proxy->NavigateToURL(url2); - tab_proxy->NavigateToURL(url3); - ASSERT_TRUE(tab_proxy->GoBack()); - - int initial_window_count; - ASSERT_TRUE(automation()->GetBrowserWindowCount(&initial_window_count)); - - // Duplicate the tab. - ASSERT_TRUE(browser_proxy->ApplyAccelerator(IDC_DUPLICATE_TAB)); - - // The duplicated tab should not end up in a new window. - int window_count; - ASSERT_TRUE(automation()->GetBrowserWindowCount(&window_count)); - ASSERT_TRUE(window_count == initial_window_count); - - tab_proxy.reset(browser_proxy->GetTab(1)); - ASSERT_TRUE(tab_proxy != NULL); - ASSERT_TRUE(tab_proxy->WaitForTabToBeRestored(action_timeout_ms())); - - // Verify the stack of urls. - GURL url; - ASSERT_TRUE(tab_proxy->GetCurrentURL(&url)); - ASSERT_EQ(url2, url); - - ASSERT_TRUE(tab_proxy->GoForward()); - ASSERT_TRUE(tab_proxy->GetCurrentURL(&url)); - ASSERT_EQ(url3, url); - - ASSERT_TRUE(tab_proxy->GoBack()); - ASSERT_TRUE(tab_proxy->GoBack()); - ASSERT_TRUE(tab_proxy->GetCurrentURL(&url)); - ASSERT_EQ(url1, url); -} - // Test that scripts can fork a new renderer process for a tab in a particular // case (which matches following a link in Gmail). The script must open a new // tab, set its window.opener to null, and redirect it to a cross-site URL. diff --git a/chrome/browser/browser_window.h b/chrome/browser/browser_window.h index 2cd1648..aec7107 100644 --- a/chrome/browser/browser_window.h +++ b/chrome/browser/browser_window.h @@ -26,6 +26,7 @@ class Rect; // BrowserWindow interface // An interface implemented by the "view" of the Browser window. // +// NOTE: all getters, save GetTabStrip(), may return NULL. class BrowserWindow { public: // Initialize the frame. diff --git a/chrome/browser/tab_contents.cc b/chrome/browser/tab_contents.cc index 66774c6..9507cd4 100644 --- a/chrome/browser/tab_contents.cc +++ b/chrome/browser/tab_contents.cc @@ -320,11 +320,14 @@ void TabContents::CloseAllSuppressedPopups() { } void TabContents::Focus() { + HWND container_hwnd = GetContainerHWND(); + if (!container_hwnd) + return; + views::FocusManager* focus_manager = - views::FocusManager::GetFocusManager(GetContainerHWND()); + views::FocusManager::GetFocusManager(container_hwnd); DCHECK(focus_manager); - views::View* v = - focus_manager->GetViewForWindow(GetContainerHWND(), true); + views::View* v = focus_manager->GetViewForWindow(container_hwnd, true); DCHECK(v); if (v) v->RequestFocus(); diff --git a/chrome/browser/views/frame/browser_view.cc b/chrome/browser/views/frame/browser_view.cc index 9d2a387..18823fd 100644 --- a/chrome/browser/views/frame/browser_view.cc +++ b/chrome/browser/views/frame/browser_view.cc @@ -1333,6 +1333,9 @@ void BrowserView::LoadingAnimationCallback() { void BrowserView::InitHangMonitor() { PrefService* pref_service = g_browser_process->local_state(); + if (!pref_service) + return; + int plugin_message_response_timeout = pref_service->GetInteger(prefs::kPluginMessageResponseTimeout); int hung_plugin_detect_freq = diff --git a/chrome/browser/views/tabs/tab_strip.cc b/chrome/browser/views/tabs/tab_strip.cc index eff78d2..c460100 100644 --- a/chrome/browser/views/tabs/tab_strip.cc +++ b/chrome/browser/views/tabs/tab_strip.cc @@ -848,7 +848,8 @@ void TabStrip::TabInsertedAt(TabContents* contents, // Don't animate the first tab, it looks weird, and don't animate anything // if the containing window isn't visible yet. - if (GetTabCount() > 1 && IsWindowVisible(GetWidget()->GetHWND())) { + if (GetTabCount() > 1 && GetWidget() && + IsWindowVisible(GetWidget()->GetHWND())) { StartInsertTabAnimation(index); } else { Layout(); diff --git a/chrome/browser/window_sizer.cc b/chrome/browser/window_sizer.cc index c6083a7..596a7c8 100644 --- a/chrome/browser/window_sizer.cc +++ b/chrome/browser/window_sizer.cc @@ -115,6 +115,9 @@ class DefaultStateProvider : public WindowSizer::StateProvider { key.append(app_name_); } + if (!g_browser_process->local_state()) + return false; + const DictionaryValue* wp_pref = g_browser_process->local_state()->GetDictionary(key.c_str()); int top = 0, left = 0, bottom = 0, right = 0; |