summaryrefslogtreecommitdiff
path: root/chromium/components/offline_pages/core/prefetch/page_bundle_update_task_unittest.cc
blob: 2c7a8da255e57b8d1d103852e7549ecd11691f81 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// 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/prefetch/page_bundle_update_task.h"

#include "base/logging.h"
#include "base/test/mock_callback.h"
#include "components/offline_pages/core/prefetch/prefetch_item.h"
#include "components/offline_pages/core/prefetch/prefetch_task_test_base.h"
#include "components/offline_pages/core/prefetch/prefetch_types.h"
#include "components/offline_pages/core/prefetch/store/prefetch_store.h"
#include "components/offline_pages/core/prefetch/store/prefetch_store_test_util.h"
#include "components/offline_pages/core/prefetch/store/prefetch_store_utils.h"
#include "components/offline_pages/core/prefetch/test_prefetch_dispatcher.h"
#include "components/offline_pages/core/task.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::HasSubstr;

namespace offline_pages {

class PageBundleUpdateTaskTest : public PrefetchTaskTestBase {
 public:
  PageBundleUpdateTaskTest() = default;
  ~PageBundleUpdateTaskTest() override = default;

  TestPrefetchDispatcher dispatcher;

  RenderPageInfo FailedPageInfoForPrefetchItem(const PrefetchItem& item) {
    RenderPageInfo rv;

    rv.url = item.url.spec();
    rv.status = RenderStatus::FAILED;
    return rv;
  }

  RenderPageInfo PendingPageInfoForPrefetchItem(const PrefetchItem& item) {
    RenderPageInfo rv;

    rv.url = item.url.spec();
    rv.status = RenderStatus::PENDING;
    return rv;
  }

  RenderPageInfo RenderedPageInfoForPrefetchItem(const PrefetchItem& item) {
    RenderPageInfo rv;

    rv.url = item.url.spec();
    rv.status = RenderStatus::RENDERED;
    rv.body_name = std::to_string(++body_name_count_);
    rv.body_length = 1024 + body_name_count_;  // a realistic number of bytes

    rv.render_time = base::Time::Now();
    return rv;
  }

