Chromium Code Reviews| Index: chrome/browser/nacl_host/nacl_file_host_unittest.cc |
| diff --git a/chrome/browser/nacl_host/nacl_file_host_unittest.cc b/chrome/browser/nacl_host/nacl_file_host_unittest.cc |
| index 7b27bed16638f2341662e9334c955b6156b5f26b..f8bc004ce0085303c4b4005f97d64eb4a53f988f 100644 |
| --- a/chrome/browser/nacl_host/nacl_file_host_unittest.cc |
| +++ b/chrome/browser/nacl_host/nacl_file_host_unittest.cc |
| @@ -7,16 +7,23 @@ |
| #include "base/basictypes.h" |
| #include "base/files/file_path.h" |
| #include "base/files/scoped_temp_dir.h" |
| -#include "base/path_service.h" |
| +#include "base/run_loop.h" |
| #include "base/test/scoped_path_override.h" |
| +#include "base/threading/sequenced_worker_pool.h" |
| #include "chrome/browser/nacl_host/nacl_browser.h" |
| #include "components/nacl/common/nacl_browser_delegate.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| using nacl_file_host::PnaclCanOpenFile; |
| +using nacl_file_host::EnsurePnaclInstalled; |
| -class DummyNaClBrowserDelegate : public NaClBrowserDelegate { |
| +class TestNaClBrowserDelegate : public NaClBrowserDelegate { |
| public: |
| + |
| + TestNaClBrowserDelegate() : should_pnacl_install_succeed_(false) { } |
| + |
| virtual void ShowNaClInfobar(int render_process_id, |
| int render_view_id, |
| int error_id) OVERRIDE { |
| @@ -52,36 +59,114 @@ class DummyNaClBrowserDelegate : public NaClBrowserDelegate { |
| return NULL; |
| } |
| + // Would have been added by: |
| + // https://codereview.chromium.org/18547006/ |
| + virtual void TryInstallPnacl( |
| + const base::Callback<void(bool)>& installed) OVERRIDE { |
| + installed.Run(should_pnacl_install_succeed_); |
| + } |
| + |
| void SetPnaclDirectory(const base::FilePath& pnacl_dir) { |
| pnacl_path_ = pnacl_dir; |
| } |
| + // Indicate if we should mock the PNaCl install as succeeding |
| + // or failing for the next test. |
| + void SetShouldPnaclInstallSucceed(bool succeed) { |
| + should_pnacl_install_succeed_ = succeed; |
| + } |
| + |
| private: |
| base::FilePath pnacl_path_; |
| + bool should_pnacl_install_succeed_; |
| +}; |
| + |
| +class NaClFileHostTest : public testing::Test { |
| + public: |
|
Derek Schuff
2013/07/23 21:39:29
these can all be protected rather than public
jvoung (off chromium)
2013/07/31 21:41:07
Done.
|
| + NaClFileHostTest(); |
| + virtual ~NaClFileHostTest(); |
| + |
| + virtual void SetUp() OVERRIDE { |
| + nacl_browser_delegate_ = new TestNaClBrowserDelegate; |
| + NaClBrowser::SetDelegate(nacl_browser_delegate_); |
| + } |
| + |
| + virtual void TearDown() OVERRIDE { |
| + NaClBrowser::SetDelegate(NULL); // This deletes nacl_browser_delegate_ |
| + } |
| + |
| + TestNaClBrowserDelegate* nacl_browser_delegate() { |
| + return nacl_browser_delegate_; |
| + } |
| + |
| + void CallbackInstall(bool success) { |
| + install_success_ = success; |
| + events_.push_back(INSTALL_DONE); |
| + } |
| + |
| + void CallbackProgress(int64_t current_progress, |
| + int64_t total_progress) { |
| + events_.push_back(INSTALL_PROGRESS); |
| + // TODO(jvoung): be able to check that current_progress |
| + // goes up monotonically and hits total_progress at the end, |
| + // when we actually get real progress events. |
| + } |
| + |
| + bool install_success() { return install_success_; } |
| + int install_call_count() { |
| + return std::count(events_.begin(), events_.end(), INSTALL_DONE); |
| + } |
| + int progress_call_count() { |
| + return std::count(events_.begin(), events_.end(), INSTALL_PROGRESS); |
| + } |
| + bool events_in_correct_order() { |
| + // INSTALL_DONE should be the last thing. |
| + // The rest should be progress events. |
| + size_t size = events_.size(); |
| + return size > 0 && events_[size - 1] == INSTALL_DONE |
| + && progress_call_count() == (size - 1); |
| + } |
| + |
| + private: |
| + enum EventType { |
| + INSTALL_DONE, |
| + INSTALL_PROGRESS |
| + }; |
| + TestNaClBrowserDelegate* nacl_browser_delegate_; |
| + bool install_success_; |
| + std::vector<EventType> events_; |
| + content::TestBrowserThreadBundle thread_bundle_; |
| + DISALLOW_COPY_AND_ASSIGN(NaClFileHostTest); |
| }; |
| -// Try to pass a few funny filenames with a dummy pnacl directory set. |
| -TEST(PnaclFileHostTest, TestFilenamesWithPnaclPath) { |
| +NaClFileHostTest::NaClFileHostTest() |
| + : nacl_browser_delegate_(NULL), |
| + install_success_(false), |
| + thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
| +} |
| + |
| +NaClFileHostTest::~NaClFileHostTest() { |
| +} |
| + |
| +// Try to pass a few funny filenames with a dummy PNaCl directory set. |
| +TEST_F(NaClFileHostTest, TestFilenamesWithPnaclPath) { |
| base::ScopedTempDir scoped_tmp_dir; |
| ASSERT_TRUE(scoped_tmp_dir.CreateUniqueTempDir()); |
| - base::FilePath kDummyPnaclPath = scoped_tmp_dir.path(); |
| + base::FilePath kTestPnaclPath = scoped_tmp_dir.path(); |
| - DummyNaClBrowserDelegate* nacl_browser_delegate = |
| - new DummyNaClBrowserDelegate; |
| - nacl_browser_delegate->SetPnaclDirectory(kDummyPnaclPath); |
| - NaClBrowser::SetDelegate(nacl_browser_delegate); |
| - ASSERT_TRUE(NaClBrowser::GetDelegate()->GetPnaclDirectory(&kDummyPnaclPath)); |
| + nacl_browser_delegate()->SetPnaclDirectory(kTestPnaclPath); |
| + ASSERT_TRUE(NaClBrowser::GetDelegate()->GetPnaclDirectory(&kTestPnaclPath)); |
| // Check allowed strings, and check that the expected prefix is added. |
| base::FilePath out_path; |
| EXPECT_TRUE(PnaclCanOpenFile("pnacl_json", &out_path)); |
| - base::FilePath expected_path = kDummyPnaclPath.Append( |
| + base::FilePath expected_path = kTestPnaclPath.Append( |
| FILE_PATH_LITERAL("pnacl_public_pnacl_json")); |
| EXPECT_EQ(out_path, expected_path); |
| EXPECT_TRUE(PnaclCanOpenFile("x86_32_llc", &out_path)); |
| - expected_path = kDummyPnaclPath.Append( |
| + expected_path = kTestPnaclPath.Append( |
| FILE_PATH_LITERAL("pnacl_public_x86_32_llc")); |
| EXPECT_EQ(out_path, expected_path); |
| @@ -117,5 +202,66 @@ TEST(PnaclFileHostTest, TestFilenamesWithPnaclPath) { |
| EXPECT_FALSE(PnaclCanOpenFile("$HOME", &out_path)); |
| EXPECT_FALSE(PnaclCanOpenFile("$HOME/.bashrc", &out_path)); |
| #endif |
| - NaClBrowser::SetDelegate(NULL); |
| +} |
| + |
| +// Test that callbacks return success when PNaCl looks like it is |
| +// already installed. No intermediate progress events are expected. |
| +TEST_F(NaClFileHostTest, TestEnsureInstalledAlreadyInstalled) { |
| + base::ScopedTempDir scoped_tmp_dir; |
| + ASSERT_TRUE(scoped_tmp_dir.CreateUniqueTempDir()); |
| + |
| + base::FilePath kTestPnaclPath = scoped_tmp_dir.path(); |
| + nacl_browser_delegate()->SetPnaclDirectory(kTestPnaclPath); |
| + ASSERT_TRUE(NaClBrowser::GetDelegate()->GetPnaclDirectory(&kTestPnaclPath)); |
| + |
| + EnsurePnaclInstalled( |
| + base::Bind(&NaClFileHostTest::CallbackInstall, base::Unretained(this)), |
| + base::Bind(&NaClFileHostTest::CallbackProgress, base::Unretained(this))); |
| + content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + EXPECT_TRUE(install_success()); |
| + EXPECT_EQ(install_call_count(), 1); |
|
Derek Schuff
2013/07/23 21:39:29
EXPECT_EQs should have the expected value first, a
jvoung (off chromium)
2013/07/31 21:41:07
Done.
|
| + EXPECT_EQ(progress_call_count(), 0); |
| + EXPECT_TRUE(events_in_correct_order()); |
| +} |
| + |
| +// Test that final callback is called with success, when PNaCl does not |
| +// look like it's already installed, but mock installer says success. |
| +// Also check that intermediate progress events are called. |
| +TEST_F(NaClFileHostTest, TestEnsureInstalledNotInstalledSuccess) { |
| + base::FilePath kTestPnaclPath; |
| + nacl_browser_delegate()->SetPnaclDirectory(kTestPnaclPath); |
| + nacl_browser_delegate()->SetShouldPnaclInstallSucceed(true); |
| + |
| + EnsurePnaclInstalled( |
| + base::Bind(&NaClFileHostTest::CallbackInstall, base::Unretained(this)), |
| + base::Bind(&NaClFileHostTest::CallbackProgress, base::Unretained(this))); |
| + content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + EXPECT_TRUE(install_success()); |
| + EXPECT_EQ(install_call_count(), 1); |
| + EXPECT_GE(progress_call_count(), 1); |
| + EXPECT_TRUE(events_in_correct_order()); |
| +} |
| + |
| +// Test that final callback is called with error, when PNaCl does not look |
| +// like it's already installed, but mock installer says error. |
| +// Also check that intermediate progress events are called. |
| +TEST_F(NaClFileHostTest, TestEnsureInstalledNotInstalledError) { |
| + base::FilePath kTestPnaclPath; |
| + nacl_browser_delegate()->SetPnaclDirectory(kTestPnaclPath); |
| + nacl_browser_delegate()->SetShouldPnaclInstallSucceed(false); |
| + |
| + EnsurePnaclInstalled( |
| + base::Bind(&NaClFileHostTest::CallbackInstall, base::Unretained(this)), |
| + base::Bind(&NaClFileHostTest::CallbackProgress, base::Unretained(this))); |
| + content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + EXPECT_FALSE(install_success()); |
| + EXPECT_EQ(install_call_count(), 1); |
| + EXPECT_GE(progress_call_count(), 1); |
| + EXPECT_TRUE(events_in_correct_order()); |
| } |