Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
|
bengr
2015/11/05 00:00:38
Remove "(c)"
sclittle
2015/11/05 01:22:48
Done.
| |
| 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_usage/tab_id_annotator.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/callback.h" | |
| 13 #include "base/location.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/run_loop.h" | |
| 17 #include "base/time/time.h" | |
| 18 #include "chrome/browser/sessions/session_tab_helper.h" | |
| 19 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
| 20 #include "components/data_usage/core/data_use.h" | |
| 21 #include "content/public/browser/browser_thread.h" | |
| 22 #include "content/public/browser/render_frame_host.h" | |
| 23 #include "content/public/browser/render_process_host.h" | |
| 24 #include "content/public/browser/resource_request_info.h" | |
| 25 #include "content/public/browser/web_contents.h" | |
| 26 #include "net/base/network_change_notifier.h" | |
| 27 #include "net/base/request_priority.h" | |
| 28 #include "net/url_request/url_request.h" | |
| 29 #include "net/url_request/url_request_test_util.h" | |
| 30 #include "testing/gtest/include/gtest/gtest.h" | |
| 31 #include "url/gurl.h" | |
| 32 | |
| 33 using content::BrowserThread; | |
| 34 using data_usage::DataUse; | |
| 35 | |
| 36 namespace chrome_browser_data_usage { | |
| 37 | |
| 38 namespace { | |
| 39 | |
| 40 class TabIdAnnotatorTest : public ChromeRenderViewHostTestHarness { | |
| 41 public: | |
| 42 TabIdAnnotatorTest() { | |
| 43 // Cannot use IO_MAIN_LOOP with RenderViewHostTestHarness. | |
| 44 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); | |
| 45 } | |
| 46 | |
| 47 ~TabIdAnnotatorTest() override {} | |
| 48 | |
| 49 private: | |
| 50 DISALLOW_COPY_AND_ASSIGN(TabIdAnnotatorTest); | |
| 51 }; | |
| 52 | |
| 53 // Creates a fake DataUse object with the given |tab_id|. | |
|
bengr
2015/11/05 00:00:38
Creates a fake -> Synthesizes
sclittle
2015/11/05 01:22:48
Done.
| |
| 54 scoped_ptr<DataUse> CreateDataUse(int32_t tab_id) { | |
| 55 return scoped_ptr<DataUse>(new DataUse( | |
| 56 GURL("http://foo.com"), base::TimeTicks(), GURL(), tab_id, | |
| 57 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, std::string(), 100, 100)); | |
| 58 } | |
| 59 | |
| 60 // Expects that |expected| and |actual| are equal. | |
| 61 void ExpectDataUse(scoped_ptr<DataUse> expected, scoped_ptr<DataUse> actual) { | |
| 62 // Single out the |tab_id| for better debug output in failure cases. | |
| 63 EXPECT_EQ(expected->tab_id, actual->tab_id); | |
| 64 EXPECT_EQ(*expected, *actual); | |
| 65 } | |
| 66 | |
| 67 // Expects that |expected| and |actual| are equal, then quits |ui_run_loop| on | |
| 68 // the UI thread. | |
| 69 void ExpectDataUseAndQuit(base::RunLoop* ui_run_loop, | |
| 70 scoped_ptr<DataUse> expected, | |
| 71 scoped_ptr<DataUse> actual) { | |
| 72 DCHECK(ui_run_loop); | |
| 73 ExpectDataUse(expected.Pass(), actual.Pass()); | |
| 74 | |
| 75 // This can't use run_loop->QuitClosure here because that uses WeakPtrs, which | |
| 76 // aren't thread safe. | |
| 77 BrowserThread::PostTask( | |
| 78 BrowserThread::UI, FROM_HERE, | |
| 79 base::Bind(&base::RunLoop::Quit, base::Unretained(ui_run_loop))); | |
| 80 } | |
| 81 | |
| 82 // Test that for a sample URLRequest, associated with the given | |
|
bengr
2015/11/05 00:00:38
Test -> Tests
sclittle
2015/11/05 01:22:48
Done.
| |
| 83 // |render_process_id| and |render_frame_id|, repeatedly annotating DataUse for | |
| 84 // that URLRequest yields the |expected_tab_id|. |ui_run_loop| is the RunLoop on | |
| 85 // the UI thread that should be quit after all the annotations are done. | |
| 86 // Passing in -1 for either or both of |render_process_id| or |render_frame_id| | |
| 87 // indicates that the URLRequest should have no associated ResourceRequestInfo. | |
| 88 void TestAnnotateOnIOThread(base::RunLoop* ui_run_loop, | |
| 89 int render_process_id, | |
| 90 int render_frame_id, | |
| 91 int32_t expected_tab_id) { | |
| 92 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 93 DCHECK(ui_run_loop); | |
| 94 | |
| 95 TabIdAnnotator annotator; | |
| 96 net::TestURLRequestContext context; | |
| 97 net::TestDelegate test_delegate; | |
| 98 scoped_ptr<net::URLRequest> request = | |
| 99 context.CreateRequest(GURL("http://foo.com"), net::IDLE, &test_delegate); | |
| 100 | |
| 101 if (render_process_id != -1 && render_frame_id != -1) { | |
| 102 content::ResourceRequestInfo::AllocateForTesting( | |
| 103 request.get(), content::RESOURCE_TYPE_MAIN_FRAME, nullptr, | |
| 104 render_process_id, -1, render_frame_id, true, false, true, true, false); | |
|
bengr
2015/11/05 00:00:38
Mind adding some comments about what all those boo
sclittle
2015/11/05 01:22:48
Done.
| |
| 105 } | |
| 106 | |
| 107 // Annotate two separate DataUse objects to ensure that repeated annotations | |
| 108 // for the same URLRequest work properly. | |
| 109 scoped_ptr<DataUse> first_expected_data_use = CreateDataUse(expected_tab_id); | |
| 110 annotator.Annotate( | |
| 111 request.get(), CreateDataUse(-2), | |
|
bengr
2015/11/05 00:00:38
What's the meaning of -2?
sclittle
2015/11/05 01:22:48
It's just an invalid tab ID that won't be confused
| |
| 112 base::Bind(&ExpectDataUse, base::Passed(&first_expected_data_use))); | |
| 113 | |
| 114 // Quit the |ui_run_loop| after the second annotation. | |
| 115 scoped_ptr<DataUse> second_expected_data_use = CreateDataUse(expected_tab_id); | |
| 116 annotator.Annotate(request.get(), CreateDataUse(-2), | |
| 117 base::Bind(&ExpectDataUseAndQuit, ui_run_loop, | |
| 118 base::Passed(&second_expected_data_use))); | |
| 119 } | |
| 120 | |
| 121 TEST_F(TabIdAnnotatorTest, AnnotateWithNoRenderFrame) { | |
|
bengr
2015/11/05 00:00:38
Does this also test for a render_process_id/render
sclittle
2015/11/05 01:22:48
Done.
| |
| 122 base::RunLoop ui_run_loop; | |
| 123 BrowserThread::PostTask( | |
| 124 BrowserThread::IO, FROM_HERE, | |
| 125 base::Bind(&TestAnnotateOnIOThread, &ui_run_loop, | |
| 126 -1 /* render_process_id */, -1 /* render_frame_id */, | |
| 127 -1 /* expected_tab_id */)); | |
| 128 ui_run_loop.Run(); | |
| 129 } | |
| 130 | |
| 131 TEST_F(TabIdAnnotatorTest, AnnotateWithRenderFrameAndTab) { | |
| 132 base::RunLoop ui_run_loop; | |
| 133 SessionTabHelper::CreateForWebContents(web_contents()); | |
| 134 int32_t expected_tab_id = SessionTabHelper::IdForTab(web_contents()); | |
| 135 EXPECT_NE(-1, expected_tab_id); | |
| 136 | |
| 137 BrowserThread::PostTask( | |
| 138 BrowserThread::IO, FROM_HERE, | |
| 139 base::Bind(&TestAnnotateOnIOThread, &ui_run_loop, | |
| 140 web_contents()->GetMainFrame()->GetProcess()->GetID(), | |
| 141 web_contents()->GetMainFrame()->GetRoutingID(), | |
| 142 expected_tab_id)); | |
| 143 ui_run_loop.Run(); | |
| 144 } | |
| 145 | |
| 146 } // namespace | |
| 147 | |
| 148 } // namespace chrome_browser_data_usage | |
| OLD | NEW |