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
|
// Copyright 2015 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 "ios/chrome/browser/web_resource/web_resource_util.h"
#include <string>
#include <utility>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "ios/web/public/web_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace web_resource {
class WebResourceUtilTest : public testing::Test {
protected:
WebResourceUtilTest() : error_called_(false), success_called_(false) {}
~WebResourceUtilTest() override {}
WebResourceService::SuccessCallback GetSuccessCallback() {
return base::Bind(&WebResourceUtilTest::OnParseSuccess,
base::Unretained(this));
}
WebResourceService::ErrorCallback GetErrorCallback() {
return base::Bind(&WebResourceUtilTest::OnParseError,
base::Unretained(this));
}
// Called on success.
void OnParseSuccess(scoped_ptr<base::Value> value) {
success_called_ = true;
value_ = std::move(value);
}
// Called on error.
void OnParseError(const std::string& error) {
error_called_ = true;
error_ = error;
}
void FlushBackgroundTasks() {
// The function is not synchronous, callbacks are not called before flushing
// the tasks.
EXPECT_FALSE(success_called_);
EXPECT_FALSE(error_called_);
web::WebThread::GetBlockingPool()->FlushForTesting();
loop_.RunUntilIdle();
}
base::MessageLoop loop_;
std::string error_;
scoped_ptr<base::Value> value_;
bool error_called_;
bool success_called_;
};
TEST_F(WebResourceUtilTest, Success) {
const std::string kExpectedKey("foo");
const std::string kExpectedValue("bar");
std::string json = base::StringPrintf("{\"%s\":\"%s\"}", kExpectedKey.c_str(),
kExpectedValue.c_str());
GetIOSChromeParseJSONCallback().Run(json, GetSuccessCallback(),
GetErrorCallback());
FlushBackgroundTasks();
// The success callback is called with the reference value.
EXPECT_FALSE(error_called_);
EXPECT_TRUE(success_called_);
base::DictionaryValue* dictionary = nullptr;
ASSERT_TRUE(value_->GetAsDictionary(&dictionary));
EXPECT_EQ(1u, dictionary->size());
base::Value* actual_value = nullptr;
ASSERT_TRUE(dictionary->Get(kExpectedKey, &actual_value));
std::string actual_value_as_string;
EXPECT_TRUE(actual_value->GetAsString(&actual_value_as_string));
EXPECT_EQ(kExpectedValue, actual_value_as_string);
}
// Only DictionartValues are expected.
TEST_F(WebResourceUtilTest, UnexpectedValue) {
GetIOSChromeParseJSONCallback().Run("foo", GetSuccessCallback(),
GetErrorCallback());
FlushBackgroundTasks();
// The error callback is called.
EXPECT_TRUE(error_called_);
EXPECT_FALSE(success_called_);
EXPECT_FALSE(error_.empty());
}
// Empty data is not expected.
TEST_F(WebResourceUtilTest, EmptyValue) {
GetIOSChromeParseJSONCallback().Run(std::string(), GetSuccessCallback(),
GetErrorCallback());
FlushBackgroundTasks();
// The error callback is called.
EXPECT_TRUE(error_called_);
EXPECT_FALSE(success_called_);
EXPECT_FALSE(error_.empty());
}
// Wrong syntax.
TEST_F(WebResourceUtilTest, SyntaxError) {
GetIOSChromeParseJSONCallback().Run("%$[", GetSuccessCallback(),
GetErrorCallback());
FlushBackgroundTasks();
// The error callback is called.
EXPECT_TRUE(error_called_);
EXPECT_FALSE(success_called_);
EXPECT_FALSE(error_.empty());
}
} // namespace web_resource
|