oobe_screen_waiter.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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/chromeos/login/test/oobe_screen_waiter.h"
6
7#include "chrome/browser/chromeos/login/login_display_host_impl.h"
8#include "content/public/test/test_utils.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11namespace chromeos {
12
13OobeScreenWaiter::OobeScreenWaiter(OobeDisplay::Screen expected_screen)
14    : waiting_for_screen_(false),
15      expected_screen_(expected_screen) {
16}
17
18OobeScreenWaiter::~OobeScreenWaiter() {
19  if (waiting_for_screen_) {
20    GetOobeUI()->RemoveObserver(this);
21  }
22}
23
24void OobeScreenWaiter::Wait() {
25  WaitNoAssertCurrentScreen();
26
27  ASSERT_EQ(expected_screen_, GetOobeUI()->current_screen());
28}
29
30void OobeScreenWaiter::WaitNoAssertCurrentScreen() {
31  if (GetOobeUI()->current_screen() == expected_screen_)
32    return;
33
34  waiting_for_screen_ = true;
35  GetOobeUI()->AddObserver(this);
36
37  runner_ = new content::MessageLoopRunner;
38  runner_->Run();
39  ASSERT_FALSE(waiting_for_screen_);
40}
41
42void OobeScreenWaiter::OnCurrentScreenChanged(
43    OobeDisplay::Screen current_screen,
44    OobeDisplay::Screen new_screen) {
45  if (waiting_for_screen_ && new_screen == expected_screen_) {
46    runner_->Quit();
47    waiting_for_screen_ = false;
48    GetOobeUI()->RemoveObserver(this);
49  }
50}
51
52OobeUI* OobeScreenWaiter::GetOobeUI() {
53  OobeUI* oobe_ui = static_cast<chromeos::LoginDisplayHostImpl*>(
54      chromeos::LoginDisplayHostImpl::default_host())->GetOobeUI();
55  CHECK(oobe_ui);
56  return oobe_ui;
57}
58
59}  // namespace chromeos
60