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