summaryrefslogtreecommitdiff
path: root/chromium/components/feed/core/v2/api_test/feed_api_notice_card_unittest.cc
blob: 45d8d5bf695893d3a721816892d3844fd5f50c50 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright 2020 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 "base/callback_helpers.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/metrics/user_action_tester.h"
#include "components/feed/core/v2/api_test/feed_api_test.h"
#include "components/feed/core/v2/config.h"
#include "components/feed/core/v2/feed_stream.h"
#include "components/feed/core/v2/proto_util.h"
#include "components/feed/core/v2/public/stream_type.h"
#include "components/feed/core/v2/test/callback_receiver.h"
#include "components/feed/core/v2/test/proto_printer.h"
#include "components/feed/core/v2/types.h"
#include "components/feed/feed_feature_list.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace feed {
namespace test {
namespace {
class FeedApiNoticeCardTest : public FeedApiTest {
 public:
  void SetUp() override {
    FeedApiTest::SetUp();
    model_generator_.privacy_notice_fulfilled = true;
  }

 protected:
  StreamModelUpdateRequestGenerator model_generator_;
};

class FeedStreamConditionalActionsUploadTest : public FeedApiNoticeCardTest {
 public:
  FeedStreamConditionalActionsUploadTest() {
    scoped_feature_list_.InitAndEnableFeature(
        feed::kInterestFeedV2ClicksAndViewsConditionalUpload);
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

TEST_F(FeedApiNoticeCardTest, LoadStreamSendsNoticeCardAcknowledgement) {
  response_translator_.InjectResponse(model_generator_.MakeFirstPage());

  TestForYouSurface surface(stream_.get());
  WaitForIdleTaskQueue();

  // Generate 3 view actions and 1 click action to trigger the acknowledgement
  // of the notice card.
  const int notice_card_index = 0;
  std::string slice_id =
      surface.initial_state->updated_slices(notice_card_index)
          .slice()
          .slice_id();
  stream_->ReportSliceViewed(surface.GetSurfaceId(), surface.GetStreamType(),
                             slice_id);
  task_environment_.FastForwardBy(base::Hours(1));
  stream_->ReportSliceViewed(surface.GetSurfaceId(), surface.GetStreamType(),
                             slice_id);
  task_environment_.FastForwardBy(base::Hours(1));
  stream_->ReportSliceViewed(surface.GetSurfaceId(), surface.GetStreamType(),
                             slice_id);
  stream_->ReportOpenAction(GURL(), surface.GetStreamType(), slice_id);

  response_translator_.InjectResponse(model_generator_.MakeFirstPage());
  stream_->UnloadModel(surface.GetStreamType());
  stream_->ExecuteRefreshTask(RefreshTaskId::kRefreshForYouFeed);
  WaitForIdleTaskQueue();

  EXPECT_TRUE(network_.query_request_sent->feed_request()
                  .feed_query()
                  .chrome_fulfillment_info()
                  .notice_card_acknowledged());
}

TEST_F(FeedStreamConditionalActionsUploadTest,
       NoActionsUploadUntilReachedConditions) {
  // Tests the flow where we:
  //   (1) Perform a ThereAndBackAgain action and a View action while upload
  //   isn't enabled => (2) Attempt an upload while the upload conditions aren't
  //   reached => (3) Reach upload conditions => (4) Perform another View action
  //   that should be dropped => (5) Simulate the backgrounding of the app to
  //   enable actions upload => (6) Trigger an upload which will upload the
  //   stored ThereAndBackAgain action.

  // WebFeed stream is only fetched when there's a subscription.
  network_.InjectListWebFeedsResponse({MakeWireWebFeed("cats")});
  response_translator_.InjectResponse(model_generator_.MakeFirstPage());
  TestWebFeedSurface surface(stream_.get());
  WaitForIdleTaskQueue();

  // Process the view action and the ThereAndBackAgain action while the upload
  // conditions aren't reached.
  stream_->ProcessViewAction(MakeFeedAction(42ul).SerializeAsString());
  WaitForIdleTaskQueue();
  // Verify that the view action was dropped.
  ASSERT_EQ(0ul, ReadStoredActions(stream_->GetStore()).size());

  stream_->ProcessThereAndBackAgain(
      MakeThereAndBackAgainData(42ul).SerializeAsString());
  WaitForIdleTaskQueue();
  // Verify that the ThereAndBackAgain action is in the action store.
  ASSERT_EQ(1ul, ReadStoredActions(stream_->GetStore()).size());

  // Attempt an upload.
  stream_->OnEnterBackground();
  WaitForIdleTaskQueue();
  // Verify that no upload is done because the conditions aren't reached.
  EXPECT_EQ(0, network_.GetActionRequestCount());

  // Reach conditions.
  stream_->ReportSliceViewed(
      surface.GetSurfaceId(), surface.GetStreamType(),
      surface.initial_state->updated_slices(0).slice().slice_id());

  // Verify that the view action is still dropped because we haven't
  // transitioned out of the current surface.
  stream_->ProcessViewAction(MakeFeedAction(42ul).SerializeAsString());
  WaitForIdleTaskQueue();
  ASSERT_EQ(1ul, ReadStoredActions(stream_->GetStore()).size());

  // Enable the upload bit and trigger the upload of actions.
  surface.Detach();
  stream_->OnEnterBackground();
  WaitForIdleTaskQueue();

  // Verify that the ThereAndBackAgain action was uploaded but not the view
  // action.
  ASSERT_EQ(1, network_.GetActionRequestCount());
  EXPECT_EQ(1, network_.GetActionRequestSent()->feed_actions_size());
}

TEST_F(FeedStreamConditionalActionsUploadTest, EnableUploadOnSurfaceAttached) {
  response_translator_.InjectResponse(model_generator_.MakeFirstPage());
  TestForYouSurface surface(stream_.get());
  WaitForIdleTaskQueue();

  // Perform a ThereAndBackAgain action.
  stream_->ProcessThereAndBackAgain(
      MakeThereAndBackAgainData(42ul).SerializeAsString());
  WaitForIdleTaskQueue();

  // Reach conditions.
  stream_->ReportSliceViewed(
      surface.GetSurfaceId(), surface.GetStreamType(),
      surface.initial_state->updated_slices(0).slice().slice_id());

  // Attach a new surface to update the bit to enable uploads.
  TestForYouSurface surface2(stream_.get());

  // Trigger an upload through load more to isolate the effect of the on-attach
  // event on enabling uploads.
  response_translator_.InjectResponse(model_generator_.MakeNextPage());
  stream_->LoadMore(surface, base::DoNothing());
  WaitForIdleTaskQueue();

  // Verify that the ThereAndBackAgain action was uploaded.
  ASSERT_EQ(1, network_.GetActionRequestCount());
  EXPECT_EQ(1, network_.GetActionRequestSent()->feed_actions_size());
}

TEST_F(FeedStreamConditionalActionsUploadTest, EnableUploadOnEnterBackground) {
  response_translator_.InjectResponse(model_generator_.MakeFirstPage());
  TestForYouSurface surface(stream_.get());
  WaitForIdleTaskQueue();

  // Perform a ThereAndBackAgain action.
  stream_->ProcessThereAndBackAgain(
      MakeThereAndBackAgainData(42ul).SerializeAsString());
  WaitForIdleTaskQueue();

  // Reach conditions.
  stream_->ReportSliceViewed(
      surface.GetSurfaceId(), surface.GetStreamType(),
      surface.initial_state->updated_slices(0).slice().slice_id());

  surface.Detach();
  stream_->OnEnterBackground();
  WaitForIdleTaskQueue();

  // Verify that the ThereAndBackAgain action was uploaded.
  ASSERT_EQ(1, network_.GetActionRequestCount());
  EXPECT_EQ(1, network_.GetActionRequestSent()->feed_actions_size());
}

TEST_F(FeedStreamConditionalActionsUploadTest,
       AllowActionsUploadWhenNoticeCardNotPresentRegardlessOfConditions) {
  model_generator_.privacy_notice_fulfilled = false;
  response_translator_.InjectResponse(model_generator_.MakeFirstPage());
  TestForYouSurface surface(stream_.get());
  WaitForIdleTaskQueue();

  // Process the view action and the ThereAndBackAgain action while the upload
  // conditions aren't reached.
  stream_->ProcessViewAction(MakeFeedAction(42ul).SerializeAsString());
  WaitForIdleTaskQueue();
  stream_->ProcessThereAndBackAgain(
      MakeThereAndBackAgainData(42ul).SerializeAsString());
  WaitForIdleTaskQueue();

  // Trigger an upload through a query.
  surface.Detach();
  stream_->OnEnterBackground();
  WaitForIdleTaskQueue();

  // Verify the ThereAndBackAgain action and the view action were uploaded.
  ASSERT_EQ(1, network_.GetActionRequestCount());
  EXPECT_EQ(2, network_.GetActionRequestSent()->feed_actions_size());
}

TEST_F(FeedStreamConditionalActionsUploadTest,
       ResetTheUploadEnableBitsOnClearAll) {
  response_translator_.InjectResponse(model_generator_.MakeFirstPage());
  TestForYouSurface surface(stream_.get());
  WaitForIdleTaskQueue();

  // Reach conditions.
  stream_->ReportSliceViewed(
      surface.GetSurfaceId(), surface.GetStreamType(),
      surface.initial_state->updated_slices(0).slice().slice_id());
  surface.Detach();
  stream_->OnEnterBackground();
  ASSERT_TRUE(stream_->CanUploadActions());

  // Trigger a ClearAll, and ensure actions cannot be uploaded until conditions
  // are reached again.
  stream_->OnSignedOut();
  WaitForIdleTaskQueue();
  ASSERT_FALSE(stream_->CanUploadActions());
}

TEST_F(FeedApiNoticeCardTest, LoadStreamUpdateNoticeCardFulfillmentHistogram) {
  base::HistogramTester histograms;

  // Trigger a stream refresh that updates the histogram.
  {
    auto model_state = model_generator_.MakeFirstPage();
    model_state->stream_data.set_privacy_notice_fulfilled(false);
    response_translator_.InjectResponse(std::move(model_state));

    refresh_scheduler_.Clear();
    stream_->ExecuteRefreshTask(RefreshTaskId::kRefreshForYouFeed);
    WaitForIdleTaskQueue();
  }

  UnloadModel(kForYouStream);

  // Trigger another stream refresh that updates the histogram.
  {
    auto model_state = model_generator_.MakeFirstPage();
    model_state->stream_data.set_privacy_notice_fulfilled(true);
    response_translator_.InjectResponse(std::move(model_state));

    refresh_scheduler_.Clear();
    stream_->ExecuteRefreshTask(RefreshTaskId::kRefreshForYouFeed);
    WaitForIdleTaskQueue();
  }

  // Verify that the notice card fulfillment histogram was properly recorded.
  histograms.ExpectBucketCount("ContentSuggestions.Feed.NoticeCardFulfilled2",
                               0, 1);
  histograms.ExpectBucketCount("ContentSuggestions.Feed.NoticeCardFulfilled2",
                               1, 1);
}

TEST_F(FeedStreamConditionalActionsUploadTest,
       DontTriggerActionsUploadWhenWasNotSignedIn) {
  auto update_request = model_generator_.MakeFirstPage();
  update_request->stream_data.set_signed_in(false);
  response_translator_.InjectResponse(std::move(update_request));
  TestForYouSurface surface(stream_.get());
  WaitForIdleTaskQueue();

  // Try to reach conditions.
  stream_->ReportSliceViewed(
      surface.GetSurfaceId(), surface.GetStreamType(),
      surface.initial_state->updated_slices(1).slice().slice_id());

  // Try to trigger an upload through a query.
  stream_->OnEnterBackground();
  WaitForIdleTaskQueue();

  // Verify that even if the conditions were reached, the pref that enables the
  // upload wasn't set to true because the latest refresh request wasn't signed
  // in.
  ASSERT_FALSE(stream_->CanUploadActions());
}

TEST_F(FeedStreamConditionalActionsUploadTest,
       LoadMoreDoesntUpdateNoticeCardPrefAndHistogram) {
  // The initial stream load has the notice card.
  response_translator_.InjectResponse(model_generator_.MakeFirstPage());
  TestForYouSurface surface(stream_.get());
  WaitForIdleTaskQueue();

  // Inject a response for the LoadMore fetch that doesn't have the notice card.
  // It shouldn't overwrite the notice card pref.
  model_generator_.privacy_notice_fulfilled = false;
  response_translator_.InjectResponse(model_generator_.MakeNextPage());

  // Start tracking histograms after the initial stream load to isolate the
  // effect of load more.
  base::HistogramTester histograms;

  stream_->LoadMore(surface, base::DoNothing());
  WaitForIdleTaskQueue();

  // Process a view action that should be dropped because the upload of actions
  // is still disabled because there is still a notice card.
  stream_->ProcessViewAction(MakeFeedAction(42ul).SerializeAsString());
  WaitForIdleTaskQueue();

  // Trigger an upload.
  stream_->OnEnterBackground();
  WaitForIdleTaskQueue();

  // Verify that there were no uploads.
  EXPECT_EQ(0, network_.GetActionRequestCount());

  // Verify that the notice card fulfillment histogram isn't recorded for load
  // more.
  histograms.ExpectTotalCount("ContentSuggestions.Feed.NoticeCardFulfilled2",
                              0);
}

}  // namespace
}  // namespace test
}  // namespace feed