| Index: chrome/browser/chromeos/app_mode/kiosk_crash_restore_browsertest.cc | 
| diff --git a/chrome/browser/chromeos/app_mode/kiosk_crash_restore_browsertest.cc b/chrome/browser/chromeos/app_mode/kiosk_crash_restore_browsertest.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..ded881cf8ad6d897beb63a075918e68ab7747464 | 
| --- /dev/null | 
| +++ b/chrome/browser/chromeos/app_mode/kiosk_crash_restore_browsertest.cc | 
| @@ -0,0 +1,200 @@ | 
| +// Copyright 2016 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#include <memory> | 
| +#include <string> | 
| + | 
| +#include "base/base64.h" | 
| +#include "base/command_line.h" | 
| +#include "base/files/file_util.h" | 
| +#include "base/path_service.h" | 
| +#include "base/run_loop.h" | 
| +#include "chrome/browser/chrome_notification_types.h" | 
| +#include "chrome/browser/chromeos/app_mode/fake_cws.h" | 
| +#include "chrome/browser/chromeos/app_mode/kiosk_app_launch_error.h" | 
| +#include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" | 
| +#include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_factory.h" | 
| +#include "chrome/browser/chromeos/policy/device_local_account.h" | 
| +#include "chrome/browser/chromeos/policy/device_policy_builder.h" | 
| +#include "chrome/browser/extensions/browsertest_util.h" | 
| +#include "chrome/browser/profiles/profile_manager.h" | 
| +#include "chrome/common/chrome_constants.h" | 
| +#include "chrome/common/chrome_paths.h" | 
| +#include "chrome/common/pref_names.h" | 
| +#include "chrome/test/base/in_process_browser_test.h" | 
| +#include "chromeos/chromeos_switches.h" | 
| +#include "chromeos/dbus/dbus_thread_manager.h" | 
| +#include "chromeos/dbus/fake_session_manager_client.h" | 
| +#include "components/ownership/mock_owner_key_util.h" | 
| +#include "content/public/browser/notification_observer.h" | 
| +#include "content/public/browser/notification_registrar.h" | 
| +#include "content/public/browser/notification_service.h" | 
| +#include "extensions/common/value_builder.h" | 
| +#include "extensions/test/extension_test_message_listener.h" | 
| +#include "net/dns/mock_host_resolver.h" | 
| + | 
| +namespace em = enterprise_management; | 
| + | 
| +namespace chromeos { | 
| + | 
| +namespace { | 
| + | 
| +const char kTestKioskApp[] = "ggbflgnkafappblpkiflbgpmkfdpnhhe"; | 
| + | 
| +// Used to listen for app termination notification. | 
| +class TerminationObserver : public content::NotificationObserver { | 
| + public: | 
| +  TerminationObserver() { | 
| +    registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, | 
| +                   content::NotificationService::AllSources()); | 
| +  } | 
| +  ~TerminationObserver() override = default; | 
| + | 
| +  // Whether app has been terminated - i.e. whether app termination notification | 
| +  // has been observed. | 
| +  bool terminated() const { return notification_seen_; } | 
| + | 
| + private: | 
| +  void Observe(int type, | 
| +               const content::NotificationSource& source, | 
| +               const content::NotificationDetails& details) override { | 
| +    ASSERT_EQ(chrome::NOTIFICATION_APP_TERMINATING, type); | 
| +    notification_seen_ = true; | 
| +  } | 
| + | 
| +  bool notification_seen_ = false; | 
| +  content::NotificationRegistrar registrar_; | 
| + | 
| +  DISALLOW_COPY_AND_ASSIGN(TerminationObserver); | 
| +}; | 
| + | 
| +}  // namespace | 
| + | 
| +class KioskCrashRestoreTest : public InProcessBrowserTest { | 
| + public: | 
| +  KioskCrashRestoreTest() | 
| +      : owner_key_util_(new ownership::MockOwnerKeyUtil()), | 
| +        fake_cws_(new FakeCWS) {} | 
| + | 
| +  // InProcessBrowserTest | 
| +  void SetUp() override { | 
| +    ASSERT_TRUE(embedded_test_server()->InitializeAndListen()); | 
| +    InProcessBrowserTest::SetUp(); | 
| +  } | 
| + | 
| +  bool SetUpUserDataDirectory() override { | 
| +    SetUpExistingKioskApp(); | 
| +    return true; | 
| +  } | 
| + | 
| +  void SetUpInProcessBrowserTestFixture() override { | 
| +    host_resolver()->AddRule("*", "127.0.0.1"); | 
| + | 
| +    OverrideDevicePolicy(); | 
| +  } | 
| + | 
| +  void SetUpCommandLine(base::CommandLine* command_line) override { | 
| +    const AccountId account_id = AccountId::FromUserEmail(GetTestAppUserId()); | 
| +    const cryptohome::Identification cryptohome_id(account_id); | 
| + | 
| +    command_line->AppendSwitchASCII(switches::kLoginUser, cryptohome_id.id()); | 
| +    command_line->AppendSwitchASCII( | 
| +        switches::kLoginProfile, | 
| +        CryptohomeClient::GetStubSanitizedUsername(cryptohome_id)); | 
| + | 
| +    fake_cws_->Init(embedded_test_server()); | 
| +    fake_cws_->SetUpdateCrx(test_app_id_, test_app_id_ + ".crx", "1.0.0"); | 
| +  } | 
| + | 
| +  void RunTestOnMainThreadLoop() override { | 
| +    termination_observer_.reset(new TerminationObserver()); | 
| + | 
| +    InProcessBrowserTest::RunTestOnMainThreadLoop(); | 
| +  } | 
| + | 
| +  void SetUpOnMainThread() override { | 
| +    extensions::browsertest_util::CreateAndInitializeLocalCache(); | 
| + | 
| +    embedded_test_server()->StartAcceptingConnections(); | 
| +  } | 
| + | 
| +  const std::string GetTestAppUserId() const { | 
| +    return policy::GenerateDeviceLocalAccountUserId( | 
| +        test_app_id_, policy::DeviceLocalAccount::TYPE_KIOSK_APP); | 
| +  } | 
| + | 
| +  const std::string& test_app_id() const { return test_app_id_; } | 
| + | 
| + protected: | 
| +  std::unique_ptr<TerminationObserver> termination_observer_; | 
| + | 
| + private: | 
| +  void SetUpExistingKioskApp() { | 
| +    // Create policy data that contains the test app as an existing kiosk app. | 
| +    em::DeviceLocalAccountsProto* const device_local_accounts = | 
| +        device_policy_.payload().mutable_device_local_accounts(); | 
| + | 
| +    em::DeviceLocalAccountInfoProto* const account = | 
| +        device_local_accounts->add_account(); | 
| +    account->set_account_id(test_app_id_); | 
| +    account->set_type( | 
| +        em::DeviceLocalAccountInfoProto_AccountType_ACCOUNT_TYPE_KIOSK_APP); | 
| +    account->mutable_kiosk_app()->set_app_id(test_app_id_); | 
| +    device_policy_.Build(); | 
| + | 
| +    // Prepare the policy data to store in device policy cache. | 
| +    em::PolicyData policy_data; | 
| +    CHECK(device_policy_.payload().SerializeToString( | 
| +        policy_data.mutable_policy_value())); | 
| +    const std::string policy_data_string = policy_data.SerializeAsString(); | 
| +    std::string encoded; | 
| +    base::Base64Encode(policy_data_string, &encoded); | 
| + | 
| +    // Store policy data and existing device local accounts in local state. | 
| +    const std::string local_state_json = | 
| +        extensions::DictionaryBuilder() | 
| +            .Set(prefs::kDeviceSettingsCache, encoded) | 
| +            .Set("PublicAccounts", | 
| +                 extensions::ListBuilder().Append(GetTestAppUserId()).Build()) | 
| +            .ToJSON(); | 
| + | 
| +    base::FilePath local_state_file; | 
| +    CHECK(PathService::Get(chrome::DIR_USER_DATA, &local_state_file)); | 
| +    local_state_file = local_state_file.Append(chrome::kLocalStateFilename); | 
| +    base::WriteFile(local_state_file, local_state_json.data(), | 
| +                    local_state_json.size()); | 
| +  } | 
| + | 
| +  void OverrideDevicePolicy() { | 
| +    OwnerSettingsServiceChromeOSFactory::GetInstance() | 
| +        ->SetOwnerKeyUtilForTesting(owner_key_util_); | 
| +    owner_key_util_->SetPublicKeyFromPrivateKey( | 
| +        *device_policy_.GetSigningKey()); | 
| + | 
| +    session_manager_client_ = new FakeSessionManagerClient; | 
| +    session_manager_client_->set_device_policy(device_policy_.GetBlob()); | 
| + | 
| +    DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( | 
| +        std::unique_ptr<SessionManagerClient>(session_manager_client_)); | 
| +  } | 
| + | 
| +  std::string test_app_id_ = kTestKioskApp; | 
| + | 
| +  policy::DevicePolicyBuilder device_policy_; | 
| +  scoped_refptr<ownership::MockOwnerKeyUtil> owner_key_util_; | 
| +  FakeSessionManagerClient* session_manager_client_; | 
| +  std::unique_ptr<FakeCWS> fake_cws_; | 
| + | 
| +  DISALLOW_COPY_AND_ASSIGN(KioskCrashRestoreTest); | 
| +}; | 
| + | 
| +IN_PROC_BROWSER_TEST_F(KioskCrashRestoreTest, AppNotInstalled) { | 
| +  // If app is not installed when restoring from crash, the kiosk launch is | 
| +  // expected to fail, as in that case the crash occured during the app | 
| +  // initialization - before the app was actually launched. | 
| +  EXPECT_TRUE(termination_observer_->terminated()); | 
| +  EXPECT_EQ(KioskAppLaunchError::UNABLE_TO_LAUNCH, KioskAppLaunchError::Get()); | 
| +} | 
| + | 
| +}  // namespace chromeos | 
|  |