Update To 11.40.268.0
[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/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   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     void Show() override { ++show_count_; }
38
39     // Number of times that the Show() method has been called.
40     int show_count_;
41
42     DISALLOW_COPY_AND_ASSIGN(Window);
43   };
44
45   BrowserWindow* CreateBrowserWindow() override { return new Window(); }
46
47  private:
48   DISALLOW_COPY_AND_ASSIGN(BookmarkBubbleSignInDelegateTest);
49 };
50
51 void BookmarkBubbleSignInDelegateTest::SetUp() {
52   BrowserWithTestWindowTest::SetUp();
53   CommandLine* command_line = CommandLine::ForCurrentProcess();
54   // Force web-based signin, otherwise tests will crash because inline signin
55   // involves IO thread operation.
56   // TODO(guohui): fix the test for inline signin.
57   command_line->AppendSwitch(switches::kEnableWebBasedSignin);
58   // Adds TestExtensionSystem, since signin uses the gaia auth extension.
59   static_cast<extensions::TestExtensionSystem*>(
60       extensions::ExtensionSystem::Get(profile()))->CreateExtensionService(
61           command_line, base::FilePath(), false);
62 }
63
64 TEST_F(BookmarkBubbleSignInDelegateTest, OnSignInLinkClicked) {
65   int starting_tab_count = browser()->tab_strip_model()->count();
66
67   scoped_ptr<BookmarkBubbleDelegate> delegate;
68   delegate.reset(new BookmarkBubbleSignInDelegate(browser()));
69
70   delegate->OnSignInLinkClicked();
71
72   // A new tab should have been opened and the browser should be visible.
73   int tab_count = browser()->tab_strip_model()->count();
74   EXPECT_EQ(starting_tab_count + 1, tab_count);
75   EXPECT_LE(1,
76             static_cast<BookmarkBubbleSignInDelegateTest::Window*>(
77                 browser()->window())->show_count());
78 }
79
80 TEST_F(BookmarkBubbleSignInDelegateTest, OnSignInLinkClickedIncognito) {
81   scoped_ptr<BrowserWindow> incognito_window;
82   incognito_window.reset(CreateBrowserWindow());
83   Browser::CreateParams params(browser()->profile()->GetOffTheRecordProfile(),
84                                browser()->host_desktop_type());
85   params.window = incognito_window.get();
86   scoped_ptr<Browser> incognito_browser;
87   incognito_browser.reset(new Browser(params));
88
89   int starting_tab_count_normal = browser()->tab_strip_model()->count();
90   int starting_tab_count_incognito =
91       incognito_browser.get()->tab_strip_model()->count();
92
93   scoped_ptr<BookmarkBubbleDelegate> delegate;
94   delegate.reset(new BookmarkBubbleSignInDelegate(incognito_browser.get()));
95
96   delegate->OnSignInLinkClicked();
97
98   // A new tab should have been opened in the normal browser, which should be
99   // visible.
100   int tab_count_normal = browser()->tab_strip_model()->count();
101   EXPECT_EQ(starting_tab_count_normal + 1, tab_count_normal);
102   EXPECT_LE(1,
103             static_cast<BookmarkBubbleSignInDelegateTest::Window*>(
104                 browser()->window())->show_count());
105
106   // No effect is expected on the incognito browser.
107   int tab_count_incognito = incognito_browser->tab_strip_model()->count();
108   EXPECT_EQ(starting_tab_count_incognito, tab_count_incognito);
109   EXPECT_EQ(0,
110             static_cast<BookmarkBubbleSignInDelegateTest::Window*>(
111                 incognito_window.get())->show_count());
112 }
113
114 // Verifies that the sign in page can be loaded in a different browser
115 // if the provided browser is invalidated.
116 TEST_F(BookmarkBubbleSignInDelegateTest, BrowserRemoved) {
117   // Create an extra browser.
118   scoped_ptr<BrowserWindow> extra_window;
119   extra_window.reset(CreateBrowserWindow());
120
121   Browser::CreateParams params(browser()->profile(),
122                                browser()->host_desktop_type());
123   params.window = extra_window.get();
124   scoped_ptr<Browser> extra_browser;
125   extra_browser.reset(new Browser(params));
126
127   int starting_tab_count = extra_browser->tab_strip_model()->count();
128
129   scoped_ptr<BookmarkBubbleDelegate> delegate;
130   delegate.reset(new BookmarkBubbleSignInDelegate(browser()));
131
132   BrowserList::SetLastActive(extra_browser.get());
133
134   browser()->tab_strip_model()->CloseAllTabs();
135   set_browser(NULL);
136
137   delegate->OnSignInLinkClicked();
138
139   // A new tab should have been opened in the extra browser, which should be
140   // visible.
141   int tab_count = extra_browser->tab_strip_model()->count();
142   EXPECT_EQ(starting_tab_count + 1, tab_count);
143   EXPECT_LE(1,
144             static_cast<BookmarkBubbleSignInDelegateTest::Window*>(
145                 extra_window.get())->show_count());
146
147   // Required to avoid a crash when the browser is deleted.
148   extra_browser->tab_strip_model()->CloseAllTabs();
149 }