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/bookmarks/bookmark_bubble_sign_in_delegate.h"
7 #include "base/basictypes.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "chrome/browser/ui/bookmarks/bookmark_bubble_delegate.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_list.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/test/base/browser_with_test_window_test.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "ui/events/event_constants.h"
16 #include "ui/gfx/range/range.h"
18 class BookmarkBubbleSignInDelegateTest : public BrowserWithTestWindowTest {
20 BookmarkBubbleSignInDelegateTest() {}
23 class Window : public TestBrowserWindow {
25 Window() : show_count_(0) {}
27 int show_count() { return show_count_; }
31 virtual void Show() OVERRIDE {
35 // Number of times that the Show() method has been called.
38 DISALLOW_COPY_AND_ASSIGN(Window);
41 virtual BrowserWindow* CreateBrowserWindow() OVERRIDE {
46 DISALLOW_COPY_AND_ASSIGN(BookmarkBubbleSignInDelegateTest);
49 TEST_F(BookmarkBubbleSignInDelegateTest, OnSignInLinkClicked) {
50 int starting_tab_count = browser()->tab_strip_model()->count();
52 scoped_ptr<BookmarkBubbleDelegate> delegate;
53 delegate.reset(new BookmarkBubbleSignInDelegate(browser()));
55 delegate->OnSignInLinkClicked();
57 // A new tab should have been opened and the browser should be visible.
58 int tab_count = browser()->tab_strip_model()->count();
59 EXPECT_EQ(starting_tab_count + 1, tab_count);
61 static_cast<BookmarkBubbleSignInDelegateTest::Window*>(
62 browser()->window())->show_count());
65 TEST_F(BookmarkBubbleSignInDelegateTest, OnSignInLinkClickedIncognito) {
66 // Create an incognito browser.
67 TestingProfile::Builder incognito_profile_builder;
68 incognito_profile_builder.SetIncognito();
69 scoped_ptr<TestingProfile> incognito_profile =
70 incognito_profile_builder.Build();
71 incognito_profile->SetOriginalProfile(profile());
72 profile()->SetOffTheRecordProfile(incognito_profile.PassAs<Profile>());
74 scoped_ptr<BrowserWindow> incognito_window;
75 incognito_window.reset(CreateBrowserWindow());
76 Browser::CreateParams params(browser()->profile()->GetOffTheRecordProfile(),
77 browser()->host_desktop_type());
78 params.window = incognito_window.get();
79 scoped_ptr<Browser> incognito_browser;
80 incognito_browser.reset(new Browser(params));
82 int starting_tab_count_normal = browser()->tab_strip_model()->count();
83 int starting_tab_count_incognito =
84 incognito_browser.get()->tab_strip_model()->count();
86 scoped_ptr<BookmarkBubbleDelegate> delegate;
87 delegate.reset(new BookmarkBubbleSignInDelegate(incognito_browser.get()));
89 delegate->OnSignInLinkClicked();
91 // A new tab should have been opened in the normal browser, which should be
93 int tab_count_normal = browser()->tab_strip_model()->count();
94 EXPECT_EQ(starting_tab_count_normal + 1, tab_count_normal);
96 static_cast<BookmarkBubbleSignInDelegateTest::Window*>(
97 browser()->window())->show_count());
99 // No effect is expected on the incognito browser.
100 int tab_count_incognito = incognito_browser->tab_strip_model()->count();
101 EXPECT_EQ(starting_tab_count_incognito, tab_count_incognito);
103 static_cast<BookmarkBubbleSignInDelegateTest::Window*>(
104 incognito_window.get())->show_count());
107 // Verifies that the sign in page can be loaded in a different browser
108 // if the provided browser is invalidated.
109 TEST_F(BookmarkBubbleSignInDelegateTest, BrowserRemoved) {
110 // Create an extra browser.
111 scoped_ptr<BrowserWindow> extra_window;
112 extra_window.reset(CreateBrowserWindow());
114 Browser::CreateParams params(browser()->profile(),
115 browser()->host_desktop_type());
116 params.window = extra_window.get();
117 scoped_ptr<Browser> extra_browser;
118 extra_browser.reset(new Browser(params));
120 int starting_tab_count = extra_browser->tab_strip_model()->count();
122 scoped_ptr<BookmarkBubbleDelegate> delegate;
123 delegate.reset(new BookmarkBubbleSignInDelegate(browser()));
125 BrowserList::SetLastActive(extra_browser.get());
127 browser()->tab_strip_model()->CloseAllTabs();
130 delegate->OnSignInLinkClicked();
132 // A new tab should have been opened in the extra browser, which should be
134 int tab_count = extra_browser->tab_strip_model()->count();
135 EXPECT_EQ(starting_tab_count + 1, tab_count);
137 static_cast<BookmarkBubbleSignInDelegateTest::Window*>(
138 extra_window.get())->show_count());
140 // Required to avoid a crash when the browser is deleted.
141 extra_browser->tab_strip_model()->CloseAllTabs();