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 "extensions/common/value_builder.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace extensions {
17 scoped_refptr<Action> CreateAction(const std::string& api_name,
18 const std::string& element,
19 const std::string& attr) {
20 scoped_refptr<Action> action = new Action("id",
22 Action::ACTION_DOM_ACCESS,
24 action->set_args(ListBuilder()
27 .Append("http://www.google.co.uk")
28 .Append("http://www.google.co.uk")
35 class TestRapporService : public rappor::RapporService {
38 virtual ~TestRapporService();
40 // Returns the active reports. This also clears the internal map of metrics
41 // as a biproduct, so if comparing numbers of reports, the comparison should
42 // be from the last time GetReports() was called (not from the beginning of
44 rappor::RapporReports GetReports();
47 TestRapporService::TestRapporService() {
48 // Initialize the RapporService for testing.
49 SetCohortForTesting(0);
50 SetSecretForTesting(rappor::HmacByteVectorGenerator::GenerateEntropyInput());
53 TestRapporService::~TestRapporService() {}
55 rappor::RapporReports TestRapporService::GetReports() {
56 rappor::RapporReports result;
57 rappor::RapporService::ExportMetrics(&result);
61 // Test that the actions properly upload the URLs to the RAPPOR service if
62 // the action may have injected the ad.
63 TEST(AdInjectionUnittest, CheckActionForAdInjectionTest) {
64 TestRapporService rappor_service;
65 rappor::RapporReports reports = rappor_service.GetReports();
66 EXPECT_EQ(0, reports.report_size());
68 scoped_refptr<Action> modify_iframe_src =
69 CreateAction("blinkSetAttribute", "iframe", "src");
70 modify_iframe_src->DidInjectAd(&rappor_service);
71 reports = rappor_service.GetReports();
72 EXPECT_EQ(1, reports.report_size());
74 scoped_refptr<Action> modify_anchor_href =
75 CreateAction("blinkSetAttribute", "a", "href");
76 modify_anchor_href->DidInjectAd(&rappor_service);
77 reports = rappor_service.GetReports();
78 EXPECT_EQ(1, reports.report_size());
80 scoped_refptr<Action> harmless_action =
81 CreateAction("Location.replace", "", "");
82 harmless_action->DidInjectAd(&rappor_service);
83 reports = rappor_service.GetReports();
84 EXPECT_EQ(0, reports.report_size());
87 } // namespace extensions