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

Side by Side Diff: chrome/browser/chromeos/app_mode/kiosk_crash_restore_browsertest.cc

Issue 2704813002: Resurect KioskCrashRestore browser test (Closed)
Patch Set: Created 3 years, 10 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <memory> 5 #include <memory>
6 #include <string> 6 #include <string>
7 7
8 #include "apps/test/app_window_waiter.h"
9 #include "base/base64.h" 8 #include "base/base64.h"
10 #include "base/command_line.h" 9 #include "base/command_line.h"
11 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
12 #include "base/path_service.h" 11 #include "base/path_service.h"
13 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/chromeos/app_mode/fake_cws.h" 14 #include "chrome/browser/chromeos/app_mode/fake_cws.h"
15 #include "chrome/browser/chromeos/app_mode/kiosk_app_launch_error.h"
15 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" 16 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
16 #include "chrome/browser/chromeos/net/network_portal_detector_test_impl.h"
17 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_fact ory.h" 17 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_fact ory.h"
18 #include "chrome/browser/chromeos/policy/device_local_account.h" 18 #include "chrome/browser/chromeos/policy/device_local_account.h"
19 #include "chrome/browser/chromeos/policy/device_policy_builder.h" 19 #include "chrome/browser/chromeos/policy/device_policy_builder.h"
20 #include "chrome/browser/extensions/browsertest_util.h" 20 #include "chrome/browser/extensions/browsertest_util.h"
21 #include "chrome/browser/profiles/profile_manager.h" 21 #include "chrome/browser/profiles/profile_manager.h"
22 #include "chrome/common/chrome_constants.h" 22 #include "chrome/common/chrome_constants.h"
23 #include "chrome/common/chrome_paths.h" 23 #include "chrome/common/chrome_paths.h"
24 #include "chrome/common/pref_names.h" 24 #include "chrome/common/pref_names.h"
25 #include "chrome/test/base/in_process_browser_test.h" 25 #include "chrome/test/base/in_process_browser_test.h"
26 #include "chromeos/chromeos_switches.h" 26 #include "chromeos/chromeos_switches.h"
27 #include "chromeos/dbus/dbus_thread_manager.h" 27 #include "chromeos/dbus/dbus_thread_manager.h"
28 #include "chromeos/dbus/fake_session_manager_client.h" 28 #include "chromeos/dbus/fake_session_manager_client.h"
29 #include "chromeos/dbus/fake_shill_manager_client.h"
30 #include "components/ownership/mock_owner_key_util.h" 29 #include "components/ownership/mock_owner_key_util.h"
31 #include "extensions/browser/app_window/app_window.h" 30 #include "content/public/browser/notification_observer.h"
32 #include "extensions/browser/app_window/app_window_registry.h" 31 #include "content/public/browser/notification_registrar.h"
33 #include "extensions/browser/app_window/native_app_window.h" 32 #include "content/public/browser/notification_service.h"
34 #include "extensions/common/value_builder.h" 33 #include "extensions/common/value_builder.h"
35 #include "extensions/test/extension_test_message_listener.h" 34 #include "extensions/test/extension_test_message_listener.h"
36 #include "net/dns/mock_host_resolver.h" 35 #include "net/dns/mock_host_resolver.h"
37 36
38 namespace em = enterprise_management; 37 namespace em = enterprise_management;
39 38
40 namespace chromeos { 39 namespace chromeos {
41 40
42 namespace { 41 namespace {
43 42
44 const char kTestKioskApp[] = "ggbflgnkafappblpkiflbgpmkfdpnhhe"; 43 const char kTestKioskApp[] = "ggbflgnkafappblpkiflbgpmkfdpnhhe";
45 44
45 // Used to listen for app termination notification.
46 class TerminationObserver : public content::NotificationObserver {
47 public:
48 TerminationObserver() {
49 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
50 content::NotificationService::AllSources());
51 }
52 ~TerminationObserver() override = default;
53
54 // Whether app has been terminated - i.e. whether app termination notification
55 // has been observed.
56 bool terminated() const { return notification_seen_; }
57
58 private:
59 void Observe(int type,
60 const content::NotificationSource& source,
61 const content::NotificationDetails& details) override {
62 ASSERT_EQ(chrome::NOTIFICATION_APP_TERMINATING, type);
63 notification_seen_ = true;
64 }
65
66 bool notification_seen_ = false;
67 content::NotificationRegistrar registrar_;
68
69 DISALLOW_COPY_AND_ASSIGN(TerminationObserver);
70 };
71
46 } // namespace 72 } // namespace
47 73
48 class KioskCrashRestoreTest : public InProcessBrowserTest { 74 class KioskCrashRestoreTest : public InProcessBrowserTest {
49 public: 75 public:
50 KioskCrashRestoreTest() 76 KioskCrashRestoreTest()
51 : owner_key_util_(new ownership::MockOwnerKeyUtil()), 77 : owner_key_util_(new ownership::MockOwnerKeyUtil()),
52 fake_cws_(new FakeCWS) {} 78 fake_cws_(new FakeCWS) {}
53 79
54 // InProcessBrowserTest 80 // InProcessBrowserTest
55 void SetUp() override { 81 void SetUp() override {
56 ASSERT_TRUE(embedded_test_server()->InitializeAndListen()); 82 ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
57 InProcessBrowserTest::SetUp(); 83 InProcessBrowserTest::SetUp();
58 } 84 }
59 85
60 bool SetUpUserDataDirectory() override { 86 bool SetUpUserDataDirectory() override {
61 SetUpExistingKioskApp(); 87 SetUpExistingKioskApp();
62 return true; 88 return true;
63 } 89 }
64 90
65 void SetUpInProcessBrowserTestFixture() override { 91 void SetUpInProcessBrowserTestFixture() override {
66 host_resolver()->AddRule("*", "127.0.0.1"); 92 host_resolver()->AddRule("*", "127.0.0.1");
67 SimulateNetworkOnline();
68 93
69 OverrideDevicePolicy(); 94 OverrideDevicePolicy();
70 } 95 }
71 96
72 void SetUpCommandLine(base::CommandLine* command_line) override { 97 void SetUpCommandLine(base::CommandLine* command_line) override {
73 const AccountId account_id = AccountId::FromUserEmail(GetTestAppUserId()); 98 const AccountId account_id = AccountId::FromUserEmail(GetTestAppUserId());
74 const cryptohome::Identification cryptohome_id(account_id); 99 const cryptohome::Identification cryptohome_id(account_id);
75 100
76 command_line->AppendSwitchASCII(switches::kLoginUser, cryptohome_id.id()); 101 command_line->AppendSwitchASCII(switches::kLoginUser, cryptohome_id.id());
77 command_line->AppendSwitchASCII( 102 command_line->AppendSwitchASCII(
78 switches::kLoginProfile, 103 switches::kLoginProfile,
79 CryptohomeClient::GetStubSanitizedUsername(cryptohome_id)); 104 CryptohomeClient::GetStubSanitizedUsername(cryptohome_id));
80 105
81 fake_cws_->Init(embedded_test_server()); 106 fake_cws_->Init(embedded_test_server());
82 fake_cws_->SetUpdateCrx(test_app_id_, test_app_id_ + ".crx", "1.0.0"); 107 fake_cws_->SetUpdateCrx(test_app_id_, test_app_id_ + ".crx", "1.0.0");
83 } 108 }
84 109
110 void RunTestOnMainThreadLoop() override {
111 termination_observer_.reset(new TerminationObserver());
112
113 InProcessBrowserTest::RunTestOnMainThreadLoop();
114 }
115
85 void SetUpOnMainThread() override { 116 void SetUpOnMainThread() override {
86 extensions::browsertest_util::CreateAndInitializeLocalCache(); 117 extensions::browsertest_util::CreateAndInitializeLocalCache();
87 118
88 embedded_test_server()->StartAcceptingConnections(); 119 embedded_test_server()->StartAcceptingConnections();
89 } 120 }
90 121
91 const std::string GetTestAppUserId() const { 122 const std::string GetTestAppUserId() const {
92 return policy::GenerateDeviceLocalAccountUserId( 123 return policy::GenerateDeviceLocalAccountUserId(
93 test_app_id_, policy::DeviceLocalAccount::TYPE_KIOSK_APP); 124 test_app_id_, policy::DeviceLocalAccount::TYPE_KIOSK_APP);
94 } 125 }
95 126
96 const std::string& test_app_id() const { return test_app_id_; } 127 const std::string& test_app_id() const { return test_app_id_; }
97 128
129 protected:
130 std::unique_ptr<TerminationObserver> termination_observer_;
131
98 private: 132 private:
99 void SetUpExistingKioskApp() { 133 void SetUpExistingKioskApp() {
100 // Create policy data that contains the test app as an existing kiosk app. 134 // Create policy data that contains the test app as an existing kiosk app.
101 em::DeviceLocalAccountsProto* const device_local_accounts = 135 em::DeviceLocalAccountsProto* const device_local_accounts =
102 device_policy_.payload().mutable_device_local_accounts(); 136 device_policy_.payload().mutable_device_local_accounts();
103 137
104 em::DeviceLocalAccountInfoProto* const account = 138 em::DeviceLocalAccountInfoProto* const account =
105 device_local_accounts->add_account(); 139 device_local_accounts->add_account();
106 account->set_account_id(test_app_id_); 140 account->set_account_id(test_app_id_);
107 account->set_type( 141 account->set_type(
(...skipping 17 matching lines...) Expand all
125 extensions::ListBuilder().Append(GetTestAppUserId()).Build()) 159 extensions::ListBuilder().Append(GetTestAppUserId()).Build())
126 .ToJSON(); 160 .ToJSON();
127 161
128 base::FilePath local_state_file; 162 base::FilePath local_state_file;
129 CHECK(PathService::Get(chrome::DIR_USER_DATA, &local_state_file)); 163 CHECK(PathService::Get(chrome::DIR_USER_DATA, &local_state_file));
130 local_state_file = local_state_file.Append(chrome::kLocalStateFilename); 164 local_state_file = local_state_file.Append(chrome::kLocalStateFilename);
131 base::WriteFile(local_state_file, local_state_json.data(), 165 base::WriteFile(local_state_file, local_state_json.data(),
132 local_state_json.size()); 166 local_state_json.size());
133 } 167 }
134 168
135 void SimulateNetworkOnline() {
136 NetworkPortalDetectorTestImpl* const network_portal_detector =
137 new NetworkPortalDetectorTestImpl();
138 // Takes ownership of |network_portal_detector|.
139 network_portal_detector::InitializeForTesting(network_portal_detector);
140 network_portal_detector->SetDefaultNetworkForTesting(
141 FakeShillManagerClient::kFakeEthernetNetworkGuid);
142
143 NetworkPortalDetector::CaptivePortalState online_state;
144 online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
145 online_state.response_code = 204;
146 network_portal_detector->SetDetectionResultsForTesting(
147 FakeShillManagerClient::kFakeEthernetNetworkGuid, online_state);
148 }
149
150 void OverrideDevicePolicy() { 169 void OverrideDevicePolicy() {
151 OwnerSettingsServiceChromeOSFactory::GetInstance() 170 OwnerSettingsServiceChromeOSFactory::GetInstance()
152 ->SetOwnerKeyUtilForTesting(owner_key_util_); 171 ->SetOwnerKeyUtilForTesting(owner_key_util_);
153 owner_key_util_->SetPublicKeyFromPrivateKey( 172 owner_key_util_->SetPublicKeyFromPrivateKey(
154 *device_policy_.GetSigningKey()); 173 *device_policy_.GetSigningKey());
155 174
156 session_manager_client_ = new FakeSessionManagerClient; 175 session_manager_client_ = new FakeSessionManagerClient;
157 session_manager_client_->set_device_policy(device_policy_.GetBlob()); 176 session_manager_client_->set_device_policy(device_policy_.GetBlob());
158 177
159 DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( 178 DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient(
160 std::unique_ptr<SessionManagerClient>(session_manager_client_)); 179 std::unique_ptr<SessionManagerClient>(session_manager_client_));
161 } 180 }
162 181
163 std::string test_app_id_ = kTestKioskApp; 182 std::string test_app_id_ = kTestKioskApp;
164 183
165 policy::DevicePolicyBuilder device_policy_; 184 policy::DevicePolicyBuilder device_policy_;
166 scoped_refptr<ownership::MockOwnerKeyUtil> owner_key_util_; 185 scoped_refptr<ownership::MockOwnerKeyUtil> owner_key_util_;
167 FakeSessionManagerClient* session_manager_client_; 186 FakeSessionManagerClient* session_manager_client_;
168 std::unique_ptr<FakeCWS> fake_cws_; 187 std::unique_ptr<FakeCWS> fake_cws_;
169 188
170 DISALLOW_COPY_AND_ASSIGN(KioskCrashRestoreTest); 189 DISALLOW_COPY_AND_ASSIGN(KioskCrashRestoreTest);
171 }; 190 };
172 191
173 IN_PROC_BROWSER_TEST_F(KioskCrashRestoreTest, Basic) { 192 IN_PROC_BROWSER_TEST_F(KioskCrashRestoreTest, AppNotInstalled) {
174 ExtensionTestMessageListener launch_data_check_listener( 193 // If app is not installed when restoring from crash, the kiosk launch is
175 "launchData.isKioskSession = true", false); 194 // expected to fail, as in that case the crash occured during the app
176 195 // initialization - before the app was actually launched.
177 Profile* const app_profile = ProfileManager::GetPrimaryUserProfile(); 196 EXPECT_TRUE(termination_observer_->terminated());
178 ASSERT_TRUE(app_profile); 197 EXPECT_EQ(KioskAppLaunchError::UNABLE_TO_LAUNCH, KioskAppLaunchError::Get());
179 extensions::AppWindowRegistry* const app_window_registry =
180 extensions::AppWindowRegistry::Get(app_profile);
181 extensions::AppWindow* const window =
182 apps::AppWindowWaiter(app_window_registry, test_app_id()).Wait();
183 ASSERT_TRUE(window);
184
185 window->GetBaseWindow()->Close();
186
187 // Wait until the app terminates if it is still running.
188 if (!app_window_registry->GetAppWindowsForApp(test_app_id()).empty())
189 base::RunLoop().Run();
190
191 EXPECT_TRUE(launch_data_check_listener.was_satisfied());
192 } 198 }
193 199
194 } // namespace chromeos 200 } // namespace chromeos
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698