Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / ui / browser_unittest.cc
blob749b982882537bb270a022715fcacb0d67d2ffc1
1 // Copyright 2013 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.
5 #include "chrome/browser/ui/browser.h"
7 #include "chrome/browser/ui/browser_commands.h"
8 #include "chrome/browser/ui/tabs/tab_strip_model.h"
9 #include "chrome/test/base/browser_with_test_window_test.h"
10 #include "content/public/browser/site_instance.h"
11 #include "content/public/browser/web_contents.h"
12 #include "content/public/test/web_contents_tester.h"
14 using content::SiteInstance;
15 using content::WebContents;
16 using content::WebContentsTester;
18 class BrowserUnitTest : public BrowserWithTestWindowTest {
19 public:
20 BrowserUnitTest() {}
21 ~BrowserUnitTest() override {}
23 // Caller owns the memory.
24 WebContents* CreateTestWebContents() {
25 return WebContentsTester::CreateTestWebContents(
26 profile(), SiteInstance::Create(profile()));
29 private:
30 DISALLOW_COPY_AND_ASSIGN(BrowserUnitTest);
33 // Ensure crashed tabs are not reloaded when selected. crbug.com/232323
34 TEST_F(BrowserUnitTest, ReloadCrashedTab) {
35 TabStripModel* tab_strip_model = browser()->tab_strip_model();
37 // Start with a single foreground tab. |tab_strip_model| owns the memory.
38 WebContents* contents1 = CreateTestWebContents();
39 tab_strip_model->AppendWebContents(contents1, true);
40 WebContentsTester::For(contents1)->NavigateAndCommit(GURL("about:blank"));
41 WebContentsTester::For(contents1)->TestSetIsLoading(false);
42 EXPECT_TRUE(tab_strip_model->IsTabSelected(0));
43 EXPECT_FALSE(contents1->IsLoading());
45 // Add a second tab in the background.
46 WebContents* contents2 = CreateTestWebContents();
47 tab_strip_model->AppendWebContents(contents2, false);
48 WebContentsTester::For(contents2)->NavigateAndCommit(GURL("about:blank"));
49 WebContentsTester::For(contents2)->TestSetIsLoading(false);
50 EXPECT_EQ(2, browser()->tab_strip_model()->count());
51 EXPECT_TRUE(tab_strip_model->IsTabSelected(0));
52 EXPECT_FALSE(contents2->IsLoading());
54 // Simulate the second tab crashing.
55 contents2->SetIsCrashed(base::TERMINATION_STATUS_PROCESS_CRASHED, -1);
56 EXPECT_TRUE(contents2->IsCrashed());
58 // Selecting the second tab does not cause a load or clear the crash.
59 tab_strip_model->ActivateTabAt(1, true);
60 EXPECT_TRUE(tab_strip_model->IsTabSelected(1));
61 EXPECT_FALSE(contents2->IsLoading());
62 EXPECT_TRUE(contents2->IsCrashed());
65 class BrowserBookmarkBarTest : public BrowserWithTestWindowTest {
66 public:
67 BrowserBookmarkBarTest() {}
68 ~BrowserBookmarkBarTest() override {}
70 protected:
71 BookmarkBar::State window_bookmark_bar_state() const {
72 return static_cast<BookmarkBarStateTestBrowserWindow*>(
73 browser()->window())->bookmark_bar_state();
76 // BrowserWithTestWindowTest:
77 void SetUp() override {
78 BrowserWithTestWindowTest::SetUp();
79 static_cast<BookmarkBarStateTestBrowserWindow*>(
80 browser()->window())->set_browser(browser());
83 BrowserWindow* CreateBrowserWindow() override {
84 return new BookmarkBarStateTestBrowserWindow();
87 private:
88 class BookmarkBarStateTestBrowserWindow : public TestBrowserWindow {
89 public:
90 BookmarkBarStateTestBrowserWindow()
91 : browser_(NULL),
92 bookmark_bar_state_(BookmarkBar::HIDDEN) {}
93 ~BookmarkBarStateTestBrowserWindow() override {}
95 void set_browser(Browser* browser) { browser_ = browser; }
97 BookmarkBar::State bookmark_bar_state() const {
98 return bookmark_bar_state_;
101 private:
102 // TestBrowserWindow:
103 void BookmarkBarStateChanged(
104 BookmarkBar::AnimateChangeType change_type) override {
105 bookmark_bar_state_ = browser_->bookmark_bar_state();
106 TestBrowserWindow::BookmarkBarStateChanged(change_type);
109 void OnActiveTabChanged(content::WebContents* old_contents,
110 content::WebContents* new_contents,
111 int index,
112 int reason) override {
113 bookmark_bar_state_ = browser_->bookmark_bar_state();
114 TestBrowserWindow::OnActiveTabChanged(old_contents, new_contents, index,
115 reason);
118 Browser* browser_; // Weak ptr.
119 BookmarkBar::State bookmark_bar_state_;
121 DISALLOW_COPY_AND_ASSIGN(BookmarkBarStateTestBrowserWindow);
124 DISALLOW_COPY_AND_ASSIGN(BrowserBookmarkBarTest);
127 // Ensure bookmark bar states in Browser and BrowserWindow are in sync after
128 // Browser::ActiveTabChanged() calls BrowserWindow::OnActiveTabChanged().
129 TEST_F(BrowserBookmarkBarTest, StateOnActiveTabChanged) {
130 ASSERT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
131 ASSERT_EQ(BookmarkBar::HIDDEN, window_bookmark_bar_state());
133 GURL ntp_url("chrome://newtab");
134 GURL non_ntp_url("http://foo");
136 // Open a tab to NTP.
137 AddTab(browser(), ntp_url);
138 EXPECT_EQ(BookmarkBar::DETACHED, browser()->bookmark_bar_state());
139 EXPECT_EQ(BookmarkBar::DETACHED, window_bookmark_bar_state());
141 // Navigate 1st tab to a non-NTP URL.
142 NavigateAndCommitActiveTab(non_ntp_url);
143 EXPECT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
144 EXPECT_EQ(BookmarkBar::HIDDEN, window_bookmark_bar_state());
146 // Open a tab to NTP at index 0.
147 AddTab(browser(), ntp_url);
148 EXPECT_EQ(BookmarkBar::DETACHED, browser()->bookmark_bar_state());
149 EXPECT_EQ(BookmarkBar::DETACHED, window_bookmark_bar_state());
151 // Activate the 2nd tab which is non-NTP.
152 browser()->tab_strip_model()->ActivateTabAt(1, true);
153 EXPECT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
154 EXPECT_EQ(BookmarkBar::HIDDEN, window_bookmark_bar_state());
156 // Toggle bookmark bar while 2nd tab (non-NTP) is active.
157 chrome::ToggleBookmarkBar(browser());
158 EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());
159 EXPECT_EQ(BookmarkBar::SHOW, window_bookmark_bar_state());
161 // Activate the 1st tab which is NTP.
162 browser()->tab_strip_model()->ActivateTabAt(0, true);
163 EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());
164 EXPECT_EQ(BookmarkBar::SHOW, window_bookmark_bar_state());
166 // Activate the 2nd tab which is non-NTP.
167 browser()->tab_strip_model()->ActivateTabAt(1, true);
168 EXPECT_EQ(BookmarkBar::SHOW, browser()->bookmark_bar_state());
169 EXPECT_EQ(BookmarkBar::SHOW, window_bookmark_bar_state());