Index: components/search_engines/template_url_service_unittest.cc |
diff --git a/components/search_engines/template_url_service_unittest.cc b/components/search_engines/template_url_service_unittest.cc |
index ad474e7745eed221e31213b0de7bf281a38d2d28..2abc67d1eb5eff5732b20f738228fe6ea5472bdc 100644 |
--- a/components/search_engines/template_url_service_unittest.cc |
+++ b/components/search_engines/template_url_service_unittest.cc |
@@ -2,17 +2,20 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+#include "components/search_engines/template_url_service.h" |
+ |
#include "base/bind.h" |
#include "base/bind_helpers.h" |
#include "base/callback.h" |
#include "base/memory/ref_counted.h" |
+#include "base/memory/scoped_ptr.h" |
#include "base/memory/scoped_vector.h" |
#include "base/run_loop.h" |
#include "base/strings/string_split.h" |
#include "base/strings/string_util.h" |
#include "base/strings/utf_string_conversions.h" |
#include "base/task/cancelable_task_tracker.h" |
-#include "base/test/mock_time_provider.h" |
+#include "base/test/simple_test_clock.h" |
#include "base/threading/thread.h" |
#include "base/time/time.h" |
#include "chrome/browser/history/history_service.h" |
@@ -24,15 +27,12 @@ |
#include "components/search_engines/search_terms_data.h" |
#include "components/search_engines/template_url.h" |
#include "components/search_engines/template_url_prepopulate_data.h" |
-#include "components/search_engines/template_url_service.h" |
#include "content/public/test/test_browser_thread_bundle.h" |
#include "testing/gtest/include/gtest/gtest.h" |
using base::ASCIIToUTF16; |
using base::Time; |
using base::TimeDelta; |
-using ::testing::Return; |
-using ::testing::StrictMock; |
namespace { |
@@ -227,7 +227,9 @@ void TemplateURLServiceTest::AssertEquals(const TemplateURL& expected, |
ASSERT_EQ(expected.input_encodings(), actual.input_encodings()); |
ASSERT_EQ(expected.id(), actual.id()); |
ASSERT_EQ(expected.date_created(), actual.date_created()); |
- ASSERT_EQ(expected.last_modified(), actual.last_modified()); |
+ // Note that times are stored with a granularity of one second. |
Peter Kasting
2015/01/22 00:00:23
Doesn't this mean the assert would have to be LE i
Ilya Sherman
2015/01/22 02:38:22
The issue is not that we might tick over a second.
Peter Kasting
2015/01/22 17:54:22
Right, but what I'm worried about is, if the start
Ilya Sherman
2015/01/22 23:13:11
That's not possible -- the test Clock will always
|
+ ASSERT_LT((expected.last_modified() - actual.last_modified()).magnitude(), |
tfarina
2015/01/16 05:01:39
Looks like this accounts for the small difference
|
+ base::TimeDelta::FromSeconds(1)); |
ASSERT_EQ(expected.sync_guid(), actual.sync_guid()); |
ASSERT_EQ(expected.search_terms_replacement_key(), |
actual.search_terms_replacement_key()); |
@@ -305,11 +307,11 @@ TEST_F(TemplateURLServiceTest, AddUpdateRemove) { |
NULL)); |
// We expect the last_modified time to be updated to the present time on an |
- // explicit reset. We have to set up the expectation here because ResetModel |
- // resets the TimeProvider in the TemplateURLService. |
- StrictMock<base::MockTimeProvider> mock_time; |
- model()->set_time_provider(&base::MockTimeProvider::StaticNow); |
- EXPECT_CALL(mock_time, Now()).WillOnce(Return(base::Time::FromDoubleT(1337))); |
+ // explicit reset. |
+ base::Time now = base::Time::Now(); |
+ scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); |
tfarina
2015/01/16 04:28:51
Doesn't have crashed for you? I mean, not this lin
Ilya Sherman
2015/01/16 04:39:18
This doesn't crash, because the ownership is trans
|
+ clock->SetNow(now); |
+ model()->set_clock(clock.Pass()); |
// Mutate an element and verify it succeeded. |
model()->ResetTemplateURL(loaded_url, ASCIIToUTF16("a"), ASCIIToUTF16("b"), |
@@ -329,8 +331,9 @@ TEST_F(TemplateURLServiceTest, AddUpdateRemove) { |
ASSERT_TRUE(loaded_url != NULL); |
AssertEquals(*cloned_url, *loaded_url); |
tfarina
2015/01/16 04:28:51
This didn't pass for me locally.
Ilya Sherman
2015/01/16 04:39:18
Note that I modified the AssertEquals implementati
|
// We changed a TemplateURL in the service, so ensure that the time was |
- // updated. |
- ASSERT_EQ(base::Time::FromDoubleT(1337), loaded_url->last_modified()); |
+ // updated. Note that times are stored with a granularity of one second. |
+ ASSERT_LT((now - loaded_url->last_modified()).magnitude(), |
+ base::TimeDelta::FromSeconds(1)); |
// Remove an element and verify it succeeded. |
model()->Remove(loaded_url); |
@@ -620,10 +623,6 @@ TEST_F(TemplateURLServiceTest, Reset) { |
VerifyObserverCount(1); |
base::RunLoop().RunUntilIdle(); |
- StrictMock<base::MockTimeProvider> mock_time; |
- model()->set_time_provider(&base::MockTimeProvider::StaticNow); |
- EXPECT_CALL(mock_time, Now()).WillOnce(Return(base::Time::FromDoubleT(1337))); |
- |
// Reset the short name, keyword, url and make sure it takes. |
const base::string16 new_short_name(ASCIIToUTF16("a")); |
const base::string16 new_keyword(ASCIIToUTF16("b")); |
@@ -640,13 +639,20 @@ TEST_F(TemplateURLServiceTest, Reset) { |
scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); |
+ base::Time now = base::Time::Now(); |
tfarina
2015/01/16 05:01:39
Have you had to move this down for some reason? Or
Ilya Sherman
2015/01/16 05:11:07
That's a good question. I took your CL as a start
Peter Kasting
2015/01/22 00:00:23
Hmm. It seems like it ought to fail to me, since
Ilya Sherman
2015/01/22 02:38:22
I'm not sure what's causing the test to continue t
Peter Kasting
2015/01/22 17:54:22
Yeah, I wasn't so much asking you to fix it as hop
Ilya Sherman
2015/01/22 23:13:11
Fair enough. I didn't investigate deeply enough t
|
+ scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); |
+ clock->SetNow(now); |
+ model()->set_clock(clock.Pass()); |
+ |
// Reload the model from the database and make sure the change took. |
test_util()->ResetModel(true); |
EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
const TemplateURL* read_url = model()->GetTemplateURLForKeyword(new_keyword); |
ASSERT_TRUE(read_url); |
AssertEquals(*cloned_url, *read_url); |
- ASSERT_EQ(base::Time::FromDoubleT(1337), read_url->last_modified()); |
+ // Note that times are stored with a granularity of one second. |
+ ASSERT_LT((now - read_url->last_modified()).magnitude(), |
+ base::TimeDelta::FromSeconds(1)); |
} |
TEST_F(TemplateURLServiceTest, DefaultSearchProvider) { |