1 // Copyright 2014 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.
5 #include "components/dom_distiller/content/web_contents_main_frame_observer.h"
7 #include "content/public/browser/navigation_details.h"
8 #include "content/public/browser/render_frame_host.h"
9 #include "content/public/browser/web_contents.h"
10 #include "content/public/browser/web_contents_observer.h"
11 #include "content/public/browser/web_contents_user_data.h"
12 #include "content/public/test/test_renderer_host.h"
14 namespace dom_distiller
{
16 class WebContentsMainFrameObserverTest
17 : public content::RenderViewHostTestHarness
{
18 void SetUp() override
{
19 content::RenderViewHostTestHarness::SetUp();
20 dom_distiller::WebContentsMainFrameObserver::CreateForWebContents(
22 main_frame_observer_
=
23 WebContentsMainFrameObserver::FromWebContents(web_contents());
24 ASSERT_FALSE(main_frame_observer_
->is_document_loaded_in_main_frame());
28 WebContentsMainFrameObserver
* main_frame_observer_
; // weak
31 TEST_F(WebContentsMainFrameObserverTest
, ListensForMainFrameNavigation
) {
32 content::LoadCommittedDetails details
= content::LoadCommittedDetails();
33 details
.is_main_frame
= true;
34 details
.is_in_page
= false;
35 content::FrameNavigateParams params
= content::FrameNavigateParams();
36 main_frame_observer_
->DidNavigateMainFrame(details
, params
);
37 ASSERT_TRUE(main_frame_observer_
->is_initialized());
38 ASSERT_FALSE(main_frame_observer_
->is_document_loaded_in_main_frame());
40 main_frame_observer_
->DocumentLoadedInFrame(main_rfh());
41 ASSERT_TRUE(main_frame_observer_
->is_document_loaded_in_main_frame());
44 TEST_F(WebContentsMainFrameObserverTest
, IgnoresChildFrameNavigation
) {
45 content::LoadCommittedDetails details
= content::LoadCommittedDetails();
46 details
.is_main_frame
= false;
47 details
.is_in_page
= false;
48 content::FrameNavigateParams params
= content::FrameNavigateParams();
49 main_frame_observer_
->DidNavigateMainFrame(details
, params
);
50 ASSERT_FALSE(main_frame_observer_
->is_initialized());
51 ASSERT_FALSE(main_frame_observer_
->is_document_loaded_in_main_frame());
54 TEST_F(WebContentsMainFrameObserverTest
, IgnoresInPageNavigation
) {
55 content::LoadCommittedDetails details
= content::LoadCommittedDetails();
56 details
.is_main_frame
= true;
57 details
.is_in_page
= true;
58 content::FrameNavigateParams params
= content::FrameNavigateParams();
59 main_frame_observer_
->DidNavigateMainFrame(details
, params
);
60 ASSERT_FALSE(main_frame_observer_
->is_initialized());
61 ASSERT_FALSE(main_frame_observer_
->is_document_loaded_in_main_frame());
64 TEST_F(WebContentsMainFrameObserverTest
,
65 IgnoresInPageNavigationUnlessMainFrameLoads
) {
66 content::LoadCommittedDetails details
= content::LoadCommittedDetails();
67 details
.is_main_frame
= true;
68 details
.is_in_page
= true;
69 content::FrameNavigateParams params
= content::FrameNavigateParams();
70 main_frame_observer_
->DidNavigateMainFrame(details
, params
);
71 ASSERT_FALSE(main_frame_observer_
->is_initialized());
72 ASSERT_FALSE(main_frame_observer_
->is_document_loaded_in_main_frame());
74 // Even if we didn't acknowledge an in_page navigation, if the main frame
75 // loads, consider a load complete.
76 main_frame_observer_
->DocumentLoadedInFrame(main_rfh());
77 ASSERT_TRUE(main_frame_observer_
->is_document_loaded_in_main_frame());
80 TEST_F(WebContentsMainFrameObserverTest
, ResetOnPageNavigation
) {
81 content::LoadCommittedDetails details
= content::LoadCommittedDetails();
82 details
.is_main_frame
= true;
83 details
.is_in_page
= false;
84 content::FrameNavigateParams params
= content::FrameNavigateParams();
85 main_frame_observer_
->DidNavigateMainFrame(details
, params
);
86 main_frame_observer_
->DocumentLoadedInFrame(main_rfh());
87 ASSERT_TRUE(main_frame_observer_
->is_document_loaded_in_main_frame());
89 // Another navigation should result in waiting for a page load.
90 main_frame_observer_
->DidNavigateMainFrame(details
, params
);
91 ASSERT_TRUE(main_frame_observer_
->is_initialized());
92 ASSERT_FALSE(main_frame_observer_
->is_document_loaded_in_main_frame());
95 TEST_F(WebContentsMainFrameObserverTest
, DoesNotResetOnInPageNavigation
) {
96 content::LoadCommittedDetails details
= content::LoadCommittedDetails();
97 details
.is_main_frame
= true;
98 details
.is_in_page
= false;
99 content::FrameNavigateParams params
= content::FrameNavigateParams();
100 main_frame_observer_
->DidNavigateMainFrame(details
, params
);
101 main_frame_observer_
->DocumentLoadedInFrame(main_rfh());
102 ASSERT_TRUE(main_frame_observer_
->is_document_loaded_in_main_frame());
104 // Navigating withing the page should not result in waiting for a page load.
105 details
.is_in_page
= true;
106 main_frame_observer_
->DidNavigateMainFrame(details
, params
);
107 ASSERT_TRUE(main_frame_observer_
->is_initialized());
108 ASSERT_TRUE(main_frame_observer_
->is_document_loaded_in_main_frame());
111 } // namespace dom_distiller