blob: 697f82853b881de8a1b0ff16265c261df4007754 [file] [log] [blame]
// Copyright 2014 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 "base/files/scoped_temp_dir.h"
#include "shell/context.h"
#include "shell/dynamic_service_runner.h"
#include "shell/filename_util.h"
#include "shell/native_application_loader.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
namespace shell {
namespace {
struct TestState {
TestState()
: runner_was_created(false),
runner_was_started(false),
runner_was_destroyed(false) {}
bool runner_was_created;
bool runner_was_started;
bool runner_was_destroyed;
};
class TestDynamicServiceRunner : public DynamicServiceRunner {
public:
explicit TestDynamicServiceRunner(TestState* state) : state_(state) {
state_->runner_was_created = true;
}
~TestDynamicServiceRunner() override {
state_->runner_was_destroyed = true;
base::MessageLoop::current()->Quit();
}
void Start(const base::FilePath& app_path,
DynamicServiceRunner::CleanupBehavior cleanup_behavior,
InterfaceRequest<Application> application_request,
const base::Closure& app_completed_callback) override {
state_->runner_was_started = true;
}
private:
TestState* state_;
};
class TestDynamicServiceRunnerFactory : public DynamicServiceRunnerFactory {
public:
explicit TestDynamicServiceRunnerFactory(TestState* state) : state_(state) {}
~TestDynamicServiceRunnerFactory() override {}
scoped_ptr<DynamicServiceRunner> Create(Context* context) override {
return scoped_ptr<DynamicServiceRunner>(
new TestDynamicServiceRunner(state_));
}
private:
TestState* state_;
};
} // namespace
class NativeApplicationLoaderTest : public testing::Test {
public:
NativeApplicationLoaderTest() {}
~NativeApplicationLoaderTest() override {}
void SetUp() override {
context_.Init();
scoped_ptr<DynamicServiceRunnerFactory> factory(
new TestDynamicServiceRunnerFactory(&state_));
loader_.reset(new NativeApplicationLoader(&context_, factory.Pass()));
}
void TearDown() override { context_.Shutdown(); }
protected:
Context context_;
base::MessageLoop loop_;
scoped_ptr<NativeApplicationLoader> loader_;
TestState state_;
};
TEST_F(NativeApplicationLoaderTest, DoesNotExist) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath nonexistent_file(FILE_PATH_LITERAL("nonexistent.txt"));
GURL url(FilePathToFileURL(temp_dir.path().Append(nonexistent_file)));
ApplicationPtr application;
loader_->Load(url, GetProxy(&application),
NativeApplicationLoader::SimpleLoadCallback());
EXPECT_FALSE(state_.runner_was_created);
EXPECT_FALSE(state_.runner_was_started);
EXPECT_FALSE(state_.runner_was_destroyed);
}
} // namespace shell
} // namespace mojo