OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "chrome/browser/data_use_measurement/chrome_data_use_ascriber.h" |
| 6 |
| 7 #include <list> |
| 8 #include <memory> |
| 9 |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "components/data_use_measurement/core/data_use_recorder.h" |
| 13 #include "content/public/browser/resource_request_info.h" |
| 14 #include "content/public/common/browser_side_navigation_policy.h" |
| 15 #include "content/public/common/process_type.h" |
| 16 #include "content/public/test/mock_resource_context.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "net/url_request/url_request_test_util.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 namespace { |
| 22 int kRenderProcessId = 1; |
| 23 int kRenderFrameId = 2; |
| 24 int kRequestId = 3; |
| 25 } |
| 26 |
| 27 namespace data_use_measurement { |
| 28 |
| 29 class ChromeDataUseAscriberTest : public testing::Test { |
| 30 protected: |
| 31 ChromeDataUseAscriberTest() |
| 32 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 33 resource_context_(new content::MockResourceContext(&context_)) {} |
| 34 |
| 35 void SetUp() override {} |
| 36 |
| 37 void TearDown() override { recorders().clear(); } |
| 38 |
| 39 std::list<ChromeDataUseRecorder>& recorders() { |
| 40 return ascriber_.data_use_recorders_; |
| 41 } |
| 42 |
| 43 net::TestURLRequestContext* context() { return &context_; } |
| 44 |
| 45 content::MockResourceContext* resource_context() { |
| 46 return resource_context_.get(); |
| 47 } |
| 48 |
| 49 ChromeDataUseAscriber* ascriber() { return &ascriber_; } |
| 50 |
| 51 std::unique_ptr<net::URLRequest> CreateNewRequest(std::string url, |
| 52 bool is_main_frame, |
| 53 int request_id, |
| 54 int render_process_id, |
| 55 int render_frame_id) { |
| 56 std::unique_ptr<net::URLRequest> request = |
| 57 context()->CreateRequest(GURL(url), net::IDLE, nullptr); |
| 58 // TODO(kundaji): Allow request_id to be specified in AllocateForTesting. |
| 59 content::ResourceRequestInfo::AllocateForTesting( |
| 60 request.get(), |
| 61 content::RESOURCE_TYPE_MAIN_FRAME, |
| 62 resource_context(), |
| 63 render_process_id, |
| 64 -1, // render_view_id |
| 65 render_frame_id, |
| 66 is_main_frame, |
| 67 false, // parent_is_main_frame |
| 68 false, // allow_download |
| 69 true, // is_async |
| 70 false); // is_using_lofi |
| 71 return request; |
| 72 } |
| 73 |
| 74 private: |
| 75 content::TestBrowserThreadBundle thread_bundle_; |
| 76 ChromeDataUseAscriber ascriber_; |
| 77 net::TestURLRequestContext context_; |
| 78 std::unique_ptr<content::MockResourceContext> resource_context_; |
| 79 }; |
| 80 |
| 81 TEST_F(ChromeDataUseAscriberTest, NoRecorderWithoutFrame) { |
| 82 if (content::IsBrowserSideNavigationEnabled()) |
| 83 return; |
| 84 |
| 85 std::unique_ptr<net::URLRequest> request = CreateNewRequest( |
| 86 "http://test.com", true, kRequestId, kRenderProcessId, kRenderFrameId); |
| 87 |
| 88 // Main frame request should not cause a recorder to be created, since the |
| 89 // frame does not exist. |
| 90 ascriber()->OnBeforeUrlRequest(request.get()); |
| 91 EXPECT_EQ(0u, recorders().size()); |
| 92 |
| 93 // Frame is created. |
| 94 ascriber()->RenderFrameCreated(kRenderProcessId, kRenderFrameId, -1, -1); |
| 95 EXPECT_EQ(1u, recorders().size()); |
| 96 |
| 97 // Request should cause a recorder to be created. |
| 98 ascriber()->OnBeforeUrlRequest(request.get()); |
| 99 EXPECT_EQ(2u, recorders().size()); |
| 100 } |
| 101 |
| 102 } // namespace data_use_measurement |
OLD | NEW |