| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/threading/platform_thread.h" | 11 #include "base/threading/platform_thread.h" |
| 12 #include "base/timer/timer.h" | |
| 13 #include "chrome/browser/ui/app_list/search/history.h" | 12 #include "chrome/browser/ui/app_list/search/history.h" |
| 14 #include "chrome/browser/ui/app_list/search/history_data.h" | 13 #include "chrome/browser/ui/app_list/search/history_data.h" |
| 15 #include "chrome/browser/ui/app_list/search/history_data_observer.h" | 14 #include "chrome/browser/ui/app_list/search/history_data_observer.h" |
| 16 #include "chrome/browser/ui/app_list/search/history_data_store.h" | 15 #include "chrome/browser/ui/app_list/search/history_data_store.h" |
| 17 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
| 18 #include "content/public/test/test_browser_thread.h" | 17 #include "content/public/test/test_browser_thread.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 19 |
| 21 namespace app_list { | 20 namespace app_list { |
| 22 namespace test { | 21 namespace test { |
| 23 | 22 |
| 24 namespace { | 23 namespace { |
| 25 | 24 |
| 26 const size_t kMaxPrimary = 3; | 25 const size_t kMaxPrimary = 3; |
| 27 const size_t kMaxSecondary = 2; | 26 const size_t kMaxSecondary = 2; |
| 28 | 27 |
| 29 // HistoryDataLoadWaiter waits for give |data| to be loaded from underlying | 28 // HistoryDataLoadWaiter waits for give |data| to be loaded from underlying |
| 30 // store on the blocking pool. The waiter waits on the main message loop until | 29 // store on the blocking pool. The waiter waits on the main message loop until |
| 31 // OnHistoryDataLoadedFromStore() is invoked or the maximum allowed wait time | 30 // OnHistoryDataLoadedFromStore() is invoked or the maximum allowed wait time |
| 32 // has passed. | 31 // has passed. |
| 33 class HistoryDataLoadWaiter : public HistoryDataObserver { | 32 class HistoryDataLoadWaiter : public HistoryDataObserver { |
| 34 public: | 33 public: |
| 35 explicit HistoryDataLoadWaiter(HistoryData* data) : data_(data) {} | 34 explicit HistoryDataLoadWaiter(HistoryData* data) : data_(data) {} |
| 36 virtual ~HistoryDataLoadWaiter() {} | 35 virtual ~HistoryDataLoadWaiter() {} |
| 37 | 36 |
| 38 void Wait(int timeout_ms) { | 37 void Wait() { |
| 39 data_->AddObserver(this); | 38 data_->AddObserver(this); |
| 40 | 39 |
| 41 timer_.Start(FROM_HERE, | |
| 42 base::TimeDelta::FromMilliseconds(timeout_ms), | |
| 43 this, | |
| 44 &HistoryDataLoadWaiter::OnTimeOut); | |
| 45 | |
| 46 run_loop_.reset(new base::RunLoop); | 40 run_loop_.reset(new base::RunLoop); |
| 47 run_loop_->Run(); | 41 run_loop_->Run(); |
| 48 | 42 |
| 49 data_->RemoveObserver(this); | 43 data_->RemoveObserver(this); |
| 50 } | 44 } |
| 51 | 45 |
| 52 private: | 46 private: |
| 53 void OnTimeOut() { | |
| 54 run_loop_->Quit(); | |
| 55 } | |
| 56 | |
| 57 // HistoryDataObserver overrides: | 47 // HistoryDataObserver overrides: |
| 58 virtual void OnHistoryDataLoadedFromStore() OVERRIDE { | 48 virtual void OnHistoryDataLoadedFromStore() OVERRIDE { |
| 59 run_loop_->Quit(); | 49 run_loop_->Quit(); |
| 60 } | 50 } |
| 61 | 51 |
| 62 HistoryData* data_; // Not owned. | 52 HistoryData* data_; // Not owned. |
| 63 scoped_ptr<base::RunLoop> run_loop_; | 53 scoped_ptr<base::RunLoop> run_loop_; |
| 64 base::OneShotTimer<HistoryDataLoadWaiter> timer_; | |
| 65 | 54 |
| 66 DISALLOW_COPY_AND_ASSIGN(HistoryDataLoadWaiter); | 55 DISALLOW_COPY_AND_ASSIGN(HistoryDataLoadWaiter); |
| 67 }; | 56 }; |
| 68 | 57 |
| 69 // StoreFlushWaiter waits for the given |store| to flush its data to disk. | 58 // StoreFlushWaiter waits for the given |store| to flush its data to disk. |
| 70 // The flush and disk write happens on the blocking pool. The waiter waits | 59 // The flush and disk write happens on the blocking pool. The waiter waits |
| 71 // on the main message loop until the OnFlushed() is invoked or the maximum | 60 // on the main message loop until the OnFlushed() is invoked or the maximum |
| 72 // allowed wait time has passed. | 61 // allowed wait time has passed. |
| 73 class StoreFlushWaiter { | 62 class StoreFlushWaiter { |
| 74 public: | 63 public: |
| 75 explicit StoreFlushWaiter(HistoryDataStore* store) : store_(store) {} | 64 explicit StoreFlushWaiter(HistoryDataStore* store) : store_(store) {} |
| 76 ~StoreFlushWaiter() {} | 65 ~StoreFlushWaiter() {} |
| 77 | 66 |
| 78 void Wait(int timeout_ms) { | 67 void Wait() { |
| 79 store_->Flush( | 68 store_->Flush( |
| 80 base::Bind(&StoreFlushWaiter::OnFlushed, base::Unretained(this))); | 69 base::Bind(&StoreFlushWaiter::OnFlushed, base::Unretained(this))); |
| 81 | 70 |
| 82 timer_.Start(FROM_HERE, | |
| 83 base::TimeDelta::FromMilliseconds(timeout_ms), | |
| 84 this, | |
| 85 &StoreFlushWaiter::OnTimeOut); | |
| 86 | |
| 87 run_loop_.reset(new base::RunLoop); | 71 run_loop_.reset(new base::RunLoop); |
| 88 run_loop_->Run(); | 72 run_loop_->Run(); |
| 89 } | 73 } |
| 90 | 74 |
| 91 private: | 75 private: |
| 92 void OnTimeOut() { | |
| 93 run_loop_->Quit(); | |
| 94 } | |
| 95 | |
| 96 void OnFlushed() { | 76 void OnFlushed() { |
| 97 run_loop_->Quit(); | 77 run_loop_->Quit(); |
| 98 } | 78 } |
| 99 | 79 |
| 100 HistoryDataStore* store_; // Not owned. | 80 HistoryDataStore* store_; // Not owned. |
| 101 scoped_ptr<base::RunLoop> run_loop_; | 81 scoped_ptr<base::RunLoop> run_loop_; |
| 102 base::OneShotTimer<StoreFlushWaiter> timer_; | |
| 103 | 82 |
| 104 DISALLOW_COPY_AND_ASSIGN(StoreFlushWaiter); | 83 DISALLOW_COPY_AND_ASSIGN(StoreFlushWaiter); |
| 105 }; | 84 }; |
| 106 | 85 |
| 107 } // namespace | 86 } // namespace |
| 108 | 87 |
| 109 class SearchHistoryTest : public testing::Test { | 88 class SearchHistoryTest : public testing::Test { |
| 110 public: | 89 public: |
| 111 SearchHistoryTest() | 90 SearchHistoryTest() |
| 112 : ui_thread_(content::BrowserThread::UI, &message_loop_) {} | 91 : ui_thread_(content::BrowserThread::UI, &message_loop_) {} |
| (...skipping 10 matching lines...) Expand all Loading... |
| 123 | 102 |
| 124 void CreateHistory() { | 103 void CreateHistory() { |
| 125 history_.reset(new History(profile_.get())); | 104 history_.reset(new History(profile_.get())); |
| 126 | 105 |
| 127 // Replace |data_| with test params. | 106 // Replace |data_| with test params. |
| 128 history_->data_->RemoveObserver(history_.get()); | 107 history_->data_->RemoveObserver(history_.get()); |
| 129 history_->data_.reset( | 108 history_->data_.reset( |
| 130 new HistoryData(history_->store_.get(), kMaxPrimary, kMaxSecondary)); | 109 new HistoryData(history_->store_.get(), kMaxPrimary, kMaxSecondary)); |
| 131 history_->data_->AddObserver(history_.get()); | 110 history_->data_->AddObserver(history_.get()); |
| 132 | 111 |
| 133 HistoryDataLoadWaiter waiter(history_->data_.get()); | 112 HistoryDataLoadWaiter(history_->data_.get()).Wait(); |
| 134 waiter.Wait(1000); | |
| 135 ASSERT_TRUE(history_->IsReady()); | 113 ASSERT_TRUE(history_->IsReady()); |
| 136 } | 114 } |
| 137 | 115 |
| 138 void Flush() { | 116 void Flush() { |
| 139 StoreFlushWaiter waiter(history_->store_.get()); | 117 StoreFlushWaiter(history_->store_.get()).Wait(); |
| 140 waiter.Wait(1000); | |
| 141 } | 118 } |
| 142 | 119 |
| 143 size_t GetKnownResults(const std::string& query) { | 120 size_t GetKnownResults(const std::string& query) { |
| 144 known_results_ = history()->GetKnownResults(query).Pass(); | 121 known_results_ = history()->GetKnownResults(query).Pass(); |
| 145 return known_results_->size(); | 122 return known_results_->size(); |
| 146 } | 123 } |
| 147 | 124 |
| 148 KnownResultType GetResultType(const std::string& result_id) { | 125 KnownResultType GetResultType(const std::string& result_id) { |
| 149 return known_results_->find(result_id) != known_results_->end() | 126 return known_results_->find(result_id) != known_results_->end() |
| 150 ? (*known_results_.get())[result_id] | 127 ? (*known_results_.get())[result_id] |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 | 286 |
| 310 // The oldest secondary is gone. | 287 // The oldest secondary is gone. |
| 311 EXPECT_EQ(UNKNOWN_RESULT, GetResultType("1")); | 288 EXPECT_EQ(UNKNOWN_RESULT, GetResultType("1")); |
| 312 | 289 |
| 313 // Touched oldest survived. | 290 // Touched oldest survived. |
| 314 EXPECT_EQ(PERFECT_SECONDARY, GetResultType("0")); | 291 EXPECT_EQ(PERFECT_SECONDARY, GetResultType("0")); |
| 315 } | 292 } |
| 316 | 293 |
| 317 } // namespace test | 294 } // namespace test |
| 318 } // namespace app_list | 295 } // namespace app_list |
| OLD | NEW |