| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "app/table_model_observer.h" | 5 #include "app/table_model_observer.h" |
| 6 #include "chrome/browser/profile.h" | 6 #include "chrome/browser/profile.h" |
| 7 #include "chrome/browser/search_engines/keyword_editor_controller.h" |
| 7 #include "chrome/browser/search_engines/template_url.h" | 8 #include "chrome/browser/search_engines/template_url.h" |
| 8 #include "chrome/browser/search_engines/template_url_model.h" | 9 #include "chrome/browser/search_engines/template_url_model.h" |
| 9 #include "chrome/browser/views/keyword_editor_view.h" | 10 #include "chrome/browser/search_engines/template_url_table_model.h" |
| 10 #include "chrome/test/testing_profile.h" | 11 #include "chrome/test/testing_profile.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 // Base class for keyword editor tests. Creates a profile containing an | 14 // Base class for keyword editor tests. Creates a profile containing an |
| 14 // empty TemplateURLModel. | 15 // empty TemplateURLModel. |
| 15 class KeywordEditorViewTest : public testing::Test, | 16 class KeywordEditorControllerTest : public testing::Test, |
| 16 public TableModelObserver { | 17 public TableModelObserver { |
| 17 public: | 18 public: |
| 18 virtual void SetUp() { | 19 virtual void SetUp() { |
| 19 model_changed_count_ = items_changed_count_ = added_count_ = | 20 model_changed_count_ = items_changed_count_ = added_count_ = |
| 20 removed_count_ = 0; | 21 removed_count_ = 0; |
| 21 | 22 |
| 22 profile_.reset(new TestingProfile()); | 23 profile_.reset(new TestingProfile()); |
| 23 profile_->CreateTemplateURLModel(); | 24 profile_->CreateTemplateURLModel(); |
| 24 | 25 |
| 25 model_ = profile_->GetTemplateURLModel(); | 26 model_ = profile_->GetTemplateURLModel(); |
| 26 | 27 |
| 27 editor_.reset(new KeywordEditorView(profile_.get())); | 28 controller_.reset(new KeywordEditorController(profile_.get())); |
| 28 editor_->table_model_->SetObserver(this); | 29 controller_->table_model()->SetObserver(this); |
| 29 } | 30 } |
| 30 | 31 |
| 31 virtual void OnModelChanged() { | 32 virtual void OnModelChanged() { |
| 32 model_changed_count_++; | 33 model_changed_count_++; |
| 33 } | 34 } |
| 34 | 35 |
| 35 virtual void OnItemsChanged(int start, int length) { | 36 virtual void OnItemsChanged(int start, int length) { |
| 36 items_changed_count_++; | 37 items_changed_count_++; |
| 37 } | 38 } |
| 38 | 39 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 52 ASSERT_EQ(removed_count, removed_count_); | 53 ASSERT_EQ(removed_count, removed_count_); |
| 53 ClearChangeCount(); | 54 ClearChangeCount(); |
| 54 } | 55 } |
| 55 | 56 |
| 56 void ClearChangeCount() { | 57 void ClearChangeCount() { |
| 57 model_changed_count_ = items_changed_count_ = added_count_ = | 58 model_changed_count_ = items_changed_count_ = added_count_ = |
| 58 removed_count_ = 0; | 59 removed_count_ = 0; |
| 59 } | 60 } |
| 60 | 61 |
| 61 TemplateURLTableModel* table_model() const { | 62 TemplateURLTableModel* table_model() const { |
| 62 return editor_->table_model_.get(); | 63 return controller_->table_model(); |
| 63 } | 64 } |
| 64 | 65 |
| 65 protected: | 66 protected: |
| 66 MessageLoopForUI message_loop_; | 67 MessageLoopForUI message_loop_; |
| 67 scoped_ptr<TestingProfile> profile_; | 68 scoped_ptr<TestingProfile> profile_; |
| 68 scoped_ptr<KeywordEditorView> editor_; | 69 scoped_ptr<KeywordEditorController> controller_; |
| 69 TemplateURLModel* model_; | 70 TemplateURLModel* model_; |
| 70 | 71 |
| 71 int model_changed_count_; | 72 int model_changed_count_; |
| 72 int items_changed_count_; | 73 int items_changed_count_; |
| 73 int added_count_; | 74 int added_count_; |
| 74 int removed_count_; | 75 int removed_count_; |
| 75 }; | 76 }; |
| 76 | 77 |
| 77 // Tests adding a TemplateURL. | 78 // Tests adding a TemplateURL. |
| 78 TEST_F(KeywordEditorViewTest, Add) { | 79 TEST_F(KeywordEditorControllerTest, Add) { |
| 79 editor_->AddTemplateURL(L"a", L"b", L"http://c"); | 80 controller_->AddTemplateURL(L"a", L"b", L"http://c"); |
| 80 | 81 |
| 81 // Verify the observer was notified. | 82 // Verify the observer was notified. |
| 82 VerifyChangeCount(0, 0, 1, 0); | 83 VerifyChangeCount(0, 0, 1, 0); |
| 83 if (HasFatalFailure()) | 84 if (HasFatalFailure()) |
| 84 return; | 85 return; |
| 85 | 86 |
| 86 // Verify the TableModel has the new data. | 87 // Verify the TableModel has the new data. |
| 87 ASSERT_EQ(1, table_model()->RowCount()); | 88 ASSERT_EQ(1, table_model()->RowCount()); |
| 88 | 89 |
| 89 // Verify the TemplateURLModel has the new entry. | 90 // Verify the TemplateURLModel has the new entry. |
| 90 ASSERT_EQ(1, model_->GetTemplateURLs().size()); | 91 ASSERT_EQ(1U, model_->GetTemplateURLs().size()); |
| 91 | 92 |
| 92 // Verify the entry is what we added. | 93 // Verify the entry is what we added. |
| 93 const TemplateURL* turl = model_->GetTemplateURLs()[0]; | 94 const TemplateURL* turl = model_->GetTemplateURLs()[0]; |
| 94 EXPECT_EQ(L"a", turl->short_name()); | 95 EXPECT_EQ(L"a", turl->short_name()); |
| 95 EXPECT_EQ(L"b", turl->keyword()); | 96 EXPECT_EQ(L"b", turl->keyword()); |
| 96 EXPECT_TRUE(turl->url() != NULL); | 97 EXPECT_TRUE(turl->url() != NULL); |
| 97 EXPECT_TRUE(turl->url()->url() == L"http://c"); | 98 EXPECT_TRUE(turl->url()->url() == L"http://c"); |
| 98 } | 99 } |
| 99 | 100 |
| 100 // Tests modifying a TemplateURL. | 101 // Tests modifying a TemplateURL. |
| 101 TEST_F(KeywordEditorViewTest, Modify) { | 102 TEST_F(KeywordEditorControllerTest, Modify) { |
| 102 editor_->AddTemplateURL(L"a", L"b", L"http://c"); | 103 controller_->AddTemplateURL(L"a", L"b", L"http://c"); |
| 103 ClearChangeCount(); | 104 ClearChangeCount(); |
| 104 | 105 |
| 105 // Modify the entry. | 106 // Modify the entry. |
| 106 const TemplateURL* turl = model_->GetTemplateURLs()[0]; | 107 const TemplateURL* turl = model_->GetTemplateURLs()[0]; |
| 107 editor_->ModifyTemplateURL(turl, L"a1", L"b1", L"http://c1"); | 108 controller_->ModifyTemplateURL(turl, L"a1", L"b1", L"http://c1"); |
| 108 | 109 |
| 109 // Make sure it was updated appropriately. | 110 // Make sure it was updated appropriately. |
| 110 VerifyChangeCount(0, 1, 0, 0); | 111 VerifyChangeCount(0, 1, 0, 0); |
| 111 EXPECT_EQ(L"a1", turl->short_name()); | 112 EXPECT_EQ(L"a1", turl->short_name()); |
| 112 EXPECT_EQ(L"b1", turl->keyword()); | 113 EXPECT_EQ(L"b1", turl->keyword()); |
| 113 EXPECT_TRUE(turl->url() != NULL); | 114 EXPECT_TRUE(turl->url() != NULL); |
| 114 EXPECT_TRUE(turl->url()->url() == L"http://c1"); | 115 EXPECT_TRUE(turl->url()->url() == L"http://c1"); |
| 115 } | 116 } |
| 116 | 117 |
| 117 // Tests making a TemplateURL the default search provider. | 118 // Tests making a TemplateURL the default search provider. |
| 118 TEST_F(KeywordEditorViewTest, MakeDefault) { | 119 TEST_F(KeywordEditorControllerTest, MakeDefault) { |
| 119 editor_->AddTemplateURL(L"a", L"b", L"http://c{searchTerms}"); | 120 controller_->AddTemplateURL(L"a", L"b", L"http://c{searchTerms}"); |
| 120 ClearChangeCount(); | 121 ClearChangeCount(); |
| 121 | 122 |
| 122 const TemplateURL* turl = model_->GetTemplateURLs()[0]; | 123 const TemplateURL* turl = model_->GetTemplateURLs()[0]; |
| 123 editor_->MakeDefaultSearchProvider(0); | 124 controller_->MakeDefaultTemplateURL(0); |
| 124 // Making an item the default sends a handful of changes. Which are sent isn't | 125 // Making an item the default sends a handful of changes. Which are sent isn't |
| 125 // important, what is important is 'something' is sent. | 126 // important, what is important is 'something' is sent. |
| 126 ASSERT_TRUE(items_changed_count_ > 0 || added_count_ > 0 || | 127 ASSERT_TRUE(items_changed_count_ > 0 || added_count_ > 0 || |
| 127 removed_count_ > 0); | 128 removed_count_ > 0); |
| 128 ASSERT_TRUE(model_->GetDefaultSearchProvider() == turl); | 129 ASSERT_TRUE(model_->GetDefaultSearchProvider() == turl); |
| 129 } | 130 } |
| 130 | 131 |
| 131 // Mutates the TemplateURLModel and make sure table model is updating | 132 // Mutates the TemplateURLModel and make sure table model is updating |
| 132 // appropriately. | 133 // appropriately. |
| 133 TEST_F(KeywordEditorViewTest, MutateTemplateURLModel) { | 134 TEST_F(KeywordEditorControllerTest, MutateTemplateURLModel) { |
| 134 TemplateURL* turl = new TemplateURL(); | 135 TemplateURL* turl = new TemplateURL(); |
| 135 turl->set_keyword(L"a"); | 136 turl->set_keyword(L"a"); |
| 136 turl->set_short_name(L"b"); | 137 turl->set_short_name(L"b"); |
| 137 model_->Add(turl); | 138 model_->Add(turl); |
| 138 | 139 |
| 139 // Table model should have updated. | 140 // Table model should have updated. |
| 140 VerifyChangeCount(1, 0, 0, 0); | 141 VerifyChangeCount(1, 0, 0, 0); |
| 141 | 142 |
| 142 // And should contain the newly added TemplateURL. | 143 // And should contain the newly added TemplateURL. |
| 143 ASSERT_EQ(1, table_model()->RowCount()); | 144 ASSERT_EQ(1, table_model()->RowCount()); |
| 144 ASSERT_EQ(0, table_model()->IndexOfTemplateURL(turl)); | 145 ASSERT_EQ(0, table_model()->IndexOfTemplateURL(turl)); |
| 145 } | 146 } |
| OLD | NEW |