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 "base/time/time.h"
6 #include "chrome/browser/extensions/activity_log/activity_actions.h"
7 #include "components/rappor/byte_vector_utils.h"
8 #include "components/rappor/proto/rappor_metric.pb.h"
9 #include "components/rappor/rappor_service.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace extensions
{
16 scoped_refptr
<Action
> CreateAction(const std::string
& api_name
) {
17 scoped_refptr
<Action
> action
= new Action("id",
19 Action::ACTION_DOM_ACCESS
,
21 action
->set_arg_url(GURL("http://www.notarealhost.notarealtld"));
27 class TestRapporService
: public rappor::RapporService
{
30 virtual ~TestRapporService();
32 // Returns the active reports. This also clears the internal map of metrics
33 // as a biproduct, so if comparing numbers of reports, the comparison should
34 // be from the last time GetReports() was called (not from the beginning of
36 rappor::RapporReports
GetReports();
39 TestRapporService::TestRapporService() {
40 // Initialize the RapporService for testing.
41 SetCohortForTesting(0);
42 SetSecretForTesting(rappor::HmacByteVectorGenerator::GenerateEntropyInput());
45 TestRapporService::~TestRapporService() {}
47 rappor::RapporReports
TestRapporService::GetReports() {
48 rappor::RapporReports result
;
49 rappor::RapporService::ExportMetrics(&result
);
53 // Test that the actions properly upload the URLs to the RAPPOR service if
54 // the action may have injected the ad.
55 TEST(AdInjectionUnittest
, CheckActionForAdInjectionTest
) {
56 TestRapporService rappor_service
;
57 rappor::RapporReports reports
= rappor_service
.GetReports();
58 EXPECT_EQ(0, reports
.report_size());
60 scoped_refptr
<Action
> modify_iframe_src
=
61 CreateAction("HTMLIFrameElement.src");
62 modify_iframe_src
->DidInjectAd(&rappor_service
);
63 reports
= rappor_service
.GetReports();
64 EXPECT_EQ(1, reports
.report_size());
66 scoped_refptr
<Action
> modify_embed_src
=
67 CreateAction("HTMLEmbedElement.src");
68 modify_embed_src
->DidInjectAd(&rappor_service
);
69 reports
= rappor_service
.GetReports();
70 EXPECT_EQ(1, reports
.report_size());
72 scoped_refptr
<Action
> modify_anchor_href
=
73 CreateAction("HTMLAnchorElement.href");
74 modify_anchor_href
->DidInjectAd(&rappor_service
);
75 reports
= rappor_service
.GetReports();
76 EXPECT_EQ(1, reports
.report_size());
78 scoped_refptr
<Action
> harmless_action
= CreateAction("Location.replace");
79 harmless_action
->DidInjectAd(&rappor_service
);
80 reports
= rappor_service
.GetReports();
81 EXPECT_EQ(0, reports
.report_size());
84 } // namespace extensions