1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "apps/test/app_window_waiter.h"
7 #include "base/threading/thread_task_runner_handle.h"
8 #include "extensions/browser/app_window/app_window.h"
9 #include "extensions/browser/app_window/native_app_window.h"
13 AppWindowWaiter::AppWindowWaiter(extensions::AppWindowRegistry* registry,
14 const std::string& app_id)
15 : registry_(registry), app_id_(app_id) {
16 registry_->AddObserver(this);
19 AppWindowWaiter::~AppWindowWaiter() {
20 registry_->RemoveObserver(this);
23 extensions::AppWindow* AppWindowWaiter::Wait() {
24 window_ = registry_->GetCurrentAppWindowForApp(app_id_);
28 wait_type_ = WAIT_FOR_ADDED;
29 run_loop_ = std::make_unique<base::RunLoop>();
35 extensions::AppWindow* AppWindowWaiter::WaitForShown() {
36 window_ = registry_->GetCurrentAppWindowForApp(app_id_);
37 if (window_ && !window_->is_hidden())
40 wait_type_ = WAIT_FOR_SHOWN;
41 run_loop_ = std::make_unique<base::RunLoop>();
47 extensions::AppWindow* AppWindowWaiter::WaitForShownWithTimeout(
48 base::TimeDelta timeout) {
49 window_ = registry_->GetCurrentAppWindowForApp(app_id_);
50 if (window_ && !window_->is_hidden())
53 wait_type_ = WAIT_FOR_SHOWN;
54 run_loop_ = std::make_unique<base::RunLoop>();
55 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
56 FROM_HERE, run_loop_->QuitClosure(), timeout);
62 extensions::AppWindow* AppWindowWaiter::WaitForActivated() {
63 window_ = registry_->GetCurrentAppWindowForApp(app_id_);
64 if (window_ && window_->GetBaseWindow()->IsActive())
67 wait_type_ = WAIT_FOR_ACTIVATED;
68 run_loop_ = std::make_unique<base::RunLoop>();
74 void AppWindowWaiter::OnAppWindowAdded(extensions::AppWindow* app_window) {
75 if (wait_type_ != WAIT_FOR_ADDED || !run_loop_ || !run_loop_->running())
78 if (app_window->extension_id() == app_id_) {
84 void AppWindowWaiter::OnAppWindowShown(extensions::AppWindow* app_window,
86 if (wait_type_ != WAIT_FOR_SHOWN || !run_loop_ || !run_loop_->running())
89 if (app_window->extension_id() == app_id_) {
95 void AppWindowWaiter::OnAppWindowActivated(extensions::AppWindow* app_window) {
96 if (wait_type_ != WAIT_FOR_ACTIVATED || !run_loop_ || !run_loop_->running())
99 if (app_window->extension_id() == app_id_) {
100 window_ = app_window;