1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/offline_pages/core/prefetch/prefetch_item.h"
6 
7 #include "base/strings/string_split.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/time/time.h"
11 #include "components/offline_pages/core/prefetch/prefetch_types.h"
12 #include "components/offline_pages/core/prefetch/store/prefetch_store_schema.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "url/gurl.h"
15 
16 namespace offline_pages {
17 
18 class PrefetchItemTest : public testing::Test {
19  public:
20   void CheckFieldAndResetItem(PrefetchItem& item, const char* tested_field);
21   void CheckAllFieldsWereTested();
22 
23   std::size_t GetTableColumnsCount();
24 
25  private:
26   std::size_t checked_fields_counter_ = 0;
27 };
28 
29 // Checks behavior of some PrefetchItem methods for an item with one single
30 // member updated to a non-default value. After testing the item is reset and
31 // |checked_fields_counter_| is incremented.
CheckFieldAndResetItem(PrefetchItem & item,const char * tested_field)32 void PrefetchItemTest::CheckFieldAndResetItem(PrefetchItem& item,
33                                               const char* tested_field) {
34   EXPECT_NE(item, PrefetchItem())
35       << "Item with updated \"" << tested_field
36       << "\" should not equal a default constructed item";
37   EXPECT_EQ(item, PrefetchItem(item))
38       << "Item with updated \"" << tested_field
39       << "\" should equal a copy constructed based off of it";
40   EXPECT_NE(item.ToString(), PrefetchItem().ToString())
41       << "Result of ToString() from an item with updated \"" << tested_field
42       << "\" should not equal the ToString() result from a default constructed"
43          " item";
44   item = PrefetchItem();
45   ++checked_fields_counter_;
46 }
47 
48 // Compares the |checked_fields_counter_| value with the number of columns in
49 // the SQLite table to make sure they match. This should be run after all
50 // PrefetchItem fields were verified with CheckFieldAndResetItem and it helps in
51 // confirming PrefetchItem implementation and tests are coping with changes in
52 // the table.
CheckAllFieldsWereTested()53 void PrefetchItemTest::CheckAllFieldsWereTested() {
54   // Note: the off-by-1 difference is due to the single client_id field, of type
55   // ClientID, representing 2 columns in the database table (client_namespace
56   // and client_id).
57   EXPECT_EQ(GetTableColumnsCount() - 1, checked_fields_counter_)
58       << "The number of tested fields mismatches the number of columns in the "
59          "database table.";
60 }
61 
62 // Computes the number of columns the SQL table has.
GetTableColumnsCount()63 std::size_t PrefetchItemTest::GetTableColumnsCount() {
64   std::string tableCreationSql =
65       PrefetchStoreSchema::GetItemTableCreationSqlForTesting();
66   std::vector<std::string> create_statement_split = base::SplitString(
67       tableCreationSql, "()", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
68   EXPECT_EQ(3U, create_statement_split.size());
69 
70   std::string& columns_list = create_statement_split[1];
71   std::vector<std::string> columns_split = base::SplitString(
72       columns_list, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
73   return columns_split.size();
74 }
75 
TEST_F(PrefetchItemTest,OperatorEqualsCopyConstructorAndToString)76 TEST_F(PrefetchItemTest, OperatorEqualsCopyConstructorAndToString) {
77   PrefetchItem item1;
78   EXPECT_EQ(item1, PrefetchItem());
79   EXPECT_EQ(item1, PrefetchItem(item1));
80   EXPECT_EQ(item1.ToString(), PrefetchItem().ToString());
81 
82   item1.offline_id = 77L;
83   CheckFieldAndResetItem(item1, "offline_id");
84 
85   item1.guid = "A";
86   CheckFieldAndResetItem(item1, "guid");
87 
88   item1.client_id = ClientId("B", "C");
89   CheckFieldAndResetItem(item1, "client_id");
90 
91   item1.state = PrefetchItemState::AWAITING_GCM;
92   CheckFieldAndResetItem(item1, "state");
93 
94   item1.url = GURL("http://test.com");
95   CheckFieldAndResetItem(item1, "url");
96 
97   item1.final_archived_url = GURL("http://test.com/final");
98   CheckFieldAndResetItem(item1, "final_archived_url");
99 
100   item1.thumbnail_url = GURL("http://thumbnail");
101   CheckFieldAndResetItem(item1, "thumbnail_url");
102 
103   item1.favicon_url = GURL("http://favicon");
104   CheckFieldAndResetItem(item1, "favicon_url");
105 
106   item1.generate_bundle_attempts = 10;
107   CheckFieldAndResetItem(item1, "generate_bundle_attempts");
108 
109   item1.get_operation_attempts = 11;
110   CheckFieldAndResetItem(item1, "get_operation_attempts");
111 
112   item1.download_initiation_attempts = 12;
113   CheckFieldAndResetItem(item1, "download_initiation_attempts");
114 
115   item1.operation_name = "D";
116   CheckFieldAndResetItem(item1, "operation_name");
117 
118   item1.archive_body_name = "E";
119   CheckFieldAndResetItem(item1, "archive_body_name");
120 
121   item1.archive_body_length = 20;
122   CheckFieldAndResetItem(item1, "archive_body_length");
123 
124   item1.creation_time = base::Time::FromJavaTime(1000L);
125   CheckFieldAndResetItem(item1, "creation_time");
126 
127   item1.freshness_time = base::Time::FromJavaTime(2000L);
128   CheckFieldAndResetItem(item1, "freshness_time");
129 
130   item1.error_code = PrefetchItemErrorCode::TOO_MANY_NEW_URLS;
131   CheckFieldAndResetItem(item1, "error_code");
132 
133   item1.title = base::UTF8ToUTF16("F");
134   CheckFieldAndResetItem(item1, "title");
135 
136   item1.file_path = base::FilePath(FILE_PATH_LITERAL("G"));
137   CheckFieldAndResetItem(item1, "file_path");
138 
139   item1.file_size = 30;
140   CheckFieldAndResetItem(item1, "file_size");
141 
142   item1.snippet = "G";
143   CheckFieldAndResetItem(item1, "snippet");
144 
145   item1.attribution = "H";
146   CheckFieldAndResetItem(item1, "attribution");
147 
148   CheckAllFieldsWereTested();
149 }
150 
151 }  // namespace offline_pages
152