| 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 "base/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "base/string16.h" | 6 #include "base/string16.h" |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/search_engines/template_url.h" | 9 #include "chrome/browser/search_engines/template_url.h" |
| 10 #include "chrome/browser/search_engines/template_url_service.h" | 10 #include "chrome/browser/search_engines/template_url_service.h" |
| 11 #include "chrome/browser/search_engines/template_url_service_factory.h" | 11 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 12 #include "chrome/browser/ui/search_engines/keyword_editor_controller.h" | 12 #include "chrome/browser/ui/search_engines/keyword_editor_controller.h" |
| 13 #include "chrome/browser/ui/search_engines/template_url_table_model.h" | 13 #include "chrome/browser/ui/search_engines/template_url_table_model.h" |
| 14 #include "chrome/browser/webdata/web_data_service_factory.h" |
| 14 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
| 15 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 16 #include "chrome/test/base/testing_pref_service.h" | 17 #include "chrome/test/base/testing_pref_service.h" |
| 17 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
| 18 #include "content/public/browser/notification_details.h" | 19 #include "content/public/browser/notification_details.h" |
| 19 #include "content/public/browser/notification_source.h" | 20 #include "content/public/browser/notification_source.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "ui/base/models/table_model_observer.h" | 22 #include "ui/base/models/table_model_observer.h" |
| 22 | 23 |
| 23 static const string16 kA(ASCIIToUTF16("a")); | 24 static const string16 kA(ASCIIToUTF16("a")); |
| 24 static const string16 kA1(ASCIIToUTF16("a1")); | 25 static const string16 kA1(ASCIIToUTF16("a1")); |
| 25 static const string16 kB(ASCIIToUTF16("b")); | 26 static const string16 kB(ASCIIToUTF16("b")); |
| 26 static const string16 kB1(ASCIIToUTF16("b1")); | 27 static const string16 kB1(ASCIIToUTF16("b1")); |
| 27 | 28 |
| 28 // Base class for keyword editor tests. Creates a profile containing an | 29 // Base class for keyword editor tests. Creates a profile containing an |
| 29 // empty TemplateURLService. | 30 // empty TemplateURLService. |
| 30 class KeywordEditorControllerTest : public testing::Test, | 31 class KeywordEditorControllerTest : public testing::Test, |
| 31 public ui::TableModelObserver { | 32 public ui::TableModelObserver { |
| 32 public: | 33 public: |
| 33 // Initializes all of the state. | 34 // Initializes all of the state. |
| 34 void Init(bool simulate_load_failure); | 35 void Init(bool simulate_load_failure); |
| 35 | 36 |
| 36 virtual void SetUp() { | 37 virtual void SetUp() OVERRIDE; |
| 37 Init(false); | 38 virtual void OnModelChanged() OVERRIDE; |
| 38 } | 39 virtual void OnItemsChanged(int start, int length) OVERRIDE; |
| 39 | 40 virtual void OnItemsAdded(int start, int length) OVERRIDE; |
| 40 virtual void OnModelChanged() { | 41 virtual void OnItemsRemoved(int start, int length) OVERRIDE; |
| 41 model_changed_count_++; | |
| 42 } | |
| 43 | |
| 44 virtual void OnItemsChanged(int start, int length) { | |
| 45 items_changed_count_++; | |
| 46 } | |
| 47 | |
| 48 virtual void OnItemsAdded(int start, int length) { | |
| 49 added_count_++; | |
| 50 } | |
| 51 | |
| 52 virtual void OnItemsRemoved(int start, int length) { | |
| 53 removed_count_++; | |
| 54 } | |
| 55 | 42 |
| 56 void VerifyChangeCount(int model_changed_count, int item_changed_count, | 43 void VerifyChangeCount(int model_changed_count, int item_changed_count, |
| 57 int added_count, int removed_count) { | 44 int added_count, int removed_count); |
| 58 ASSERT_EQ(model_changed_count, model_changed_count_); | 45 void ClearChangeCount(); |
| 59 ASSERT_EQ(item_changed_count, items_changed_count_); | 46 void SimulateDefaultSearchIsManaged(const std::string& url); |
| 60 ASSERT_EQ(added_count, added_count_); | 47 TemplateURLTableModel* table_model() const; |
| 61 ASSERT_EQ(removed_count, removed_count_); | |
| 62 ClearChangeCount(); | |
| 63 } | |
| 64 | |
| 65 void ClearChangeCount() { | |
| 66 model_changed_count_ = items_changed_count_ = added_count_ = | |
| 67 removed_count_ = 0; | |
| 68 } | |
| 69 | |
| 70 void SimulateDefaultSearchIsManaged(const std::string& url) { | |
| 71 ASSERT_FALSE(url.empty()); | |
| 72 TestingPrefService* service = profile_->GetTestingPrefService(); | |
| 73 service->SetManagedPref( | |
| 74 prefs::kDefaultSearchProviderEnabled, | |
| 75 Value::CreateBooleanValue(true)); | |
| 76 service->SetManagedPref( | |
| 77 prefs::kDefaultSearchProviderSearchURL, | |
| 78 Value::CreateStringValue(url)); | |
| 79 service->SetManagedPref( | |
| 80 prefs::kDefaultSearchProviderName, | |
| 81 Value::CreateStringValue("managed")); | |
| 82 // Clear the IDs that are not specified via policy. | |
| 83 service->SetManagedPref( | |
| 84 prefs::kDefaultSearchProviderID, new StringValue("")); | |
| 85 service->SetManagedPref( | |
| 86 prefs::kDefaultSearchProviderPrepopulateID, new StringValue("")); | |
| 87 model_->Observe( | |
| 88 chrome::NOTIFICATION_PREF_CHANGED, | |
| 89 content::Source<PrefService>(profile_->GetTestingPrefService()), | |
| 90 content::Details<std::string>(NULL)); | |
| 91 } | |
| 92 | |
| 93 TemplateURLTableModel* table_model() const { | |
| 94 return controller_->table_model(); | |
| 95 } | |
| 96 | 48 |
| 97 protected: | 49 protected: |
| 98 MessageLoopForUI message_loop_; | 50 MessageLoopForUI message_loop_; |
| 99 scoped_ptr<TestingProfile> profile_; | 51 scoped_ptr<TestingProfile> profile_; |
| 100 scoped_ptr<KeywordEditorController> controller_; | 52 scoped_ptr<KeywordEditorController> controller_; |
| 101 TemplateURLService* model_; | 53 TemplateURLService* model_; |
| 102 | 54 |
| 103 int model_changed_count_; | 55 int model_changed_count_; |
| 104 int items_changed_count_; | 56 int items_changed_count_; |
| 105 int added_count_; | 57 int added_count_; |
| 106 int removed_count_; | 58 int removed_count_; |
| 107 }; | 59 }; |
| 108 | 60 |
| 109 void KeywordEditorControllerTest::Init(bool simulate_load_failure) { | 61 void KeywordEditorControllerTest::Init(bool simulate_load_failure) { |
| 110 ClearChangeCount(); | 62 ClearChangeCount(); |
| 111 | 63 |
| 112 // If init is called twice, make sure that the controller is destroyed before | 64 // If init is called twice, make sure that the controller is destroyed before |
| 113 // the profile is. | 65 // the profile is. |
| 114 controller_.reset(); | 66 controller_.reset(); |
| 115 profile_.reset(new TestingProfile()); | 67 profile_.reset(new TestingProfile()); |
| 116 profile_->CreateTemplateURLService(); | 68 profile_->CreateTemplateURLService(); |
| 117 | 69 |
| 70 WebDataServiceFactory::GetInstance()->SetTestingFactory(profile_.get(), NULL); |
| 71 |
| 118 model_ = TemplateURLServiceFactory::GetForProfile(profile_.get()); | 72 model_ = TemplateURLServiceFactory::GetForProfile(profile_.get()); |
| 119 if (simulate_load_failure) | 73 if (simulate_load_failure) |
| 120 model_->OnWebDataServiceRequestDone(0, NULL); | 74 model_->OnWebDataServiceRequestDone(0, NULL); |
| 121 | 75 |
| 122 controller_.reset(new KeywordEditorController(profile_.get())); | 76 controller_.reset(new KeywordEditorController(profile_.get())); |
| 123 controller_->table_model()->SetObserver(this); | 77 controller_->table_model()->SetObserver(this); |
| 124 } | 78 } |
| 125 | 79 |
| 80 void KeywordEditorControllerTest::SetUp() { |
| 81 Init(false); |
| 82 } |
| 83 |
| 84 void KeywordEditorControllerTest::OnModelChanged() { |
| 85 model_changed_count_++; |
| 86 } |
| 87 |
| 88 void KeywordEditorControllerTest::OnItemsChanged(int start, int length) { |
| 89 items_changed_count_++; |
| 90 } |
| 91 |
| 92 void KeywordEditorControllerTest::OnItemsAdded(int start, int length) { |
| 93 added_count_++; |
| 94 } |
| 95 |
| 96 void KeywordEditorControllerTest::OnItemsRemoved(int start, int length) { |
| 97 removed_count_++; |
| 98 } |
| 99 |
| 100 void KeywordEditorControllerTest::VerifyChangeCount(int model_changed_count, |
| 101 int item_changed_count, |
| 102 int added_count, |
| 103 int removed_count) { |
| 104 ASSERT_EQ(model_changed_count, model_changed_count_); |
| 105 ASSERT_EQ(item_changed_count, items_changed_count_); |
| 106 ASSERT_EQ(added_count, added_count_); |
| 107 ASSERT_EQ(removed_count, removed_count_); |
| 108 ClearChangeCount(); |
| 109 } |
| 110 |
| 111 void KeywordEditorControllerTest::ClearChangeCount() { |
| 112 model_changed_count_ = items_changed_count_ = added_count_ = |
| 113 removed_count_ = 0; |
| 114 } |
| 115 |
| 116 void KeywordEditorControllerTest::SimulateDefaultSearchIsManaged( |
| 117 const std::string& url) { |
| 118 ASSERT_FALSE(url.empty()); |
| 119 TestingPrefService* service = profile_->GetTestingPrefService(); |
| 120 service->SetManagedPref( |
| 121 prefs::kDefaultSearchProviderEnabled, |
| 122 Value::CreateBooleanValue(true)); |
| 123 service->SetManagedPref( |
| 124 prefs::kDefaultSearchProviderSearchURL, |
| 125 Value::CreateStringValue(url)); |
| 126 service->SetManagedPref( |
| 127 prefs::kDefaultSearchProviderName, |
| 128 Value::CreateStringValue("managed")); |
| 129 // Clear the IDs that are not specified via policy. |
| 130 service->SetManagedPref( |
| 131 prefs::kDefaultSearchProviderID, new StringValue("")); |
| 132 service->SetManagedPref( |
| 133 prefs::kDefaultSearchProviderPrepopulateID, new StringValue("")); |
| 134 model_->Observe( |
| 135 chrome::NOTIFICATION_PREF_CHANGED, |
| 136 content::Source<PrefService>(profile_->GetTestingPrefService()), |
| 137 content::Details<std::string>(NULL)); |
| 138 } |
| 139 |
| 140 TemplateURLTableModel* KeywordEditorControllerTest::table_model() const { |
| 141 return controller_->table_model(); |
| 142 } |
| 143 |
| 126 // Tests adding a TemplateURL. | 144 // Tests adding a TemplateURL. |
| 127 TEST_F(KeywordEditorControllerTest, Add) { | 145 TEST_F(KeywordEditorControllerTest, Add) { |
| 128 controller_->AddTemplateURL(kA, kB, "http://c"); | 146 controller_->AddTemplateURL(kA, kB, "http://c"); |
| 129 | 147 |
| 130 // Verify the observer was notified. | 148 // Verify the observer was notified. |
| 131 VerifyChangeCount(0, 0, 1, 0); | 149 VerifyChangeCount(0, 0, 1, 0); |
| 132 if (HasFatalFailure()) | 150 if (HasFatalFailure()) |
| 133 return; | 151 return; |
| 134 | 152 |
| 135 // Verify the TableModel has the new data. | 153 // Verify the TableModel has the new data. |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 turl->set_short_name(ASCIIToUTF16("b")); | 270 turl->set_short_name(ASCIIToUTF16("b")); |
| 253 model_->Add(turl); | 271 model_->Add(turl); |
| 254 | 272 |
| 255 // Table model should have updated. | 273 // Table model should have updated. |
| 256 VerifyChangeCount(1, 0, 0, 0); | 274 VerifyChangeCount(1, 0, 0, 0); |
| 257 | 275 |
| 258 // And should contain the newly added TemplateURL. | 276 // And should contain the newly added TemplateURL. |
| 259 ASSERT_EQ(1, table_model()->RowCount()); | 277 ASSERT_EQ(1, table_model()->RowCount()); |
| 260 ASSERT_EQ(0, table_model()->IndexOfTemplateURL(turl)); | 278 ASSERT_EQ(0, table_model()->IndexOfTemplateURL(turl)); |
| 261 } | 279 } |
| OLD | NEW |