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 yesterday and the day before yesterday. We cannot use | |
422 // TimeDelta::FromDays(1), as this simply removes 24 hours and thus does not | |
423 // work correctly with DST shifts. Instead, we'll jump 36 hours (i.e. | |
424 // somewhere in the middle of the previous day), and use |LocalMidnight()| to | |
425 // round down to the beginning of the day in the local time, taking timezones | |
426 // and DST into account. This is necessary to achieve the same equivalence | |
427 // class on days as the DATE(..., 'localtime') function in SQL. | |
428 Time yesterday = (today - TimeDelta::FromHours(36)).LocalMidnight(); | |
lwchkg
2015/10/01 17:01:48
It should be (today - TimeDelta::FromSeconds(1)).L
msramek
2015/10/06 16:07:27
Oops! And it passes the test perfectly anyway, bec
| |
429 Time two_days_ago = (yesterday - TimeDelta::FromHours(36)).LocalMidnight(); | |
lwchkg
2015/10/01 17:01:48
A similar mistake is found here. So effectively yo
msramek
2015/10/06 16:07:27
Done.
| |
430 Time now = two_days_ago; | |
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 the day before yesterday. | |
438 // Whether the URL was browsed on this machine or synced has no effect. | |
439 VisitRow first_day_1(1, now, 0, standard_transition, 0); | |
440 first_day_1.visit_id = 1; | |
441 AddVisit(&first_day_1, SOURCE_BROWSED); | |
442 now += TimeDelta::FromHours(1); | |
443 | |
444 VisitRow first_day_2(2, now, 0, standard_transition, 0); | |
445 first_day_2.visit_id = 2; | |
446 AddVisit(&first_day_2, SOURCE_BROWSED); | |
447 now += TimeDelta::FromHours(1); | |
448 | |
449 VisitRow first_day_3(1, now, 0, standard_transition, 0); | |
450 first_day_3.visit_id = 3; | |
451 AddVisit(&first_day_3, SOURCE_SYNCED); | |
452 now += TimeDelta::FromHours(1); | |
453 | |
454 VisitRow first_day_4(3, now, 0, standard_transition, 0); | |
455 first_day_4.visit_id = 4; | |
456 AddVisit(&first_day_4, SOURCE_SYNCED); | |
457 now += TimeDelta::FromHours(1); | |
458 | |
459 VisitRow first_day_5(2, now, 0, standard_transition, 0); | |
460 first_day_5.visit_id = 5; | |
461 AddVisit(&first_day_5, SOURCE_BROWSED); | |
462 now += TimeDelta::FromHours(1); | |
463 | |
464 // Add 4 more visits for yesterday. One of them is invalid, as it's not | |
465 // a user-visible navigation. Of the remaining 3, only 2 are unique. | |
466 now = yesterday; | |
467 | |
468 VisitRow second_day_1(1, now, 0, standard_transition, 0); | |
469 second_day_1.visit_id = 6; | |
470 AddVisit(&second_day_1, SOURCE_BROWSED); | |
471 now += TimeDelta::FromHours(1); | |
472 | |
473 VisitRow second_day_2(1, now, 0, standard_transition, 0); | |
474 second_day_2.visit_id = 7; | |
475 AddVisit(&second_day_2, SOURCE_BROWSED); | |
476 now += TimeDelta::FromHours(1); | |
477 | |
478 VisitRow second_day_3(2, now, 0, ui::PAGE_TRANSITION_AUTO_SUBFRAME, 0); | |
479 second_day_3.visit_id = 8; | |
480 AddVisit(&second_day_3, SOURCE_BROWSED); | |
481 now += TimeDelta::FromHours(1); | |
482 | |
483 VisitRow second_day_4(3, now, 0, standard_transition, 0); | |
484 second_day_4.visit_id = 9; | |
485 AddVisit(&second_day_4, SOURCE_BROWSED); | |
486 now += TimeDelta::FromHours(1); | |
487 | |
488 int result; | |
489 | |
490 // There were 3 distinct URLs two days ago. | |
491 EXPECT_TRUE(GetHistoryCount(two_days_ago, yesterday, &result)); | |
492 EXPECT_EQ(3, result); | |
493 | |
494 // For both previous days, there should be 5 per-day unique URLs. | |
495 EXPECT_TRUE(GetHistoryCount(two_days_ago, today, &result)); | |
496 EXPECT_EQ(5, result); | |
497 | |
498 // Since we only have entries for the two previous days, 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 |first_day_1| will still return 5, | |
504 // because |first_day_1| is not unique. | |
505 EXPECT_TRUE(GetHistoryCount( | |
506 two_days_ago + TimeDelta::FromHours(2), today, &result)); | |
507 EXPECT_EQ(5, result); | |
508 | |
509 // Narrowing the range to exclude |second_day_4| will return 4, | |
510 // because |second_day_4| is unique. | |
511 EXPECT_TRUE(GetHistoryCount( | |
512 two_days_ago, yesterday + TimeDelta::FromHours(3), &result)); | |
513 EXPECT_EQ(4, result); | |
514 | |
515 // Narrowing the range to exclude both |first_day_1| and |second_day_4| will | |
516 // still return 4. | |
517 EXPECT_TRUE(GetHistoryCount(two_days_ago + TimeDelta::FromHours(2), | |
518 yesterday + 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(two_days_ago + TimeDelta::FromMicroseconds(1), | |
524 two_days_ago + 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 Time current_day = (two_days_ago - TimeDelta::FromSeconds(1)).LocalMidnight(); | |
533 for (int i = 0; i < 366; i++) { | |
534 current_day = (current_day - TimeDelta::FromSeconds(1)).LocalMidnight(); | |
535 Time after_24_hours = current_day + TimeDelta::FromHours(24); | |
536 | |
537 if (current_day == after_24_hours.LocalMidnight()) { | |
538 // More than 24 hours. Shift backward. | |
539 shift_backward = current_day; | |
540 } else if (after_24_hours > after_24_hours.LocalMidnight()) { | |
541 // Less than 24 hours. Shift forward. | |
542 shift_forward = current_day; | |
543 } | |
544 | |
545 if (!shift_backward.is_null() && !shift_forward.is_null()) | |
546 break; | |
547 } | |
548 | |
549 // Test the backward shift. Add two visits for the same page on midnight and | |
550 // 24 hours later. The count should be 1, not 2, because the day is longer | |
551 // than 24 hours, and the two visits will be regarded as duplicate. | |
552 if (!shift_backward.is_null()) { | |
553 VisitRow backward_1(1, shift_backward, 0, standard_transition, 0); | |
554 backward_1.visit_id = 10; | |
555 AddVisit(&backward_1, SOURCE_BROWSED); | |
556 | |
557 VisitRow backward_2(1, shift_backward + TimeDelta::FromHours(24), | |
558 0, standard_transition, 0); | |
559 backward_2.visit_id = 11; | |
560 AddVisit(&backward_2, SOURCE_BROWSED); | |
561 | |
562 EXPECT_TRUE(GetHistoryCount(shift_backward, | |
563 shift_backward + TimeDelta::FromHours(25), | |
564 &result)); | |
565 EXPECT_EQ(1, result); | |
566 } | |
567 | |
568 // Test the forward shift. Add two visits for the same page at midnight and | |
569 // almost 24 hours later. The count should be 2, not 1. The visits would be | |
570 // regarded as duplicate in a normal 24 hour day, but in this case the second | |
571 // visit is already in the next day. | |
572 if (!shift_forward.is_null()) { | |
573 VisitRow forward_1(1, shift_forward, 0, standard_transition, 0); | |
574 forward_1.visit_id = 12; | |
575 AddVisit(&forward_1, SOURCE_BROWSED); | |
576 | |
577 Time almost_24_hours_later = shift_forward + | |
578 TimeDelta::FromHours(24) - | |
579 TimeDelta::FromMicroseconds(1); | |
580 VisitRow forward_2(1, almost_24_hours_later, 0, standard_transition, 0); | |
581 forward_2.visit_id = 13; | |
582 AddVisit(&forward_2, SOURCE_BROWSED); | |
583 | |
584 EXPECT_TRUE(GetHistoryCount(shift_forward, | |
585 shift_forward + TimeDelta::FromHours(24), | |
586 &result)); | |
587 EXPECT_EQ(2, result); | |
588 } | |
589 } | |
590 | |
419 } // namespace history | 591 } // namespace history |
OLD | NEW |