Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <set> | 5 #include <set> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 409 | 409 |
| 410 // Now try without de-duping, expect to see all visible visits to url id 1. | 410 // Now try without de-duping, expect to see all visible visits to url id 1. |
| 411 options.duplicate_policy = QueryOptions::KEEP_ALL_DUPLICATES; | 411 options.duplicate_policy = QueryOptions::KEEP_ALL_DUPLICATES; |
| 412 GetVisibleVisitsForURL(url_id, options, &results); | 412 GetVisibleVisitsForURL(url_id, options, &results); |
| 413 ASSERT_EQ(static_cast<size_t>(3), results.size()); | 413 ASSERT_EQ(static_cast<size_t>(3), results.size()); |
| 414 EXPECT_TRUE(IsVisitInfoEqual(results[0], test_visit_rows[5])); | 414 EXPECT_TRUE(IsVisitInfoEqual(results[0], test_visit_rows[5])); |
| 415 EXPECT_TRUE(IsVisitInfoEqual(results[1], test_visit_rows[1])); | 415 EXPECT_TRUE(IsVisitInfoEqual(results[1], test_visit_rows[1])); |
| 416 EXPECT_TRUE(IsVisitInfoEqual(results[2], test_visit_rows[0])); | 416 EXPECT_TRUE(IsVisitInfoEqual(results[2], test_visit_rows[0])); |
| 417 } | 417 } |
| 418 | 418 |
| 419 TEST_F(VisitDatabaseTest, GetHistoryCount) { | |
| 420 Time today = Time::Now().LocalMidnight(); | |
| 421 // Find the beginning of tomorrow and the day after tomorrow. We cannot use | |
| 422 // TimeDelta::FromDays(1), as this simply adds 24 hours and thus does not work | |
| 423 // correctly with DST shifts. Instead, we'll jump 36 hours (i.e. somewhere | |
| 424 // in the middle of the next day), and use |LocalMidnight()| to round down | |
| 425 // to the beginning of the day in the local time, taking timezones and DST | |
| 426 // into account. This is necessary to achieve the same equivalence class | |
| 427 // on days as the DATE(..., 'localtime') function in SQL. | |
| 428 Time tomorrow = (today + TimeDelta::FromHours(36)).LocalMidnight(); | |
|
sdefresne
2015/09/29 08:21:49
nit: this confused me, as there can never be visit
msramek
2015/09/29 11:04:41
Done. Changed to the two previous days. And in the
| |
| 429 Time after_tomorrow = (tomorrow + TimeDelta::FromHours(36)).LocalMidnight(); | |
| 430 Time now = today; | |
| 431 | |
| 432 ui::PageTransition standard_transition = ui::PageTransitionFromInt( | |
| 433 ui::PAGE_TRANSITION_TYPED | | |
| 434 ui::PAGE_TRANSITION_CHAIN_START | | |
| 435 ui::PAGE_TRANSITION_CHAIN_END); | |
| 436 | |
| 437 // Add 5 visits (3 distinct URLs) for today. Whether the URL was browsed | |
| 438 // on this machine or synced has no effect. | |
| 439 VisitRow today_1(1, now, 0, standard_transition, 0); | |
| 440 today_1.visit_id = 1; | |
| 441 AddVisit(&today_1, SOURCE_BROWSED); | |
| 442 now += TimeDelta::FromHours(1); | |
| 443 | |
| 444 VisitRow today_2(2, now, 0, standard_transition, 0); | |
| 445 today_2.visit_id = 2; | |
| 446 AddVisit(&today_2, SOURCE_BROWSED); | |
| 447 now += TimeDelta::FromHours(1); | |
| 448 | |
| 449 VisitRow today_3(1, now, 0, standard_transition, 0); | |
| 450 today_3.visit_id = 3; | |
| 451 AddVisit(&today_3, SOURCE_SYNCED); | |
| 452 now += TimeDelta::FromHours(1); | |
| 453 | |
| 454 VisitRow today_4(3, now, 0, standard_transition, 0); | |
| 455 today_4.visit_id = 4; | |
| 456 AddVisit(&today_4, SOURCE_SYNCED); | |
| 457 now += TimeDelta::FromHours(1); | |
| 458 | |
| 459 VisitRow today_5(2, now, 0, standard_transition, 0); | |
| 460 today_5.visit_id = 5; | |
| 461 AddVisit(&today_5, SOURCE_BROWSED); | |
| 462 now += TimeDelta::FromHours(1); | |
| 463 | |
| 464 // Add 4 more visits for tomorrow. One of them is invalid, as it's not | |
| 465 // a user-visible navigation. Of the remaining 3, only 2 are unique. | |
| 466 now = tomorrow; | |
| 467 | |
| 468 VisitRow tomorrow_1(1, now, 0, standard_transition, 0); | |
| 469 tomorrow_1.visit_id = 6; | |
| 470 AddVisit(&tomorrow_1, SOURCE_BROWSED); | |
| 471 now += TimeDelta::FromHours(1); | |
| 472 | |
| 473 VisitRow tomorrow_2(1, now, 0, standard_transition, 0); | |
| 474 tomorrow_2.visit_id = 7; | |
| 475 AddVisit(&tomorrow_2, SOURCE_BROWSED); | |
| 476 now += TimeDelta::FromHours(1); | |
| 477 | |
| 478 VisitRow tomorrow_3(2, now, 0, ui::PAGE_TRANSITION_AUTO_SUBFRAME, 0); | |
| 479 tomorrow_3.visit_id = 8; | |
| 480 AddVisit(&tomorrow_3, SOURCE_BROWSED); | |
| 481 now += TimeDelta::FromHours(1); | |
| 482 | |
| 483 VisitRow tomorrow_4(3, now, 0, standard_transition, 0); | |
| 484 tomorrow_4.visit_id = 9; | |
| 485 AddVisit(&tomorrow_4, SOURCE_BROWSED); | |
| 486 now += TimeDelta::FromHours(1); | |
| 487 | |
| 488 int result; | |
| 489 | |
| 490 // There are 3 distinct URLs today. | |
| 491 EXPECT_TRUE(GetHistoryCount(today, tomorrow, &result)); | |
| 492 EXPECT_EQ(3, result); | |
| 493 | |
| 494 // For today and tomorrow, there should be 5 per-day unique URLs. | |
| 495 EXPECT_TRUE(GetHistoryCount(today, after_tomorrow, &result)); | |
| 496 EXPECT_EQ(5, result); | |
| 497 | |
| 498 // Since we only have entries for today and tomorrow, the infinite time | |
| 499 // range should yield the same result. | |
| 500 EXPECT_TRUE(GetHistoryCount(Time(), Time::Max(), &result)); | |
| 501 EXPECT_EQ(5, result); | |
| 502 | |
| 503 // Narrowing the range to exclude |today_1| will still return 5, | |
| 504 // because |today_1| is not unique. | |
| 505 EXPECT_TRUE(GetHistoryCount( | |
| 506 today + TimeDelta::FromHours(2), after_tomorrow, &result)); | |
| 507 EXPECT_EQ(5, result); | |
| 508 | |
| 509 // Narrowing the range to exclude |tomorrow_4| will return 4, | |
| 510 // because |tomorrow_4| is unique. | |
| 511 EXPECT_TRUE(GetHistoryCount( | |
| 512 today, tomorrow + TimeDelta::FromHours(3), &result)); | |
| 513 EXPECT_EQ(4, result); | |
| 514 | |
| 515 // Narrowing the range to exclude both |today_1| and |tomorrow_4| will | |
| 516 // still return 4. | |
| 517 EXPECT_TRUE(GetHistoryCount(today + TimeDelta::FromHours(2), | |
| 518 tomorrow + TimeDelta::FromHours(3), | |
| 519 &result)); | |
| 520 EXPECT_EQ(4, result); | |
| 521 | |
| 522 // A range that contains no visits will return 0. | |
| 523 EXPECT_TRUE(GetHistoryCount(today + TimeDelta::FromMicroseconds(1), | |
| 524 today + TimeDelta::FromHours(1), | |
| 525 &result)); | |
| 526 EXPECT_EQ(0, result); | |
| 527 | |
| 528 // If this timezone uses DST, test the behavior on days when the time | |
| 529 // is shifted forward and backward. | |
| 530 Time shift_forward; | |
| 531 Time shift_backward; | |
| 532 for (int i = 0; i < 366; i++) { | |
|
lwchkg
2015/09/28 18:14:51
Forward at least one day before the loop start. Ot
msramek
2015/09/29 11:04:41
Thanks for catching this. Since now after sdefresn
| |
| 533 today = (today + TimeDelta::FromHours(36)).LocalMidnight(); | |
| 534 tomorrow = today + TimeDelta::FromHours(24); | |
|
lwchkg
2015/09/28 18:14:51
Is it better to name it after_24_hours?
msramek
2015/09/29 11:04:41
Done.
| |
| 535 | |
| 536 if (today.LocalMidnight() == tomorrow.LocalMidnight()) { | |
|
lwchkg
2015/09/28 18:14:51
nits: today.LocalMidnight() can be replaced by jus
msramek
2015/09/29 11:04:41
Done.
| |
| 537 // More than 24 hours. Shift backward. | |
| 538 shift_backward = today; | |
| 539 } else if (tomorrow > tomorrow.LocalMidnight()) { | |
| 540 // Less than 24 hours. Shift forward. | |
| 541 shift_forward = today; | |
| 542 } | |
| 543 | |
| 544 if (!shift_backward.is_null() && !shift_forward.is_null()) | |
| 545 break; | |
| 546 } | |
| 547 | |
| 548 // Test the backward shift. Add two visits for the same page on midnight and | |
| 549 // 24 hours later. The count should be 1, not 2, because the day is longer | |
| 550 // than 24 hours, and the two visits will be regarded as duplicate. | |
| 551 if (!shift_backward.is_null()) { | |
| 552 VisitRow backward_1(1, shift_backward, 0, standard_transition, 0); | |
| 553 backward_1.visit_id = 10; | |
| 554 AddVisit(&backward_1, SOURCE_BROWSED); | |
| 555 | |
| 556 VisitRow backward_2(1, shift_backward + TimeDelta::FromHours(24), | |
| 557 0, standard_transition, 0); | |
| 558 backward_2.visit_id = 11; | |
| 559 AddVisit(&backward_2, SOURCE_BROWSED); | |
| 560 | |
| 561 EXPECT_TRUE(GetHistoryCount(shift_backward, | |
| 562 shift_backward + TimeDelta::FromHours(25), | |
| 563 &result)); | |
| 564 EXPECT_EQ(1, result); | |
| 565 } | |
| 566 | |
| 567 // Test the forward shift. Add two visits for the same page at midnight and | |
| 568 // almost 24 hours later. The count should be 2, not 1. The visits would be | |
| 569 // regarded as duplicate in a normal 24 hour day, but in this case the second | |
| 570 // visit is already in the next day. | |
| 571 if (!shift_forward.is_null()) { | |
| 572 VisitRow forward_1(1, shift_forward, 0, standard_transition, 0); | |
| 573 forward_1.visit_id = 12; | |
| 574 AddVisit(&forward_1, SOURCE_BROWSED); | |
| 575 | |
| 576 Time almost_24_hours_later = shift_forward + | |
| 577 TimeDelta::FromHours(24) - | |
| 578 TimeDelta::FromMicroseconds(1); | |
| 579 VisitRow forward_2(1, almost_24_hours_later, 0, standard_transition, 0); | |
| 580 forward_2.visit_id = 13; | |
| 581 AddVisit(&forward_2, SOURCE_BROWSED); | |
| 582 | |
| 583 EXPECT_TRUE(GetHistoryCount(shift_forward, | |
| 584 shift_forward + TimeDelta::FromHours(24), | |
| 585 &result)); | |
| 586 EXPECT_EQ(2, result); | |
| 587 } | |
| 588 } | |
| 589 | |
| 419 } // namespace history | 590 } // namespace history |
| OLD | NEW |