aboutsummaryrefslogtreecommitdiffstats
path: root/documentation/articles/CreatingAnApplicationThatPreservesStateOnRefresh.asciidoc
blob: 1226359d0a74ba2158020b6af037413d11952414 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
---
title: Creating An Application That Preserves State On Refresh
order: 19
layout: page
---

[[creating-an-application-that-preserves-state-on-refresh]]
Creating an application that preserves state on refresh
-------------------------------------------------------

By default, Vaadin 7 does not preserve UI state when the browser page is
refreshed. This means that the instance number in this example is
incremented and the text field cleared on every page refresh:

[source,java]
....
public class CreatingPreserveState extends UI {
    private static int instanceCounter = 0;

    private final CssLayout content = new CssLayout();

    @Override
    public void init(VaadinRequest request) {
        TextField tf = new TextField("Instance #" + (++instanceCounter));
        tf.setImmediate(true);

        content.addComponent(tf);
        setContent(content);
    }
}
....

You can however modify your application to preserve your UI between page
refreshes with the `@PreserveOnRefresh` annotation like so

[source,java]
....
@PreserveOnRefresh
public class PreserveStateUI extends UI {
    ...
}
....

If you want to reinitialize some part of your application when the page
is refreshed, you can (starting from Vaadin 7.2) override the refresh
method in your UI class. This method is called whenever an already
initialized UI is refreshed.

[source,java]
....
@Override
protected void refresh(VaadinRequest request) {
    content.addComponent(new Label("UI was refreshed @"
            + System.currentTimeMillis()));
}
....
4017/stable26 Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/AppFramework/Http/JSONResponseTest.php
blob: 56f67b23f0d56d737cfa121548f939bebbd6e18f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php

/**
 * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace Test\AppFramework\Http;

use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;

class JSONResponseTest extends \Test\TestCase {
	/**
	 * @var JSONResponse
	 */
	private $json;

	protected function setUp(): void {
		parent::setUp();
		$this->json = new JSONResponse();
	}


	public function testHeader(): void {
		$headers = $this->json->getHeaders();
		$this->assertEquals('application/json; charset=utf-8', $headers['Content-Type']);
	}


	public function testSetData(): void {
		$params = ['hi', 'yo'];
		$this->json->setData($params);

		$this->assertEquals(['hi', 'yo'], $this->json->getData());
	}


	public function testSetRender(): void {
		$params = ['test' => 'hi'];
		$this->json->setData($params);

		$expected = '{"test":"hi"}';

		$this->assertEquals($expected, $this->json->render());
	}

	public static function renderDataProvider(): array {
		return [
			[
				['test' => 'hi'], '{"test":"hi"}',
			],
			[
				['<h1>test' => '<h1>hi'], '{"\u003Ch1\u003Etest":"\u003Ch1\u003Ehi"}',
			],
		];
	}

	/**
	 * @param array $input
	 * @param string $expected
	 */
	#[\PHPUnit\Framework\Attributes\DataProvider('renderDataProvider')]
	public function testRender(array $input, $expected): void {
		$this->json->setData($input);
		$this->assertEquals($expected, $this->json->render());
	}


	public function testRenderWithNonUtf8Encoding(): void {
		$this->expectException(\JsonException::class);
		$this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded');

		$params = ['test' => hex2bin('e9')];
		$this->json->setData($params);
		$this->json->render();
	}

	public function testConstructorAllowsToSetData(): void {
		$data = ['hi'];
		$code = 300;
		$response = new JSONResponse($data, $code);

		$expected = '["hi"]';
		$this->assertEquals($expected, $response->render());
		$this->assertEquals($code, $response->getStatus());
	}

	public function testChainability(): void {
		$params = ['hi', 'yo'];
		$this->json->setData($params)
			->setStatus(Http::STATUS_NOT_FOUND);

		$this->assertEquals(Http::STATUS_NOT_FOUND, $this->json->getStatus());
		$this->assertEquals(['hi', 'yo'], $this->json->getData());
	}
}