[M108 Migration] Support standard build for armv7hl architecture
[platform/framework/web/chromium-efl.git] / apps / test / app_window_waiter.cc
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.
4
5 #include "apps/test/app_window_waiter.h"
6
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"
10
11 namespace apps {
12
13 AppWindowWaiter::AppWindowWaiter(extensions::AppWindowRegistry* registry,
14                                  const std::string& app_id)
15     : registry_(registry), app_id_(app_id) {
16   registry_->AddObserver(this);
17 }
18
19 AppWindowWaiter::~AppWindowWaiter() {
20   registry_->RemoveObserver(this);
21 }
22
23 extensions::AppWindow* AppWindowWaiter::Wait() {
24   window_ = registry_->GetCurrentAppWindowForApp(app_id_);
25   if (window_)
26     return window_;
27
28   wait_type_ = WAIT_FOR_ADDED;
29   run_loop_ = std::make_unique<base::RunLoop>();
30   run_loop_->Run();
31
32   return window_;
33 }
34
35 extensions::AppWindow* AppWindowWaiter::WaitForShown() {
36   window_ = registry_->GetCurrentAppWindowForApp(app_id_);
37   if (window_ && !window_->is_hidden())
38     return window_;
39
40   wait_type_ = WAIT_FOR_SHOWN;
41   run_loop_ = std::make_unique<base::RunLoop>();
42   run_loop_->Run();
43
44   return window_;
45 }
46
47 extensions::AppWindow* AppWindowWaiter::WaitForShownWithTimeout(
48     base::TimeDelta timeout) {
49   window_ = registry_->GetCurrentAppWindowForApp(app_id_);
50   if (window_ && !window_->is_hidden())
51     return window_;
52
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);
57   run_loop_->Run();
58
59   return window_;
60 }
61
62 extensions::AppWindow* AppWindowWaiter::WaitForActivated() {
63   window_ = registry_->GetCurrentAppWindowForApp(app_id_);
64   if (window_ && window_->GetBaseWindow()->IsActive())
65     return window_;
66
67   wait_type_ = WAIT_FOR_ACTIVATED;
68   run_loop_ = std::make_unique<base::RunLoop>();
69   run_loop_->Run();
70
71   return window_;
72 }
73
74 void AppWindowWaiter::OnAppWindowAdded(extensions::AppWindow* app_window) {
75   if (wait_type_ != WAIT_FOR_ADDED || !run_loop_ || !run_loop_->running())
76     return;
77
78   if (app_window->extension_id() == app_id_) {
79     window_ = app_window;
80     run_loop_->Quit();
81   }
82 }
83
84 void AppWindowWaiter::OnAppWindowShown(extensions::AppWindow* app_window,
85                                        bool was_hidden) {
86   if (wait_type_ != WAIT_FOR_SHOWN || !run_loop_ || !run_loop_->running())
87     return;
88
89   if (app_window->extension_id() == app_id_) {
90     window_ = app_window;
91     run_loop_->Quit();
92   }
93 }
94
95 void AppWindowWaiter::OnAppWindowActivated(extensions::AppWindow* app_window) {
96   if (wait_type_ != WAIT_FOR_ACTIVATED || !run_loop_ || !run_loop_->running())
97     return;
98
99   if (app_window->extension_id() == app_id_) {
100     window_ = app_window;
101     run_loop_->Quit();
102   }
103 }
104
105 }  // namespace apps