 private:
  int body_name_count_ = 0;
};

TEST_F(PageBundleUpdateTaskTest, StoreFailure) {
  store_util()->SimulateInitializationError();
  PageBundleUpdateTask task(store(), &dispatcher, "operation", {});
  RunTask(&task);
}

TEST_F(PageBundleUpdateTaskTest, EmptyTask) {
  PageBundleUpdateTask task(store(), &dispatcher, "operation", {});
  RunTask(&task);
  EXPECT_EQ(0, dispatcher.processing_schedule_count);
}

TEST_F(PageBundleUpdateTaskTest, UpdatesItemsFromSentGeneratePageBundle) {
  // Tests that SENT_GENERATE_PAGE_BUNDLE can correctly transition pages to
  // RECEIVED_BUNDLE.
  PrefetchItem item1 = item_generator()->CreateItem(
      PrefetchItemState::SENT_GENERATE_PAGE_BUNDLE);
  ASSERT_EQ("", item1.operation_name);
  PrefetchItem item2 =
      item_generator()->CreateItem(PrefetchItemState::AWAITING_GCM);
  ASSERT_NE("", item2.operation_name);
  ASSERT_TRUE(store_util()->InsertPrefetchItem(item1));
  ASSERT_TRUE(store_util()->InsertPrefetchItem(item2));
  ASSERT_EQ(2, store_util()->CountPrefetchItems());

  // We assume that the bundle contains items for two entries, but one of those
  // entries is not in the correct state to be updated (different operation
  // name).
  PageBundleUpdateTask task(store(), &dispatcher, "operation",
                            {
                                RenderedPageInfoForPrefetchItem(item1),
                                RenderedPageInfoForPrefetchItem(item2),
                            });
  RunTask(&task);

  // The matching entry has been updated.
  EXPECT_EQ(PrefetchItemState::RECEIVED_BUNDLE,
            store_util()->GetPrefetchItem(item1.offline_id)->state);
  EXPECT_NE("",
            store_util()->GetPrefetchItem(item1.offline_id)->archive_body_name);
  EXPECT_LT(
      0, store_util()->GetPrefetchItem(item1.offline_id)->archive_body_length);
  // The non-matching entry has not changed.
  EXPECT_EQ(item2, *(store_util()->GetPrefetchItem(item2.offline_id)));
  // The dispatcher knows to reschedule the action tasks.
  EXPECT_LE(1, dispatcher.processing_schedule_count);
}

TEST_F(PageBundleUpdateTaskTest, SentGetOperationToReceivedBundle) {
  PrefetchItem item1 =
      item_generator()->CreateItem(PrefetchItemState::SENT_GET_OPERATION);
  ASSERT_EQ("", item1.archive_body_name);
  ASSERT_EQ(-1, item1.archive_body_length);

  ASSERT_TRUE(store_util()->InsertPrefetchItem(item1));

  PageBundleUpdateTask task(store(), &dispatcher, item1.operation_name,
                            {
                                RenderedPageInfoForPrefetchItem(item1),
                            });
  RunTask(&task);

  // The matching entry has been updated.
  EXPECT_EQ(store_util()->GetPrefetchItem(item1.offline_id)->state,
            PrefetchItemState::RECEIVED_BUNDLE);
  EXPECT_NE("",
            store_util()->GetPrefetchItem(item1.offline_id)->archive_body_name);
  EXPECT_LT(
      0, store_util()->GetPrefetchItem(item1.offline_id)->archive_body_length);
  EXPECT_LE(1, dispatcher.processing_schedule_count);
}

TEST_F(PageBundleUpdateTaskTest, UrlDoesNotMatch) {
  // Tests that no change happens if the URLs passed into the task don't match
  // the ones in the store.
  PrefetchItem item = item_generator()->CreateItem(
      PrefetchItemState::SENT_GENERATE_PAGE_BUNDLE);
  ASSERT_TRUE(store_util()->InsertPrefetchItem(item));

  // Dummy item not inserted into store.
  PrefetchItem item2 = item_generator()->CreateItem(
      PrefetchItemState::SENT_GENERATE_PAGE_BUNDLE);

  ASSERT_NE(item.url, item2.url);

  PageBundleUpdateTask task(store(), &dispatcher, "operation",
                            {
                                RenderedPageInfoForPrefetchItem(item2),
                            });
  RunTask(&task);
  EXPECT_EQ(item, *(store_util()->GetPrefetchItem(item.offline_id)));
  EXPECT_EQ(0, dispatcher.processing_schedule_count);
}

TEST_F(PageBundleUpdateTaskTest, PendingRenderAwaitsGCM) {
  // Tests that the transition to AWAITING_GCM happens correctly from
  // SENT_GENERATE_PAGE_BUNDLE.
  PrefetchItem item = item_generator()->CreateItem(
      PrefetchItemState::SENT_GENERATE_PAGE_BUNDLE);
  ASSERT_EQ("", item.operation_name);
  ASSERT_TRUE(store_util()->InsertPrefetchItem(item));

  PageBundleUpdateTask task(store(), &dispatcher, "operation",
                            {
                                PendingPageInfoForPrefetchItem(item),
                            });
  RunTask(&task);

  EXPECT_EQ(PrefetchItemState::AWAITING_GCM,
            store_util()->GetPrefetchItem(item.offline_id)->state);
  EXPECT_EQ("operation",
            store_util()->GetPrefetchItem(item.offline_id)->operation_name);
  EXPECT_EQ(0, dispatcher.processing_schedule_count);
}

TEST_F(PageBundleUpdateTaskTest, FailedRenderToFinished) {
  // Tests one item with a archiving failed result, and one with a limit
  // exceeded result.
  PrefetchItem item1 = item_generator()->CreateItem(
      PrefetchItemState::SENT_GENERATE_PAGE_BUNDLE);
  ASSERT_EQ(PrefetchItemErrorCode::SUCCESS, item1.error_code);
  ASSERT_TRUE(store_util()->InsertPrefetchItem(item1));
  RenderPageInfo item1_render_info = FailedPageInfoForPrefetchItem(item1);

  PrefetchItem item2 = item_generator()->CreateItem(
      PrefetchItemState::SENT_GENERATE_PAGE_BUNDLE);
  ASSERT_EQ(PrefetchItemErrorCode::SUCCESS, item2.error_code);
  ASSERT_TRUE(store_util()->InsertPrefetchItem(item2));
  RenderPageInfo item2_render_info = FailedPageInfoForPrefetchItem(item2);
  item2_render_info.status = RenderStatus::EXCEEDED_LIMIT;
  PageBundleUpdateTask task(store(), &dispatcher, "operation",
                            {item2_render_info, item1_render_info});
  RunTask(&task);

  EXPECT_EQ(PrefetchItemState::FINISHED,
            store_util()->GetPrefetchItem(item1.offline_id)->state);
  EXPECT_EQ(PrefetchItemErrorCode::ARCHIVING_FAILED,
            store_util()->GetPrefetchItem(item1.offline_id)->error_code);

  EXPECT_EQ(PrefetchItemState::FINISHED,
            store_util()->GetPrefetchItem(item2.offline_id)->state);
  EXPECT_EQ(PrefetchItemErrorCode::ARCHIVING_LIMIT_EXCEEDED,
            store_util()->GetPrefetchItem(item2.offline_id)->error_code);
  EXPECT_EQ(0, dispatcher.processing_schedule_count);
}

TEST_F(PageBundleUpdateTaskTest, MixOfResults) {
  // Tests that 3 items all with different render results get updated
  // independently and properly.
  PrefetchItem item1 = item_generator()->CreateItem(
      PrefetchItemState::SENT_GENERATE_PAGE_BUNDLE);
  ASSERT_TRUE(store_util()->InsertPrefetchItem(item1));
  RenderPageInfo item1_render_info = PendingPageInfoForPrefetchItem(item1);

  PrefetchItem item2 = item_generator()->CreateItem(
      PrefetchItemState::SENT_GENERATE_PAGE_BUNDLE);
  ASSERT_TRUE(store_util()->InsertPrefetchItem(item2));
  RenderPageInfo item2_render_info = FailedPageInfoForPrefetchItem(item2);

  PrefetchItem item3 = item_generator()->CreateItem(
      PrefetchItemState::SENT_GENERATE_PAGE_BUNDLE);
  ASSERT_TRUE(store_util()->InsertPrefetchItem(item3));
  RenderPageInfo item3_render_info = RenderedPageInfoForPrefetchItem(item3);

  PageBundleUpdateTask task(
      store(), &dispatcher, "operation",
      {item3_render_info, item2_render_info, item1_render_info});
  RunTask(&task);

  EXPECT_EQ(PrefetchItemState::AWAITING_GCM,
            store_util()->GetPrefetchItem(item1.offline_id)->state);
  EXPECT_EQ(PrefetchItemState::FINISHED,
            store_util()->GetPrefetchItem(item2.offline_id)->state);
  EXPECT_EQ(PrefetchItemState::RECEIVED_BUNDLE,
            store_util()->GetPrefetchItem(item3.offline_id)->state);
  EXPECT_LE(1, dispatcher.processing_schedule_count);
}

}  // namespace offline_pages