OLD | NEW |
---|---|
(Empty) | |
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 "base/bind.h" | |
6 #include "base/files/file_path.h" | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "base/path_service.h" | |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "chrome/browser/chromeos/first_run/drive_first_run_controller.h" | |
12 #include "chrome/browser/extensions/crx_installer.h" | |
13 #include "chrome/browser/extensions/extension_service.h" | |
14 #include "chrome/browser/extensions/extension_system.h" | |
15 #include "chrome/browser/extensions/extension_test_notification_observer.h" | |
16 #include "chrome/common/chrome_paths.h" | |
17 #include "chrome/test/base/in_process_browser_test.h" | |
18 #include "content/public/test/test_utils.h" | |
19 #include "net/dns/mock_host_resolver.h" | |
20 #include "net/http/http_status_code.h" | |
21 #include "net/test/embedded_test_server/embedded_test_server.h" | |
22 #include "net/test/embedded_test_server/http_request.h" | |
23 #include "net/test/embedded_test_server/http_response.h" | |
24 | |
25 namespace chromeos { | |
26 | |
27 namespace { | |
28 | |
29 // Directory containing data files for the tests. | |
30 const char kTestDirectory[] = "drive_first_run"; | |
31 | |
32 // Directory containing correct hosted app page served by the test server. | |
33 const char kGoodServerDirectory[] = "good"; | |
34 | |
35 // Directory containing incorrect hosted app page served by the test server. | |
36 const char kBadServerDirectory[] = "bad"; | |
37 | |
38 // Name of the test hosted app .crx file. | |
39 const char kTestAppCrxName[] = "app.crx"; | |
40 | |
41 // App id of the test hosted app. | |
42 const char kTestAppId[] = "kipccbklifbfblhpplnmklieangbjnhb"; | |
43 | |
44 // The endpoint belonging to the test hosted app. | |
45 const char kTestEndpointUrl[] = "http://example.com/endpoint.html"; | |
46 | |
47 } // namespace | |
48 | |
49 class DriveFirstRunTest : public InProcessBrowserTest, | |
50 public DriveFirstRunController::Observer { | |
51 protected: | |
52 DriveFirstRunTest() : | |
53 timed_out_(false), | |
54 waiting_for_result_(false), | |
55 success_(false) {} | |
56 | |
57 virtual void SetUpOnMainThread() OVERRIDE { | |
58 InProcessBrowserTest::SetUpOnMainThread(); | |
achuithb
2013/11/13 04:13:10
Do you mind pulling out the function bodies to out
Tim Song
2013/11/13 20:00:25
Done. There was a discussion on chromium-dev about
| |
59 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_); | |
60 test_data_dir_ = test_data_dir_.AppendASCII(kTestDirectory); | |
61 | |
62 host_resolver()->AddRule("*", "127.0.0.1"); | |
63 | |
64 // |controller_| will delete itself when it completes. | |
65 controller_ = new DriveFirstRunController(); | |
66 controller_->AddObserver(this); | |
67 controller_->SetDelaysForTest(0, 10); | |
68 controller_->SetAppInfoForTest(kTestAppId, kTestEndpointUrl); | |
69 } | |
70 | |
71 virtual void CleanUpOnMainThread() OVERRIDE { | |
72 InProcessBrowserTest::CleanUpOnMainThread(); | |
73 content::RunAllPendingInMessageLoop(); | |
74 } | |
75 | |
76 void InitTestServer(const std::string& directory) { | |
77 embedded_test_server()->ServeFilesFromDirectory( | |
78 test_data_dir_.AppendASCII(directory)); | |
79 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
80 | |
81 // Configure the endpoint to use the test server's port. | |
82 const GURL url(kTestEndpointUrl); | |
83 GURL::Replacements replacements; | |
84 std::string port(base::IntToString(embedded_test_server()->port())); | |
85 replacements.SetPortStr(port); | |
86 endpoint_url_ = url.ReplaceComponents(replacements).spec(); | |
87 controller_->SetAppInfoForTest(kTestAppId, endpoint_url_); | |
88 } | |
89 | |
90 void InstallApp() { | |
91 ExtensionService* extension_service = extensions::ExtensionSystem::Get( | |
92 browser()->profile())->extension_service(); | |
93 scoped_refptr<extensions::CrxInstaller> installer = | |
94 extensions::CrxInstaller::CreateSilent(extension_service); | |
95 | |
96 installer->InstallCrx(test_data_dir_.AppendASCII(kTestAppCrxName)); | |
97 ExtensionTestNotificationObserver observer(browser()); | |
98 observer.WaitForExtensionLoad(); | |
99 | |
100 ASSERT_TRUE(extension_service->GetExtensionById(kTestAppId, false)); | |
101 } | |
102 | |
103 bool WaitForFirstRunResult() { | |
104 waiting_for_result_ = true; | |
105 runner_ = new content::MessageLoopRunner; | |
106 runner_->Run(); | |
107 EXPECT_FALSE(waiting_for_result_); | |
108 return success_; | |
109 } | |
110 | |
111 // DriveFirstRunController::Observer overrides: | |
112 virtual void OnCompletion(bool success) OVERRIDE { | |
113 EXPECT_TRUE(waiting_for_result_); | |
114 waiting_for_result_ = false; | |
115 success_ = success; | |
116 runner_->Quit(); | |
achuithb
2013/11/13 04:13:10
Should you set controller_ to NULL in this functio
Tim Song
2013/11/13 20:00:25
Done.
| |
117 } | |
118 | |
119 virtual void OnTimedOut() { | |
achuithb
2013/11/13 04:13:10
OVERRIDE?
Tim Song
2013/11/13 20:00:25
Done.
| |
120 timed_out_ = true; | |
121 } | |
122 | |
123 DriveFirstRunController* controller() { return controller_; } | |
achuithb
2013/11/13 04:13:10
Why do you need this?
Tim Song
2013/11/13 20:00:25
controller_ is private and can't be referenced in
| |
124 bool timed_out() { return timed_out_; } | |
achuithb
2013/11/13 04:13:10
Should these functions be const?
Tim Song
2013/11/13 20:00:25
Done.
| |
125 | |
126 private: | |
127 // |controller_| is responsible for its own lifetime. | |
128 DriveFirstRunController* controller_; | |
129 scoped_refptr<content::MessageLoopRunner> runner_; | |
130 | |
131 bool timed_out_; | |
132 bool waiting_for_result_; | |
133 bool success_; | |
134 base::FilePath test_data_dir_; | |
135 std::string endpoint_url_; | |
136 }; | |
137 | |
138 IN_PROC_BROWSER_TEST_F(DriveFirstRunTest, OfflineEnabled) { | |
139 InstallApp(); | |
140 InitTestServer(kGoodServerDirectory); | |
141 controller()->EnableOfflineMode(); | |
142 EXPECT_TRUE(WaitForFirstRunResult()); | |
143 } | |
144 | |
145 IN_PROC_BROWSER_TEST_F(DriveFirstRunTest, AppNotInstalled) { | |
146 InitTestServer(kGoodServerDirectory); | |
147 controller()->EnableOfflineMode(); | |
148 EXPECT_FALSE(WaitForFirstRunResult()); | |
149 EXPECT_FALSE(timed_out()); | |
150 } | |
151 | |
152 IN_PROC_BROWSER_TEST_F(DriveFirstRunTest, TimedOut) { | |
153 InstallApp(); | |
154 InitTestServer(kBadServerDirectory); | |
155 controller()->SetDelaysForTest(0, 0); | |
achuithb
2013/11/13 04:13:10
Won't the test pass even with a good directory if
Tim Song
2013/11/13 20:00:25
That's true, but it would take a few seconds to co
achuithb
2013/11/13 23:11:54
Could you please add a comment explaining what thi
Tim Song
2013/11/14 01:18:49
Done.
| |
156 controller()->EnableOfflineMode(); | |
157 EXPECT_FALSE(WaitForFirstRunResult()); | |
158 EXPECT_TRUE(timed_out()); | |
159 } | |
160 | |
161 } // namespace chromeos | |
OLD | NEW |