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.
5 #include "ios/chrome/browser/web_resource/web_resource_util.h"
10 #include "base/bind_helpers.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/values.h"
14 #include "ios/web/public/web_thread.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace web_resource
{
19 class WebResourceUtilTest
: public testing::Test
{
21 WebResourceUtilTest() : error_called_(false), success_called_(false) {}
22 ~WebResourceUtilTest() override
{}
24 WebResourceService::SuccessCallback
GetSuccessCallback() {
25 return base::Bind(&WebResourceUtilTest::OnParseSuccess
,
26 base::Unretained(this));
29 WebResourceService::ErrorCallback
GetErrorCallback() {
30 return base::Bind(&WebResourceUtilTest::OnParseError
,
31 base::Unretained(this));
35 void OnParseSuccess(scoped_ptr
<base::Value
> value
) {
36 success_called_
= true;
37 value_
= value
.Pass();
41 void OnParseError(const std::string
& error
) {
46 void FlushBackgroundTasks() {
47 // The function is not synchronous, callbacks are not called before flushing
49 EXPECT_FALSE(success_called_
);
50 EXPECT_FALSE(error_called_
);
52 web::WebThread::GetBlockingPool()->FlushForTesting();
56 base::MessageLoop loop_
;
58 scoped_ptr
<base::Value
> value_
;
63 TEST_F(WebResourceUtilTest
, Success
) {
64 const std::string
kExpectedKey("foo");
65 const std::string
kExpectedValue("bar");
66 std::string json
= base::StringPrintf("{\"%s\":\"%s\"}", kExpectedKey
.c_str(),
67 kExpectedValue
.c_str());
68 GetIOSChromeParseJSONCallback().Run(json
, GetSuccessCallback(),
71 FlushBackgroundTasks();
73 // The success callback is called with the reference value.
74 EXPECT_FALSE(error_called_
);
75 EXPECT_TRUE(success_called_
);
77 base::DictionaryValue
* dictionary
= nullptr;
78 ASSERT_TRUE(value_
->GetAsDictionary(&dictionary
));
79 EXPECT_EQ(1u, dictionary
->size());
80 base::Value
* actual_value
= nullptr;
81 ASSERT_TRUE(dictionary
->Get(kExpectedKey
, &actual_value
));
82 std::string actual_value_as_string
;
83 EXPECT_TRUE(actual_value
->GetAsString(&actual_value_as_string
));
84 EXPECT_EQ(kExpectedValue
, actual_value_as_string
);
87 // Only DictionartValues are expected.
88 TEST_F(WebResourceUtilTest
, UnexpectedValue
) {
89 GetIOSChromeParseJSONCallback().Run("foo", GetSuccessCallback(),
92 FlushBackgroundTasks();
94 // The error callback is called.
95 EXPECT_TRUE(error_called_
);
96 EXPECT_FALSE(success_called_
);
97 EXPECT_FALSE(error_
.empty());
100 // Empty data is not expected.
101 TEST_F(WebResourceUtilTest
, EmptyValue
) {
102 GetIOSChromeParseJSONCallback().Run(std::string(), GetSuccessCallback(),
105 FlushBackgroundTasks();
107 // The error callback is called.
108 EXPECT_TRUE(error_called_
);
109 EXPECT_FALSE(success_called_
);
110 EXPECT_FALSE(error_
.empty());
114 TEST_F(WebResourceUtilTest
, SyntaxError
) {
115 GetIOSChromeParseJSONCallback().Run("%$[", GetSuccessCallback(),
118 FlushBackgroundTasks();
120 // The error callback is called.
121 EXPECT_TRUE(error_called_
);
122 EXPECT_FALSE(success_called_
);
123 EXPECT_FALSE(error_
.empty());
126 } // namespace web_resource