- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / bookmarks / bookmark_bubble_sign_in_delegate_unittest.cc
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/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"
17
18 class BookmarkBubbleSignInDelegateTest : public BrowserWithTestWindowTest {
19  public:
20   BookmarkBubbleSignInDelegateTest() {}
21
22  protected:
23   class Window : public TestBrowserWindow {
24    public:
25     Window() : show_count_(0) {}
26
27     int show_count() { return show_count_; }
28
29    private:
30     // TestBrowserWindow:
31     virtual void Show() OVERRIDE {
32       ++show_count_;
33     }
34
35     // Number of times that the Show() method has been called.
36     int show_count_;
37
38     DISALLOW_COPY_AND_ASSIGN(Window);
39   };
40
41   virtual BrowserWindow* CreateBrowserWindow() OVERRIDE {
42     return new Window();
43   }
44
45  private:
46   DISALLOW_COPY_AND_ASSIGN(BookmarkBubbleSignInDelegateTest);
47 };
48
49 TEST_F(BookmarkBubbleSignInDelegateTest, OnSignInLinkClicked) {
50   int starting_tab_count = browser()->tab_strip_model()->count();
51
52   scoped_ptr<BookmarkBubbleDelegate> delegate;
53   delegate.reset(new BookmarkBubbleSignInDelegate(browser()));
54
55   delegate->OnSignInLinkClicked();
56
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);
60   EXPECT_LE(1,
61             static_cast<BookmarkBubbleSignInDelegateTest::Window*>(
62                 browser()->window())->show_count());
63 }
64
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>());
73
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));
81
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();
85
86   scoped_ptr<BookmarkBubbleDelegate> delegate;
87   delegate.reset(new BookmarkBubbleSignInDelegate(incognito_browser.get()));
88
89   delegate->OnSignInLinkClicked();
90
91   // A new tab should have been opened in the normal browser, which should be
92   // visible.
93   int tab_count_normal = browser()->tab_strip_model()->count();
94   EXPECT_EQ(starting_tab_count_normal + 1, tab_count_normal);
95   EXPECT_LE(1,
96             static_cast<BookmarkBubbleSignInDelegateTest::Window*>(
97                 browser()->window())->show_count());
98
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);
102   EXPECT_EQ(0,
103             static_cast<BookmarkBubbleSignInDelegateTest::Window*>(
104                 incognito_window.get())->show_count());
105 }
106
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());
113
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));
119
120   int starting_tab_count = extra_browser->tab_strip_model()->count();
121
122   scoped_ptr<BookmarkBubbleDelegate> delegate;
123   delegate.reset(new BookmarkBubbleSignInDelegate(browser()));
124
125   BrowserList::SetLastActive(extra_browser.get());
126
127   browser()->tab_strip_model()->CloseAllTabs();
128   set_browser(NULL);
129
130   delegate->OnSignInLinkClicked();
131
132   // A new tab should have been opened in the extra browser, which should be
133   // visible.
134   int tab_count = extra_browser->tab_strip_model()->count();
135   EXPECT_EQ(starting_tab_count + 1, tab_count);
136   EXPECT_LE(1,
137             static_cast<BookmarkBubbleSignInDelegateTest::Window*>(
138                 extra_window.get())->show_count());
139
140   // Required to avoid a crash when the browser is deleted.
141   extra_browser->tab_strip_model()->CloseAllTabs();
142 }