OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/test/test_tab_strip_model_observer.h" | 5 #include "chrome/test/test_tab_strip_model_observer.h" |
6 | 6 |
7 #include "chrome/browser/tabs/tab_strip_model.h" | 7 #include "chrome/browser/tabs/tab_strip_model.h" |
8 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 8 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
9 #include "chrome/test/ui_test_utils.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 TestTabStripModelObserver::LoadStartObserver::LoadStartObserver() { | |
13 } | |
14 | |
15 TestTabStripModelObserver::LoadStartObserver::~LoadStartObserver() { | |
16 } | |
17 | 9 |
18 TestTabStripModelObserver::TestTabStripModelObserver( | 10 TestTabStripModelObserver::TestTabStripModelObserver( |
19 TabStripModel* tab_strip_model, | 11 TabStripModel* tab_strip_model, |
20 TestTabStripModelObserver::LoadStartObserver* load_start_observer) | 12 TestTabStripModelObserver::JsInjectionReadyObserver* |
21 : navigation_started_(false), | 13 js_injection_ready_observer) |
22 navigations_completed_(0), | 14 : TestNavigationObserver(js_injection_ready_observer, 1), |
23 number_of_navigations_(1), | 15 tab_strip_model_(tab_strip_model) { |
24 tab_strip_model_(tab_strip_model), | |
25 load_start_observer_(load_start_observer), | |
26 done_(false), | |
27 running_(false) { | |
28 tab_strip_model_->AddObserver(this); | 16 tab_strip_model_->AddObserver(this); |
29 } | 17 } |
30 | 18 |
31 TestTabStripModelObserver::~TestTabStripModelObserver() { | 19 TestTabStripModelObserver::~TestTabStripModelObserver() { |
32 tab_strip_model_->RemoveObserver(this); | 20 tab_strip_model_->RemoveObserver(this); |
33 } | 21 } |
34 | 22 |
35 void TestTabStripModelObserver::WaitForObservation() { | |
36 if (!done_) { | |
37 EXPECT_FALSE(running_); | |
38 running_ = true; | |
39 ui_test_utils::RunMessageLoop(); | |
40 } | |
41 } | |
42 | |
43 void TestTabStripModelObserver::TabInsertedAt( | 23 void TestTabStripModelObserver::TabInsertedAt( |
44 TabContentsWrapper* contents, int index, bool foreground) { | 24 TabContentsWrapper* contents, int index, bool foreground) { |
45 NavigationController* controller = &contents->controller(); | 25 RegisterAsObserver(&contents->controller()); |
46 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
47 Source<NavigationController>(controller)); | |
48 registrar_.Add(this, content::NOTIFICATION_LOAD_START, | |
49 Source<NavigationController>(controller)); | |
50 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, | |
51 Source<NavigationController>(controller)); | |
52 } | 26 } |
53 | |
54 void TestTabStripModelObserver::Observe( | |
55 int type, const NotificationSource& source, | |
56 const NotificationDetails& details) { | |
57 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED || | |
58 type == content::NOTIFICATION_LOAD_START) { | |
59 if (!navigation_started_) { | |
60 load_start_observer_->OnLoadStart(); | |
61 navigation_started_ = true; | |
62 } | |
63 } else if (type == content::NOTIFICATION_LOAD_STOP) { | |
64 if (navigation_started_ && | |
65 ++navigations_completed_ == number_of_navigations_) { | |
66 navigation_started_ = false; | |
67 done_ = true; | |
68 if (running_) | |
69 MessageLoopForUI::current()->Quit(); | |
70 } | |
71 } | |
72 } | |
OLD | NEW |