OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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/net/net_log_temp_file.h" | |
6 | |
7 #include <stdio.h> | |
mmenke
2013/01/18 15:58:00
Is this needed?
ramant (doing other things)
2013/01/18 23:22:33
Done.
| |
8 #include <string> | |
mmenke
2013/01/18 15:58:00
nit: believe there should be a blank line between
ramant (doing other things)
2013/01/18 23:22:33
Deleted the include (not needed).
| |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/file_path.h" | |
12 #include "base/file_util.h" | |
13 #include "base/values.h" | |
14 #include "build/build_config.h" | |
15 #include "chrome/browser/net/chrome_net_log.h" | |
16 #include "content/public/test/test_browser_thread.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 using content::BrowserThread; | |
20 | |
21 class NetLogTempFileTest : public ::testing::Test { | |
22 protected: | |
23 NetLogTempFileTest() { | |
24 net_log_.reset(new ChromeNetLog); | |
25 | |
26 file_user_blocking_thread_.reset( | |
27 new content::TestBrowserThread(BrowserThread::FILE_USER_BLOCKING)); | |
28 file_user_blocking_thread_->Start(); | |
29 | |
30 net_log_temp_file_ = net_log_->net_log_temp_file(); | |
31 } | |
32 | |
33 virtual void SetUp() OVERRIDE { | |
34 // Get a temporary file name for unit tests. | |
35 FilePath net_export_log; | |
36 ASSERT_TRUE(file_util::CreateTemporaryFile(&net_export_log)); | |
37 | |
38 // Delete the file to start with no temporary file, but we will use the | |
39 // temporary file to log NetLog entries. | |
40 file_util::Delete(net_export_log, false); | |
mmenke
2013/01/18 15:58:00
This isn't thread safe. While the file doesn't ex
ramant (doing other things)
2013/01/18 23:22:33
Implemented Subclass approach.
Done.
| |
41 | |
42 FilePath net_export_log_filename = net_export_log.BaseName(); | |
43 net_log_temp_file_->chrome_net_export_ = FILE_PATH_LITERAL(""); | |
44 net_log_temp_file_->log_filename_ = net_export_log_filename.value(); | |
45 net_log_temp_file_->log_path_ = net_export_log; | |
46 } | |
47 | |
48 virtual void TearDown() OVERRIDE { | |
49 // Delete the temporary file we have created. | |
50 ASSERT_TRUE(file_util::Delete(net_log_temp_file_->log_path(), false)); | |
51 } | |
52 | |
53 NetLogTempFile* net_log_temp_file_; | |
54 | |
55 private: | |
56 scoped_ptr<ChromeNetLog> net_log_; | |
57 scoped_ptr<content::TestBrowserThread> file_user_blocking_thread_; | |
58 }; | |
59 | |
60 TEST_F(NetLogTempFileTest, InitAllowStart) { | |
61 net_log_temp_file_->Init(); | |
62 base::DictionaryValue* dict = net_log_temp_file_->GetState(); | |
63 std::string state; | |
64 dict->GetString("state", &state); | |
65 EXPECT_EQ("ALLOW_START", state); | |
66 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START, net_log_temp_file_->state()); | |
mmenke
2013/01/18 15:58:00
Think you should check the return value of net_log
ramant (doing other things)
2013/01/18 23:22:33
Done.
| |
67 } | |
68 | |
69 TEST_F(NetLogTempFileTest, InitAllowStartOrSend) { | |
70 FilePath log_path = net_log_temp_file_->log_path(); | |
71 // Create the temporary file. | |
72 FILE* fp = file_util::OpenFile(log_path, "w"); | |
73 CHECK(fp); | |
74 file_util::CloseFile(fp); | |
75 | |
76 net_log_temp_file_->Init(); | |
77 base::DictionaryValue* dict = net_log_temp_file_->GetState(); | |
78 std::string state; | |
79 dict->GetString("state", &state); | |
80 EXPECT_EQ("ALLOW_START_SEND", state); | |
81 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND, | |
82 net_log_temp_file_->state()); | |
83 } | |
84 | |
85 TEST_F(NetLogTempFileTest, ProcessCommandDoStart) { | |
86 net_log_temp_file_->Init(); | |
mmenke
2013/01/18 15:58:00
We don't need to call this ourselves except in the
ramant (doing other things)
2013/01/18 23:22:33
Done.
| |
87 // Execute DO_START command via ProcessCommand and verfiy that we have | |
88 // transitioned to STATE_ALLOW_STOP state. | |
89 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); | |
90 base::DictionaryValue* dict = net_log_temp_file_->GetState(); | |
91 std::string state; | |
92 dict->GetString("state", &state); | |
93 EXPECT_EQ("ALLOW_STOP", state); | |
94 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file_->state()); | |
95 FilePath log_path = net_log_temp_file_->log_path(); | |
96 EXPECT_TRUE(file_util::PathExists(log_path)); | |
97 // Execute DO_STOP so that temporary file could be deleted. | |
98 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | |
99 } | |
100 | |
101 TEST_F(NetLogTempFileTest, ProcessCommandDoStop) { | |
102 net_log_temp_file_->Init(); | |
103 // Execute DO_START command via ProcessCommand and verfiy that we transition | |
104 // to ALLOW_STOP state. | |
105 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); | |
106 base::DictionaryValue* dict = net_log_temp_file_->GetState(); | |
107 std::string state; | |
108 dict->GetString("state", &state); | |
109 EXPECT_EQ("ALLOW_STOP", state); | |
110 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file_->state()); | |
111 FilePath log_path = net_log_temp_file_->log_path(); | |
112 EXPECT_TRUE(file_util::PathExists(log_path)); | |
113 | |
114 // Execute DO_STOP command via ProcessCommand and verfiy that we have | |
115 // transitioned to STATE_ALLOW_START_SEND state. | |
116 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | |
117 dict = net_log_temp_file_->GetState(); | |
118 dict->GetString("state", &state); | |
119 EXPECT_EQ("ALLOW_START_SEND", state); | |
120 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND, | |
121 net_log_temp_file_->state()); | |
122 FilePath log_path1 = net_log_temp_file_->log_path(); | |
123 EXPECT_TRUE(file_util::PathExists(log_path1)); | |
124 } | |
125 | |
126 TEST_F(NetLogTempFileTest, GetFilePath) { | |
127 net_log_temp_file_->Init(); | |
128 // Execute DO_START and DO_STOP commands via ProcessCommand and verfiy that we | |
129 // have transitioned to STATE_ALLOW_START_SEND state. | |
130 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); | |
131 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | |
132 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND, | |
133 net_log_temp_file_->state()); | |
134 | |
135 FilePath net_export_file_path; | |
136 EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path)); | |
137 EXPECT_TRUE(file_util::PathExists(net_export_file_path)); | |
138 } | |
mmenke
2013/01/18 15:58:00
Worth testing that DoStart clears the log file?
C
mmenke
2013/01/18 16:02:02
Testing this is actually a little tricky. Could s
ramant (doing other things)
2013/01/18 23:22:33
Added a check the file_size is > 0.
Done.
ramant (doing other things)
2013/01/18 23:22:33
Added a TODO.
| |
OLD | NEW |