This source file includes following definitions.
- GetSnapshotAsValue
- disabled_
- IsDisabled
- SendAddHeapSnapshotChunkEvent
- SendCommand
- TEST
- TEST
- TEST
- TEST
#include <list>
#include <string>
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "chrome/test/chromedriver/chrome/heap_snapshot_taker.h"
#include "chrome/test/chromedriver/chrome/status.h"
#include "chrome/test/chromedriver/chrome/stub_devtools_client.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const char* chunks[] = {"{\"a\": 1,", "\"b\": 2}"};
scoped_ptr<base::Value> GetSnapshotAsValue() {
scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
dict->SetInteger("a", 1);
dict->SetInteger("b", 2);
return dict.PassAs<base::Value>();
}
class DummyDevToolsClient : public StubDevToolsClient {
public:
DummyDevToolsClient(const std::string& method, bool error_after_events)
: method_(method),
error_after_events_(error_after_events),
uid_(1),
disabled_(false) {}
virtual ~DummyDevToolsClient() {}
bool IsDisabled() { return disabled_; }
Status SendAddHeapSnapshotChunkEvent() {
base::DictionaryValue event_params;
event_params.SetInteger("uid", uid_);
for (size_t i = 0; i < arraysize(chunks); ++i) {
event_params.SetString("chunk", chunks[i]);
Status status = listeners_.front()->OnEvent(
this, "HeapProfiler.addHeapSnapshotChunk", event_params);
if (status.IsError())
return status;
}
return Status(kOk);
}
virtual Status SendCommand(const std::string& method,
const base::DictionaryValue& params) OVERRIDE {
if (!disabled_)
disabled_ = method == "Debugger.disable";
if (method == method_ && !error_after_events_)
return Status(kUnknownError);
if (method == "HeapProfiler.takeHeapSnapshot") {
Status status = SendAddHeapSnapshotChunkEvent();
if (status.IsError())
return status;
}
if (method == method_ && error_after_events_)
return Status(kUnknownError);
return StubDevToolsClient::SendCommand(method, params);
}
protected:
std::string method_;
bool error_after_events_;
int uid_;
bool disabled_;
};
}
TEST(HeapSnapshotTaker, SuccessfulCase) {
DummyDevToolsClient client("", false);
HeapSnapshotTaker taker(&client);
scoped_ptr<base::Value> snapshot;
Status status = taker.TakeSnapshot(&snapshot);
ASSERT_EQ(kOk, status.code());
ASSERT_TRUE(GetSnapshotAsValue()->Equals(snapshot.get()));
ASSERT_TRUE(client.IsDisabled());
}
TEST(HeapSnapshotTaker, FailIfErrorOnDebuggerEnable) {
DummyDevToolsClient client("Debugger.enable", false);
HeapSnapshotTaker taker(&client);
scoped_ptr<base::Value> snapshot;
Status status = taker.TakeSnapshot(&snapshot);
ASSERT_TRUE(status.IsError());
ASSERT_FALSE(snapshot.get());
ASSERT_TRUE(client.IsDisabled());
}
TEST(HeapSnapshotTaker, FailIfErrorOnCollectGarbage) {
DummyDevToolsClient client("HeapProfiler.collectGarbage", false);
HeapSnapshotTaker taker(&client);
scoped_ptr<base::Value> snapshot;
Status status = taker.TakeSnapshot(&snapshot);
ASSERT_TRUE(status.IsError());
ASSERT_FALSE(snapshot.get());
ASSERT_TRUE(client.IsDisabled());
}
TEST(HeapSnapshotTaker, ErrorBeforeWhenReceivingSnapshot) {
DummyDevToolsClient client("HeapProfiler.takeHeapSnapshot", false);
HeapSnapshotTaker taker(&client);
scoped_ptr<base::Value> snapshot;
Status status = taker.TakeSnapshot(&snapshot);
ASSERT_TRUE(status.IsError());
ASSERT_FALSE(snapshot.get());
ASSERT_TRUE(client.IsDisabled());
}