Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: chrome/browser/net/net_log_temp_file_unittest.cc

Issue 11828036: First cut at UI for saving net_logs data into a temporary file on (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // 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.
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>
8 #include <string>
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 file_user_blocking_thread_.reset(
25 new content::TestBrowserThread(BrowserThread::FILE_USER_BLOCKING));
26 file_user_blocking_thread_->Start();
27
28 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.
29 NetLogTempFile* net_log_temp_file = chrome_net_log()->net_log_temp_file();
30 net_log_temp_file->Init();
31
32 // 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.
33 FilePath log_path = net_log_temp_file->log_path();
34 if (file_util::PathExists(log_path))
35 file_util::Delete(log_path, false);
36
37 // Call Init() again, to start in correct state.
38 net_log_temp_file->set_state(NetLogTempFile::STATE_UNINITIALIZED);
39 net_log_temp_file->Init();
40 }
41
42 ChromeNetLog* chrome_net_log() { return net_log_.get(); }
43
44 ~NetLogTempFileTest() {
45 net_log_.reset();
46 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
47 }
48
49 private:
50 scoped_ptr<content::TestBrowserThread> file_user_blocking_thread_;
51 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.
52 };
53
54 TEST_F(NetLogTempFileTest, Init) {
55 NetLogTempFile* net_log_temp_file = chrome_net_log()->net_log_temp_file();
56
57 // Start the test, by setting the state to STATE_UNINITIALIZED.
58 net_log_temp_file->set_state(NetLogTempFile::STATE_UNINITIALIZED);
59 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.
60
61 // Check the state is not ALLOW_STOP.
62 base::DictionaryValue* dict = net_log_temp_file->GetState();
63 std::string state;
64 dict->GetString("state", &state);
65 EXPECT_NE("ALLOW_STOP", state);
66 EXPECT_NE(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file->state());
67 }
68
69 TEST_F(NetLogTempFileTest, InitAllowStart) {
70 NetLogTempFile* net_log_temp_file = chrome_net_log()->net_log_temp_file();
71
72 net_log_temp_file->set_state(NetLogTempFile::STATE_UNINITIALIZED);
73 net_log_temp_file->Init();
74
75 base::DictionaryValue* dict = net_log_temp_file->GetState();
76 std::string state;
77 dict->GetString("state", &state);
78 EXPECT_EQ("ALLOW_START", state);
79 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START, net_log_temp_file->state());
80 }
81
82 TEST_F(NetLogTempFileTest, InitAllowStartOrSend) {
83 NetLogTempFile* net_log_temp_file = chrome_net_log()->net_log_temp_file();
84
85 // Touch the temporary file and assert that the state is in ALLOW_START_SEND.
86 base::Time access_time;
87 base::Time modification_time;
88 FilePath log_path = net_log_temp_file->log_path();
89 FILE* fp = file_util::OpenFile(log_path, "w");
90 EXPECT_TRUE(fp);
91 file_util::CloseFile(fp);
92
93 // Start the test, by setting the state to STATE_UNINITIALIZED.
94 net_log_temp_file->set_state(NetLogTempFile::STATE_UNINITIALIZED);
95 net_log_temp_file->Init();
96
97 base::DictionaryValue* dict = net_log_temp_file->GetState();
98 std::string state;
99 dict->GetString("state", &state);
100 EXPECT_EQ("ALLOW_START_SEND", state);
101 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND, net_log_temp_file->state());
102 }
103
104 TEST_F(NetLogTempFileTest, ProcessCommandDoStart) {
105 NetLogTempFile* net_log_temp_file = chrome_net_log()->net_log_temp_file();
106
107 // Execute DO_START command via ProcessCommand and verfiy that we have
108 // transitioned to STATE_ALLOW_STOP state.
109 net_log_temp_file->ProcessCommand(NetLogTempFile::DO_START);
110 base::DictionaryValue* dict = net_log_temp_file->GetState();
111 std::string state;
112 dict->GetString("state", &state);
113 EXPECT_EQ("ALLOW_STOP", state);
114 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file->state());
115 FilePath log_path = net_log_temp_file->log_path();
116 EXPECT_TRUE(file_util::PathExists(log_path));
117 }
118
119 TEST_F(NetLogTempFileTest, ProcessCommandDoStop) {
120 NetLogTempFile* net_log_temp_file = chrome_net_log()->net_log_temp_file();
121
122 // Execute DO_START command via ProcessCommand and verfiy that we transition
123 // to ALLOW_STOP state.
124 net_log_temp_file->ProcessCommand(NetLogTempFile::DO_START);
125 base::DictionaryValue* dict = net_log_temp_file->GetState();
126 std::string state;
127 dict->GetString("state", &state);
128 EXPECT_EQ("ALLOW_STOP", state);
129 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file->state());
130 FilePath log_path = net_log_temp_file->log_path();
131 EXPECT_TRUE(file_util::PathExists(log_path));
132
133 // Execute DO_STOP command via ProcessCommand and verfiy that we have
134 // transitioned to STATE_ALLOW_START_SEND state.
135 net_log_temp_file->ProcessCommand(NetLogTempFile::DO_STOP);
136 dict = net_log_temp_file->GetState();
137 dict->GetString("state", &state);
138 EXPECT_EQ("ALLOW_START_SEND", state);
139 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND, net_log_temp_file->state());
140 FilePath log_path1 = net_log_temp_file->log_path();
141 EXPECT_TRUE(file_util::PathExists(log_path1));
142 }
143
144 TEST_F(NetLogTempFileTest, GetNetLogToSend) {
145 NetLogTempFile* net_log_temp_file = chrome_net_log()->net_log_temp_file();
146
147 // Execute DO_START and DO_STOP commands via ProcessCommand and verfiy that we
148 // have transitioned to STATE_ALLOW_START_SEND state.
149 net_log_temp_file->ProcessCommand(NetLogTempFile::DO_START);
150 net_log_temp_file->ProcessCommand(NetLogTempFile::DO_STOP);
151 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND, net_log_temp_file->state());
152
153 FilePath net_export_file_path;
154 EXPECT_TRUE(net_log_temp_file->GetNetLogToSend(&net_export_file_path));
155 EXPECT_TRUE(file_util::PathExists(net_export_file_path));
156 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698