Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1 | // Copyright 2013 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. |
| 4 | |
| 5 | #include "chrome/browser/nacl_host/pnacl_host.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/files/scoped_temp_dir.h" |
| 9 | #include "base/run_loop.h" |
| 10 | #include "base/threading/sequenced_worker_pool.h" |
| 11 | #include "content/public/browser/browser_thread.h" |
| 12 | #include "content/public/test/test_browser_thread_bundle.h" |
| 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
| 15 | namespace pnacl { |
| 16 | |
| 17 | class PnaclHostTest : public testing::Test { |
| 18 | protected: |
| 19 | PnaclHostTest() |
| 20 | : temp_callback_count_(0), |
| 21 | thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 22 | virtual void SetUp() { |
| 23 | host_ = new PnaclHost(); |
| 24 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 25 | host_->InitForTest(temp_dir_.path()); |
| 26 | EXPECT_EQ(host_->cache_state_, PnaclHost::CacheReady); |
| 27 | } |
| 28 | virtual void TearDown() { |
| 29 | ExpectPendingTranslations(0); |
| 30 | delete host_; |
| 31 | } |
| 32 | // Utilities for inspecting internal state of the pnacl host |
| 33 | void ExpectPendingTranslations(size_t count) { |
| 34 | EXPECT_EQ(count, host_->pending_translations_.size()); |
| 35 | } |
| 36 | void ExpectCallbackCount(int count) { |
| 37 | EXPECT_EQ(count, temp_callback_count_); |
| 38 | } |
| 39 | |
| 40 | public: // Required for derived classes to bind this method |
| 41 | void CallbackExpectMiss(IPC::PlatformFileForTransit fd, bool is_hit) { |
| 42 | temp_callback_count_++; |
| 43 | EXPECT_FALSE(is_hit); |
| 44 | EXPECT_FALSE(fd == IPC::InvalidPlatformFileForTransit()); |
| 45 | } |
| 46 | |
| 47 | protected: |
| 48 | PnaclHost* host_; |
| 49 | int temp_callback_count_; |
| 50 | content::TestBrowserThreadBundle thread_bundle_; |
| 51 | base::ScopedTempDir temp_dir_; |
| 52 | }; |
| 53 | |
| 54 | #define EXPECT_PENDING_TRANSLATIONS(n) \ |
| 55 | do { \ |
| 56 | SCOPED_TRACE(""); \ |
| 57 | ExpectPendingTranslations(n); \ |
| 58 | } while (0) |
| 59 | |
| 60 | // We don't do actual caching yet, but just return a new temp file with a miss |
| 61 | TEST_F(PnaclHostTest, BasicMiss) { |
| 62 | nacl::PnaclCacheInfo info; |
| 63 | host_->GetNexeFd( |
| 64 | 0, |
| 65 | base::ProcessHandle(), |
| 66 | 0, |
| 67 | 0, |
| 68 | info, |
| 69 | base::Bind(&PnaclHostTest::CallbackExpectMiss, base::Unretained(this))); |
| 70 | |
| 71 | EXPECT_PENDING_TRANSLATIONS(1); |
| 72 | content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 73 | base::RunLoop().RunUntilIdle(); |
| 74 | EXPECT_PENDING_TRANSLATIONS(1); |
| 75 | ExpectCallbackCount(1); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 76 | host_->TranslationFinished(0, 0, true); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 77 | EXPECT_PENDING_TRANSLATIONS(0); |
| 78 | host_->RendererClosing(0); |
| 79 | } |
| 80 | |
| 81 | TEST_F(PnaclHostTest, BadArguments) { |
| 82 | nacl::PnaclCacheInfo info; |
| 83 | host_->GetNexeFd( |
| 84 | 0, |
| 85 | base::ProcessHandle(), |
| 86 | 0, |
| 87 | 0, |
| 88 | info, |
| 89 | base::Bind(&PnaclHostTest::CallbackExpectMiss, base::Unretained(this))); |
| 90 | |
| 91 | EXPECT_PENDING_TRANSLATIONS(1); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 92 | host_->TranslationFinished(0, 1, true); // nonexistent translation |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 93 | EXPECT_PENDING_TRANSLATIONS(1); |
| 94 | host_->RendererClosing(1); // nonexistent renderer |
| 95 | EXPECT_PENDING_TRANSLATIONS(1); |
| 96 | content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 97 | base::RunLoop().RunUntilIdle(); |
| 98 | ExpectCallbackCount(1); |
| 99 | host_->RendererClosing(0); // close without finishing |
| 100 | EXPECT_PENDING_TRANSLATIONS(0); |
| 101 | } |
| 102 | |
| 103 | } // namespace pnacl |