summaryrefslogtreecommitdiff
path: root/chromium/components/offline_pages/core/model/mark_page_accessed_task_unittest.cc
blob: a432190751e10d79525a4e59da2275ad74d92037 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/offline_pages/core/model/mark_page_accessed_task.h"

#include <memory>

#include "base/test/histogram_tester.h"
#include "base/time/time.h"
#include "components/offline_pages/core/model/model_task_test_base.h"
#include "components/offline_pages/core/model/offline_page_model_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace offline_pages {

namespace {
const GURL kTestUrl("http://example.com");
const int64_t kTestOfflineId = 1234LL;
const char kTestClientNamespace[] = "default";
const ClientId kTestClientId(kTestClientNamespace, "1234");
const base::FilePath kTestFilePath(FILE_PATH_LITERAL("/test/path/file"));
const int64_t kTestFileSize = 876543LL;
}  // namespace

class MarkPageAccessedTaskTest : public ModelTaskTestBase {
 public:
  base::HistogramTester* histogram_tester() { return &histogram_tester_; }

 private:
  base::HistogramTester histogram_tester_;
};

TEST_F(MarkPageAccessedTaskTest, MarkPageAccessed) {
  OfflinePageItem page(kTestUrl, kTestOfflineId, kTestClientId, kTestFilePath,
                       kTestFileSize);
  store_test_util()->InsertItem(page);

  base::Time current_time = base::Time::Now();
  auto task = std::make_unique<MarkPageAccessedTask>(store(), kTestOfflineId,
                                                     current_time);
  RunTask(std::move(task));

  auto offline_page = store_test_util()->GetPageByOfflineId(kTestOfflineId);
  EXPECT_EQ(kTestUrl, offline_page->url);
  EXPECT_EQ(kTestClientId, offline_page->client_id);
  EXPECT_EQ(kTestFileSize, offline_page->file_size);
  EXPECT_EQ(1, offline_page->access_count);
  EXPECT_EQ(current_time, offline_page->last_access_time);
  histogram_tester()->ExpectUniqueSample(
      "OfflinePages.AccessPageCount",
      static_cast<int>(model_utils::ToNamespaceEnum(kTestClientId.name_space)),
      1);
  histogram_tester()->ExpectUniqueSample(
      model_utils::AddHistogramSuffix(kTestClientId.name_space,
                                      "OfflinePages.PageAccessInterval"),
      (current_time - page.last_access_time).InMinutes(), 1);
}

TEST_F(MarkPageAccessedTaskTest, MarkPageAccessedTwice) {
  OfflinePageItem page(kTestUrl, kTestOfflineId, kTestClientId, kTestFilePath,
                       kTestFileSize);
  store_test_util()->InsertItem(page);

  base::Time current_time = base::Time::Now();
  auto task = std::make_unique<MarkPageAccessedTask>(store(), kTestOfflineId,
                                                     current_time);
  RunTask(std::move(task));

  auto offline_page = store_test_util()->GetPageByOfflineId(kTestOfflineId);
  EXPECT_EQ(kTestOfflineId, offline_page->offline_id);
  EXPECT_EQ(kTestUrl, offline_page->url);
  EXPECT_EQ(kTestClientId, offline_page->client_id);
  EXPECT_EQ(kTestFileSize, offline_page->file_size);
  EXPECT_EQ(1, offline_page->access_count);
  EXPECT_EQ(current_time, offline_page->last_access_time);
  histogram_tester()->ExpectUniqueSample(
      "OfflinePages.AccessPageCount",
      static_cast<int>(model_utils::ToNamespaceEnum(kTestClientId.name_space)),
      1);
  histogram_tester()->ExpectUniqueSample(
      model_utils::AddHistogramSuffix(kTestClientId.name_space,
                                      "OfflinePages.PageAccessInterval"),
      (current_time - page.last_access_time).InMinutes(), 1);

  base::Time second_time = base::Time::Now();
  task = std::make_unique<MarkPageAccessedTask>(store(), kTestOfflineId,
                                                second_time);
  RunTask(std::move(task));

  offline_page = store_test_util()->GetPageByOfflineId(kTestOfflineId);
  EXPECT_EQ(kTestOfflineId, offline_page->offline_id);
  EXPECT_EQ(2, offline_page->access_count);
  EXPECT_EQ(second_time, offline_page->last_access_time);
  histogram_tester()->ExpectUniqueSample(
      "OfflinePages.AccessPageCount",
      static_cast<int>(model_utils::ToNamespaceEnum(kTestClientId.name_space)),
      2);
  histogram_tester()->ExpectBucketCount(
      model_utils::AddHistogramSuffix(kTestClientId.name_space,
                                      "OfflinePages.PageAccessInterval"),
      (second_time - current_time).InMinutes(), 1);
}

}  // namespace offline_pages