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_navigation_observer.h" | 5 #include "chrome/test/test_navigation_observer.h" |
6 | 6 |
7 #include "chrome/test/base/ui_test_utils.h" | 7 #include "chrome/test/base/ui_test_utils.h" |
8 #include "content/browser/renderer_host/render_view_host_observer.h" | 8 #include "content/browser/renderer_host/render_view_host_observer.h" |
| 9 #include "content/public/browser/notification_service.h" |
9 #include "content/public/browser/notification_types.h" | 10 #include "content/public/browser/notification_types.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 // This class observes |rvh| and calls OnJsInjectionReady() of | 13 // This class observes |rvh| and calls OnJsInjectionReady() of |
13 // |js_injection_ready_observer| when the time is right to inject | 14 // |js_injection_ready_observer| when the time is right to inject |
14 // JavaScript into the page. | 15 // JavaScript into the page. |
15 class TestNavigationObserver::RVHOSendJS : public RenderViewHostObserver { | 16 class TestNavigationObserver::RVHOSendJS : public RenderViewHostObserver { |
16 public: | 17 public: |
17 RVHOSendJS(RenderViewHost* rvh, | 18 RVHOSendJS(RenderViewHost* rvh, |
18 JsInjectionReadyObserver* js_injection_ready_observer) | 19 JsInjectionReadyObserver* js_injection_ready_observer) |
(...skipping 26 matching lines...) Expand all Loading... |
45 int number_of_navigations) | 46 int number_of_navigations) |
46 : navigation_started_(false), | 47 : navigation_started_(false), |
47 navigations_completed_(0), | 48 navigations_completed_(0), |
48 number_of_navigations_(number_of_navigations), | 49 number_of_navigations_(number_of_navigations), |
49 js_injection_ready_observer_(js_injection_ready_observer), | 50 js_injection_ready_observer_(js_injection_ready_observer), |
50 done_(false), | 51 done_(false), |
51 running_(false) { | 52 running_(false) { |
52 // When we need to do javascript injection, register for RVH creation. | 53 // When we need to do javascript injection, register for RVH creation. |
53 if (js_injection_ready_observer_) { | 54 if (js_injection_ready_observer_) { |
54 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CREATED, | 55 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CREATED, |
55 NotificationService::AllSources()); | 56 content::NotificationService::AllSources()); |
56 } | 57 } |
57 RegisterAsObserver(source); | 58 RegisterAsObserver(source); |
58 } | 59 } |
59 | 60 |
60 TestNavigationObserver::~TestNavigationObserver() { | 61 TestNavigationObserver::~TestNavigationObserver() { |
61 } | 62 } |
62 | 63 |
63 void TestNavigationObserver::WaitForObservation() { | 64 void TestNavigationObserver::WaitForObservation() { |
64 if (!done_) { | 65 if (!done_) { |
65 EXPECT_FALSE(running_); | 66 EXPECT_FALSE(running_); |
66 running_ = true; | 67 running_ = true; |
67 ui_test_utils::RunMessageLoop(); | 68 ui_test_utils::RunMessageLoop(); |
68 } | 69 } |
69 } | 70 } |
70 | 71 |
71 TestNavigationObserver::TestNavigationObserver( | 72 TestNavigationObserver::TestNavigationObserver( |
72 TestNavigationObserver::JsInjectionReadyObserver* | 73 TestNavigationObserver::JsInjectionReadyObserver* |
73 js_injection_ready_observer, | 74 js_injection_ready_observer, |
74 int number_of_navigations) | 75 int number_of_navigations) |
75 : navigation_started_(false), | 76 : navigation_started_(false), |
76 navigations_completed_(0), | 77 navigations_completed_(0), |
77 number_of_navigations_(number_of_navigations), | 78 number_of_navigations_(number_of_navigations), |
78 js_injection_ready_observer_(js_injection_ready_observer), | 79 js_injection_ready_observer_(js_injection_ready_observer), |
79 done_(false), | 80 done_(false), |
80 running_(false) { | 81 running_(false) { |
81 // When we need to do javascript injection, register for RVH creation. | 82 // When we need to do javascript injection, register for RVH creation. |
82 if (js_injection_ready_observer_) { | 83 if (js_injection_ready_observer_) { |
83 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CREATED, | 84 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CREATED, |
84 NotificationService::AllSources()); | 85 content::NotificationService::AllSources()); |
85 } | 86 } |
86 } | 87 } |
87 | 88 |
88 void TestNavigationObserver::RegisterAsObserver( | 89 void TestNavigationObserver::RegisterAsObserver( |
89 const content::NotificationSource& source) { | 90 const content::NotificationSource& source) { |
90 // Register for events to know when we've finished loading the page and are | 91 // Register for events to know when we've finished loading the page and are |
91 // ready to quit the current message loop to return control back to the | 92 // ready to quit the current message loop to return control back to the |
92 // waiting test. | 93 // waiting test. |
93 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, source); | 94 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, source); |
94 registrar_.Add(this, content::NOTIFICATION_LOAD_START, source); | 95 registrar_.Add(this, content::NOTIFICATION_LOAD_START, source); |
(...skipping 19 matching lines...) Expand all Loading... |
114 break; | 115 break; |
115 case content::NOTIFICATION_RENDER_VIEW_HOST_CREATED: | 116 case content::NOTIFICATION_RENDER_VIEW_HOST_CREATED: |
116 rvho_send_js_.reset(new RVHOSendJS( | 117 rvho_send_js_.reset(new RVHOSendJS( |
117 content::Source<RenderViewHost>(source).ptr(), | 118 content::Source<RenderViewHost>(source).ptr(), |
118 js_injection_ready_observer_)); | 119 js_injection_ready_observer_)); |
119 break; | 120 break; |
120 default: | 121 default: |
121 NOTREACHED(); | 122 NOTREACHED(); |
122 } | 123 } |
123 } | 124 } |
OLD | NEW |