| 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/browser/renderer_host/test/test_render_view_host.h" | 5 #include "chrome/browser/renderer_host/test/test_render_view_host.h" |
| 6 | 6 |
| 7 #include <set> |
| 8 |
| 7 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
| 9 #include "chrome/browser/prefs/pref_service.h" | 11 #include "chrome/browser/prefs/pref_service.h" |
| 10 #include "chrome/browser/prefs/pref_change_registrar.h" | 12 #include "chrome/browser/prefs/pref_change_registrar.h" |
| 11 #include "chrome/browser/renderer_host/mock_render_process_host.h" | 13 #include "chrome/browser/renderer_host/mock_render_process_host.h" |
| 12 #include "chrome/browser/tab_contents/navigation_controller.h" | 14 #include "chrome/browser/tab_contents/navigation_controller.h" |
| 13 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 15 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
| 14 #include "chrome/browser/tab_contents/test_tab_contents.h" | 16 #include "chrome/browser/tab_contents/test_tab_contents.h" |
| 15 #include "chrome/browser/translate/translate_infobar_delegate.h" | 17 #include "chrome/browser/translate/translate_infobar_delegate.h" |
| 16 #include "chrome/browser/translate/translate_manager.h" | 18 #include "chrome/browser/translate/translate_manager.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 if (original_lang) | 79 if (original_lang) |
| 78 *original_lang = translate_param.c; | 80 *original_lang = translate_param.c; |
| 79 if (target_lang) | 81 if (target_lang) |
| 80 *target_lang = translate_param.d; | 82 *target_lang = translate_param.d; |
| 81 return true; | 83 return true; |
| 82 } | 84 } |
| 83 | 85 |
| 84 // Returns the translate infobar if there is 1 infobar and it is a translate | 86 // Returns the translate infobar if there is 1 infobar and it is a translate |
| 85 // infobar. | 87 // infobar. |
| 86 TranslateInfoBarDelegate* GetTranslateInfoBar() { | 88 TranslateInfoBarDelegate* GetTranslateInfoBar() { |
| 87 if (contents()->infobar_delegate_count() != 1) | 89 return (contents()->infobar_delegate_count() == 1) ? |
| 88 return NULL; | 90 contents()->GetInfoBarDelegateAt(0)->AsTranslateInfoBarDelegate() : |
| 89 return contents()->GetInfoBarDelegateAt(0)->AsTranslateInfoBarDelegate(); | 91 NULL; |
| 90 } | 92 } |
| 91 | 93 |
| 92 // If there is 1 infobar and it is a translate infobar, closes it and returns | 94 // If there is 1 infobar and it is a translate infobar, closes it and returns |
| 93 // true. Returns false otherwise. | 95 // true. Returns false otherwise. |
| 94 bool CloseTranslateInfoBar() { | 96 bool CloseTranslateInfoBar() { |
| 95 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); | 97 InfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 96 if (!infobar) | 98 if (!infobar) |
| 97 return false; | 99 return false; |
| 98 infobar->InfoBarDismissed(); // Simulates closing the infobar. | 100 infobar->InfoBarDismissed(); // Simulates closing the infobar. |
| 99 contents()->RemoveInfoBar(infobar); | 101 contents()->RemoveInfoBar(infobar); |
| 100 return true; | 102 return true; |
| 101 } | 103 } |
| 102 | 104 |
| 103 // Checks whether |infobar| has been removed and clears the removed infobar | 105 // Checks whether |infobar| has been removed and clears the removed infobar |
| 104 // list. | 106 // list. |
| 105 bool CheckInfoBarRemovedAndReset(InfoBarDelegate* infobar) { | 107 bool CheckInfoBarRemovedAndReset(InfoBarDelegate* delegate) { |
| 106 bool found = std::find(removed_infobars_.begin(), removed_infobars_.end(), | 108 bool found = removed_infobars_.count(delegate) != 0; |
| 107 infobar) != removed_infobars_.end(); | |
| 108 removed_infobars_.clear(); | 109 removed_infobars_.clear(); |
| 109 return found; | 110 return found; |
| 110 } | 111 } |
| 111 | 112 |
| 112 // Returns true if at least one infobar was closed. | 113 // Returns true if at least one infobar was closed. |
| 113 bool InfoBarRemoved() { | 114 bool InfoBarRemoved() { |
| 114 return !removed_infobars_.empty(); | 115 return !removed_infobars_.empty(); |
| 115 } | 116 } |
| 116 | 117 |
| 117 // Clears the list of stored removed infobars. | 118 // Clears the list of stored removed infobars. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 130 if (!infobar) | 131 if (!infobar) |
| 131 return false; | 132 return false; |
| 132 infobar->TranslationDeclined(); | 133 infobar->TranslationDeclined(); |
| 133 contents()->RemoveInfoBar(infobar); | 134 contents()->RemoveInfoBar(infobar); |
| 134 return true; | 135 return true; |
| 135 } | 136 } |
| 136 | 137 |
| 137 virtual void Observe(NotificationType type, | 138 virtual void Observe(NotificationType type, |
| 138 const NotificationSource& source, | 139 const NotificationSource& source, |
| 139 const NotificationDetails& details) { | 140 const NotificationDetails& details) { |
| 140 DCHECK(type == NotificationType::TAB_CONTENTS_INFOBAR_REMOVED); | 141 DCHECK_EQ(NotificationType::TAB_CONTENTS_INFOBAR_REMOVED, type.value); |
| 141 removed_infobars_.push_back(Details<InfoBarDelegate>(details).ptr()); | 142 removed_infobars_.insert(Details<InfoBarDelegate>(details).ptr()); |
| 142 } | 143 } |
| 143 | 144 |
| 144 protected: | 145 protected: |
| 145 virtual void SetUp() { | 146 virtual void SetUp() { |
| 146 URLFetcher::set_factory(&url_fetcher_factory_); | 147 URLFetcher::set_factory(&url_fetcher_factory_); |
| 147 | 148 |
| 148 // Access the TranslateManager singleton so it is created before we call | 149 // Access the TranslateManager singleton so it is created before we call |
| 149 // RenderViewHostTestHarness::SetUp() to match what's done in Chrome, where | 150 // RenderViewHostTestHarness::SetUp() to match what's done in Chrome, where |
| 150 // the TranslateManager is created before the TabContents. This matters as | 151 // the TranslateManager is created before the TabContents. This matters as |
| 151 // they both register for similar events and we want the notifications to | 152 // they both register for similar events and we want the notifications to |
| 152 // happen in the same sequence (TranslateManager first, TabContents second). | 153 // happen in the same sequence (TranslateManager first, TabContents second). |
| 153 // Also clears the translate script so it is fetched everytime and sets the | 154 // Also clears the translate script so it is fetched everytime and sets the |
| 154 // expiration delay to a large value by default (in case it was zeroed in | 155 // expiration delay to a large value by default (in case it was zeroed in |
| 155 // a previous test). | 156 // a previous test). |
| 156 TranslateManager::GetInstance()->ClearTranslateScript(); | 157 TranslateManager::GetInstance()->ClearTranslateScript(); |
| 157 TranslateManager::GetInstance()-> | 158 TranslateManager::GetInstance()-> |
| 158 set_translate_script_expiration_delay(60 * 60 * 1000); | 159 set_translate_script_expiration_delay(60 * 60 * 1000); |
| 159 | 160 |
| 160 RenderViewHostTestHarness::SetUp(); | 161 RenderViewHostTestHarness::SetUp(); |
| 161 | 162 |
| 162 notification_registrar_.Add( | 163 notification_registrar_.Add(this, |
| 163 this, | |
| 164 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED, | 164 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED, |
| 165 Source<TabContents>(contents())); | 165 Source<TabContents>(contents())); |
| 166 } | 166 } |
| 167 | 167 |
| 168 virtual void TearDown() { | 168 virtual void TearDown() { |
| 169 process()->sink().ClearMessages(); | 169 process()->sink().ClearMessages(); |
| 170 | 170 |
| 171 notification_registrar_.Remove( | 171 notification_registrar_.Remove(this, |
| 172 this, | |
| 173 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED, | 172 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED, |
| 174 Source<TabContents>(contents())); | 173 Source<TabContents>(contents())); |
| 175 | 174 |
| 176 RenderViewHostTestHarness::TearDown(); | 175 RenderViewHostTestHarness::TearDown(); |
| 177 | 176 |
| 178 URLFetcher::set_factory(NULL); | 177 URLFetcher::set_factory(NULL); |
| 179 } | 178 } |
| 180 | 179 |
| 181 void SimulateURLFetch(bool success) { | 180 void SimulateURLFetch(bool success) { |
| 182 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 181 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 197 _, | 196 _, |
| 198 Property(&Details<std::string>::ptr, Pointee(path)))); | 197 Property(&Details<std::string>::ptr, Pointee(path)))); |
| 199 } | 198 } |
| 200 | 199 |
| 201 NotificationObserverMock pref_observer_; | 200 NotificationObserverMock pref_observer_; |
| 202 | 201 |
| 203 private: | 202 private: |
| 204 NotificationRegistrar notification_registrar_; | 203 NotificationRegistrar notification_registrar_; |
| 205 TestURLFetcherFactory url_fetcher_factory_; | 204 TestURLFetcherFactory url_fetcher_factory_; |
| 206 | 205 |
| 207 // The list of infobars that have been removed. | 206 // The infobars that have been removed. |
| 208 // WARNING: the pointers points to deleted objects, use only for comparison. | 207 // WARNING: the pointers point to deleted objects, use only for comparison. |
| 209 std::vector<InfoBarDelegate*> removed_infobars_; | 208 std::set<InfoBarDelegate*> removed_infobars_; |
| 210 | 209 |
| 211 DISALLOW_COPY_AND_ASSIGN(TranslateManagerTest); | 210 DISALLOW_COPY_AND_ASSIGN(TranslateManagerTest); |
| 212 }; | 211 }; |
| 213 | 212 |
| 214 // An observer that keeps track of whether a navigation entry was committed. | 213 // An observer that keeps track of whether a navigation entry was committed. |
| 215 class NavEntryCommittedObserver : public NotificationObserver { | 214 class NavEntryCommittedObserver : public NotificationObserver { |
| 216 public: | 215 public: |
| 217 explicit NavEntryCommittedObserver(TabContents* tab_contents) { | 216 explicit NavEntryCommittedObserver(TabContents* tab_contents) { |
| 218 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | 217 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, |
| 219 Source<NavigationController>(&tab_contents->controller())); | 218 Source<NavigationController>(&tab_contents->controller())); |
| (...skipping 1002 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1222 NULL); | 1221 NULL); |
| 1223 // Now simulate the URL fetch. | 1222 // Now simulate the URL fetch. |
| 1224 SimulateURLFetch(true); | 1223 SimulateURLFetch(true); |
| 1225 // Now the message should have been sent. | 1224 // Now the message should have been sent. |
| 1226 int page_id = 0; | 1225 int page_id = 0; |
| 1227 std::string original_lang, target_lang; | 1226 std::string original_lang, target_lang; |
| 1228 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); | 1227 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1229 EXPECT_EQ("es", original_lang); | 1228 EXPECT_EQ("es", original_lang); |
| 1230 EXPECT_EQ("en", target_lang); | 1229 EXPECT_EQ("en", target_lang); |
| 1231 } | 1230 } |
| OLD | NEW |