Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / login_ui_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/prefs/pref_service.h"
7 #include "base/timer/timer.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/chromeos/login/login_manager_test.h"
11 #include "chrome/browser/chromeos/login/screenshot_tester.h"
12 #include "chrome/browser/chromeos/login/startup_utils.h"
13 #include "chrome/browser/chromeos/login/test/oobe_screen_waiter.h"
14 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h"
15 #include "chrome/common/pref_names.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "content/public/browser/notification_service.h"
20 #include "content/public/browser/notification_types.h"
21 #include "ui/compositor/compositor_switches.h"
22
23 namespace chromeos {
24
25 namespace {
26
27 const char kTestUser1[] = "test-user1@gmail.com";
28 const char kTestUser2[] = "test-user2@gmail.com";
29
30 // A class that provides a way to wait until all the animation
31 // has loaded and is properly shown on the screen.
32 class AnimationDelayHandler : public content::NotificationObserver {
33  public:
34   AnimationDelayHandler();
35
36   // Should be run as early as possible on order not to miss notifications.
37   // It seems though that it can't be moved to constructor(?).
38   void Initialize();
39
40   // Override from content::NotificationObserver.
41   virtual void Observe(int type,
42                        const content::NotificationSource& source,
43                        const content::NotificationDetails& details) OVERRIDE;
44
45   // This method checks if animation is loaded, and, if not,
46   // waits until it is loaded and properly shown on the screen.
47   void WaitUntilAnimationLoads();
48
49  private:
50   void InitializeForWaiting(const base::Closure& quitter);
51
52   // It turns out that it takes some more time for the animation
53   // to finish loading even after all the notifications have been sent.
54   // That happens due to some properties of compositor.
55   // This method should be used after getting all the necessary notifications
56   // to wait for the actual load of animation.
57   void SynchronizeAnimationLoadWithCompositor();
58
59   // This method exists only because of the current implementation of
60   // SynchronizeAnimationLoadWithCompositor.
61   void HandleAnimationLoad();
62
63   // Returns true if, according to the notificatons received, animation has
64   // finished loading by now.
65   bool IsAnimationLoaded();
66
67   base::OneShotTimer<AnimationDelayHandler> timer_;
68   bool waiter_loop_is_on_;
69   bool login_or_lock_webui_visible_;
70   base::Closure animation_waiter_quitter_;
71   content::NotificationRegistrar registrar_;
72 };
73
74 }  // anonymous namespace
75
76 AnimationDelayHandler::AnimationDelayHandler()
77     : waiter_loop_is_on_(false), login_or_lock_webui_visible_(false) {
78 }
79
80 void AnimationDelayHandler::Initialize() {
81   waiter_loop_is_on_ = false;
82   registrar_.Add(this,
83                  chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
84                  content::NotificationService::AllSources());
85 }
86
87 bool AnimationDelayHandler::IsAnimationLoaded() {
88   return login_or_lock_webui_visible_;
89 }
90
91 void AnimationDelayHandler::Observe(
92     int type,
93     const content::NotificationSource& source,
94     const content::NotificationDetails& details) {
95   if (chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE == type) {
96     login_or_lock_webui_visible_ = true;
97     registrar_.Remove(this,
98                       chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
99                       content::NotificationService::AllSources());
100   }
101   if (waiter_loop_is_on_ && IsAnimationLoaded()) {
102     content::BrowserThread::PostTask(
103         content::BrowserThread::UI, FROM_HERE, animation_waiter_quitter_);
104   }
105 }
106
107 void AnimationDelayHandler::InitializeForWaiting(const base::Closure& quitter) {
108   waiter_loop_is_on_ = true;
109   animation_waiter_quitter_ = quitter;
110 }
111
112 void AnimationDelayHandler::HandleAnimationLoad() {
113   timer_.Stop();
114   content::BrowserThread::PostTask(
115       content::BrowserThread::UI, FROM_HERE, animation_waiter_quitter_);
116 }
117
118 // Current implementation is a mockup.
119 // It simply waits for 5 seconds, assuming that this time is enough for
120 // animation to load completely.
121 // TODO(elizavetai): Replace this temporary hack with getting a
122 // valid notification from compositor.
123 void AnimationDelayHandler::SynchronizeAnimationLoadWithCompositor() {
124   base::RunLoop waiter;
125   animation_waiter_quitter_ = waiter.QuitClosure();
126   timer_.Start(FROM_HERE,
127                base::TimeDelta::FromSeconds(5),
128                this,
129                &AnimationDelayHandler::HandleAnimationLoad);
130   waiter.Run();
131 }
132
133 void AnimationDelayHandler::WaitUntilAnimationLoads() {
134   if (!IsAnimationLoaded()) {
135     base::RunLoop animation_waiter;
136     InitializeForWaiting(animation_waiter.QuitClosure());
137     animation_waiter.Run();
138   }
139   SynchronizeAnimationLoadWithCompositor();
140 }
141
142 class LoginUITest : public chromeos::LoginManagerTest {
143  public:
144   bool enable_test_screenshots_;
145   LoginUITest() : LoginManagerTest(false) {}
146   virtual ~LoginUITest() {}
147   virtual void SetUpOnMainThread() OVERRIDE {
148     enable_test_screenshots_ = screenshot_tester.TryInitialize();
149     if (enable_test_screenshots_) {
150       animation_delay_handler.Initialize();
151     }
152     LoginManagerTest::SetUpOnMainThread();
153   }
154
155  protected:
156   AnimationDelayHandler animation_delay_handler;
157   ScreenshotTester screenshot_tester;
158 };
159
160 IN_PROC_BROWSER_TEST_F(LoginUITest, PRE_LoginUIVisible) {
161   RegisterUser(kTestUser1);
162   RegisterUser(kTestUser2);
163   StartupUtils::MarkOobeCompleted();
164 }
165
166 // Verifies basic login UI properties.
167 IN_PROC_BROWSER_TEST_F(LoginUITest, LoginUIVisible) {
168   JSExpect("!!document.querySelector('#account-picker')");
169   JSExpect("!!document.querySelector('#pod-row')");
170   JSExpect(
171       "document.querySelectorAll('.pod:not(#user-pod-template)').length == 2");
172
173   JSExpect("document.querySelectorAll('.pod:not(#user-pod-template)')[0]"
174            ".user.emailAddress == '" + std::string(kTestUser1) + "'");
175   JSExpect("document.querySelectorAll('.pod:not(#user-pod-template)')[1]"
176            ".user.emailAddress == '" + std::string(kTestUser2) + "'");
177   if (enable_test_screenshots_) {
178     animation_delay_handler.WaitUntilAnimationLoads();
179     screenshot_tester.Run("LoginUITest-LoginUIVisible");
180   }
181 }
182
183 IN_PROC_BROWSER_TEST_F(LoginUITest, PRE_InterruptedAutoStartEnrollment) {
184   StartupUtils::MarkOobeCompleted();
185   PrefService* prefs = g_browser_process->local_state();
186   prefs->SetBoolean(prefs::kDeviceEnrollmentAutoStart, true);
187 }
188
189 // Tests that the default first screen is the network screen after OOBE
190 // when auto enrollment is enabled and device is not yet enrolled.
191 IN_PROC_BROWSER_TEST_F(LoginUITest, InterruptedAutoStartEnrollment) {
192   OobeScreenWaiter(OobeDisplay::SCREEN_OOBE_NETWORK).Wait();
193 }
194
195 }  // namespace chromeos