File: promo_resource_service_mobile_ntp_unittest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (181 lines) | stat: -rw-r--r-- 6,512 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <vector>

#include "base/json/json_reader.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/web_resource/notification_promo.h"
#include "chrome/browser/web_resource/promo_resource_service.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_service.h"
#include "testing/gtest/include/gtest/gtest.h"

#include "chrome/browser/web_resource/notification_promo_mobile_ntp.h"

class PromoResourceServiceMobileNtpTest : public testing::Test {
 public:
  // |promo_resource_service_| must be created after |local_state_|.
  PromoResourceServiceMobileNtpTest()
      : local_state_(TestingBrowserProcess::GetGlobal()),
        promo_resource_service_(new PromoResourceService) {}

 protected:
  ScopedTestingLocalState local_state_;
  scoped_refptr<PromoResourceService> promo_resource_service_;
  base::MessageLoop loop_;
};

class NotificationPromoMobileNtpTest {
 public:
  NotificationPromoMobileNtpTest() : received_notification_(false) {}

  void Init(const std::string& json,
            const std::string& promo_text,
            const std::string& promo_text_long,
            const std::string& promo_action_type,
            const std::string& promo_action_arg0,
            const std::string& promo_action_arg1) {
    base::Value* value(base::JSONReader::Read(json));
    ASSERT_TRUE(value);
    base::DictionaryValue* dict = NULL;
    value->GetAsDictionary(&dict);
    ASSERT_TRUE(dict);
    test_json_.reset(dict);

    promo_text_ = promo_text;
    promo_text_long_ = promo_text_long;
    promo_action_type_ = promo_action_type;
    promo_action_args_.push_back(promo_action_arg0);
    promo_action_args_.push_back(promo_action_arg1);

    received_notification_ = false;
  }

  void InitPromoFromJson(bool should_receive_notification) {
    EXPECT_TRUE(mobile_promo_.InitFromJson(*test_json_));
    EXPECT_TRUE(mobile_promo_.valid());
    EXPECT_EQ(should_receive_notification,
              mobile_promo_.notification_promo().new_notification());

    // Test the fields.
    TestNotification();
  }

  void TestNotification() {
    // Check values.
    EXPECT_TRUE(mobile_promo_.valid());
    EXPECT_EQ(mobile_promo_.text(), promo_text_);
    EXPECT_EQ(mobile_promo_.text_long(), promo_text_long_);
    EXPECT_EQ(mobile_promo_.action_type(), promo_action_type_);
    EXPECT_TRUE(mobile_promo_.action_args() != NULL);
    EXPECT_EQ(2u, promo_action_args_.size());
    EXPECT_EQ(mobile_promo_.action_args()->GetSize(),
              promo_action_args_.size());
    for (std::size_t i = 0; i < promo_action_args_.size(); ++i) {
      std::string value;
      EXPECT_TRUE(mobile_promo_.action_args()->GetString(i, &value));
      EXPECT_EQ(value, promo_action_args_[i]);
    }
  }

  // Create a new NotificationPromo from prefs and compare to current
  // notification.
  void TestInitFromPrefs() {
    NotificationPromoMobileNtp prefs_mobile_promo;
    EXPECT_TRUE(prefs_mobile_promo.InitFromPrefs());
    EXPECT_TRUE(prefs_mobile_promo.valid());
    EXPECT_TRUE(mobile_promo_.valid());

    EXPECT_EQ(prefs_mobile_promo.text(),
              mobile_promo_.text());
    EXPECT_EQ(prefs_mobile_promo.text_long(),
              mobile_promo_.text_long());
    EXPECT_EQ(prefs_mobile_promo.action_type(),
              mobile_promo_.action_type());
    EXPECT_TRUE(mobile_promo_.action_args() != NULL);
    EXPECT_EQ(prefs_mobile_promo.action_args()->GetSize(),
              mobile_promo_.action_args()->GetSize());
    for (std::size_t i = 0;
         i < prefs_mobile_promo.action_args()->GetSize();
         ++i) {
      std::string promo_value;
      std::string prefs_value;
      EXPECT_TRUE(
          prefs_mobile_promo.action_args()->GetString(i, &prefs_value));
      EXPECT_TRUE(
          mobile_promo_.action_args()->GetString(i, &promo_value));
      EXPECT_EQ(promo_value, prefs_value);
    }
  }

 private:
  NotificationPromoMobileNtp mobile_promo_;
  bool received_notification_;
  scoped_ptr<base::DictionaryValue> test_json_;

  std::string promo_text_;
  std::string promo_text_long_;
  std::string promo_action_type_;
  std::vector<std::string> promo_action_args_;
};

TEST_F(PromoResourceServiceMobileNtpTest, NotificationPromoMobileNtpTest) {
  NotificationPromoMobileNtpTest promo_test;

  // Set up start and end dates and promo line in a Dictionary as if parsed
  // from the service.
  promo_test.Init(
      "{"
      "  \"mobile_ntp_sync_promo\": ["
      "    {"
      "      \"date\":"
      "        ["
      "          {"
      "            \"start\":\"3 Aug 1999 9:26:06 GMT\","
      "            \"end\":\"7 Jan 2013 5:40:75 PST\""
      "          }"
      "        ],"
      "      \"strings\":"
      "        {"
      "          \"MOBILE_PROMO_CHROME_SHORT_TEXT\":"
      "              \"Like Chrome? Go http://www.google.com/chrome/\","
      "          \"MOBILE_PROMO_CHROME_LONG_TEXT\":"
      "              \"It's simple. Go http://www.google.com/chrome/\","
      "          \"MOBILE_PROMO_EMAIL_BODY\":\"This is the body.\","
      "          \"XXX\":\"XXX value\""
      "        },"
      "      \"payload\":"
      "        {"
      "          \"payload_format_version\":3,"
      "          \"promo_message_long\":"
      "              \"MOBILE_PROMO_CHROME_LONG_TEXT\","
      "          \"promo_message_short\":"
      "              \"MOBILE_PROMO_CHROME_SHORT_TEXT\","
      "          \"promo_action_type\":\"ACTION_EMAIL\","
      "          \"promo_action_args\":[\"MOBILE_PROMO_EMAIL_BODY\",\"XXX\"]"
      "        },"
      "      \"max_views\":30"
      "    }"
      "  ]"
      "}",
      "Like Chrome? Go http://www.google.com/chrome/",
      "It\'s simple. Go http://www.google.com/chrome/",
      "ACTION_EMAIL", "This is the body.", "XXX value");

  promo_test.InitPromoFromJson(true);

  // Second time should not trigger a notification.
  promo_test.InitPromoFromJson(false);

  promo_test.TestInitFromPrefs();
}