forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_navigation_observer.cc
69 lines (56 loc) · 2.3 KB
/
test_navigation_observer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "weblayer/test/test_navigation_observer.h"
#include "base/test/bind_test_util.h"
#include "url/gurl.h"
#include "weblayer/public/navigation.h"
#include "weblayer/public/navigation_controller.h"
#include "weblayer/public/tab.h"
#include "weblayer/shell/browser/shell.h"
namespace weblayer {
TestNavigationObserver::TestNavigationObserver(const GURL& url,
NavigationEvent target_event,
Shell* shell)
: TestNavigationObserver(url, target_event, shell->tab()) {}
TestNavigationObserver::TestNavigationObserver(const GURL& url,
NavigationEvent target_event,
Tab* tab)
: url_(url), target_event_(target_event), tab_(tab) {
tab_->GetNavigationController()->AddObserver(this);
}
TestNavigationObserver::~TestNavigationObserver() {
tab_->GetNavigationController()->RemoveObserver(this);
}
void TestNavigationObserver::NavigationStarted(Navigation* navigation) {
// Note: We don't go through CheckNavigationCompleted() here as that waits
// for the load to be complete, which isn't appropriate when just waiting for
// the navigation to be started.
if (navigation->GetURL() == url_ &&
target_event_ == NavigationEvent::kStart) {
run_loop_.Quit();
}
}
void TestNavigationObserver::NavigationCompleted(Navigation* navigation) {
if (navigation->GetURL() == url_)
observed_event_ = NavigationEvent::kCompletion;
CheckNavigationCompleted();
}
void TestNavigationObserver::NavigationFailed(Navigation* navigation) {
if (navigation->GetURL() == url_)
observed_event_ = NavigationEvent::kFailure;
CheckNavigationCompleted();
}
void TestNavigationObserver::LoadStateChanged(bool is_loading,
bool to_different_document) {
done_loading_ = !is_loading;
CheckNavigationCompleted();
}
void TestNavigationObserver::CheckNavigationCompleted() {
if (done_loading_ && observed_event_ == target_event_)
run_loop_.Quit();
}
void TestNavigationObserver::Wait() {
run_loop_.Run();
}
} // namespace weblayer