| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/public/test/browser_test_utils.h" | 5 #include "content/public/test/browser_test_utils.h" |
| 6 | 6 |
| 7 #include "content/public/browser/notification_types.h" |
| 8 #include "content/public/browser/web_contents.h" |
| 9 #include "content/public/test/test_utils.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 7 namespace content { | 12 namespace content { |
| 8 | 13 |
| 14 TitleWatcher::TitleWatcher(WebContents* web_contents, |
| 15 const string16& expected_title) |
| 16 : web_contents_(web_contents), |
| 17 expected_title_observed_(false), |
| 18 quit_loop_on_observation_(false) { |
| 19 EXPECT_TRUE(web_contents != NULL); |
| 20 expected_titles_.push_back(expected_title); |
| 21 notification_registrar_.Add(this, |
| 22 NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED, |
| 23 Source<WebContents>(web_contents)); |
| 24 |
| 25 // When navigating through the history, the restored NavigationEntry's title |
| 26 // will be used. If the entry ends up having the same title after we return |
| 27 // to it, as will usually be the case, the |
| 28 // NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED will then be suppressed, since the |
| 29 // NavigationEntry's title hasn't changed. |
| 30 notification_registrar_.Add( |
| 31 this, |
| 32 NOTIFICATION_LOAD_STOP, |
| 33 Source<NavigationController>(&web_contents->GetController())); |
| 34 } |
| 35 |
| 36 void TitleWatcher::AlsoWaitForTitle(const string16& expected_title) { |
| 37 expected_titles_.push_back(expected_title); |
| 38 } |
| 39 |
| 40 TitleWatcher::~TitleWatcher() { |
| 41 } |
| 42 |
| 43 const string16& TitleWatcher::WaitAndGetTitle() { |
| 44 if (expected_title_observed_) |
| 45 return observed_title_; |
| 46 quit_loop_on_observation_ = true; |
| 47 message_loop_runner_ = new MessageLoopRunner; |
| 48 message_loop_runner_->Run(); |
| 49 return observed_title_; |
| 50 } |
| 51 |
| 52 void TitleWatcher::Observe(int type, |
| 53 const NotificationSource& source, |
| 54 const NotificationDetails& details) { |
| 55 if (type == NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED) { |
| 56 WebContents* source_contents = Source<WebContents>(source).ptr(); |
| 57 ASSERT_EQ(web_contents_, source_contents); |
| 58 } else if (type == NOTIFICATION_LOAD_STOP) { |
| 59 NavigationController* controller = |
| 60 Source<NavigationController>(source).ptr(); |
| 61 ASSERT_EQ(&web_contents_->GetController(), controller); |
| 62 } else { |
| 63 FAIL() << "Unexpected notification received."; |
| 64 } |
| 65 |
| 66 std::vector<string16>::const_iterator it = |
| 67 std::find(expected_titles_.begin(), |
| 68 expected_titles_.end(), |
| 69 web_contents_->GetTitle()); |
| 70 if (it == expected_titles_.end()) |
| 71 return; |
| 72 observed_title_ = *it; |
| 73 expected_title_observed_ = true; |
| 74 if (quit_loop_on_observation_) { |
| 75 // Only call Quit once, on first Observe: |
| 76 quit_loop_on_observation_ = false; |
| 77 message_loop_runner_->Quit(); |
| 78 } |
| 79 } |
| 80 |
| 9 } // namespace content | 81 } // namespace content |
| OLD | NEW |