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. 4 5 #include "chrome/browser/ui/bookmarks/bookmark_bubble_sign_in_delegate.h" 6 7 #include "base/basictypes.h" 8 #include "base/command_line.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "chrome/browser/extensions/test_extension_service.h" 11 #include "chrome/browser/extensions/test_extension_system.h" 12 #include "chrome/browser/ui/bookmarks/bookmark_bubble_delegate.h" 13 #include "chrome/browser/ui/browser.h" 14 #include "chrome/browser/ui/browser_list.h" 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" 16 #include "chrome/common/chrome_switches.h" 17 #include "chrome/test/base/browser_with_test_window_test.h" 18 #include "chrome/test/base/testing_profile.h" 19 #include "ui/events/event_constants.h" 20 #include "ui/gfx/range/range.h" 21 22 class BookmarkBubbleSignInDelegateTest : public BrowserWithTestWindowTest { 23 public: 24 BookmarkBubbleSignInDelegateTest() {} 25 26 virtual void SetUp() OVERRIDE; 27 28 protected: 29 class Window : public TestBrowserWindow { 30 public: 31 Window() : show_count_(0) {} 32 33 int show_count() { return show_count_; } 34 35 private: 36 // TestBrowserWindow: 37 virtual void Show() OVERRIDE { 38 ++show_count_; 39 } 40 41 // Number of times that the Show() method has been called. 42 int show_count_; 43 44 DISALLOW_COPY_AND_ASSIGN(Window); 45 }; 46 47 virtual BrowserWindow* CreateBrowserWindow() OVERRIDE { 48 return new Window(); 49 } 50 51 private: 52 DISALLOW_COPY_AND_ASSIGN(BookmarkBubbleSignInDelegateTest); 53 }; 54 55 void BookmarkBubbleSignInDelegateTest::SetUp() { 56 BrowserWithTestWindowTest::SetUp(); 57 CommandLine* command_line = CommandLine::ForCurrentProcess(); 58 // Force web-based signin, otherwise tests will crash because inline signin 59 // involves IO thread operation. 60 // TODO(guohui): fix the test for inline signin. 61 command_line->AppendSwitch(switches::kEnableWebBasedSignin); 62 // Adds TestExtensionSystem, since signin uses the gaia auth extension. 63 static_cast<extensions::TestExtensionSystem*>( 64 extensions::ExtensionSystem::Get(profile()))->CreateExtensionService( 65 command_line, base::FilePath(), false); 66 } 67 68 TEST_F(BookmarkBubbleSignInDelegateTest, OnSignInLinkClicked) { 69 int starting_tab_count = browser()->tab_strip_model()->count(); 70 71 scoped_ptr<BookmarkBubbleDelegate> delegate; 72 delegate.reset(new BookmarkBubbleSignInDelegate(browser())); 73 74 delegate->OnSignInLinkClicked(); 75 76 // A new tab should have been opened and the browser should be visible. 77 int tab_count = browser()->tab_strip_model()->count(); 78 EXPECT_EQ(starting_tab_count + 1, tab_count); 79 EXPECT_LE(1, 80 static_cast<BookmarkBubbleSignInDelegateTest::Window*>( 81 browser()->window())->show_count()); 82 } 83 84 TEST_F(BookmarkBubbleSignInDelegateTest, OnSignInLinkClickedIncognito) { 85 scoped_ptr<BrowserWindow> incognito_window; 86 incognito_window.reset(CreateBrowserWindow()); 87 Browser::CreateParams params(browser()->profile()->GetOffTheRecordProfile(), 88 browser()->host_desktop_type()); 89 params.window = incognito_window.get(); 90 scoped_ptr<Browser> incognito_browser; 91 incognito_browser.reset(new Browser(params)); 92 93 int starting_tab_count_normal = browser()->tab_strip_model()->count(); 94 int starting_tab_count_incognito = 95 incognito_browser.get()->tab_strip_model()->count(); 96 97 scoped_ptr<BookmarkBubbleDelegate> delegate; 98 delegate.reset(new BookmarkBubbleSignInDelegate(incognito_browser.get())); 99 100 delegate->OnSignInLinkClicked(); 101 102 // A new tab should have been opened in the normal browser, which should be 103 // visible. 104 int tab_count_normal = browser()->tab_strip_model()->count(); 105 EXPECT_EQ(starting_tab_count_normal + 1, tab_count_normal); 106 EXPECT_LE(1, 107 static_cast<BookmarkBubbleSignInDelegateTest::Window*>( 108 browser()->window())->show_count()); 109 110 // No effect is expected on the incognito browser. 111 int tab_count_incognito = incognito_browser->tab_strip_model()->count(); 112 EXPECT_EQ(starting_tab_count_incognito, tab_count_incognito); 113 EXPECT_EQ(0, 114 static_cast<BookmarkBubbleSignInDelegateTest::Window*>( 115 incognito_window.get())->show_count()); 116 } 117 118 // Verifies that the sign in page can be loaded in a different browser 119 // if the provided browser is invalidated. 120 TEST_F(BookmarkBubbleSignInDelegateTest, BrowserRemoved) { 121 // Create an extra browser. 122 scoped_ptr<BrowserWindow> extra_window; 123 extra_window.reset(CreateBrowserWindow()); 124 125 Browser::CreateParams params(browser()->profile(), 126 browser()->host_desktop_type()); 127 params.window = extra_window.get(); 128 scoped_ptr<Browser> extra_browser; 129 extra_browser.reset(new Browser(params)); 130 131 int starting_tab_count = extra_browser->tab_strip_model()->count(); 132 133 scoped_ptr<BookmarkBubbleDelegate> delegate; 134 delegate.reset(new BookmarkBubbleSignInDelegate(browser())); 135 136 BrowserList::SetLastActive(extra_browser.get()); 137 138 browser()->tab_strip_model()->CloseAllTabs(); 139 set_browser(NULL); 140 141 delegate->OnSignInLinkClicked(); 142 143 // A new tab should have been opened in the extra browser, which should be 144 // visible. 145 int tab_count = extra_browser->tab_strip_model()->count(); 146 EXPECT_EQ(starting_tab_count + 1, tab_count); 147 EXPECT_LE(1, 148 static_cast<BookmarkBubbleSignInDelegateTest::Window*>( 149 extra_window.get())->show_count()); 150 151 // Required to avoid a crash when the browser is deleted. 152 extra_browser->tab_strip_model()->CloseAllTabs(); 153 } 154