Chromium Code Reviews| Index: chrome/browser/ui/app_list/search/mixer_unittest.cc |
| diff --git a/chrome/browser/ui/app_list/search/mixer_unittest.cc b/chrome/browser/ui/app_list/search/mixer_unittest.cc |
| index ddcf3a9f31cd40eac20ee2e79bc78c5d4f1173b3..2627a6dec54c6ed67e715c9b23e02dc9ec7945f1 100644 |
| --- a/chrome/browser/ui/app_list/search/mixer_unittest.cc |
| +++ b/chrome/browser/ui/app_list/search/mixer_unittest.cc |
| @@ -20,14 +20,14 @@ namespace test { |
| class TestSearchResult : public ChromeSearchResult { |
| public: |
| - TestSearchResult(const std::string& id, double relevance) { |
| + TestSearchResult(const std::string& id, double relevance) |
| + : instance_id_(instantiation_count++) { |
| set_id(id); |
| set_title(base::UTF8ToUTF16(id)); |
| set_relevance(relevance); |
| } |
| virtual ~TestSearchResult() {} |
| - private: |
| // ChromeSearchResult overides: |
| virtual void Open(int event_flags) OVERRIDE {} |
| virtual void InvokeAction(int action_index, int event_flags) OVERRIDE {} |
| @@ -39,8 +39,18 @@ class TestSearchResult : public ChromeSearchResult { |
| return SEARCH_RESULT_TYPE_BOUNDARY; |
| } |
| + static int GetInstanceId(SearchResult* result) { |
|
Matt Giuca
2014/07/09 01:53:22
// For reference equality testing. (Addresses cann
calamity
2014/07/09 02:52:51
Done.
|
| + return static_cast<const TestSearchResult*>(result)->instance_id_; |
| + } |
| + |
| + private: |
| + static int instantiation_count; |
| + |
| + int instance_id_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(TestSearchResult); |
| }; |
| +int TestSearchResult::instantiation_count = 0; |
| class TestSearchProvider : public SearchProvider { |
| public: |
| @@ -182,5 +192,65 @@ TEST_F(MixerTest, RemoveDuplicates) { |
| EXPECT_EQ("dup0,dup1,dup2", GetResults()); |
| } |
| +TEST_F(MixerTest, Publish) { |
| + scoped_ptr<ChromeSearchResult> result1(new TestSearchResult("app1", 0)); |
| + scoped_ptr<ChromeSearchResult> result2(new TestSearchResult("app2", 0)); |
| + scoped_ptr<ChromeSearchResult> result3(new TestSearchResult("app3", 0)); |
| + scoped_ptr<ChromeSearchResult> result3_copy = result3->Duplicate(); |
| + scoped_ptr<ChromeSearchResult> result4(new TestSearchResult("app4", 0)); |
| + |
| + AppListModel::SearchResults ui_results; |
| + |
| + // Publish the first three results to |ui_results|. |
| + Mixer::SortedResults new_results; |
| + new_results.push_back(Mixer::SortData(result1.get(), 1.0f)); |
| + new_results.push_back(Mixer::SortData(result2.get(), 1.0f)); |
| + new_results.push_back(Mixer::SortData(result3.get(), 1.0f)); |
| + |
| + Mixer::Publish(new_results, &ui_results); |
| + EXPECT_EQ(3u, ui_results.item_count()); |
| + // The objects in |ui_results| should be new copies because the input results |
| + // are owned and |ui_results| needs to own its results as well. |
| + EXPECT_NE(new_results[0].result, ui_results.GetItemAt(0)); |
| + EXPECT_NE(new_results[1].result, ui_results.GetItemAt(1)); |
| + EXPECT_NE(new_results[2].result, ui_results.GetItemAt(2)); |
| + |
| + // Save the current |ui_results| instance ids for comparison later. |
| + std::vector<int> old_ui_result_ids; |
| + for (size_t i = 0; i < ui_results.item_count(); ++i) { |
| + old_ui_result_ids.push_back( |
| + TestSearchResult::GetInstanceId(ui_results.GetItemAt(i))); |
| + } |
| + |
| + // Change the first result to a totally new object (with a new ID). |
| + new_results[0] = Mixer::SortData(result4.get(), 1.0f); |
| + |
| + // Change the second result's title, but keep the same id. (The result will |
| + // keep the id "app2" but change its title to "New App 2 Title".) |
| + const base::string16 kNewAppTitle = base::UTF8ToUTF16("New App 2 Title"); |
| + new_results[1].result->set_title(kNewAppTitle); |
| + |
| + // Change the third result's object address (it points to an object with the |
| + // same data). |
| + new_results[2] = Mixer::SortData(result3_copy.get(), 1.0f); |
| + |
| + Mixer::Publish(new_results, &ui_results); |
| + EXPECT_EQ(3u, ui_results.item_count()); |
| + |
| + // The first result will be a new object, as the ID has changed. |
| + EXPECT_NE(old_ui_result_ids[0], |
| + TestSearchResult::GetInstanceId(ui_results.GetItemAt(0))); |
| + |
| + // The second result will still use the original object, but have a different |
| + // title, since the ID did not change. |
| + EXPECT_EQ(old_ui_result_ids[1], |
| + TestSearchResult::GetInstanceId(ui_results.GetItemAt(1))); |
| + EXPECT_EQ(kNewAppTitle, ui_results.GetItemAt(1)->title()); |
| + |
| + // The third result will use the original object as the ID did not change. |
| + EXPECT_EQ(old_ui_result_ids[2], |
| + TestSearchResult::GetInstanceId(ui_results.GetItemAt(2))); |
| +} |
| + |
| } // namespace test |
| } // namespace app_list |