Index: chrome/browser/net/net_log_temp_file_unittest.cc |
=================================================================== |
--- chrome/browser/net/net_log_temp_file_unittest.cc (revision 0) |
+++ chrome/browser/net/net_log_temp_file_unittest.cc (revision 0) |
@@ -0,0 +1,156 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
mmenke
2013/01/17 17:49:43
nit: 2013
ramant (doing other things)
2013/01/18 04:59:52
Done.
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/net/net_log_temp_file.h" |
+ |
+#include <stdio.h> |
+#include <string> |
+ |
+#include "base/basictypes.h" |
+#include "base/file_path.h" |
+#include "base/file_util.h" |
+#include "base/values.h" |
+#include "build/build_config.h" |
+#include "chrome/browser/net/chrome_net_log.h" |
+#include "content/public/test/test_browser_thread.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using content::BrowserThread; |
+ |
+class NetLogTempFileTest : public ::testing::Test { |
+ protected: |
+ NetLogTempFileTest() { |
+ file_user_blocking_thread_.reset( |
+ new content::TestBrowserThread(BrowserThread::FILE_USER_BLOCKING)); |
+ file_user_blocking_thread_->Start(); |
+ |
+ net_log_.reset(new ChromeNetLog); |
mmenke
2013/01/17 17:49:43
Suggest creating this first, and destroying it sec
ramant (doing other things)
2013/01/18 04:59:52
Done.
|
+ NetLogTempFile* net_log_temp_file = chrome_net_log()->net_log_temp_file(); |
+ net_log_temp_file->Init(); |
+ |
+ // Delete the old temporary file if it exists. |
mmenke
2013/01/17 17:49:43
unit tests are currently run multi-threaded, I bel
ramant (doing other things)
2013/01/18 04:59:52
Done.
|
+ FilePath log_path = net_log_temp_file->log_path(); |
+ if (file_util::PathExists(log_path)) |
+ file_util::Delete(log_path, false); |
+ |
+ // Call Init() again, to start in correct state. |
+ net_log_temp_file->set_state(NetLogTempFile::STATE_UNINITIALIZED); |
+ net_log_temp_file->Init(); |
+ } |
+ |
+ ChromeNetLog* chrome_net_log() { return net_log_.get(); } |
+ |
+ ~NetLogTempFileTest() { |
+ net_log_.reset(); |
+ file_user_blocking_thread_.reset(); |
mmenke
2013/01/17 17:49:43
We should be deleting the temp file / directory he
mmenke
2013/01/17 17:49:43
Is explicit destruction needed?
ramant (doing other things)
2013/01/18 04:59:52
Done.
ramant (doing other things)
2013/01/18 04:59:52
Deleted the temp file. Didn't delete the directory
|
+ } |
+ |
+ private: |
+ scoped_ptr<content::TestBrowserThread> file_user_blocking_thread_; |
+ scoped_ptr<ChromeNetLog> net_log_; |
mmenke
2013/01/17 17:49:43
Suggest just reversing the order of these, to matc
ramant (doing other things)
2013/01/18 04:59:52
Done.
|
+}; |
+ |
+TEST_F(NetLogTempFileTest, Init) { |
+ NetLogTempFile* net_log_temp_file = chrome_net_log()->net_log_temp_file(); |
+ |
+ // Start the test, by setting the state to STATE_UNINITIALIZED. |
+ net_log_temp_file->set_state(NetLogTempFile::STATE_UNINITIALIZED); |
+ net_log_temp_file->Init(); |
mmenke
2013/01/17 17:49:43
Rather than using internal functions, I suggest on
ramant (doing other things)
2013/01/18 04:59:52
Implemented what we had discussed.
Done.
|
+ |
+ // Check the state is not ALLOW_STOP. |
+ base::DictionaryValue* dict = net_log_temp_file->GetState(); |
+ std::string state; |
+ dict->GetString("state", &state); |
+ EXPECT_NE("ALLOW_STOP", state); |
+ EXPECT_NE(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file->state()); |
+} |
+ |
+TEST_F(NetLogTempFileTest, InitAllowStart) { |
+ NetLogTempFile* net_log_temp_file = chrome_net_log()->net_log_temp_file(); |
+ |
+ net_log_temp_file->set_state(NetLogTempFile::STATE_UNINITIALIZED); |
+ net_log_temp_file->Init(); |
+ |
+ base::DictionaryValue* dict = net_log_temp_file->GetState(); |
+ std::string state; |
+ dict->GetString("state", &state); |
+ EXPECT_EQ("ALLOW_START", state); |
+ EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START, net_log_temp_file->state()); |
+} |
+ |
+TEST_F(NetLogTempFileTest, InitAllowStartOrSend) { |
+ NetLogTempFile* net_log_temp_file = chrome_net_log()->net_log_temp_file(); |
+ |
+ // Touch the temporary file and assert that the state is in ALLOW_START_SEND. |
+ base::Time access_time; |
+ base::Time modification_time; |
+ FilePath log_path = net_log_temp_file->log_path(); |
+ FILE* fp = file_util::OpenFile(log_path, "w"); |
+ EXPECT_TRUE(fp); |
+ file_util::CloseFile(fp); |
+ |
+ // Start the test, by setting the state to STATE_UNINITIALIZED. |
+ net_log_temp_file->set_state(NetLogTempFile::STATE_UNINITIALIZED); |
+ net_log_temp_file->Init(); |
+ |
+ base::DictionaryValue* dict = net_log_temp_file->GetState(); |
+ std::string state; |
+ dict->GetString("state", &state); |
+ EXPECT_EQ("ALLOW_START_SEND", state); |
+ EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND, net_log_temp_file->state()); |
+} |
+ |
+TEST_F(NetLogTempFileTest, ProcessCommandDoStart) { |
+ NetLogTempFile* net_log_temp_file = chrome_net_log()->net_log_temp_file(); |
+ |
+ // Execute DO_START command via ProcessCommand and verfiy that we have |
+ // transitioned to STATE_ALLOW_STOP state. |
+ net_log_temp_file->ProcessCommand(NetLogTempFile::DO_START); |
+ base::DictionaryValue* dict = net_log_temp_file->GetState(); |
+ std::string state; |
+ dict->GetString("state", &state); |
+ EXPECT_EQ("ALLOW_STOP", state); |
+ EXPECT_EQ(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file->state()); |
+ FilePath log_path = net_log_temp_file->log_path(); |
+ EXPECT_TRUE(file_util::PathExists(log_path)); |
+} |
+ |
+TEST_F(NetLogTempFileTest, ProcessCommandDoStop) { |
+ NetLogTempFile* net_log_temp_file = chrome_net_log()->net_log_temp_file(); |
+ |
+ // Execute DO_START command via ProcessCommand and verfiy that we transition |
+ // to ALLOW_STOP state. |
+ net_log_temp_file->ProcessCommand(NetLogTempFile::DO_START); |
+ base::DictionaryValue* dict = net_log_temp_file->GetState(); |
+ std::string state; |
+ dict->GetString("state", &state); |
+ EXPECT_EQ("ALLOW_STOP", state); |
+ EXPECT_EQ(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file->state()); |
+ FilePath log_path = net_log_temp_file->log_path(); |
+ EXPECT_TRUE(file_util::PathExists(log_path)); |
+ |
+ // Execute DO_STOP command via ProcessCommand and verfiy that we have |
+ // transitioned to STATE_ALLOW_START_SEND state. |
+ net_log_temp_file->ProcessCommand(NetLogTempFile::DO_STOP); |
+ dict = net_log_temp_file->GetState(); |
+ dict->GetString("state", &state); |
+ EXPECT_EQ("ALLOW_START_SEND", state); |
+ EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND, net_log_temp_file->state()); |
+ FilePath log_path1 = net_log_temp_file->log_path(); |
+ EXPECT_TRUE(file_util::PathExists(log_path1)); |
+} |
+ |
+TEST_F(NetLogTempFileTest, GetNetLogToSend) { |
+ NetLogTempFile* net_log_temp_file = chrome_net_log()->net_log_temp_file(); |
+ |
+ // Execute DO_START and DO_STOP commands via ProcessCommand and verfiy that we |
+ // have transitioned to STATE_ALLOW_START_SEND state. |
+ net_log_temp_file->ProcessCommand(NetLogTempFile::DO_START); |
+ net_log_temp_file->ProcessCommand(NetLogTempFile::DO_STOP); |
+ EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND, net_log_temp_file->state()); |
+ |
+ FilePath net_export_file_path; |
+ EXPECT_TRUE(net_log_temp_file->GetNetLogToSend(&net_export_file_path)); |
+ EXPECT_TRUE(file_util::PathExists(net_export_file_path)); |
+} |