Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / activity_log / ad_injection_unittest.cc
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.
4
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"
12
13 namespace extensions {
14
15 namespace {
16
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",
21                                             base::Time::Now(),
22                                             Action::ACTION_DOM_ACCESS,
23                                             api_name);
24   action->set_args(ListBuilder()
25                    .Append(element)
26                    .Append(attr)
27                    .Append("http://www.google.co.uk")
28                    .Append("http://www.google.co.uk")
29                    .Build());
30   return action;
31 }
32
33 }  // namespace
34
35 class TestRapporService : public rappor::RapporService {
36  public:
37   TestRapporService();
38   virtual ~TestRapporService();
39
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
43   // the test).
44   rappor::RapporReports GetReports();
45 };
46
47 TestRapporService::TestRapporService() {
48   // Initialize the RapporService for testing.
49   SetCohortForTesting(0);
50   SetSecretForTesting(rappor::HmacByteVectorGenerator::GenerateEntropyInput());
51 }
52
53 TestRapporService::~TestRapporService() {}
54
55 rappor::RapporReports TestRapporService::GetReports() {
56   rappor::RapporReports result;
57   rappor::RapporService::ExportMetrics(&result);
58   return result;
59 }
60
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());
67
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());
73
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());
79
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());
85 }
86
87 }  // namespace extensions