Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: chrome/browser/data_usage/tab_id_annotator_unittest.cc

Issue 1421983002: Include tab IDs when reporting data use accounting. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@data_use_scoped_vector
Patch Set: Fixed external_data_use_observer test Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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_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 // Synthesizes a DataUse object with the given |tab_id|.
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 // Tests that for a sample URLRequest, associated with the given
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 // The only args that matter here for the ResourceRequestInfo are the
103 // |request|, the |render_process_id|, and the |render_frame_id|; arbitrary
104 // values are used for all the other args.
105 content::ResourceRequestInfo::AllocateForTesting(
106 request.get(), content::RESOURCE_TYPE_MAIN_FRAME, nullptr,
107 render_process_id, -1, render_frame_id, true, false, true, true, false);
108 }
109
110 // An invalid tab ID to check that the annotator always sets the tab ID. -2 is
111 // used because a tab ID of -1 is a valid value that means "no tab was found".
112 const int32_t kInvalidTabId = -2;
113
114 // Annotate two separate DataUse objects to ensure that repeated annotations
115 // for the same URLRequest work properly.
116 scoped_ptr<DataUse> first_expected_data_use = CreateDataUse(expected_tab_id);
117 annotator.Annotate(
118 request.get(), CreateDataUse(kInvalidTabId),
119 base::Bind(&ExpectDataUse, base::Passed(&first_expected_data_use)));
120
121 // Quit the |ui_run_loop| after the second annotation.
122 scoped_ptr<DataUse> second_expected_data_use = CreateDataUse(expected_tab_id);
123 annotator.Annotate(request.get(), CreateDataUse(kInvalidTabId),
124 base::Bind(&ExpectDataUseAndQuit, ui_run_loop,
125 base::Passed(&second_expected_data_use)));
126 }
127
128 TEST_F(TabIdAnnotatorTest, AnnotateWithNoRenderFrame) {
129 base::RunLoop ui_run_loop;
130 BrowserThread::PostTask(
131 BrowserThread::IO, FROM_HERE,
132 base::Bind(&TestAnnotateOnIOThread, &ui_run_loop,
133 -1 /* render_process_id */, -1 /* render_frame_id */,
134 -1 /* expected_tab_id */));
135 ui_run_loop.Run();
136 }
137
138 TEST_F(TabIdAnnotatorTest, AnnotateWithRenderFrameAndNoTab) {
139 base::RunLoop ui_run_loop;
140 // |web_contents()| isn't a tab, so it shouldn't have a tab ID.
141 EXPECT_EQ(-1, SessionTabHelper::IdForTab(web_contents()));
142
143 BrowserThread::PostTask(
144 BrowserThread::IO, FROM_HERE,
145 base::Bind(&TestAnnotateOnIOThread, &ui_run_loop,
146 web_contents()->GetMainFrame()->GetProcess()->GetID(),
147 web_contents()->GetMainFrame()->GetRoutingID(),
148 -1 /* expected_tab_id */));
149 ui_run_loop.Run();
150 }
151
152 TEST_F(TabIdAnnotatorTest, AnnotateWithRenderFrameAndTab) {
153 base::RunLoop ui_run_loop;
154 // Make |web_contents()| into a tab.
155 SessionTabHelper::CreateForWebContents(web_contents());
156 int32_t expected_tab_id = SessionTabHelper::IdForTab(web_contents());
157 // |web_contents()| is a tab, so it should have a tab ID.
158 EXPECT_NE(-1, expected_tab_id);
159
160 BrowserThread::PostTask(
161 BrowserThread::IO, FROM_HERE,
162 base::Bind(&TestAnnotateOnIOThread, &ui_run_loop,
163 web_contents()->GetMainFrame()->GetProcess()->GetID(),
164 web_contents()->GetMainFrame()->GetRoutingID(),
165 expected_tab_id));
166 ui_run_loop.Run();
167 }
168
169 } // namespace
170
171 } // namespace chrome_browser_data_usage
OLDNEW
« no previous file with comments | « chrome/browser/data_usage/tab_id_annotator.cc ('k') | chrome/browser/data_usage/tab_id_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698