content_capture_receiver_test.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. // Copyright 2019 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. #include "components/content_capture/browser/content_capture_receiver.h"
  5. #include "base/json/json_reader.h"
  6. #include "base/test/scoped_feature_list.h"
  7. #include "base/test/test_mock_time_task_runner.h"
  8. #include "build/build_config.h"
  9. #include "components/content_capture/browser/content_capture_test_helper.h"
  10. #include "content/public/common/content_features.h"
  11. #include "content/public/test/test_renderer_host.h"
  12. #include "content/public/test/test_utils.h"
  13. #include "third_party/blink/public/mojom/favicon/favicon_url.mojom.h"
  14. #include "ui/gfx/geometry/size.h"
  15. namespace content_capture {
  16. namespace {
  17. static constexpr char16_t kMainFrameUrl[] = u"http://foo.com/main.html";
  18. static constexpr char16_t kMainFrameUrl2[] = u"http://foo.com/2.html";
  19. static constexpr char16_t kChildFrameUrl[] = u"http://foo.org/child.html";
  20. static constexpr char16_t kMainFrameSameDocument[] =
  21. u"http://foo.com/main.html#1";
  22. } // namespace
  23. class ContentCaptureReceiverTest : public content::RenderViewHostTestHarness,
  24. public ::testing::WithParamInterface<bool> {
  25. public:
  26. void SetUp() override {
  27. // TODO (crbug.com/1115234): Remove the param when BFCache same site feature
  28. // launched.
  29. if (GetParam()) {
  30. scoped_feature_list_.InitWithFeaturesAndParameters(
  31. {{{features::kBackForwardCache}, {{"enable_same_site", "true"}}}},
  32. // Allow BackForwardCache for all devices regardless of their memory.
  33. {features::kBackForwardCacheMemoryControls});
  34. }
  35. content::RenderViewHostTestHarness::SetUp();
  36. helper_.CreateProviderAndConsumer(web_contents(),
  37. &session_removed_test_helper_);
  38. // This needed to keep the WebContentsObserverConsistencyChecker checks
  39. // happy for when AppendChild is called.
  40. NavigateAndCommit(GURL(kMainFrameUrl));
  41. main_frame_ = web_contents()->GetPrimaryMainFrame();
  42. EXPECT_TRUE(main_frame_);
  43. main_frame_sender_ = std::make_unique<FakeContentCaptureSender>();
  44. main_frame_sender_->Bind(main_frame_);
  45. helper_.InitTestData(kMainFrameUrl, kChildFrameUrl);
  46. }
  47. void NavigateMainFrame(const GURL& url) {
  48. consumer()->Reset();
  49. NavigateAndCommit(url);
  50. main_frame_ = web_contents()->GetPrimaryMainFrame();
  51. }
  52. void NavigateMainFrameSameDocument() {
  53. consumer()->Reset();
  54. NavigateAndCommit(GURL(kMainFrameSameDocument));
  55. }
  56. void SetupChildFrame() {
  57. child_frame_ = content::RenderFrameHostTester::For(main_frame_.get())
  58. ->AppendChild("child");
  59. EXPECT_TRUE(child_frame_);
  60. child_frame_sender_ = std::make_unique<FakeContentCaptureSender>();
  61. child_frame_sender_->Bind(child_frame_);
  62. }
  63. void BuildChildSession(const ContentCaptureSession& parent,
  64. const ContentCaptureFrame& data,
  65. ContentCaptureSession* child) {
  66. ContentCaptureFrame child_frame = data;
  67. child_frame.children.clear();
  68. child->clear();
  69. child->push_back(child_frame);
  70. DCHECK(parent.size() == 1);
  71. child->push_back(parent.front());
  72. }
  73. int64_t GetFrameId(bool main_frame) {
  74. return ContentCaptureReceiver::GetIdFrom(main_frame ? main_frame_.get()
  75. : child_frame_.get());
  76. }
  77. const std::vector<int64_t>& expected_removed_ids() const {
  78. return expected_removed_ids_;
  79. }
  80. SessionRemovedTestHelper* session_removed_test_helper() {
  81. return &session_removed_test_helper_;
  82. }
  83. OnscreenContentProvider* provider() const {
  84. return helper_.onscreen_content_provider();
  85. }
  86. ContentCaptureConsumerHelper* consumer() const {
  87. return helper_.content_capture_consumer();
  88. }
  89. FakeContentCaptureSender* main_frame_sender() const {
  90. return main_frame_sender_.get();
  91. }
  92. FakeContentCaptureSender* child_frame_sender() const {
  93. return child_frame_sender_.get();
  94. }
  95. const ContentCaptureTestHelper* helper() const { return &helper_; }
  96. private:
  97. ContentCaptureTestHelper helper_;
  98. // The sender for main frame.
  99. std::unique_ptr<FakeContentCaptureSender> main_frame_sender_;
  100. // The sender for child frame.
  101. std::unique_ptr<FakeContentCaptureSender> child_frame_sender_;
  102. raw_ptr<content::RenderFrameHost> main_frame_ = nullptr;
  103. raw_ptr<content::RenderFrameHost> child_frame_ = nullptr;
  104. // Expected removed Ids.
  105. std::vector<int64_t> expected_removed_ids_{2};
  106. SessionRemovedTestHelper session_removed_test_helper_;
  107. base::test::ScopedFeatureList scoped_feature_list_;
  108. };
  109. INSTANTIATE_TEST_SUITE_P(,
  110. ContentCaptureReceiverTest,
  111. testing::Values(true, false));
  112. TEST_P(ContentCaptureReceiverTest, DidCaptureContent) {
  113. main_frame_sender()->DidCaptureContent(helper()->test_data(),
  114. true /* first_data */);
  115. EXPECT_TRUE(consumer()->parent_session().empty());
  116. EXPECT_TRUE(consumer()->removed_sessions().empty());
  117. EXPECT_EQ(GetExpectedTestData(helper()->test_data(),
  118. GetFrameId(true /* main_frame */)),
  119. consumer()->captured_data());
  120. }
  121. TEST_P(ContentCaptureReceiverTest, MultipleConsumers) {
  122. std::unique_ptr<ContentCaptureConsumerHelper> consumer2 =
  123. std::make_unique<ContentCaptureConsumerHelper>(nullptr);
  124. provider()->AddConsumer(*(consumer2.get()));
  125. main_frame_sender()->DidCaptureContent(helper()->test_data(),
  126. true /* first_data */);
  127. EXPECT_TRUE(consumer()->parent_session().empty());
  128. EXPECT_TRUE(consumer()->removed_sessions().empty());
  129. EXPECT_EQ(GetExpectedTestData(helper()->test_data(),
  130. GetFrameId(true /* main_frame */)),
  131. consumer()->captured_data());
  132. EXPECT_TRUE(consumer2->parent_session().empty());
  133. EXPECT_TRUE(consumer2->removed_sessions().empty());
  134. EXPECT_EQ(GetExpectedTestData(helper()->test_data(),
  135. GetFrameId(true /* main_frame */)),
  136. consumer2->captured_data());
  137. // Verifies to get the remove session callback in RemoveConsumer.
  138. provider()->RemoveConsumer(*(consumer2.get()));
  139. EXPECT_TRUE(consumer()->removed_sessions().empty());
  140. EXPECT_EQ(1u, consumer2->removed_sessions().size());
  141. std::vector<ContentCaptureFrame> expected{GetExpectedTestData(
  142. helper()->test_data(), GetFrameId(true /* main_frame */))};
  143. VerifySession(expected, consumer2->removed_sessions().front());
  144. EXPECT_EQ(1u, provider()->GetConsumersForTesting().size());
  145. EXPECT_EQ(consumer(), provider()->GetConsumersForTesting()[0]);
  146. }
  147. // TODO(https://crbug.com/1010179): Fix flakes on win10_chromium_x64_rel_ng and
  148. // re-enable this test.
  149. #if BUILDFLAG(IS_WIN)
  150. #define MAYBE_DidCaptureContentWithUpdate DISABLED_DidCaptureContentWithUpdate
  151. #else
  152. #define MAYBE_DidCaptureContentWithUpdate DidCaptureContentWithUpdate
  153. #endif
  154. TEST_P(ContentCaptureReceiverTest, MAYBE_DidCaptureContentWithUpdate) {
  155. main_frame_sender()->DidCaptureContent(helper()->test_data(),
  156. true /* first_data */);
  157. // Verifies to get test_data() with correct frame content id.
  158. EXPECT_TRUE(consumer()->parent_session().empty());
  159. EXPECT_TRUE(consumer()->removed_sessions().empty());
  160. EXPECT_EQ(GetExpectedTestData(helper()->test_data(),
  161. GetFrameId(true /* main_frame */)),
  162. consumer()->captured_data());
  163. // Simulates to update the content within the same document.
  164. main_frame_sender()->DidCaptureContent(helper()->test_data_update(),
  165. false /* first_data */);
  166. // Verifies to get test_data2() with correct frame content id.
  167. EXPECT_TRUE(consumer()->parent_session().empty());
  168. // Verifies that the session isn't removed.
  169. EXPECT_TRUE(consumer()->removed_sessions().empty());
  170. EXPECT_EQ(GetExpectedTestData(helper()->test_data_update(),
  171. GetFrameId(true /* main_frame */)),
  172. consumer()->captured_data());
  173. }
  174. // TODO(https://crbug.com/1011204): Fix flakes on win10_chromium_x64_rel_ng and
  175. // re-enable this test.
  176. #if BUILDFLAG(IS_WIN)
  177. #define MAYBE_DidUpdateContent DISABLED_DidUpdateContent
  178. #else
  179. #define MAYBE_DidUpdateContent DidUpdateContent
  180. #endif
  181. TEST_P(ContentCaptureReceiverTest, MAYBE_DidUpdateContent) {
  182. main_frame_sender()->DidCaptureContent(helper()->test_data(),
  183. true /* first_data */);
  184. EXPECT_TRUE(consumer()->parent_session().empty());
  185. EXPECT_TRUE(consumer()->removed_sessions().empty());
  186. ContentCaptureFrame expected_data = GetExpectedTestData(
  187. helper()->test_data(), GetFrameId(true /* main_frame */));
  188. EXPECT_EQ(expected_data, consumer()->captured_data());
  189. // Simulate content change.
  190. main_frame_sender()->DidUpdateContent(helper()->test_data_change());
  191. EXPECT_TRUE(consumer()->updated_parent_session().empty());
  192. EXPECT_TRUE(consumer()->removed_sessions().empty());
  193. EXPECT_EQ(GetExpectedTestData(helper()->test_data_change(), expected_data.id),
  194. consumer()->updated_data());
  195. }
  196. TEST_P(ContentCaptureReceiverTest, DidRemoveSession) {
  197. main_frame_sender()->DidCaptureContent(helper()->test_data(),
  198. true /* first_data */);
  199. // Verifies to get test_data() with correct frame content id.
  200. EXPECT_TRUE(consumer()->parent_session().empty());
  201. EXPECT_TRUE(consumer()->removed_sessions().empty());
  202. EXPECT_EQ(GetExpectedTestData(helper()->test_data(),
  203. GetFrameId(true /* main_frame */)),
  204. consumer()->captured_data());
  205. // Simulates to navigate other document.
  206. main_frame_sender()->DidCaptureContent(helper()->test_data2(),
  207. true /* first_data */);
  208. EXPECT_TRUE(consumer()->parent_session().empty());
  209. // Verifies that the previous session was removed.
  210. EXPECT_EQ(1u, consumer()->removed_sessions().size());
  211. std::vector<ContentCaptureFrame> expected{GetExpectedTestData(
  212. helper()->test_data(), GetFrameId(true /* main_frame */))};
  213. VerifySession(expected, consumer()->removed_sessions().front());
  214. // Verifies that we get the test_data2() from the new document.
  215. EXPECT_EQ(GetExpectedTestData(helper()->test_data2(),
  216. GetFrameId(true /* main_frame */)),
  217. consumer()->captured_data());
  218. }
  219. TEST_P(ContentCaptureReceiverTest, DidRemoveContent) {
  220. main_frame_sender()->DidCaptureContent(helper()->test_data(),
  221. true /* first_data */);
  222. // Verifies to get test_data() with correct frame content id.
  223. EXPECT_TRUE(consumer()->parent_session().empty());
  224. EXPECT_TRUE(consumer()->removed_sessions().empty());
  225. EXPECT_EQ(GetExpectedTestData(helper()->test_data(),
  226. GetFrameId(true /* main_frame */)),
  227. consumer()->captured_data());
  228. // Simulates to remove the content.
  229. main_frame_sender()->DidRemoveContent(expected_removed_ids());
  230. EXPECT_TRUE(consumer()->parent_session().empty());
  231. EXPECT_TRUE(consumer()->removed_sessions().empty());
  232. // Verifies that the removed_ids() was removed from the correct session.
  233. EXPECT_EQ(expected_removed_ids(), consumer()->removed_ids());
  234. std::vector<ContentCaptureFrame> expected{GetExpectedTestData(
  235. helper()->test_data(), GetFrameId(true /* main_frame */))};
  236. VerifySession(expected, consumer()->session());
  237. }
  238. TEST_P(ContentCaptureReceiverTest, ChildFrameDidCaptureContent) {
  239. // Simulate add child frame.
  240. SetupChildFrame();
  241. // Simulate to capture the content from main frame.
  242. main_frame_sender()->DidCaptureContent(helper()->test_data(),
  243. true /* first_data */);
  244. // Verifies to get test_data() with correct frame content id.
  245. EXPECT_TRUE(consumer()->parent_session().empty());
  246. EXPECT_TRUE(consumer()->removed_sessions().empty());
  247. EXPECT_EQ(GetExpectedTestData(helper()->test_data(),
  248. GetFrameId(true /* main_frame */)),
  249. consumer()->captured_data());
  250. // Simulate to capture the content from child frame.
  251. child_frame_sender()->DidCaptureContent(helper()->test_data2(),
  252. true /* first_data */);
  253. // Verifies that the parent_session was set correctly.
  254. EXPECT_FALSE(consumer()->parent_session().empty());
  255. std::vector<ContentCaptureFrame> expected{GetExpectedTestData(
  256. helper()->test_data(), GetFrameId(true /* main_frame */))};
  257. VerifySession(expected, consumer()->parent_session());
  258. EXPECT_TRUE(consumer()->removed_sessions().empty());
  259. // Verifies that we receive the correct content from child frame.
  260. EXPECT_EQ(GetExpectedTestData(helper()->test_data2(),
  261. GetFrameId(false /* main_frame */)),
  262. consumer()->captured_data());
  263. }
  264. // This test is for issue crbug.com/995121 .
  265. TEST_P(ContentCaptureReceiverTest, RenderFrameHostGone) {
  266. auto* receiver = provider()->ContentCaptureReceiverForFrameForTesting(
  267. web_contents()->GetPrimaryMainFrame());
  268. // No good way to simulate crbug.com/995121, just set rfh_ to nullptr in
  269. // ContentCaptureReceiver, so content::WebContents::FromRenderFrameHost()
  270. // won't return WebContents.
  271. receiver->rfh_ = nullptr;
  272. // Ensure no crash.
  273. main_frame_sender()->DidCaptureContent(helper()->test_data(),
  274. true /* first_data */);
  275. main_frame_sender()->DidUpdateContent(helper()->test_data());
  276. main_frame_sender()->DidRemoveContent(expected_removed_ids());
  277. }
  278. TEST_P(ContentCaptureReceiverTest, TitleUpdateTaskDelay) {
  279. auto* receiver = provider()->ContentCaptureReceiverForFrameForTesting(
  280. web_contents()->GetPrimaryMainFrame());
  281. auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>();
  282. // Uses TestMockTimeTaskRunner to check the task state.
  283. receiver->title_update_task_runner_ = task_runner;
  284. receiver->SetTitle(u"title 1");
  285. // No task scheduled because no content has been captured.
  286. EXPECT_FALSE(receiver->notify_title_update_callback_);
  287. EXPECT_FALSE(task_runner->HasPendingTask());
  288. // Capture content, then update the title.
  289. main_frame_sender()->DidCaptureContent(helper()->test_data(),
  290. /*first_data=*/true);
  291. std::u16string title2 = u"title 2";
  292. receiver->SetTitle(title2);
  293. // A task should be scheduled.
  294. EXPECT_TRUE(receiver->notify_title_update_callback_);
  295. EXPECT_TRUE(task_runner->HasPendingTask());
  296. EXPECT_EQ(2u, receiver->exponential_delay_);
  297. // Run the pending task.
  298. task_runner->FastForwardBy(base::Seconds(receiver->exponential_delay_ / 2));
  299. task_runner->RunUntilIdle();
  300. // Verify the title is updated and the task is reset.
  301. EXPECT_EQ(title2, consumer()->updated_title());
  302. EXPECT_FALSE(receiver->notify_title_update_callback_);
  303. EXPECT_FALSE(task_runner->HasPendingTask());
  304. // Set the task_runner again since it is reset after task runs.
  305. receiver->title_update_task_runner_ = task_runner;
  306. // Change title again and verify the result.
  307. receiver->SetTitle(u"title 3");
  308. EXPECT_TRUE(receiver->notify_title_update_callback_);
  309. EXPECT_TRUE(task_runner->HasPendingTask());
  310. EXPECT_EQ(4u, receiver->exponential_delay_);
  311. // Remove the session to verify the pending task cancelled.
  312. receiver->RemoveSession();
  313. EXPECT_FALSE(receiver->notify_title_update_callback_);
  314. // The cancelled task isn't removed from the TaskRunner, prune it.
  315. task_runner->TakePendingTasks();
  316. EXPECT_FALSE(task_runner->HasPendingTask());
  317. // The delay time is reset after session is removed.
  318. EXPECT_EQ(1u, receiver->exponential_delay_);
  319. // Verify the latest task isn't run.
  320. EXPECT_EQ(title2, consumer()->updated_title());
  321. }
  322. // TODO(https://crbug.com/1010416): Fix flakes on win10_chromium_x64_rel_ng and
  323. // re-enable this test.
  324. #if BUILDFLAG(IS_WIN)
  325. #define MAYBE_ChildFrameCaptureContentFirst \
  326. DISABLED_ChildFrameCaptureContentFirst
  327. #else
  328. #define MAYBE_ChildFrameCaptureContentFirst ChildFrameCaptureContentFirst
  329. #endif
  330. TEST_P(ContentCaptureReceiverTest, MAYBE_ChildFrameCaptureContentFirst) {
  331. // Simulate add child frame.
  332. SetupChildFrame();
  333. // Simulate to capture the content from child frame.
  334. child_frame_sender()->DidCaptureContent(helper()->test_data2(),
  335. true /* first_data */);
  336. // Verifies that the parent_session was set correctly.
  337. EXPECT_FALSE(consumer()->parent_session().empty());
  338. ContentCaptureFrame data = GetExpectedTestData(
  339. helper()->test_data(), GetFrameId(true /* main_frame */));
  340. // Currently, there is no way to fake frame size, set it to 0.
  341. data.bounds = gfx::Rect();
  342. ContentCaptureSession expected{data};
  343. VerifySession(expected, consumer()->parent_session());
  344. EXPECT_TRUE(consumer()->removed_sessions().empty());
  345. // Verifies that we receive the correct content from child frame.
  346. EXPECT_EQ(GetExpectedTestData(helper()->test_data2(),
  347. GetFrameId(false /* main_frame */)),
  348. consumer()->captured_data());
  349. // Get the child session, so we can verify that it has been removed in next
  350. // navigation
  351. ContentCaptureFrame child_frame = GetExpectedTestData(
  352. helper()->test_data2(), GetFrameId(false /* main_frame */));
  353. // child_frame.children.clear();
  354. ContentCaptureSession removed_child_session;
  355. BuildChildSession(expected, consumer()->captured_data(),
  356. &removed_child_session);
  357. ContentCaptureSession removed_main_session = expected;
  358. // When main frame navigates to same url, the parent session will not change.
  359. NavigateMainFrame(GURL(kMainFrameUrl));
  360. SetupChildFrame();
  361. child_frame_sender()->DidCaptureContent(helper()->test_data2(),
  362. true /* first_data */);
  363. VerifySession(expected, consumer()->parent_session());
  364. EXPECT_EQ(2u, consumer()->removed_sessions().size());
  365. VerifySession(removed_child_session, consumer()->removed_sessions().back());
  366. VerifySession(removed_main_session, consumer()->removed_sessions().front());
  367. // Get main and child session to verify that they are removed in next
  368. // navigateion.
  369. removed_main_session = expected;
  370. BuildChildSession(expected, consumer()->captured_data(),
  371. &removed_child_session);
  372. // When main frame navigates to same domain, the parent session will change.
  373. NavigateMainFrame(GURL(kMainFrameUrl2));
  374. SetupChildFrame();
  375. child_frame_sender()->DidCaptureContent(helper()->test_data2(),
  376. true /* first_data */);
  377. // Intentionally reuse the data.id from previous result, so we know navigating
  378. // to same domain didn't create new ContentCaptureReceiver when call
  379. // VerifySession(), otherwise, we can't test the code to handle the navigation
  380. // in ContentCaptureReceiver. - except when ProactivelySwapBrowsingInstance
  381. // or RenderDocument is enabled on same-site main frame navigation, where we
  382. // will get new RenderFrameHosts after the navigation to |kMainFrameUrl2|.
  383. if (content::CanSameSiteMainFrameNavigationsChangeRenderFrameHosts())
  384. data = GetExpectedTestData(helper()->test_data(),
  385. GetFrameId(true /* main_frame */));
  386. data.url = kMainFrameUrl2;
  387. // Currently, there is no way to fake frame size, set it to 0.
  388. data.bounds = gfx::Rect();
  389. expected.clear();
  390. expected.push_back(data);
  391. VerifySession(expected, consumer()->parent_session());
  392. // There are two sessions removed, one the main frame because we navigate to
  393. // different URL (though the domain is same), another one is child frame
  394. // because of the main frame change.
  395. EXPECT_EQ(2u, consumer()->removed_sessions().size());
  396. VerifySession(removed_child_session, consumer()->removed_sessions().back());
  397. VerifySession(removed_main_session, consumer()->removed_sessions().front());
  398. // Keep current sessions to verify removed sessions later.
  399. removed_main_session = expected;
  400. BuildChildSession(expected, consumer()->captured_data(),
  401. &removed_child_session);
  402. // When main frame navigates to different domain, the parent session will
  403. // change.
  404. NavigateMainFrame(GURL(kChildFrameUrl));
  405. SetupChildFrame();
  406. child_frame_sender()->DidCaptureContent(helper()->test_data2(),
  407. true /* first_data */);
  408. data = GetExpectedTestData(helper()->test_data2(),
  409. GetFrameId(true /* main_frame */));
  410. // Currently, there is no way to fake frame size, set it to 0.
  411. data.bounds = gfx::Rect();
  412. expected.clear();
  413. expected.push_back(data);
  414. VerifySession(expected, consumer()->parent_session());
  415. EXPECT_EQ(2u, consumer()->removed_sessions().size());
  416. VerifySession(removed_child_session, consumer()->removed_sessions().back());
  417. VerifySession(removed_main_session, consumer()->removed_sessions().front());
  418. // Keep current sessions to verify removed sessions later.
  419. removed_main_session = expected;
  420. BuildChildSession(expected, consumer()->captured_data(),
  421. &removed_child_session);
  422. session_removed_test_helper()->Reset();
  423. DeleteContents();
  424. EXPECT_EQ(2u, session_removed_test_helper()->removed_sessions().size());
  425. VerifySession(removed_child_session,
  426. session_removed_test_helper()->removed_sessions().front());
  427. VerifySession(removed_main_session,
  428. session_removed_test_helper()->removed_sessions().back());
  429. }
  430. TEST_P(ContentCaptureReceiverTest, SameDocumentSameSession) {
  431. main_frame_sender()->DidCaptureContent(helper()->test_data(),
  432. true /* first_data */);
  433. // Verifies to get test_data() with correct frame content id.
  434. EXPECT_TRUE(consumer()->parent_session().empty());
  435. EXPECT_TRUE(consumer()->removed_sessions().empty());
  436. EXPECT_EQ(GetExpectedTestData(helper()->test_data(),
  437. GetFrameId(true /* main_frame */)),
  438. consumer()->captured_data());
  439. NavigateMainFrameSameDocument();
  440. // Verifies the session wasn't removed for the same document navigation.
  441. EXPECT_TRUE(consumer()->removed_sessions().empty());
  442. }
  443. TEST_P(ContentCaptureReceiverTest, ConvertFaviconURLToJSON) {
  444. std::vector<blink::mojom::FaviconURLPtr> favicon_urls;
  445. EXPECT_TRUE(ContentCaptureReceiver::ToJSON(favicon_urls).empty());
  446. favicon_urls.push_back(blink::mojom::FaviconURL::New(
  447. GURL{"https://a.com"}, blink::mojom::FaviconIconType::kFavicon,
  448. std::vector<gfx::Size>{gfx::Size(10, 10)}));
  449. favicon_urls.push_back(blink::mojom::FaviconURL::New(
  450. GURL{"https://b.com"}, blink::mojom::FaviconIconType::kTouchIcon,
  451. std::vector<gfx::Size>{gfx::Size(100, 100), gfx::Size(20, 20)}));
  452. favicon_urls.push_back(blink::mojom::FaviconURL::New(
  453. GURL{"https://c.com"},
  454. blink::mojom::FaviconIconType::kTouchPrecomposedIcon,
  455. std::vector<gfx::Size>{}));
  456. std::string actual_json = ContentCaptureReceiver::ToJSON(favicon_urls);
  457. absl::optional<base::Value> actual = base::JSONReader::Read(actual_json);
  458. std::string expected_json =
  459. R"JSON(
  460. [
  461. {
  462. "sizes":[{"height":10,"width":10}],
  463. "type":"favicon",
  464. "url":"https://a.com/"
  465. },
  466. {
  467. "sizes":[{"height":100,"width":100},
  468. {"height":20,"width":20}],
  469. "type":"touch icon",
  470. "url":"https://b.com/"
  471. },
  472. {
  473. "type":"touch precomposed icon",
  474. "url":"https://c.com/"
  475. }
  476. ]
  477. )JSON";
  478. absl::optional<base::Value> expected = base::JSONReader::Read(expected_json);
  479. EXPECT_TRUE(actual);
  480. EXPECT_EQ(expected, actual);
  481. }
  482. class ContentCaptureReceiverMultipleFrameTest
  483. : public content::RenderViewHostTestHarness {
  484. public:
  485. void SetUp() override {
  486. // Setup multiple frames before creates OnscreenContentProvider.
  487. content::RenderViewHostTestHarness::SetUp();
  488. // This needed to keep the WebContentsObserverConsistencyChecker checks
  489. // happy for when AppendChild is called.
  490. NavigateAndCommit(GURL("about:blank"));
  491. content::RenderFrameHostTester::For(web_contents()->GetPrimaryMainFrame())
  492. ->AppendChild("child");
  493. helper_.CreateProviderAndConsumer(web_contents());
  494. }
  495. OnscreenContentProvider* provider() const {
  496. return helper_.onscreen_content_provider();
  497. }
  498. private:
  499. ContentCaptureTestHelper helper_;
  500. };
  501. // TODO(https://crbug.com/1010417): Fix flakes on win10_chromium_x64_rel_ng and
  502. // re-enable this test.
  503. #if BUILDFLAG(IS_WIN)
  504. #define MAYBE_ReceiverCreatedForExistingFrame \
  505. DISABLED_ReceiverCreatedForExistingFrame
  506. #else
  507. #define MAYBE_ReceiverCreatedForExistingFrame ReceiverCreatedForExistingFrame
  508. #endif
  509. TEST_F(ContentCaptureReceiverMultipleFrameTest,
  510. MAYBE_ReceiverCreatedForExistingFrame) {
  511. EXPECT_EQ(2u, provider()->GetFrameMapSizeForTesting());
  512. }
  513. } // namespace content_capture