- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / new_avatar_menu_button_browsertest.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 "base/command_line.h"
6 #include "base/path_service.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/profiles/profile_manager.h"
10 #include "chrome/browser/profiles/profiles_state.h"
11 #include "chrome/browser/ui/browser_dialogs.h"
12 #include "chrome/browser/ui/browser_list.h"
13 #include "chrome/browser/ui/views/avatar_menu_button.h"
14 #include "chrome/browser/ui/views/frame/browser_view.h"
15 #include "chrome/browser/ui/views/new_avatar_button.h"
16 #include "chrome/browser/ui/views/profile_chooser_view.h"
17 #include "chrome/browser/ui/views/user_manager_view.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "chrome/test/base/test_switches.h"
22 #include "chrome/test/base/testing_browser_process.h"
23 #include "content/public/test/test_utils.h"
24 #include "grit/generated_resources.h"
25
26 class NewAvatarMenuButtonTest : public InProcessBrowserTest {
27  public:
28   NewAvatarMenuButtonTest();
29   virtual ~NewAvatarMenuButtonTest();
30
31  protected:
32   virtual void SetUp() OVERRIDE;
33   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE;
34   void CreateTestingProfile();
35   void StartAvatarMenu();
36
37  private:
38   DISALLOW_COPY_AND_ASSIGN(NewAvatarMenuButtonTest);
39 };
40
41 NewAvatarMenuButtonTest::NewAvatarMenuButtonTest() {
42 }
43
44 NewAvatarMenuButtonTest::~NewAvatarMenuButtonTest() {
45 }
46
47 void NewAvatarMenuButtonTest::SetUp() {
48   InProcessBrowserTest::SetUp();
49   DCHECK(CommandLine::ForCurrentProcess()->HasSwitch(
50       switches::kNewProfileManagement));
51 }
52
53 void NewAvatarMenuButtonTest::SetUpCommandLine(CommandLine* command_line) {
54   command_line->AppendSwitch(switches::kNewProfileManagement);
55 }
56
57 void NewAvatarMenuButtonTest::CreateTestingProfile() {
58   ProfileManager* profile_manager = g_browser_process->profile_manager();
59   EXPECT_EQ(1u, profile_manager->GetNumberOfProfiles());
60
61   // Sign in the default profile
62   ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
63   cache.SetUserNameOfProfileAtIndex(0, UTF8ToUTF16("user_name"));
64
65   base::FilePath path;
66   PathService::Get(chrome::DIR_USER_DATA, &path);
67   path = path.AppendASCII("test_profile");
68   if (!base::PathExists(path))
69     ASSERT_TRUE(file_util::CreateDirectory(path));
70   Profile* profile =
71       Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
72   profile_manager->RegisterTestingProfile(profile, true, false);
73   EXPECT_EQ(2u, profile_manager->GetNumberOfProfiles());
74 }
75
76 void NewAvatarMenuButtonTest::StartAvatarMenu() {
77   BrowserView* browser_view = reinterpret_cast<BrowserView*>(
78       browser()->window());
79
80   // Ensure that the avatar icon button is not also showing.
81   NewAvatarButton* button = browser_view->frame()->GetNewAvatarMenuButton();
82   ASSERT_TRUE(button);
83   ASSERT_FALSE(browser_view->frame()->GetAvatarMenuButton());
84
85   ProfileChooserView::set_close_on_deactivate(false);
86   ui::MouseEvent mouse_ev(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), 0);
87   button->NotifyClick(mouse_ev);
88   base::MessageLoop::current()->RunUntilIdle();
89   EXPECT_TRUE(ProfileChooserView::IsShowing());
90 }
91
92 IN_PROC_BROWSER_TEST_F(NewAvatarMenuButtonTest, SignOut) {
93   // If multiprofile mode is not enabled, you can't switch between profiles.
94   if (!profiles::IsMultipleProfilesEnabled())
95     return;
96
97   CreateTestingProfile();
98   ASSERT_NO_FATAL_FAILURE(StartAvatarMenu());
99
100   BrowserList* browser_list =
101       BrowserList::GetInstance(chrome::GetActiveDesktop());
102   EXPECT_EQ(1U, browser_list->size());
103   content::WindowedNotificationObserver window_close_observer(
104       chrome::NOTIFICATION_BROWSER_CLOSED,
105       content::Source<Browser>(browser()));
106
107   AvatarMenu* menu =
108       ProfileChooserView::profile_bubble_->avatar_menu_.get();
109   const AvatarMenu::Item& menu_item_before =
110       menu->GetItemAt(menu->GetActiveProfileIndex());
111   EXPECT_FALSE(menu_item_before.signin_required);
112
113   ui::MouseEvent mouse_ev(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), 0);
114   menu->SetLogoutURL("about:blank");
115
116   ProfileChooserView::profile_bubble_->LinkClicked(
117       static_cast<views::Link*>(
118           ProfileChooserView::profile_bubble_->signout_current_profile_link_),
119       0);
120
121   EXPECT_TRUE(menu->GetItemAt(menu->GetActiveProfileIndex()).signin_required);
122
123   window_close_observer.Wait();  // Rely on test timeout for failure indication.
124   EXPECT_TRUE(browser_list->empty());
125
126   // If the User Manager hasn't shown yet, wait for it to show up.
127   if (!UserManagerView::IsShowing())
128     base::MessageLoop::current()->RunUntilIdle();
129
130   // We need to hide the User Manager or else the process can't die.
131   chrome::HideUserManager();
132 }