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