Home | History | Annotate | Download | only in login
      1 // Copyright (c) 2012 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 "ash/shell.h"
      6 #include "base/command_line.h"
      7 #include "chrome/browser/chrome_browser_main.h"
      8 #include "chrome/browser/chrome_browser_main_extra_parts.h"
      9 #include "chrome/browser/chrome_content_browser_client.h"
     10 #include "chrome/browser/chrome_notification_types.h"
     11 #include "chrome/browser/chromeos/login/login_display_host_impl.h"
     12 #include "chrome/browser/chromeos/login/login_wizard.h"
     13 #include "chrome/browser/chromeos/login/wizard_controller.h"
     14 #include "chrome/browser/extensions/extension_system.h"
     15 #include "chrome/browser/profiles/profile_manager.h"
     16 #include "chrome/browser/ui/browser.h"
     17 #include "chrome/common/chrome_switches.h"
     18 #include "chrome/test/base/in_process_browser_test.h"
     19 #include "chrome/test/base/interactive_test_utils.h"
     20 #include "chrome/test/base/ui_test_utils.h"
     21 #include "chromeos/chromeos_switches.h"
     22 #include "content/public/browser/notification_observer.h"
     23 #include "content/public/browser/notification_registrar.h"
     24 #include "content/public/browser/notification_service.h"
     25 #include "content/public/test/test_utils.h"
     26 #include "testing/gmock/include/gmock/gmock.h"
     27 #include "testing/gtest/include/gtest/gtest.h"
     28 
     29 using ::testing::_;
     30 using ::testing::AnyNumber;
     31 using ::testing::Return;
     32 
     33 namespace {
     34 
     35 class LoginUserTest : public InProcessBrowserTest {
     36  protected:
     37   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
     38     command_line->AppendSwitchASCII(
     39         chromeos::switches::kLoginUser, "TestUser (at) gmail.com");
     40     command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user");
     41   }
     42 };
     43 
     44 class LoginGuestTest : public InProcessBrowserTest {
     45  protected:
     46   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
     47     command_line->AppendSwitch(chromeos::switches::kGuestSession);
     48     command_line->AppendSwitch(::switches::kIncognito);
     49     command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user");
     50   }
     51 };
     52 
     53 class LoginCursorTest : public InProcessBrowserTest {
     54  protected:
     55   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
     56     command_line->AppendSwitch(chromeos::switches::kLoginManager);
     57   }
     58 };
     59 
     60 // Used to add an observer to NotificationService after it's created.
     61 class TestBrowserMainExtraParts
     62     : public ChromeBrowserMainExtraParts,
     63       public content::NotificationObserver {
     64  public:
     65   TestBrowserMainExtraParts() {}
     66   virtual ~TestBrowserMainExtraParts() {}
     67 
     68   // ChromeBrowserMainExtraParts implementation.
     69   virtual void PreEarlyInitialization() OVERRIDE {
     70     registrar_.Add(this, chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
     71                    content::NotificationService::AllSources());
     72   }
     73 
     74   void set_quit_task(const base::Closure& quit_task) { quit_task_ = quit_task; }
     75 
     76  private:
     77   // Overridden from content::NotificationObserver:
     78   virtual void Observe(int type,
     79                        const content::NotificationSource& source,
     80                        const content::NotificationDetails& details) OVERRIDE {
     81     quit_task_.Run();
     82   }
     83 
     84   content::NotificationRegistrar registrar_;
     85   base::Closure quit_task_;
     86 
     87   DISALLOW_COPY_AND_ASSIGN(TestBrowserMainExtraParts);
     88 };
     89 
     90 class TestContentBrowserClient : public chrome::ChromeContentBrowserClient {
     91  public:
     92   TestContentBrowserClient() {}
     93   virtual ~TestContentBrowserClient() {}
     94 
     95   virtual content::BrowserMainParts* CreateBrowserMainParts(
     96       const content::MainFunctionParams& parameters) OVERRIDE {
     97     ChromeBrowserMainParts* main_parts = static_cast<ChromeBrowserMainParts*>(
     98         ChromeContentBrowserClient::CreateBrowserMainParts(parameters));
     99 
    100     browser_main_extra_parts_ = new TestBrowserMainExtraParts();
    101     main_parts->AddParts(browser_main_extra_parts_);
    102     return main_parts;
    103   }
    104 
    105   TestBrowserMainExtraParts* browser_main_extra_parts_;
    106 
    107  private:
    108   DISALLOW_COPY_AND_ASSIGN(TestContentBrowserClient);
    109 };
    110 
    111 
    112 class LoginSigninTest : public InProcessBrowserTest {
    113  protected:
    114   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
    115     command_line->AppendSwitch(chromeos::switches::kLoginManager);
    116     command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests);
    117   }
    118 
    119   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
    120     content_browser_client_.reset(new TestContentBrowserClient());
    121     original_content_browser_client_ = content::SetBrowserClientForTesting(
    122         content_browser_client_.get());
    123   }
    124 
    125   virtual void TearDownInProcessBrowserTestFixture() OVERRIDE {
    126     content::SetBrowserClientForTesting(original_content_browser_client_);
    127   }
    128 
    129   scoped_ptr<TestContentBrowserClient> content_browser_client_;
    130   content::ContentBrowserClient* original_content_browser_client_;
    131 };
    132 
    133 // After a chrome crash, the session manager will restart chrome with
    134 // the -login-user flag indicating that the user is already logged in.
    135 // This profile should NOT be an OTR profile.
    136 IN_PROC_BROWSER_TEST_F(LoginUserTest, UserPassed) {
    137   Profile* profile = browser()->profile();
    138   EXPECT_EQ("user", profile->GetPath().BaseName().value());
    139   EXPECT_FALSE(profile->IsOffTheRecord());
    140 }
    141 
    142 // Verifies the cursor is not hidden at startup when user is logged in.
    143 IN_PROC_BROWSER_TEST_F(LoginUserTest, CursorShown) {
    144   EXPECT_TRUE(ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible());
    145 }
    146 
    147 // After a guest login, we should get the OTR default profile.
    148 IN_PROC_BROWSER_TEST_F(LoginGuestTest, GuestIsOTR) {
    149   Profile* profile = browser()->profile();
    150   EXPECT_EQ("Default", profile->GetPath().BaseName().value());
    151   EXPECT_TRUE(profile->IsOffTheRecord());
    152   // Ensure there's extension service for this profile.
    153   EXPECT_TRUE(extensions::ExtensionSystem::Get(profile)->extension_service());
    154 }
    155 
    156 // Verifies the cursor is not hidden at startup when running guest session.
    157 IN_PROC_BROWSER_TEST_F(LoginGuestTest, CursorShown) {
    158   EXPECT_TRUE(ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible());
    159 }
    160 
    161 // Verifies the cursor is hidden at startup on login screen.
    162 IN_PROC_BROWSER_TEST_F(LoginCursorTest, CursorHidden) {
    163   // Login screen needs to be shown explicitly when running test.
    164   chromeos::ShowLoginWizard(chromeos::WizardController::kLoginScreenName);
    165 
    166   // Cursor should be hidden at startup
    167   EXPECT_FALSE(ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible());
    168 
    169   // Cursor should be shown after cursor is moved.
    170   EXPECT_TRUE(ui_test_utils::SendMouseMoveSync(gfx::Point()));
    171   EXPECT_TRUE(ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible());
    172 
    173   base::MessageLoop::current()->DeleteSoon(
    174       FROM_HERE, chromeos::LoginDisplayHostImpl::default_host());
    175 }
    176 
    177 // Verifies that the webui for login comes up successfully.
    178 IN_PROC_BROWSER_TEST_F(LoginSigninTest, WebUIVisible) {
    179   scoped_refptr<content::MessageLoopRunner> runner =
    180       new content::MessageLoopRunner;
    181   content_browser_client_->browser_main_extra_parts_->set_quit_task(
    182       runner->QuitClosure());
    183   runner->Run();
    184 }
    185 
    186 }  // namespace
    187